def test_model_with_image(self): import Image size = (200,200) color = (255,0,0,0) img = Image.new("RGBA",size,color) import StringIO f = StringIO.StringIO() img.save(f, 'png') f.name = "test.png" f.seek(0) dummy = DummyImageModel.objects.create( title = "dummy2" ) from django.core.files.base import ContentFile dummy.image.save('dummy_file_name.png', ContentFile(f.read())) expected_json = '\{"pk": \d+, "model": "json_utils.dummyimagemodel", "fields": \{"image": "/tmp/dummy_file_name_?\d*\.png", "image_height": 200, "image_width": 200, "title": "dummy2"\}\}' self.assertRegexpMatches( to_json(dummy), expected_json ) dummy.image.delete();
def test_queryset(self): for i in range(3): DummyModel.objects.create(title="dummy%i" % i, number=i, description="Some Dummy Description") expected_json = '\[\{"pk": \d+, "model": "json_utils.dummymodel", "fields": \{"description": "Some Dummy Description", "number": 0, "title": "dummy0"\}\}, {"pk": \d+, "model": "json_utils.dummymodel", "fields": \{"description": "Some Dummy Description", "number": 1, "title": "dummy1"\}\}, \{"pk": \d+, "model": "json_utils.dummymodel", "fields": \{"description": "Some Dummy Description", "number": 2, "title": "dummy2"\}\}\]' self.assertRegexpMatches(to_json(DummyModel.objects.all()), expected_json)
def test_basic_model_encoding_unicode(self): dummy = DummyModel.objects.create( title=u"Déjà Vu", number = 42, description=u"Æ û â Ð Ý ï þ ł ś ć ·" ) expected_json = u'\{"pk": \d+, "model": "json_utils.dummymodel", "fields": \{"description": "Æ û â Ð Ý ï þ ł ś ć ·", "number": 42, "title": "Déjà Vu"\}\}' self.assertRegexpMatches( to_json(dummy), expected_json )
def test_basic_model_encoding(self): dummy = DummyModel.objects.create( title="dummy title", number = 42, description="Some Dummy Description" ) expected_json = '\{"pk": \d+, "model": "json_utils.dummymodel", "fields": \{"description": "Some Dummy Description", "number": 42, "title": "dummy title"\}\}' self.assertRegexpMatches( to_json(dummy), expected_json )
def test_queryset(self): for i in range(3): DummyModel.objects.create( title="dummy%i" % i, number = i, description="Some Dummy Description" ) expected_json = '\[\{"pk": \d+, "model": "json_utils.dummymodel", "fields": \{"description": "Some Dummy Description", "number": 0, "title": "dummy0"\}\}, {"pk": \d+, "model": "json_utils.dummymodel", "fields": \{"description": "Some Dummy Description", "number": 1, "title": "dummy1"\}\}, \{"pk": \d+, "model": "json_utils.dummymodel", "fields": \{"description": "Some Dummy Description", "number": 2, "title": "dummy2"\}\}\]' self.assertRegexpMatches( to_json(DummyModel.objects.all()), expected_json )
def test_mixed(self): for i in range(3): DummyModel.objects.create(title="dummy%i" % i, number=i, description="Some Dummy Description") result = DummyModel.objects.all() data = { 'result': 1, 'count': DummyModel.objects.count(), 'payload': result } expected_json = '\{"count": \d+, "result": 1, "payload": \[\{"pk": \d+, "model": "json_utils.dummymodel", "fields": \{"description": "Some Dummy Description", "number": 0, "title": "dummy0"\}' '}, \{"pk": \d+, "model": "json_utils.dummymodel", "fields": \{"description": "Some Dummy Description", "number": 1, "title": "dummy1"\}\}, \{"pk": \d+, "model": "json_utils.dummymodel", "fields": \{"description": "Some Dummy Description", "number": 2, "title": "dummy2"\}\}\]\}' self.assertRegexpMatches(to_json(data), expected_json)
def test_mixed(self): for i in range(3): DummyModel.objects.create( title="dummy%i" % i, number = i, description="Some Dummy Description" ) result = DummyModel.objects.all() data = {'result':1, 'count':DummyModel.objects.count(), 'payload':result} expected_json = '\{"count": \d+, "result": 1, "payload": \[\{"pk": \d+, "model": "json_utils.dummymodel", "fields": \{"description": "Some Dummy Description", "number": 0, "title": "dummy0"\}''}, \{"pk": \d+, "model": "json_utils.dummymodel", "fields": \{"description": "Some Dummy Description", "number": 1, "title": "dummy1"\}\}, \{"pk": \d+, "model": "json_utils.dummymodel", "fields": \{"description": "Some Dummy Description", "number": 2, "title": "dummy2"\}\}\]\}' self.assertRegexpMatches( to_json(data), expected_json )
def test_model_with_image(self): import Image size = (200, 200) color = (255, 0, 0, 0) img = Image.new("RGBA", size, color) import StringIO f = StringIO.StringIO() img.save(f, 'png') f.name = "test.png" f.seek(0) dummy = DummyImageModel.objects.create(title="dummy2") from django.core.files.base import ContentFile dummy.image.save('dummy_file_name.png', ContentFile(f.read())) expected_json = '\{"pk": \d+, "model": "json_utils.dummyimagemodel", "fields": \{"image": "/tmp/dummy_file_name_?\d*\.png", "image_height": 200, "image_width": 200, "title": "dummy2"\}\}' self.assertRegexpMatches(to_json(dummy), expected_json) dummy.image.delete()
def __init__(self, content, mimetype='application/json'): # Considered adding some CSRF protection. Decided against it in favour # of the Django middleware and other more generic solutions. content = utils.to_json(content) super(JSONResponse, self).__init__(content, mimetype=mimetype)
def __init__(self, content): # Considered adding some CSRF protection. Decided against it in favour # of the Django middleware and other more generic solutions. content = utils.to_json(content) super(JSONResponse, self).__init__(content, mimetype='application/json')
def get_db_prep_save(self, value): """Convert python object to a JSON string before saving.""" value = utils.to_json(value) return super(JSONField, self).get_db_prep_save(value)
def test_basic_model_encoding_unicode(self): dummy = DummyModel.objects.create(title=u"Déjà Vu", number=42, description=u"Æ û â Ð Ý ï þ ł ś ć ·") expected_json = u'\{"pk": \d+, "model": "json_utils.dummymodel", "fields": \{"description": "Æ û â Ð Ý ï þ ł ś ć ·", "number": 42, "title": "Déjà Vu"\}\}' self.assertRegexpMatches(to_json(dummy), expected_json)
def test_basic_model_encoding(self): dummy = DummyModel.objects.create(title="dummy title", number=42, description="Some Dummy Description") expected_json = '\{"pk": \d+, "model": "json_utils.dummymodel", "fields": \{"description": "Some Dummy Description", "number": 42, "title": "dummy title"\}\}' self.assertRegexpMatches(to_json(dummy), expected_json)