Example #1
0
    def create(self, request, *args, **kwargs):
        document_type = request.path.split('/')[2]
        document_id = get_document_id_from_path(request)
        document = RELATED_DISCUSSION_MODELS[document_type].objects.get(
            id=document_id)
        unified_document = document.unified_document
        unified_doc_id = unified_document.id

        if request.query_params.get('created_location') == 'progress':
            request.data['created_location'] = (
                BaseComment.CREATED_LOCATION_PROGRESS)

        response = super().create(request, *args, **kwargs)
        hubs = list(unified_document.hubs.all().values_list('id', flat=True))
        self.sift_track_create_content_comment(request, response, Reply)

        discussion_id = response.data['id']
        create_contribution.apply_async((Contribution.COMMENTER, {
            'app_label': 'discussion',
            'model': 'reply'
        }, request.user.id, unified_doc_id, discussion_id),
                                        priority=3,
                                        countdown=10)

        doc_type = get_doc_type_key(unified_document)
        reset_unified_document_cache(hub_ids=hubs,
                                     document_type=[doc_type, 'all'],
                                     filters=[DISCUSSED, TRENDING])

        return self.get_self_upvote_response(request, response, Reply)
 def create(self, validated_data):
     request = self.context["request"]
     user = request.user
     paper_id = get_document_id_from_path(request)
     validated_data["created_by"] = user
     validated_data["paper"] = Paper.objects.get(pk=paper_id)
     additional_file = super().create(validated_data)
     return additional_file
Example #3
0
    def get_queryset(self):
        upvotes = Count('votes', filter=Q(votes__vote_type=Vote.UPVOTE, ))
        downvotes = Count('votes', filter=Q(votes__vote_type=Vote.DOWNVOTE, ))
        source = self.request.query_params.get('source')
        is_removed = self.request.query_params.get('is_removed', False)
        document_type = self.request.path.split('/')[2]

        if document_type == 'paper':
            paper_id = get_document_id_from_path(self.request)
            if source and source == 'twitter':
                try:
                    Paper.objects.get(id=paper_id).extract_twitter_comments(
                        use_celery=True)
                except Exception as e:
                    sentry.log_error(e)

                threads = Thread.objects.filter(paper=paper_id, source=source)
            elif source == "researchhub":
                threads = Thread.objects.filter(
                    paper=paper_id,
                    source__in=[source, Thread.INLINE_PAPER_BODY])
            elif source:
                threads = Thread.objects.filter(paper=paper_id, source=source)
            else:
                threads = Thread.objects.filter(paper=paper_id)
        elif document_type == 'post':
            post_id = get_document_id_from_path(self.request)
            threads = Thread.objects.filter(post=post_id, )
        elif document_type == 'hypothesis':
            hypothesis_id = get_document_id_from_path(self.request)
            threads = Thread.objects.filter(hypothesis=hypothesis_id, )
        elif document_type == 'citation':
            citation_id = get_document_id_from_path(self.request)
            threads = Thread.objects.filter(
                citation=citation_id,
                source__in=[source, Thread.CITATION_COMMENT])

        threads = threads.filter(is_removed=is_removed)
        threads = threads.annotate(score=upvotes - downvotes)

        return threads.prefetch_related('paper')
Example #4
0
    def create(self, request, *args, **kwargs):
        # Do not allow user to manually set created_by
        try:
            del request.data['created_by']
        except KeyError:
            pass

        paper = request.data.get('paper', None)
        if paper is None:
            paper = get_document_id_from_path(request)
            if paper is None:
                return Response('Missing required field `paper`', status=400)
            request.data['paper'] = paper

        context = self.get_serializer_context()
        response = super().create(request, *args, **kwargs)
        bullet_id = response.data['id']

        bullet_point = BulletPoint.objects.get(pk=response.data['id'])
        update_or_create_vote(request, request.user, bullet_point, Vote.UPVOTE)
        response.data = BulletPointSerializer(
            bullet_point,
            context=context
        ).data

        tracked_bullet_point = events_api.track_content_bullet_point(
            bullet_point.created_by,
            bullet_point,
            request,
        )
        update_user_risk_score(bullet_point.created_by, tracked_bullet_point)

        # Deprecating bulletpoint contributions until(?)
        # we bring bulletpoints back

        # create_contribution.apply_async(
        #     (
        #         Contribution.CURATOR,
        #         {'app_label': 'bullet_point', 'model': 'bulletpoint'},
        #         request.user.id,
        #         paper,
        #         bullet_id
        #     ),
        #     priority=2,
        #     countdown=10
        # )
        return response
