コード例 #1
0
ファイル: filters.py プロジェクト: bobisjan/django-shanghai
    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))
コード例 #2
0
    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()
コード例 #3
0
    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()
コード例 #4
0
    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)
コード例 #5
0
ファイル: filters.py プロジェクト: bobisjan/django-shanghai
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)