Skip to content

Commit

Permalink
Merge branch 'main' into connection_status_tweak
Browse files Browse the repository at this point in the history
  • Loading branch information
rowanseymour authored Aug 17, 2021
2 parents 1909239 + d892d06 commit df568a4
Show file tree
Hide file tree
Showing 10 changed files with 74 additions and 65 deletions.
9 changes: 9 additions & 0 deletions temba/channels/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -1513,6 +1513,15 @@ def get_context_data(self, **kwargs):
class Connection(AnonMixin, SmartReadView):
model = ChannelConnection

def get_gear_links(self):
return [
dict(
title=_("More Calls"),
style="button-light",
href=reverse("channels.channellog_list", args=[self.get_object().channel.uuid]) + "?connections=1",
)
]

class Read(OrgObjPermsMixin, SmartReadView):
fields = ("description", "created_on")

Expand Down
2 changes: 1 addition & 1 deletion temba/contacts/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -990,7 +990,7 @@ def get(self, request, *args, **kwargs):
"fields": contact.fields if contact.fields else {},
"primary_urn_formatted": primary_urn,
}
contact_json["created_on"] = org.format_datetime(contact.created_on, False)
contact_json["created_on"] = org.format_datetime(contact.created_on, show_time=False)

json_contacts.append(contact_json)
summary["sample"] = json_contacts
Expand Down
13 changes: 7 additions & 6 deletions temba/orgs/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -897,16 +897,17 @@ def set_flow_languages(self, user, codes):
self.modified_by = user
self.save(update_fields=("flow_languages", "modified_by", "modified_on"))

def get_datetime_formats(self):
format_date = Org.DATE_FORMATS_PYTHON.get(self.date_format)
format_datetime = format_date + " %H:%M"
return format_date, format_datetime
def get_datetime_formats(self, *, seconds=False):
date_format = Org.DATE_FORMATS_PYTHON.get(self.date_format)
time_format = "%H:%M:%S" if seconds else "%H:%M"
datetime_format = f"{date_format} {time_format}"
return date_format, datetime_format

def format_datetime(self, d, show_time=True):
def format_datetime(self, d, *, show_time=True, seconds=False):
"""
Formats a datetime with or without time using this org's date format
"""
formats = self.get_datetime_formats()
formats = self.get_datetime_formats(seconds=seconds)
format = formats[1] if show_time else formats[0]
return datetime_to_str(d, format, self.timezone)

Expand Down
1 change: 1 addition & 0 deletions temba/orgs/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -3814,6 +3814,7 @@ def test_edit(self):
def test_org_timezone(self):
self.assertEqual(self.org.timezone, pytz.timezone("Africa/Kigali"))
self.assertEqual(("%d-%m-%Y", "%d-%m-%Y %H:%M"), self.org.get_datetime_formats())
self.assertEqual(("%d-%m-%Y", "%d-%m-%Y %H:%M:%S"), self.org.get_datetime_formats(seconds=True))

contact = self.create_contact("Bob", phone="+250788382382")
self.create_incoming_msg(contact, "My name is Frank")
Expand Down
15 changes: 9 additions & 6 deletions temba/utils/templatetags/temba.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,18 +251,21 @@ def short_datetime(context, dtime):


@register.simple_tag(takes_context=True)
def format_datetime(context, dtime):
if dtime.tzinfo is None:
dtime = dtime.replace(tzinfo=pytz.utc)
def format_datetime(context, dt, seconds: bool = False):
if dt.tzinfo is None:
dt = dt.replace(tzinfo=pytz.utc)

tz = pytz.UTC
org = context.get("user_org")
if org:
tz = org.timezone
dtime = dtime.astimezone(tz)
dt = dt.astimezone(tz)

if org:
return org.format_datetime(dtime)
return datetime_to_str(dtime, "%d-%m-%Y %H:%M", tz)
return org.format_datetime(dt, seconds=seconds)

fmt = "%d-%m-%Y %H:%M:%S" if seconds else "%d-%m-%Y %H:%M"
return datetime_to_str(dt, fmt, tz)


@register.filter
Expand Down
10 changes: 5 additions & 5 deletions temba/utils/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
from temba.orgs.models import Org
from temba.tests import ESMockWithScroll, TembaTest, matchers
from temba.utils import json, uuid
from temba.utils.templatetags.temba import format_datetime

