def _decoded_input(self): content_type = self._request.META.get('CONTENT_TYPE', _JSON_CONTENT_TYPE) raw_data = self._request.raw_post_data if content_type == _JSON_CONTENT_TYPE: try: raw_dict = simplejson.loads(raw_data) except ValueError as exc: raise exceptions.BadRequest('Error decoding request body: ' '%s\n%r' % (exc, raw_data)) if not isinstance(raw_dict, dict): raise exceptions.BadRequest('Expected dict input, got %s: %r' % (type(raw_dict), raw_dict)) elif content_type == 'application/x-www-form-urlencoded': cgi_dict = cgi.parse_qs(raw_data) # django won't do this for PUT raw_dict = {} for key, values in cgi_dict.items(): value = values[-1] # take last value if multiple were given try: # attempt to parse numbers, booleans and nulls raw_dict[key] = simplejson.loads(value) except ValueError: # otherwise, leave it as a string raw_dict[key] = value else: raise exceptions.RequestError( 415, 'Unsupported media type: %s' % content_type) return _InputDict(raw_dict)
def _decoded_input(self): content_type = self._request.META.get('CONTENT_TYPE', _JSON_CONTENT_TYPE) raw_data = self._request.raw_post_data if content_type == _JSON_CONTENT_TYPE: try: raw_dict = simplejson.loads(raw_data) except ValueError, exc: raise exceptions.BadRequest('Error decoding request body: ' '%s\n%r' % (exc, raw_data)) if not isinstance(raw_dict, dict): raise exceptions.BadRequest('Expected dict input, got %s: %r' % (type(raw_dict), raw_dict))
def post(self): input_dict = self._decoded_input() try: instance = self.entry_class.create_instance(input_dict, self) entry = self._entry_from_instance(instance) entry.update(input_dict) except model_logic.ValidationError, exc: raise exceptions.BadRequest('Invalid input: %s' % exc)
def resolve_link(self, link): if isinstance(link, dict): uri = link['href'] elif isinstance(link, basestring): uri = link else: raise exceptions.BadRequest('Unable to understand link %s' % link) return self.resolve_uri(uri)
def _read_int_parameter(self, name, default): if name not in self._query_params: return default input_value = self._query_params[name] try: return int(input_value) except ValueError: raise exceptions.BadRequest( 'Invalid non-numeric value for %s: %r' % (name, input_value))
def resolve_uri(self, uri): # check for absolute URIs match = re.match(r'(?P<root>https?://[^/]+)(?P<path>/.*)', uri) if match: # is this URI for a different host? my_root = self._request.build_absolute_uri('/') request_root = match.group('root') + '/' if my_root != request_root: # might support this in the future, but not now raise exceptions.BadRequest('Unable to resolve remote URI %s' % uri) uri = match.group('path') try: view_method, args, kwargs = urlresolvers.resolve(uri) except http.Http404: raise exceptions.BadRequest('Unable to resolve URI %s' % uri) resource_class = view_method.im_self # class owning this classmethod return resource_class.from_uri_args(self._request, **kwargs)
def read_query_parameters(self, query_params): super(RelationshipCollection, self).read_query_parameters(query_params) if not self._query_params: raise exceptions.BadRequest( 'You must specify one of the parameters %s and %s' % tuple(self.related_classes.keys())) query_items = self._query_params.items() fixed_entry = self._resolve_query_param(*query_items[0]) self._set_fixed_entry(fixed_entry) if len(query_items) > 1: other_fixed_entry = self._resolve_query_param(*query_items[1]) self.related_manager = self.related_manager.filter( pk=other_fixed_entry.instance.id)
def post(self): input_dict = self._decoded_input() try: instance = self.entry_class.create_instance(input_dict, self) entry = self._entry_from_instance(instance) entry.update(input_dict) except model_logic.ValidationError as exc: raise exceptions.BadRequest('Invalid input: %s' % exc) # RFC 2616 specifies that we provide the new URI in both the Location # header and the body response = http.HttpResponse( status=201, # Created content=entry.href()) response['Location'] = entry.href() return response
def put(self): try: self.update(self._decoded_input()) except model_logic.ValidationError, exc: raise exceptions.BadRequest('Invalid input: %s' % exc)
def _check_for_required_fields(cls, input_dict, fields): assert isinstance(fields, (list, tuple)), fields missing_fields = ', '.join(field for field in fields if field not in input_dict) if missing_fields: raise exceptions.BadRequest('Missing input: ' + missing_fields)
def put(self): try: self.update(self._decoded_input()) except model_logic.ValidationError as exc: raise exceptions.BadRequest('Invalid input: %s' % exc) return self._basic_response(self.full_representation())