Пример #1
0
	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();
Пример #2
0
    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)
Пример #3
0
	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
		)
Пример #4
0
	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
		)
Пример #5
0
	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
		)
Пример #6
0
 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)
Пример #7
0
	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
		)
Пример #8
0
    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()
Пример #9
0
 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)
Пример #10
0
 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')
Пример #11
0
 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)
Пример #12
0
 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)
Пример #13
0
 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)