From a19ae8df0f5d1c991e93fa2d486a17274247fc86 Mon Sep 17 00:00:00 2001 From: Ridwan Olawumi Date: Mon, 9 Dec 2024 22:47:16 +0100 Subject: [PATCH] Created API to fetch latest posts --- apps/obstracts_api/urls.py | 20 +++++++++++++++++++- apps/obstracts_api/utils.py | 33 +++++++++++++++++++++++++++++++-- apps/obstracts_api/views.py | 23 ++++++++++++++++++++++- 3 files changed, 72 insertions(+), 4 deletions(-) diff --git a/apps/obstracts_api/urls.py b/apps/obstracts_api/urls.py index 181ac00..a6a6fa3 100644 --- a/apps/obstracts_api/urls.py +++ b/apps/obstracts_api/urls.py @@ -3,7 +3,17 @@ from drf_spectacular.views import SpectacularSwaggerView from .schema import SchemaView -from .views import ProxyView, FeedViewSet, FeedProxyView, TeamFeedProxyView, TeamFeedViewSet, OpenFeedProxyView, TeamTokenFeedViewSet, PostsByExtractionView +from .views import ( + ProxyView, + FeedViewSet, + FeedProxyView, + TeamFeedProxyView, + TeamFeedViewSet, + OpenFeedProxyView, + TeamTokenFeedViewSet, + PostsByExtractionView, + LatestPostView, +) router = routers.DefaultRouter() @@ -41,4 +51,12 @@ "teams//objects//", PostsByExtractionView.as_view(), ), + path( + "posts/", + LatestPostView.as_view(), + ), + path( + "teams//posts/", + LatestPostView.as_view(), + ), ] diff --git a/apps/obstracts_api/utils.py b/apps/obstracts_api/utils.py index 07a191e..de0f4f7 100644 --- a/apps/obstracts_api/utils.py +++ b/apps/obstracts_api/utils.py @@ -12,6 +12,7 @@ def get_obstracts_job(feed_id, job_id): response.raise_for_status() return response.json() + def create_obstracts_feed( profile_id, url, @@ -32,7 +33,7 @@ def create_obstracts_feed( OBSTRACT_SERVICE_API + "/feeds/", json=data, ) - if(response.status_code == 400): + if (response.status_code == 400): raise ValidationError(response.json()) response.raise_for_status() return response.json() @@ -54,7 +55,7 @@ def create_obstracts_skeleton_feed( OBSTRACT_SERVICE_API + "/feeds/skeleton/", json=data, ) - if(response.status_code == 400): + if (response.status_code == 400): raise ValidationError(response.json()) response.raise_for_status() return response.json() @@ -63,11 +64,13 @@ def create_obstracts_skeleton_feed( def delete_obstracts_feed(feed_id): return requests.delete(f"{OBSTRACT_SERVICE_API}/feeds/{feed_id}/") + def get_obstracts_feed(feed_id): url = f"{OBSTRACT_SERVICE_API}/feeds/{feed_id}/" print(url) return requests.get(url).json() + def init_reload_feed(profile_id, feed_id): data = { "profile_id": str(profile_id), @@ -80,6 +83,7 @@ def init_reload_feed(profile_id, feed_id): response.raise_for_status() return response.json() + def get_post_for_report_object(object): external_references = object['external_references'] obstracts_feed = next(filter(lambda external_reference: external_reference['source_name'] == 'obstracts_feed_id', external_references), None) @@ -112,3 +116,28 @@ def get_posts_by_extractions(object_id, page): feed_id_dict[feed_id] = True posts.append(post) return posts, feed_id_dict.keys(), response.json() + + +def get_latest_posts(feed_ids, sort, title, page): + if feed_ids == []: + return { + "page_size": 10, + "page_number": 1, + "page_results_count": 0, + "total_results_count": 0, + "posts": [], + } + + response = requests.get( + OBSTRACT_SERVICE_API + f"/posts/", + params={ + "feed_id": feed_ids, + "page": page, + "page_size": 10, + "title": title, + "sort": sort, + } + ) + response.raise_for_status() + response_data = response.json() + return response_data diff --git a/apps/obstracts_api/views.py b/apps/obstracts_api/views.py index 40f39cc..48d29e7 100644 --- a/apps/obstracts_api/views.py +++ b/apps/obstracts_api/views.py @@ -40,7 +40,7 @@ FeedWithSubscriptionSerializer, SubscribeFeedSerializer, ) -from .utils import delete_obstracts_feed, get_posts_by_extractions +from .utils import delete_obstracts_feed, get_posts_by_extractions, get_latest_posts class ProxyView(APIView): @@ -296,6 +296,27 @@ def unsubscribe(self, *args, **kwargs): # return Response(serializer.data) +class LatestPostView(ListAPIView): + permission_classes = [IsAuthenticated] + + def list(self, *args, **kwargs): + team_id = kwargs.get("team_id") + page = self.request.query_params.get("page") + title = self.request.query_params.get("title") + sort = self.request.query_params.get("sort", "pubdate_descending") + feeds = [] + feed_ids = None + if team_id: + feeds = FeedSubsription.objects.filter(team_id=team_id) + feed_ids = [feed.feed_id for feed in feeds] + else: + if not self.request.user.is_staff: + raise PermissionDenied() + + response = get_latest_posts(feed_ids, sort, title, page) + return Response(response) + + class PostsByExtractionView(ListAPIView): permission_classes = [IsAuthenticated]