def normalize_value(self, value): items = value.split(self.ITEMS_DELIMITER) if len(items) != 2: raise ForbiddenError( 'Range filter accepts only start and end values.') return list(map(super(RangeFilter, self).normalize_value, items))
def post_linked(self): obj = self.fetch_object() relationship = self.linked_relationship() if not relationship.is_has_many(): raise ForbiddenError() self.post_linked_has_many(obj, relationship) return HttpResponseNoContent()
def delete_linked(self): obj = self.fetch_object() data = self.resolve_linked_input() relationship = self.linked_relationship() if not relationship.is_has_many(): raise ForbiddenError() self.delete_linked_has_many(obj, data, relationship) return HttpResponseNoContent()
def post_linked_has_many(self, obj, relationship): related_manager = relationship.get_from(obj) data = self.resolve_linked_input() resource = self.linked_resource() pks = [item['id'] for item in data] count = related_manager.all().filter(pk__in=pks).count() if count > 0: raise ForbiddenError() linked_objects = resource._get_objects_data(pks) related_manager.add(*linked_objects)
def filter_for(resource, path): parts = path.split(Filter.SUFFIX_DELIMITER) key = parts[0] suffix = None if len(parts) == 2: suffix = parts[1] if suffix is None: return Filter(resource, key) suffix = inflection.underscore(suffix) if suffix not in _filters: message = 'Unknown filter suffix `{0}` for key `{1}`.' raise ForbiddenError(message.format(suffix, key)) filter = _filters[suffix] return filter(resource, key)