diff --git a/src/analytics/migrations/0008_delete_paperevent.py b/src/analytics/migrations/0008_delete_paperevent.py new file mode 100644 index 000000000..2fd75d9dd --- /dev/null +++ b/src/analytics/migrations/0008_delete_paperevent.py @@ -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", + ), + ] diff --git a/src/analytics/models.py b/src/analytics/models.py index 67b1d0c22..50af0e0c4 100644 --- a/src/analytics/models.py +++ b/src/analytics/models.py @@ -1,7 +1,5 @@ from django.db import models -from django.db.models import JSONField -from researchhub.lib import CREATED_LOCATION_CHOICES from user.models import User INTERACTIONS = { @@ -24,39 +22,3 @@ class WebsiteVisits(models.Model): def __str__(self): return f"{self.uuid}" - - -class PaperEvent(models.Model): - CLICK = "CLICK" - VIEW = "VIEW" - PAPER = "PAPER" - - paper = models.ForeignKey( - "paper.Paper", - on_delete=models.SET_NULL, - related_name="events", - related_query_name="event", - null=True, - ) - user = models.ForeignKey( - "user.User", - on_delete=models.SET_NULL, - related_name="paper_events", - related_query_name="paper_event", - null=True, - blank=True, - ) - interaction = models.CharField( - choices=INTERACTION_CHOICES, max_length=255, default=None, null=True, blank=True - ) - created_date = models.DateTimeField(auto_now_add=True) - updated_date = models.DateTimeField(auto_now=True) - created_location = models.CharField( - choices=CREATED_LOCATION_CHOICES, - max_length=255, - default=None, - null=True, - blank=True, - ) - created_location_meta = JSONField(blank=True, null=True) - paper_is_boosted = models.BooleanField() diff --git a/src/analytics/permissions.py b/src/analytics/permissions.py deleted file mode 100644 index 68c673b60..000000000 --- a/src/analytics/permissions.py +++ /dev/null @@ -1,11 +0,0 @@ -from utils.http import POST -from utils.permissions import AuthorizationBasedPermission - - -class UpdateOrDelete(AuthorizationBasedPermission): - message = 'User is not authorized.' - - def is_authorized(self, request, view, obj): - if request.method == POST: - return True - return request.user.is_staff diff --git a/src/analytics/serializers.py b/src/analytics/serializers.py index 8e815ab4a..52fca0f96 100644 --- a/src/analytics/serializers.py +++ b/src/analytics/serializers.py @@ -1,7 +1,6 @@ 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): @@ -9,16 +8,3 @@ 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 = [] diff --git a/src/analytics/views.py b/src/analytics/views.py index c3a2ed8d6..60aeea2b3 100644 --- a/src/analytics/views.py +++ b/src/analytics/views.py @@ -1,14 +1,7 @@ -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): @@ -16,86 +9,3 @@ class WebsiteVisitsViewSet(viewsets.ModelViewSet): 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) diff --git a/src/purchase/serializers/purchase_serializer.py b/src/purchase/serializers/purchase_serializer.py index d4ade8e84..ad6f0c206 100644 --- a/src/purchase/serializers/purchase_serializer.py +++ b/src/purchase/serializers/purchase_serializer.py @@ -1,10 +1,10 @@ import datetime import rest_framework.serializers as serializers -from django.db.models import CharField, Count, F, Func, IntegerField, Sum, Value +from django.db.models import IntegerField, Sum from django.db.models.functions import Cast -from analytics.models import INTERACTIONS, PaperEvent +from analytics.models import INTERACTIONS from discussion.serializers import ( CommentSerializer, DynamicCommentSerializer, @@ -194,49 +194,10 @@ def get_purchases(self, purchase): return data def get_stats(self, purchase): - distinct_views = purchase.purchases.filter( - paper__event__interaction=INTERACTIONS["VIEW"], - paper__event__paper_is_boosted=True, - ).distinct() - distinct_clicks = purchase.purchases.filter( - paper__event__interaction=INTERACTIONS["CLICK"], - paper__event__paper_is_boosted=True, - ).distinct() - - total_views = distinct_views.values("paper__event").count() - total_clicks = distinct_clicks.values("paper__event").count() total_amount = sum( map(float, purchase.purchases.values_list("amount", flat=True)) ) - distinct_views_ids = distinct_views.values_list("paper__event", flat=True) - views = ( - PaperEvent.objects.filter(id__in=distinct_views_ids) - .values( - date=Func( - F("created_date"), - Value("YYYY-MM-DD"), - function="to_char", - output_field=CharField(), - ) - ) - .annotate(views=Count("date")) - ) - - distinct_clicks_ids = distinct_clicks.values_list("paper__event", flat=True) - clicks = ( - PaperEvent.objects.filter(id__in=distinct_clicks_ids) - .values( - date=Func( - F("created_date"), - Value("YYYY-MM-DD"), - function="to_char", - output_field=CharField(), - ) - ) - .annotate(clicks=Count("date")) - ) - created_date = purchase.created_date max_boost = ( @@ -250,10 +211,8 @@ def get_stats(self, purchase): end_date = (created_date + timedelta).isoformat() stats = { - "views": views, - "clicks": clicks, - "total_views": total_views, - "total_clicks": total_clicks, + "total_views": 0, # TODO: Remove deprecated field + "total_clicks": 0, # TODO: Remove deprecated field "total_amount": total_amount, "end_date": end_date, } diff --git a/src/researchhub/urls.py b/src/researchhub/urls.py index c1ef253e4..1ae8be520 100644 --- a/src/researchhub/urls.py +++ b/src/researchhub/urls.py @@ -105,10 +105,6 @@ basename="websiteviews", ) -router.register( - r"events/paper", analytics.views.PaperEventViewSet, basename="events_paper" -) - router.register(r"purchase", purchase.views.PurchaseViewSet, basename="purchase") router.register(r"transactions", purchase.views.BalanceViewSet, basename="transactions")