示例#1
0
 def test_json(self):
     EUR = MoneyMaker('EUR')
     renderer = JSONRenderer()
     data = {'amount': EUR('1.23')}
     rendered_json = renderer.render(data, 'application/json')
     self.assertDictEqual({'amount': "€ 1.23"},
                          json.loads(rendered_json.decode('utf-8')))
示例#2
0
def rest_json(value, arg=None):
    """
    Renders a `ReturnDict` as used by the REST framework into a safe JSON string.
    """
    if not value:
        return mark_safe('{}')
    if not isinstance(value, (dict, OrderedDict, list, tuple)):
        msg = "Given value must be of type dict, OrderedDict, list or tuple but it is {}."
        raise ValueError(msg.format(value.__class__.__name__))
    data = JSONRenderer().render(value)
    return mark_safe(data)
示例#3
0
 def render(self, context):
     try:
         cart = CartModel.objects.get_from_request(context['request'])
         serializer = CartSerializer(instance=cart,
                                     context=context,
                                     label='dropdown',
                                     with_items=self.with_items)
         cart_data = JSONRenderer().render(serializer.data)
     except CartModel.DoesNotExist:
         cart_data = {'total_quantity': 0, 'num_items': 0}
     context.update({
         'cart_as_json': mark_safe(force_text(cart_data)),
         'has_dropdown': self.with_items != CartItems.without,
     })
     return self.get_template().render(context)
示例#4
0
def test_json_renderer():
    renderer = JSONRenderer()
    data = {'amount': EUR('1.23')}
    rendered_json = renderer.render(data, 'application/json')
    assert {'amount': "€ 1.23"} == json.loads(rendered_json.decode('utf-8'))
示例#5
0
 def test_json(self):
     Money = MoneyMaker('EUR')
     renderer = JSONRenderer()
     data = {'amount': Money('1.23')}
     rendered_json = renderer.render(data, 'application/json')
     self.assertDictEqual({'amount': '€ 1.23'}, json.loads(rendered_json))