-
Notifications
You must be signed in to change notification settings - Fork 26
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 #2869 from uktrade/feat/recent-catalogue-items-end…
…point Add recent catalogue items endpoint
- Loading branch information
Showing
5 changed files
with
91 additions
and
0 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,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
29
dataworkspace/dataworkspace/apps/api_v2/recent_items/views.py
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,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) | ||
) |
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
Empty file.
49 changes: 49 additions & 0 deletions
49
dataworkspace/dataworkspace/tests/api_v2/recent_items/test_recent_items.py
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,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 |