コード例 #1
0
 def error(self, status_code, code, msg):
     data = {
         'code': code if code is not None else '',
         'message': msg if msg is not None else errors.message(code)
     }
     return http.HttpResponse(json.dumps(data),
                              status=status_code,
                              content_type='application/json')
コード例 #2
0
 def post(self, request, *args, **kwargs):
     # Only accept JSON
     if request.META['CONTENT_TYPE'] != 'application/json':
         return self.bad_request(
             msg="Requests must have CONTENT_TYPE 'application/json'")
     try:
         payload = json.loads(request.body)
     except ValueError:
         return self.bad_request(msg="JSON payload could not be decoded")
     try:
         self.validate_payload(payload)
     except InvalidPayload as e:
         return self.bad_request(msg=str(e))
     except ValidationError as e:
         return self.forbidden(code=e.code, msg=errors.message(e.code))
     # We can still get a ValidationError even if the payload itself is
     # valid.
     try:
         return self.valid_payload(payload)
     except ValidationError as e:
         return self.forbidden(code=e.code, msg=errors.message(e.code))
コード例 #3
0
 def post(self, request, *args, **kwargs):
     # Only accept JSON
     if request.META['CONTENT_TYPE'] != 'application/json':
         return self.bad_request(
             msg="Requests must have CONTENT_TYPE 'application/json'")
     try:
         payload = json.loads(request.body)
     except ValueError:
         return self.bad_request(
             msg="JSON payload could not be decoded")
     try:
         self.validate_payload(payload)
     except InvalidPayload as e:
         return self.bad_request(msg=str(e))
     except ValidationError as e:
         return self.forbidden(code=e.code, msg=errors.message(e.code))
     # We can still get a ValidationError even if the payload itself is
     # valid.
     try:
         return self.valid_payload(payload)
     except ValidationError as e:
         return self.forbidden(code=e.code, msg=errors.message(e.code))
コード例 #4
0
class JSONView(generic.View):
    required_keys = ()
    optional_keys = ()

    # Error handlers

    def forbidden(self, code=None, msg=None):
        # Forbidden by business logic
        return self.error(403, code, msg)

    def bad_request(self, code=None, msg=None):
        # Bad syntax (eg missing keys)
        return self.error(400, code, msg)

    def error(self, status_code, code, msg):
        data = {
            'code': code if code is not None else '',
            'message': msg if msg is not None else errors.message(code)
        }
        return http.HttpResponse(json.dumps(data),
                                 status=status_code,
                                 content_type='application/json')

    # Success handlers

    def created(self, url, data):
        response = http.HttpResponse(json.dumps(data),
                                     content_type='application/json',
                                     status=201)
        response['Location'] = url
        return response

    def ok(self, data):
        return http.HttpResponse(json.dumps(data),
                                 content_type='application/json')

    def post(self, request, *args, **kwargs):
        # Only accept JSON
        if request.META['CONTENT_TYPE'] != 'application/json':
            return self.bad_request(
                msg="Requests must have CONTENT_TYPE 'application/json'")
        try:
            payload = json.loads(request.body)
        except ValueError:
            return self.bad_request(msg="JSON payload could not be decoded")
        try:
            self.validate_payload(payload)
        except InvalidPayload, e:
            return self.bad_request(msg=str(e))
        except ValidationError, e:
            return self.forbidden(code=e.code, msg=errors.message(e.code))
コード例 #5
0
        try:
            payload = json.loads(request.body)
        except ValueError:
            return self.bad_request(msg="JSON payload could not be decoded")
        try:
            self.validate_payload(payload)
        except InvalidPayload, e:
            return self.bad_request(msg=str(e))
        except ValidationError, e:
            return self.forbidden(code=e.code, msg=errors.message(e.code))
        # We can still get a ValidationError even if the payload itself is
        # valid.
        try:
            return self.valid_payload(payload)
        except ValidationError, e:
            return self.forbidden(code=e.code, msg=errors.message(e.code))

    def validate_payload(self, payload):
        # We mimic Django's forms API by using dynamic dispatch to call clean_*
        # methods, and use a single 'clean' method to validate relations
        # between fields.
        for key in self.required_keys:
            if key not in payload:
                raise InvalidPayload(
                    ("Mandatory field '%s' is missing from JSON "
                     "payload") % key)
            validator_method = 'clean_%s' % key
            if hasattr(self, validator_method):
                payload[key] = getattr(self, validator_method)(payload[key])
        for key in self.optional_keys:
            validator_method = 'clean_%s' % key
コード例 #6
0
 def error(self, status_code, code, msg):
     data = {'code': code if code is not None else '',
             'message': msg if msg is not None else errors.message(code)}
     return http.HttpResponse(json.dumps(data),
                              status=status_code,
                              content_type='application/json')
コード例 #7
0
            payload = json.loads(request.raw_post_data)
        except ValueError:
            return self.bad_request(
                msg="JSON payload could not be decoded")
        try:
            self.validate_payload(payload)
        except InvalidPayload, e:
            return self.bad_request(msg=str(e))
        except ValidationError, e:
            return self.forbidden(code=e.code, msg=errors.message(e.code))
        # We can still get a ValidationError even if the payload itself is
        # valid.
        try:
            return self.valid_payload(payload)
        except ValidationError, e:
            return self.forbidden(code=e.code, msg=errors.message(e.code))

    def validate_payload(self, payload):
        # We mimic Django's forms API by using dynamic dispatch to call clean_*
        # methods, and use a single 'clean' method to validate relations
        # between fields.
        for key in self.required_keys:
            if key not in payload:
                raise InvalidPayload((
                    "Mandatory field '%s' is missing from JSON "
                    "payload") % key)
            validator_method = 'clean_%s' % key
            if hasattr(self, validator_method):
                payload[key] = getattr(self, validator_method)(payload[key])
        for key in self.optional_keys:
            validator_method = 'clean_%s' % key