Commit fddea2c8 authored by Imane Misrar's avatar Imane Misrar
Browse files

feat(viewsets.py) : add the new viewset LastVisitedUniversityViewSet

Relates to #149
parent d8a3b968
import logging
from django.conf import settings
from django.db import connection
from rest_framework import status
from rest_framework.permissions import BasePermission
from rest_framework.response import Response
from rest_framework.viewsets import ViewSet
......@@ -20,6 +22,7 @@ from backend_app.models.file_picture import FileViewSet, PictureViewSet
from backend_app.models.for_testing.moderation import ForTestingModerationViewSet
from backend_app.models.for_testing.versioning import ForTestingVersioningViewSet
from backend_app.models.language import LanguageViewSet
from backend_app.models.lastVisitedUniversities import LastVisitedUniversity
from backend_app.models.offer import OfferViewSet
from backend_app.models.partner import Partner
from backend_app.models.pendingModeration import (
......@@ -35,7 +38,7 @@ from backend_app.models.taggedItems import (
CountryTaggedItemViewSet,
UniversityTaggedItemViewSet,
)
from backend_app.models.university import UniversityViewSet
from backend_app.models.university import University, UniversityViewSet
from backend_app.models.universityDri import UniversityDriViewSet
from backend_app.models.universityInfo import UniversityInfoViewSet
from backend_app.models.universityScholarship import UniversityScholarshipViewSet
......@@ -346,6 +349,43 @@ class RecommendationListChangeFollowerViewSet(ViewSet):
return Response(status=200)
class LastVisitedUniversityViewSet(ViewSet):
"""
Viewset to update the LastVisitedUniversity model when a user looks at a university
Doc available here: https://www.django-rest-framework.org/api-guide/viewsets/#viewset-actions
"""
end_point_route = "lastVisitedUniversities"
permission_classes = (IsAuthenticated,)
def list(self, request):
sql = """
SELECT
university_id as university,
max(ts) AS ts
FROM backend_app_lastvisiteduniversity
WHERE
user_id = %s
GROUP BY university_id
ORDER BY ts DESC
LIMIT 5
"""
with connection.cursor() as cursor:
cursor.execute(sql, [request.user.pk])
rows = cursor.fetchall()
data = [{"university": r[0], "ts": r[1]} for r in rows]
return Response(data, status=status.HTTP_200_OK)
def create(self, request):
LastVisitedUniversity.objects.create(
user=request.user,
university=University.objects.get(pk=request.data["university"]),
)
return Response(status=status.HTTP_201_CREATED)
ALL_API_VIEW_VIEWSETS = [
AppModerationStatusViewSet,
LatestUpdateExternalDataViewSet,
......@@ -355,6 +395,7 @@ ALL_API_VIEW_VIEWSETS = [
BannedUserViewSet,
RecommendationListChangeFollowerViewSet,
DeleteUserViewSet,
LastVisitedUniversityViewSet,
]
ALL_VIEWSETS = ALL_API_VIEWSETS + ALL_API_VIEW_VIEWSETS
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment