Esempio n. 1
0
    def create_and_respond(self, request, contribution):
        """
        Respond to a POST request by creating a comment.

        Parameters
        ----------
        request : rest_framework.request.Request
            Object representing the request.
        contribution : geokey.contributions.models.Observation
            Contribution the comment should be added to.

        Returns
        -------
        rest_framework.response.Respone
            Contains the serialized comment.

        Raises
        ------
        MalformedRequestData
            When contribution is a draft or comment does not belong to the
            contribution.
        """
        user = self.get_user(request)

        if contribution.status == 'draft':
            raise MalformedRequestData(
                'This contribution is a draft. You may not comment on drafts.')

        respondsto = request.data.get('respondsto') or None
        if respondsto:
            try:
                respondsto = contribution.comments.get(pk=respondsto)
            except Comment.DoesNotExist:
                raise MalformedRequestData(
                    'The comment you try to respond to is not a comment '
                    'to the contribution.')

        review_status = request.data.get('review_status') or None
        if review_status == 'open' and contribution.status != 'review':
            contribution.update(None, user, status='review')

        comment = Comment.objects.create(text=request.data.get('text'),
                                         commentto=contribution,
                                         respondsto=respondsto,
                                         creator=user,
                                         review_status=review_status)

        serializer = CommentSerializer(comment, context={'user': user})
        return Response(serializer.data, status=status.HTTP_201_CREATED)
Esempio n. 2
0
    def create_and_respond(self, request, observation):
        """
        Reponsds to a POST request by creating a comment

        Parameters
        ----------
        request : rest_framework.request.Request
            User authenticated with the request
        observation : geokey.contributions.models.Observation
            Observation the comments are requested for

        Returns
        -------
        rest_framework.response.Respone
            Contains the serialised comment
        """
        if observation.status == 'draft':
            raise MalformedRequestData(
                'This contribution is a draft. You may not comment on drafts.')

        user = request.user
        if user.is_anonymous():
            user = User.objects.get(display_name='AnonymousUser')

        respondsto = request.DATA.get('respondsto') or None
        if respondsto is not None:
            try:
                respondsto = observation.comments.get(pk=respondsto)
            except Comment.DoesNotExist:
                raise MalformedRequestData('The comment you try to respond to'
                                           ' is not a comment to the '
                                           'observation.')

        review_status = request.DATA.get('review_status') or None
        if review_status == 'open' and observation.status != 'review':
            observation.update(None, user, status='review')

        comment = Comment.objects.create(text=request.DATA.get('text'),
                                         respondsto=respondsto,
                                         commentto=observation,
                                         creator=user,
                                         review_status=review_status)

        serializer = CommentSerializer(comment, context={'user': request.user})
        return Response(serializer.data, status=status.HTTP_201_CREATED)
Esempio n. 3
0
    def create_and_respond(self, request, contribution):
        """
        Respond to a POST request by creating a media file.

        Parameters
        ----------
        request : rest_framework.request.Request
            Object representing the request.
        contribution : geokey.contributions.models.Observation
            Contribution the media file should be added to.

        Returns
        -------
        rest_framework.response.Respone
            Contains the serialized media file.

        Raises
        ------
        MalformedRequestData
            When name is not set or file is not attached.
        PermissionDenied
            When user is not allowed to contribute to the project.
        """
        user = self.get_user(request)

        data = self.request.POST
        name = data.get('name')
        description = data.get('description')
        file = self.request.FILES.get('file')

        errors = []
        if name is None:
            errors.append('Property `name` is not set')
        if file is None:
            errors.append('No file attached')
        if errors:
            raise MalformedRequestData('%s.' % ', '.join(errors))

        if contribution.project.can_contribute(user):
            file = MediaFile.objects.create(
                name=name,
                description=description,
                contribution=contribution,
                creator=user,
                the_file=file
            )

            serializer = FileSerializer(file, context={'user': request.user})
            return Response(serializer.data, status=status.HTTP_201_CREATED)
        else:
            raise PermissionDenied(
                'You are not allowed to contribute to the project.'
            )
Esempio n. 4
0
    def create_and_respond(self, user, contribution):
        """
        Creates an file and responds with the file information

        Parameter
        ---------
        user : geokey.users.models.User
            User authenticated with the request
        contribution : geokey.contributions.models.Observation
            contribution the comments are requested for

        Returns
        -------
        rest_framework.response.Respones
            Contains the serialised file
        """
        if user.is_anonymous():
            user = User.objects.get(display_name='AnonymousUser')

        data = self.request.POST
        name = data.get('name')
        description = data.get('description')
        the_file = self.request.FILES.get('file')
        errors = []

        if name is None:
            errors.append('Property name is not set.')

        if the_file is None:
            errors.append('No file attached.')

        if errors:
            raise MalformedRequestData(', '.join(errors))

        if contribution.project.can_contribute(user):
            the_file = MediaFile.objects.create(
                name=name,
                description=description,
                contribution=contribution,
                creator=user,
                the_file=the_file
            )

            serializer = FileSerializer(the_file, context={'user': user})
            return Response(serializer.data, status=status.HTTP_201_CREATED)
        else:
            raise PermissionDenied('You are not allowed to contribute to the'
                                   'project.')