Commit c921684e by Őry Máté

Merge branch 'issue-229' into 'master'

Human readable dates in HumanReadableObjects

See merge request !242
parents 4d151888 e5192e4b
...@@ -32,6 +32,7 @@ from django.core.serializers.json import DjangoJSONEncoder ...@@ -32,6 +32,7 @@ from django.core.serializers.json import DjangoJSONEncoder
from django.db.models import ( from django.db.models import (
CharField, DateTimeField, ForeignKey, NullBooleanField CharField, DateTimeField, ForeignKey, NullBooleanField
) )
from django.template import defaultfilters
from django.utils import timezone from django.utils import timezone
from django.utils.encoding import force_text from django.utils.encoding import force_text
from django.utils.functional import Promise from django.utils.functional import Promise
...@@ -428,6 +429,14 @@ class HumanReadableObject(object): ...@@ -428,6 +429,14 @@ class HumanReadableObject(object):
admin_text_template = admin_text_template._proxy____args[0] admin_text_template = admin_text_template._proxy____args[0]
self.user_text_template = user_text_template self.user_text_template = user_text_template
self.admin_text_template = admin_text_template self.admin_text_template = admin_text_template
for k, v in params.iteritems():
try:
v = timezone.datetime.strptime(
v, "%Y-%m-%dT%H:%M:%S.%fZ").replace(tzinfo=timezone.UTC())
except (ValueError, TypeError): # Mock raises TypeError
pass
if isinstance(v, timezone.datetime):
params[k] = defaultfilters.date(v, "DATETIME_FORMAT")
self.params = params self.params = params
@classmethod @classmethod
...@@ -444,24 +453,27 @@ class HumanReadableObject(object): ...@@ -444,24 +453,27 @@ class HumanReadableObject(object):
def from_dict(cls, d): def from_dict(cls, d):
return None if d is None else cls(**d) return None if d is None else cls(**d)
def get_admin_text(self): def _get_parsed_text(self, key):
if self.admin_text_template == "": value = getattr(self, key)
if value == "":
return "" return ""
try: try:
return _(self.admin_text_template) % self.params return _(value) % self.params
except KeyError:
logger.exception("Can't render %s '%s' %% %s",
key, value, unicode(self.params))
raise
def get_admin_text(self):
try:
return self._get_parsed_text("admin_text_template")
except KeyError: except KeyError:
logger.exception("Can't render admin_text_template '%s' %% %s",
self.admin_text_template, unicode(self.params))
return self.get_user_text() return self.get_user_text()
def get_user_text(self): def get_user_text(self):
if self.user_text_template == "":
return ""
try: try:
return _(self.user_text_template) % self.params return self._get_parsed_text("user_text_template")
except KeyError: except KeyError:
logger.exception("Can't render user_text_template '%s' %% %s",
self.user_text_template, unicode(self.params))
return self.user_text_template return self.user_text_template
def get_text(self, user): def get_text(self, user):
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment