Skip to content

Commit

Permalink
Merge pull request #2869 from uktrade/feat/recent-catalogue-items-end…
Browse files Browse the repository at this point in the history
…point

Add recent catalogue items endpoint
  • Loading branch information
ian-leggett authored Nov 21, 2023
2 parents ef469f1 + 8b07a10 commit d92ba90
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 0 deletions.
7 changes: 7 additions & 0 deletions dataworkspace/dataworkspace/apps/api_v2/recent_items/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from rest_framework.routers import DefaultRouter

from dataworkspace.apps.api_v2.recent_items.views import RecentItemsViewSet

router = DefaultRouter()
router.register(r"recent_items", RecentItemsViewSet)
urlpatterns = router.urls
29 changes: 29 additions & 0 deletions dataworkspace/dataworkspace/apps/api_v2/recent_items/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
from rest_framework import viewsets
from rest_framework.pagination import PageNumberPagination
from rest_framework.authentication import SessionAuthentication
from rest_framework.permissions import IsAuthenticated

from dataworkspace.apps.api_v1.eventlog.serializers import EventLogSerializer
from dataworkspace.apps.api_v1.mixins import TimestampFilterMixin
from dataworkspace.apps.eventlog.models import EventLog


class TimestampPageNumberPagination(PageNumberPagination):
ordering = ("-timestamp", "id")
page_size_query_param = "page_size"
max_page_size = 10_000


class RecentItemsViewSet(TimestampFilterMixin, viewsets.ModelViewSet):
queryset = EventLog.objects.all()
serializer_class = EventLogSerializer
pagination_class = TimestampPageNumberPagination
authentication_classes = [SessionAuthentication]
permission_classes = [IsAuthenticated]

def get_queryset(self):
return (
super()
.get_queryset()
.filter(user=self.request.user, event_type=EventLog.TYPE_DATASET_VIEW)
)
6 changes: 6 additions & 0 deletions dataworkspace/dataworkspace/apps/api_v2/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,10 @@
("dataworkspace.apps.api_v2.data_collections.urls", "api_v2"), namespace="collections"
),
),
path(
"",
include(
("dataworkspace.apps.api_v2.recent_items.urls", "api_v2"), namespace="recent_items"
),
),
]
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import pytest
from django.urls import reverse
from rest_framework import status

from dataworkspace.tests import factories
from dataworkspace.apps.eventlog.models import EventLog


@pytest.mark.django_db
def test_unauthenticated_recent_items(unauthenticated_client):
response = unauthenticated_client.get(reverse("api-v2:recent_items:eventlog-list"))
assert response.status_code == status.HTTP_403_FORBIDDEN


@pytest.mark.django_db
def test_ordering_and_filtering_recent_items(client, user):
client.force_login(user)
user_event1 = factories.EventLogFactory(
user=user,
event_type=EventLog.TYPE_DATASET_VIEW,
related_object=factories.ReferenceDatasetFactory.create(),
)
user_event2 = factories.EventLogFactory(
user=user,
event_type=EventLog.TYPE_DATASET_VIEW,
related_object=factories.ReferenceDatasetFactory.create(),
)
user_event3 = factories.EventLogFactory(
user=user,
event_type=EventLog.TYPE_DATASET_VIEW,
related_object=factories.ReferenceDatasetFactory.create(),
)
factories.EventLogFactory(
event_type=EventLog.TYPE_DATASET_VIEW,
related_object=factories.ReferenceDatasetFactory.create(),
)
factories.EventLogFactory(
user=user,
event_type=EventLog.TYPE_DATASET_FIND_FORM_QUERY,
related_object=factories.ReferenceDatasetFactory.create(),
)

response = client.get(reverse("api-v2:recent_items:eventlog-list"))
recent_items = response.json()
assert len(recent_items["results"]) == 3
assert recent_items["results"][0]["id"] == user_event3.id
assert recent_items["results"][1]["id"] == user_event2.id
assert recent_items["results"][2]["id"] == user_event1.id
assert response.status_code == status.HTTP_200_OK

0 comments on commit d92ba90

Please sign in to comment.