def build_json(self, exception=None): """Build the JSON dictionary.""" content = { 'success': self.success, 'details': self.details, 'messages': [], } if exception is not None: content['success'] = False content['details'] = {} if hasattr(exception, 'message'): exception = exception.message else: exception = '%s: %s' % (_('Internal error'), exception) content['exception'] = exception else: content['messages'] = self.get_messages() redirect = self.get_redirect() if redirect: content['redirect'] = self.redirect if self.extra_context: content['extra_context'] = self.extra_context try: encoded = encode(content) if self.request.GET.get("format") == 'jsonp': callback = self.request.GET.get("callback", "alert") return u"%s(%s)" % (callback, encoded) return encoded except Exception, e: if exception is not None: raise return self.build_json(e)
def build_json(self, exception=None): """Build the JSON dictionary.""" content = { 'success': self.success, 'details': self.details, 'messages': [], } if exception is not None: content['success'] = False content['details'] = {} if hasattr(exception, 'message'): exception = exception.message else: exception = '%s: %s' % (_('Internal error'), exception) content['exception'] = exception else: content['messages'] = self.get_messages() redirect = self.get_redirect() if redirect: content['redirect'] = self.redirect if self.extra_context: content['extra_context'] = self.extra_context try: return encode(content) except Exception as e: if exception is not None: raise return self.build_json(e)
def build_json(self, exception=None): """Build the JSON dictionary.""" content = {"success": self.success, "details": self.details, "messages": []} if exception is not None: content["success"] = False content["details"] = {} if hasattr(exception, "message"): exception = exception.message else: exception = "%s: %s" % (_("Internal error"), exception) content["exception"] = exception else: content["messages"] = self.get_messages() redirect = self.get_redirect() if redirect: content["redirect"] = self.redirect if self.extra_context: content["extra_context"] = self.extra_context try: return encode(content) except Exception as e: if exception is not None: raise return self.build_json(e)
def test_custom_encoder(self): encode_dt = lambda d: d.strftime('%d %b %Y') self.assertEqual(encode(datetime.datetime(1980, 1, 1), encoders=[(datetime.datetime, encode_dt)]), u'"01 Jan 1980"')
def test_date(self): self.assertEqual(encode(datetime.date(1980, 1, 1)), '"1980-01-01"')
def test_datetime(self): self.assertEqual(encode(datetime.datetime(1980, 1, 1, 12, 0, 5)), '"1980-01-01T12:00:05"')
def test_lazy(self): test_msg = lazy(lambda: 'Test!', str) self.assertEqual(encode(test_msg()), '"Test!"')
def test_custom_encoder(self): encode_dt = lambda d: d.strftime('%d %b %Y') self.assertEqual( encode(datetime.datetime(1980, 1, 1), encoders=[(datetime.datetime, encode_dt)]), u'"01 Jan 1980"')
def test_lazy(self): test_msg = lazy(lambda: 'Test!', unicode) self.assertEqual(encode(test_msg()), '"Test!"')
def test_lazy(self): test_msg = lazy(lambda: 'Test!', six.text_type) self.assertEqual(encode(test_msg()), '"Test!"')