Ejemplo n.º 1
0
 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)
Ejemplo n.º 2
0
 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)
Ejemplo n.º 3
0
 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)
Ejemplo n.º 4
0
 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)
Ejemplo n.º 5
0
 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"')
Ejemplo n.º 6
0
 def test_date(self):
     self.assertEqual(encode(datetime.date(1980, 1, 1)),
                      '"1980-01-01"')
Ejemplo n.º 7
0
 def test_datetime(self):
     self.assertEqual(encode(datetime.datetime(1980, 1, 1, 12, 0, 5)),
                      '"1980-01-01T12:00:05"')
Ejemplo n.º 8
0
 def test_lazy(self):
     test_msg = lazy(lambda: 'Test!', str)
     self.assertEqual(encode(test_msg()), '"Test!"')
Ejemplo n.º 9
0
 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"')
Ejemplo n.º 10
0
 def test_date(self):
     self.assertEqual(encode(datetime.date(1980, 1, 1)), '"1980-01-01"')
Ejemplo n.º 11
0
 def test_datetime(self):
     self.assertEqual(encode(datetime.datetime(1980, 1, 1, 12, 0, 5)),
                      '"1980-01-01T12:00:05"')
Ejemplo n.º 12
0
 def test_lazy(self):
     test_msg = lazy(lambda: 'Test!', unicode)
     self.assertEqual(encode(test_msg()), '"Test!"')
Ejemplo n.º 13
0
 def test_lazy(self):
     test_msg = lazy(lambda: 'Test!', six.text_type)
     self.assertEqual(encode(test_msg()), '"Test!"')