-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1917 from ResearchHub/remove-paper-event
chore: Remove paper event
- Loading branch information
Showing
7 changed files
with
24 additions
and
206 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# Generated by Django 4.2.15 on 2024-10-16 08:24 | ||
|
||
from django.db import migrations | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
("analytics", "0007_alter_paperevent_created_location_meta"), | ||
] | ||
|
||
operations = [ | ||
migrations.DeleteModel( | ||
name="PaperEvent", | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,10 @@ | ||
from rest_framework import serializers | ||
|
||
from analytics.models import PaperEvent, WebsiteVisits | ||
from user.models import User | ||
from analytics.models import WebsiteVisits | ||
|
||
|
||
class WebsiteVisitsSerializer(serializers.ModelSerializer): | ||
|
||
class Meta: | ||
fields = '__all__' | ||
model = WebsiteVisits | ||
|
||
|
||
class PaperEventSerializer(serializers.ModelSerializer): | ||
user = serializers.PrimaryKeyRelatedField( | ||
queryset=User.objects.all(), | ||
many=False, | ||
read_only=False, | ||
required=False | ||
) | ||
|
||
class Meta: | ||
model = PaperEvent | ||
exclude = [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,101 +1,11 @@ | ||
from django_filters.rest_framework import DjangoFilterBackend | ||
from rest_framework import status, viewsets | ||
from rest_framework.decorators import action | ||
from rest_framework.filters import OrderingFilter | ||
from rest_framework.response import Response | ||
from rest_framework import viewsets | ||
|
||
from analytics.models import PaperEvent, WebsiteVisits | ||
from analytics.permissions import UpdateOrDelete | ||
from analytics.serializers import PaperEventSerializer, WebsiteVisitsSerializer | ||
from reputation.models import Contribution | ||
from reputation.tasks import create_contribution | ||
from analytics.models import WebsiteVisits | ||
from analytics.serializers import WebsiteVisitsSerializer | ||
|
||
|
||
class WebsiteVisitsViewSet(viewsets.ModelViewSet): | ||
queryset = WebsiteVisits.objects.all() | ||
serializer_class = WebsiteVisitsSerializer | ||
http_method_names = ["post"] | ||
permission_classes = () | ||
|
||
|
||
class PaperEventViewSet(viewsets.ModelViewSet): | ||
queryset = PaperEvent.objects.all() | ||
serializer_class = PaperEventSerializer | ||
permission_classes = [UpdateOrDelete] | ||
filter_backends = [DjangoFilterBackend, OrderingFilter] | ||
filterset_fields = [ | ||
"paper", | ||
"created_date", | ||
"created_location", | ||
"interaction", | ||
"paper_is_boosted", | ||
] | ||
ordering = ["-created_date"] | ||
ordering_fields = ["created_date"] | ||
|
||
def create(self, request, *args, **kwargs): | ||
# Is deprecated? | ||
user = request.user | ||
if not user.is_anonymous: | ||
request.data["user"] = user.id | ||
|
||
created_location = request.data.get("created_location") | ||
if created_location is not None: | ||
created_location = created_location.upper() | ||
request.data["created_location"] = created_location | ||
else: | ||
return Response( | ||
"Missing required field `created_location`", | ||
status=status.HTTP_400_BAD_REQUEST, | ||
) | ||
|
||
interaction = request.data.get("interaction", None) | ||
if interaction is not None: | ||
interaction = interaction.upper() | ||
request.data["interaction"] = interaction | ||
|
||
paper_id = request.data["paper"] | ||
res = super().create(request, *args, **kwargs) | ||
paper_event_id = res.data["id"] | ||
if created_location == PaperEvent.PAPER and PaperEvent.VIEW: | ||
create_contribution.apply_async( | ||
( | ||
Contribution.SUPPORTER, | ||
{"app_label": "analytics", "model": "paperevent"}, | ||
user.id, | ||
paper_id, | ||
paper_event_id, | ||
), | ||
priority=2, | ||
countdown=10, | ||
) | ||
|
||
@action(detail=False, methods=["POST"], permission_classes=[]) | ||
def batch_views(self, request, *args, **kwargs): | ||
user = request.user | ||
if not user.is_anonymous: | ||
request.data["user"] = user | ||
|
||
created_location = request.data.get("created_location") | ||
if created_location is not None: | ||
created_location = created_location.upper() | ||
request.data["created_location"] = created_location | ||
else: | ||
return Response( | ||
"Missing required field `created_location`", | ||
status=status.HTTP_400_BAD_REQUEST, | ||
) | ||
|
||
interaction = request.data.get("interaction", None) | ||
if interaction is not None: | ||
interaction = interaction.upper() | ||
request.data["interaction"] = interaction | ||
|
||
events = [] | ||
paper_ids = request.data["paper_ids"] | ||
del request.data["paper_ids"] | ||
for id in paper_ids: | ||
events.append(PaperEvent(paper_id=id, **request.data)) | ||
|
||
PaperEvent.objects.bulk_create(events) | ||
return Response({"msg": "Events Created"}, 201) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters