Example #1
0
 def to_representation(self, value):
     ret = super(TargetRelationshipField, self).to_representation(value)
     ret['data']['type'] = get_meta_type(
         PreprintRequestSerializer,
         self.context.get('request'),
     )
     return ret
 def to_representation(self, value):
     ret = super(TargetRelationshipField, self).to_representation(value)
     ret['data']['type'] = get_meta_type(
         PreprintRequestSerializer,
         self.context.get('request'),
     )
     return ret
Example #3
0
    def get_serializer_context(self):
        """Inject request into the serializer context. Additionally, inject partial functions
        (request, object -> embed items) if the query string contains embeds.  Allows
         multiple levels of nesting.
        """
        context = super(JSONAPIBaseView, self).get_serializer_context()
        if self.kwargs.get('is_embedded'):
            embeds = []
        else:
            embeds = self.request.query_params.getlist(
                'embed') or self.request.query_params.getlist('embed[]')

        fields_check = self.get_serializer_class()._declared_fields.copy()
        serializer_class_type = get_meta_type(self.serializer_class,
                                              self.request)
        if 'fields[{}]'.format(
                serializer_class_type) in self.request.query_params:
            # Check only requested and mandatory fields
            sparse_fields = self.request.query_params['fields[{}]'.format(
                serializer_class_type)]
            for field in fields_check.copy().keys():
                if field not in ('type', 'id',
                                 'links') and field not in sparse_fields:
                    fields_check.pop(field)

        for field in fields_check:
            if getattr(fields_check[field], 'field', None):
                fields_check[field] = fields_check[field].field

        for field in fields_check:
            if getattr(fields_check[field], 'always_embed',
                       False) and field not in embeds:
                embeds.append(unicode(field))
            if getattr(fields_check[field], 'never_embed',
                       False) and field in embeds:
                embeds.remove(field)
        embeds_partials = {}
        for embed in embeds:
            embed_field = fields_check.get(embed)
            embeds_partials[embed] = self._get_embed_partial(
                embed, embed_field)

        context.update({
            'enable_esi': (utils.is_truthy(
                self.request.query_params.get('esi',
                                              django_settings.ENABLE_ESI))
                           and self.request.accepted_renderer.media_type
                           in django_settings.ESI_MEDIA_TYPES),
            'embed':
            embeds_partials,
            'envelope':
            self.request.query_params.get('envelope', 'data'),
        })
        return context
Example #4
0
    def perform_destroy(self, instance):
        data = self.request.data['data']
        user = self.request.user
        current_institutions = set(user.affiliated_institutions.values_list('_id', flat=True))

        # DELETEs normally dont get type checked
        # not the best way to do it, should be enforced everywhere, maybe write a test for it
        for val in data:
            if val['type'] != get_meta_type(self.serializer_class, self.request):
                raise Conflict()
        for val in data:
            if val['id'] in current_institutions:
                user.remove_institution(val['id'])
        user.save()
Example #5
0
    def get_serializer_context(self):
        """Inject request into the serializer context. Additionally, inject partial functions
        (request, object -> embed items) if the query string contains embeds.  Allows
         multiple levels of nesting.
        """
        context = super(JSONAPIBaseView, self).get_serializer_context()
        if self.kwargs.get('is_embedded'):
            embeds = []
        else:
            embeds = self.request.query_params.getlist('embed') or self.request.query_params.getlist('embed[]')

        fields_check = self.get_serializer_class()._declared_fields.copy()
        serializer_class_type = get_meta_type(self.serializer_class, self.request)
        if 'fields[{}]'.format(serializer_class_type) in self.request.query_params:
            # Check only requested and mandatory fields
            sparse_fields = self.request.query_params['fields[{}]'.format(serializer_class_type)]
            for field in fields_check.copy().keys():
                if field not in ('type', 'id', 'links') and field not in sparse_fields:
                    fields_check.pop(field)

        for field in fields_check:
            if getattr(fields_check[field], 'field', None):
                fields_check[field] = fields_check[field].field

        for field in fields_check:
            if getattr(fields_check[field], 'always_embed', False) and field not in embeds:
                embeds.append(unicode(field))
            if getattr(fields_check[field], 'never_embed', False) and field in embeds:
                embeds.remove(field)
        embeds_partials = {}
        for embed in embeds:
            embed_field = fields_check.get(embed)
            embeds_partials[embed] = self._get_embed_partial(embed, embed_field)

        context.update({
            'enable_esi': (
                utils.is_truthy(self.request.query_params.get('esi', django_settings.ENABLE_ESI)) and
                self.request.accepted_renderer.media_type in django_settings.ESI_MEDIA_TYPES
            ),
            'embed': embeds_partials,
            'envelope': self.request.query_params.get('envelope', 'data'),
        })
        return context
    def bulk_destroy(self, request, *args, **kwargs):
        """
        Handles bulk destroy of resource objects.

        Handles some validation and enforces bulk limit.
        """
        if hasattr(request, 'query_params') and 'id' in request.query_params:
            if hasattr(request, 'data') and len(request.data) > 0:
                raise Conflict(
                    'A bulk DELETE can only have a body or query parameters, not both.'
                )

            ids = request.query_params['id'].split(',')

            if 'type' in request.query_params:
                request_type = request.query_params['type']
                data = []
                for id in ids:
                    data.append({'type': request_type, 'id': id})
            else:
                raise ValidationError(
                    'Type query parameter is also required for a bulk DELETE using query parameters.'
                )
        elif not request.data:
            raise ValidationError(
                'Request must contain array of resource identifier objects.')
        else:
            data = request.data

        num_items = len(data)
        bulk_limit = BULK_SETTINGS['DEFAULT_BULK_LIMIT']

        if num_items > bulk_limit:
            raise JSONAPIException(
                source={'pointer': '/data'},
                detail='Bulk operation limit is {}, got {}.'.format(
                    bulk_limit, num_items),
            )

        user = self.request.user
        object_type = get_meta_type(self.serializer_class, self.request)

        resource_object_list = self.get_requested_resources(request=request,
                                                            request_data=data)

        for item in data:
            item_type = item[u'type']
            if item_type != object_type:
                raise Conflict(
                    'Type needs to match type expected at this endpoint.')

        if not self.allow_bulk_destroy_resources(user, resource_object_list):
            raise PermissionDenied

        skip_uneditable = self.bulk_destroy_skip_uneditable(
            resource_object_list, user, object_type)
        if skip_uneditable:
            skipped = skip_uneditable['skipped']
            allowed = skip_uneditable['allowed']
            if skipped:
                self.perform_bulk_destroy(allowed)
                return Response(status=status.HTTP_200_OK,
                                data={'errors': skipped})

        self.perform_bulk_destroy(resource_object_list)
        return Response(status=status.HTTP_204_NO_CONTENT)