Browse Source

Fix json encoder

Denis K 9 years ago
parent
commit
ec8d20ce78
3 changed files with 11 additions and 7 deletions
  1. 3 3
      jet/models.py
  2. 3 3
      jet/modules.py
  3. 5 1
      jet/utils.py

+ 3 - 3
jet/models.py

@@ -4,7 +4,7 @@ from django.db import models
 from django.utils import timezone
 from django.utils.encoding import python_2_unicode_compatible
 from django.utils.translation import ugettext_lazy as _
-from jet.utils import DateTimeEncoder
+from jet.utils import LazyDateTimeEncoder
 
 
 @python_2_unicode_compatible
@@ -75,7 +75,7 @@ class UserDashboardModule(models.Model):
             if setting in settings:
                 settings.pop(setting)
 
-        self.settings = json.dumps(settings, cls=DateTimeEncoder)
+        self.settings = json.dumps(settings, cls=LazyDateTimeEncoder)
         self.save()
 
     def update_settings(self, update_settings):
@@ -83,7 +83,7 @@ class UserDashboardModule(models.Model):
 
         settings.update(update_settings)
 
-        self.settings = json.dumps(settings, cls=DateTimeEncoder)
+        self.settings = json.dumps(settings, cls=LazyDateTimeEncoder)
         self.save()
 
 

+ 3 - 3
jet/modules.py

@@ -4,7 +4,7 @@ from django.contrib.admin.models import LogEntry
 from django.db.models import Q
 from django.template.loader import render_to_string
 from django.utils.translation import ugettext_lazy as _
-from jet.utils import get_app_list, DateTimeEncoder
+from jet.utils import get_app_list, LazyDateTimeEncoder
 import datetime
 
 
@@ -69,13 +69,13 @@ class DashboardModule(object):
     def dump_settings(self, settings=None):
         settings = settings or self.settings_dict()
         if settings:
-            return json.dumps(settings, cls=DateTimeEncoder)
+            return json.dumps(settings, cls=LazyDateTimeEncoder)
         else:
             return ''
 
     def dump_children(self):
         if self.store_children():
-            return json.dumps(self.children, cls=DateTimeEncoder)
+            return json.dumps(self.children, cls=LazyDateTimeEncoder)
         else:
             return ''
 

+ 5 - 1
jet/utils.py

@@ -9,6 +9,8 @@ from django.contrib.admin import AdminSite
 from django.utils.encoding import smart_text
 from jet import settings
 from django.contrib import messages
+from django.utils.encoding import force_text
+from django.utils.functional import Promise
 
 
 class JsonResponse(HttpResponse):
@@ -72,10 +74,12 @@ def get_current_dashboard(location):
     return index_dashboard_cls
 
 
-class DateTimeEncoder(json.JSONEncoder):
+class LazyDateTimeEncoder(json.JSONEncoder):
     def default(self, obj):
         if isinstance(obj, datetime.datetime) or isinstance(obj, datetime.date):
             return obj.isoformat()
+        elif isinstance(obj, Promise):
+            return force_text(obj)
         return json.JSONEncoder.default(self, obj)