Example #5
0
    def create(self, request, *args, **kwargs):
        model = request.path.split('/')[2]
        model_id = get_document_id_from_path(request)
        instance = RELATED_DISCUSSION_MODELS[model].objects.get(id=model_id)

        if model == 'citation':
            unified_document = instance.source
        else:
            unified_document = instance.unified_document

        if request.query_params.get('created_location') == 'progress':
            request.data['created_location'] = (
                BaseComment.CREATED_LOCATION_PROGRESS)

        response = super().create(request, *args, **kwargs)
        response = self.get_self_upvote_response(request, response, Thread)

        created_thread = Thread.objects.get(id=response.data['id'])
        if request.data.get('review'):
            created_thread.review_id = request.data.get('review')
            created_thread.save()

        hubs = list(unified_document.hubs.all().values_list('id', flat=True))
        discussion_id = response.data['id']

        self.sift_track_create_content_comment(request,
                                               response,
                                               Thread,
                                               is_thread=True)

        create_contribution.apply_async((Contribution.COMMENTER, {
            'app_label': 'discussion',
            'model': 'thread'
        }, request.user.id, unified_document.id, discussion_id),
                                        priority=1,
                                        countdown=10)

        doc_type = get_doc_type_key(unified_document)
        reset_unified_document_cache(
            hub_ids=hubs,
            document_type=[doc_type, 'all'],
            filters=[DISCUSSED, TRENDING],
        )

        return Response(self.serializer_class(created_thread).data,
                        status=status.HTTP_201_CREATED)
Example #6
0
    def edit(self, request, pk=None):
        bullet_point = self.get_object()
        user = request.user
        paper_id = request.data.get('paper', None)
        if paper_id is None:
            paper_id = get_document_id_from_path(request)
            if paper_id is None:
                return Response(
                    'Missing required field `paper`',
                    status=status.HTTP_400_BAD_REQUEST
                )
        text = request.data.get('text')
        plain_text = request.data.get('plain_text')

        tail = bullet_point.tail
        if tail is None:
            tail = bullet_point

        with transaction.atomic():
            head_bullet_point = BulletPoint.objects.create(
                bullet_type=bullet_point.bullet_type,
                paper_id=paper_id,
                tail=tail,
                previous=bullet_point,
                created_by=user,
                text=text,
                plain_text=plain_text,
                ordinal=bullet_point.ordinal,
                ordinal_is_locked=bullet_point.ordinal_is_locked,
                is_head=True,
                is_tail=False
            )
            bullet_point.remove_from_head()
            bullet_point.save()

            tracked_bullet_point = events_api.track_content_bullet_point(
                head_bullet_point.created_by,
                head_bullet_point,
                request,
                update=True
            )
            update_user_risk_score(head_bullet_point.created_by, tracked_bullet_point)

        serialized = self.get_serializer(instance=head_bullet_point)
        return Response(serialized.data, status=status.HTTP_201_CREATED)
Example #7
0
    def get_queryset(self):
        filters = {}

        paper_id = get_document_id_from_path(self.request)
        if paper_id is not None:
            filters['paper'] = paper_id
        bullet_type = self.request.query_params.get('bullet__type', None)
        only_heads = not self.request.query_params.get('all', False)

        if only_heads:
            filters['is_head'] = True
        if bullet_type is not None:
            filters['bullet_type'] = bullet_type

        if paper_id is None:
            bullet_points = BulletPoint.objects.all()
        else:
            bullet_points = BulletPoint.objects.filter(**filters)
        return bullet_points
 def is_authorized(self, request, view, obj):
     paper_id = get_document_id_from_path(request)
     paper = Paper.objects.get(pk=paper_id)
     author = Author.objects.get(user=request.user)
     return author in paper.authors.all()
Example #9
0
 def get_queryset(self):
     queryset = super().get_queryset()
     paper_id = get_document_id_from_path(self.request)
     if paper_id is not None:
         queryset = queryset.filter(paper=paper_id)
     return queryset