def setUp(self):
        self.testObj = {
            'testval': 99,
            'testStr': 'Ice Cone',
            'today': datetime(2012, 5, 17)
        }
        self.jsonres = JsonResponse(self.testObj)

        self.modelWithSerializeFields = JsonResponse(
            ModelWithSerializeFields(
                title='Hadouken',
                text='is said repeatedly in Street Fighter',
                _password='******'))

        self.modelbaseWithoutSerializeFields = ModelWithoutSerializeFields(
            title='Hadouken',
            text='is said repeatedly in Street Fighter',
            _password='******')

        self.modelWithoutSerializeFields = JsonResponse(
            self.modelbaseWithoutSerializeFields)

        self.modelWithVersionedSerializeFieldsV1 = JsonResponse(
            ModelWithVersionedSerializeFields(
                title='Hadouken',
                text='is said repeatedly in Street Fighter',
                _password='******'),
            version='v1')

        self.modelWithVersionedSerializeFieldsVNull = JsonResponse(
            ModelWithVersionedSerializeFields(
                title='Hadouken',
                text='is said repeatedly in Street Fighter',
                _password='******'))
Beispiel #2
0
def search(request):
	try:
		if request.method == 'POST':
			search_form = SearchForm(request.POST)
			if search_form.is_valid():
				results = search_form.search(request.user)
				return JsonResponse({'search_results':results})
		return JsonResponse([])
	except:
		logger.exception("Could not search")
		traceback.print_exc()
Beispiel #3
0
    def serialize(self):
        """
        Serializes the context as JSON, or returns a HTTP response with corresponding status.
        """

        key, status_code = self.status
        if status_code == 200:
            return JsonResponse(self.context)
        else:
            return HttpResponse(status=status_code)
    def testEmittableResource(self):
        emittableResource = EmittableResource(1, 'test')
        resp = JsonResponse(emittableResource)
        result = simplejson.loads(resp.content)
        for key, value in result.items():
            self.assertEqual(
                getattr(emittableResource, key).__str__(), value.__str__())
        self.assert_('id' in result.keys())
        self.assert_('title' in result.keys())
        self.assert_('text' not in result.keys())

        # test that version is ignored
        resp = JsonResponse(emittableResource, version='v1')
        result = simplejson.loads(resp.content)
        for key, value in result.items():
            self.assertEqual(
                getattr(emittableResource, key).__str__(), value.__str__())
        self.assert_('id' in result.keys())
        self.assert_('title' in result.keys())
        self.assert_('text' not in result.keys())
Beispiel #5
0
 def serialize(self):
     """
     Serializes the context as JSON, or returns a HTTP response with corresponding status.
     """
     
     key, status_code = self.status
     
     if status_code == CR_OK[1]:
         return JsonResponse(self.context)
     
     elif status_code == CR_INVALID_DATA[1]:
         
         # Output form errors when return status is invalid
         if hasattr(self, 'extra') and getattr(settings, 'DYNAMICRESPONSE_JSON_FORM_ERRORS', False):
             errors = {}
             
             for form in [value for key, value in self.extra.items() if isinstance(value, Form) or isinstance(value, ModelForm)]:
                 if not form.is_valid():
                     
                     for key, val in form.errors.items():
                         
                         # Flatten field errors
                         if isinstance(val, list):
                             val = ' '.join(val)
                         
                         # Split general and field specific errors
                         if key == '__all__':
                             errors['general_errors'] = val
                         else:
                             
                             # Field specific errors
                             if not 'field_errors' in errors:
                                 errors['field_errors'] = {}
                             
                             errors['field_errors'][key] = val
             
             return JsonResponse(errors, status=status_code)
     
     # Return blank response for all other status codes
     return HttpResponse(status=status_code)
    def testModelsWithDynamiclyAddedFieldsConvertsToJson(self):
        to_equal = {
            u'text': u'is said repeatedly in Street Fighter',
            u'title': u'Hadouken',
            u'id': None,
            u'dummy': u'blah'
        }

        self.modelbaseWithoutSerializeFields.dummy = "blah"
        self.modelbaseWithoutSerializeFields._dummy = "blah"
        result = simplejson.loads(
            JsonResponse(self.modelbaseWithoutSerializeFields).content)

        for key, value in result.items():
            self.assertEqual(to_equal.get(key).__str__(), value.__str__())