from . import (
chunk_list,
Expand Down Expand Up @@ -282,19 +283,18 @@ def test_icon(self):
self.assertEqual("", icon(None))

def test_format_datetime(self):
import pytz
from temba.utils.templatetags.temba import format_datetime

with patch.object(timezone, "now", return_value=datetime.datetime(2015, 9, 15, 0, 0, 0, 0, pytz.UTC)):
self.org.date_format = "D"
self.org.save()

# date without timezone and no user org in context
test_date = datetime.datetime(2012, 7, 20, 17, 5, 0, 0)
test_date = datetime.datetime(2012, 7, 20, 17, 5, 30, 0)
self.assertEqual("20-07-2012 17:05", format_datetime(dict(), test_date))
self.assertEqual("20-07-2012 17:05:30", format_datetime(dict(), test_date, seconds=True))

test_date = datetime.datetime(2012, 7, 20, 17, 5, 0, 0).replace(tzinfo=pytz.utc)
test_date = datetime.datetime(2012, 7, 20, 17, 5, 30, 0).replace(tzinfo=pytz.utc)
self.assertEqual("20-07-2012 17:05", format_datetime(dict(), test_date))
self.assertEqual("20-07-2012 17:05:30", format_datetime(dict(), test_date, seconds=True))

context = dict(user_org=self.org)

Expand Down
24 changes: 18 additions & 6 deletions templates/channels/channellog_connection.haml
Original file line number Diff line number Diff line change
@@ -1,10 +1,22 @@
-extends "channels/channellog_read.html"
-load i18n

-load i18n humanize compress
-block title
-trans "Call Log"

-block titles
%h2
-trans "Call Log"
-block log-recipient
%a(href='{% url "contacts.contact_read" object.contact.uuid %}')
{{ object.contact_urn.path }}

-block above-bar
%a.btn.btn-secondary{ href: '{% url "channels.channellog_list" object.channel.uuid %}?connections=1' } More Calls
-block log-direction
{{ object.get_direction_display }}

-block log-status
{{ object.get_status_display }}
-if object.status == "E" or object.status == "F"
-if object.error_reason
\({{ object.get_error_reason_display }})

-block log-entries
-for log in object.channel_logs.all
-include "channels/channellog_log.haml"
15 changes: 8 additions & 7 deletions templates/channels/channellog_list.haml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{% extends "smartmin/list.html" %}
-load i18n contacts smartmin humanize
-load i18n contacts smartmin humanize temba

-block page-title
-trans "Channel Events"
Expand Down Expand Up @@ -72,8 +72,9 @@
%span
{{ obj.request_time|default:"0"|intcomma }}ms
%td{ style:'text-align: right', nowrap:'true' }
{{obj.created_on|date:"M d, Y, H:m:s"}}
%td(style='text-align: right' nowrap='true')
{% format_datetime obj.created_on seconds=True %}
-elif request.GET.others
%tr{ class:"{% if obj.is_error %}warning{% endif %}" }
%td
Expand All @@ -84,8 +85,8 @@
%span
{{ obj.request_time|default:"0"|intcomma }}ms
%td{ style:'text-align: right', nowrap:'true' }
{{obj.created_on}}
%td(style='text-align: right' nowrap='true')
{% format_datetime obj.created_on seconds=True %}
-else
%tr{ class:"{% if obj.is_error %}warning{% endif %}" }
Expand All @@ -101,8 +102,8 @@
%span
{{ obj.request_time|default:"0"|intcomma }}ms
%td{ style:'text-align: right', nowrap:'true' }
{{obj.created_on}}
%td(style='text-align: right' nowrap='true')
{% format_datetime obj.created_on seconds=True %}
.flex.flex-col.mb-16
-include "includes/pagination.haml"
3 changes: 1 addition & 2 deletions templates/channels/channellog_log.haml
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@
{{ log.request_time|default:"0"|intcomma }}ms
%td
.text-right.whitespace-nowrap
{{log.created_on|date:"M j, Y g:i:s a"}}

{% format_datetime log.created_on seconds=True %}

%tr
%td(colspan="4" style="padding:0;")
Expand Down
47 changes: 15 additions & 32 deletions templates/channels/channellog_read.haml
Original file line number Diff line number Diff line change
@@ -1,75 +1,58 @@
{% extends "smartmin/read.html" %}
-extends "smartmin/read.html"
-load i18n compress

-block page-title
-trans "Channel Event"

-block title
-if object.is_error
-trans "Message Error"
-else
-if object.connection_type
-trans "Call Log"
-else
-trans "Message Log"
-trans "Message Log"

-block subtitle
{{ object.channel.get_channel_type_display }}


-block content

.card.flex.mt-0
.inline-flex.mr-8
.font-normal.mr-2
-trans "Recipient"
-if object.msg
%a{href:'{% url "contacts.contact_read" object.msg.contact.uuid %}'}
{{object.msg.contact_urn.get_display}}
-elif object.connection_type
%a{href:'{% url "contacts.contact_read" object.contact.uuid %}'}
{{object.contact_urn.path}}
-block log-recipient
-if object.msg
%a(href='{% url "contacts.contact_read" object.msg.contact.uuid %}')
{{ object.msg.contact_urn.get_display }}

.inline-flex.mr-8
.font-normal.mr-2
-trans "Channel"
%a{href:'{% url "channels.channel_read" object.channel.uuid %}'}
{{object.channel}}
%a(href='{% url "channels.channel_read" object.channel.uuid %}')
{{ object.channel }}

.inline-flex.mr-8
.font-normal.mr-2
-trans "Direction"
-if object.connection_type
{{object.get_direction_display}}
-else
{{object.msg.get_direction_display}}
-block log-direction
{{ object.msg.get_direction_display }}

.inline-flex.mr-8
.font-normal.mr-2
-trans "Date"
{{object.created_on|date:"M j, Y g:i:s a"}}
{% format_datetime object.created_on seconds=True %}

-if object.duration
.inline-flex.mr-8
.font-normal.mr-2
-trans "Duration"
{{object.get_duration|delta}}
{{ object.get_duration|delta }}

.inline-flex.mr-8
.font-normal.mr-2
-trans "Status"
-if object.connection_type
{{object.get_status_display}}
-else
-block log-status
-if object.is_error
-trans "Error"
-else
{{object.msg.get_status_display}}
{{ object.msg.get_status_display }}

-if object.connection_type
-for log in object.channel_logs.all
-include "channels/channellog_log.haml"
-else
-block log-entries
-for log in object.log_group
-include "channels/channellog_log.haml"

Expand Down

0 comments on commit df568a4

Please sign in to comment.