Skip to content

Commit

Permalink
Merge pull request #31 from muchdogesec/latest-posts
Browse files Browse the repository at this point in the history
Created API to fetch latest posts
  • Loading branch information
himynamesdave authored Dec 10, 2024
2 parents 2108081 + a19ae8d commit 632a48b
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 4 deletions.
20 changes: 19 additions & 1 deletion apps/obstracts_api/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -41,4 +51,12 @@
"teams/<uuid:team_id>/objects/<str:object_id>/",
PostsByExtractionView.as_view(),
),
path(
"posts/",
LatestPostView.as_view(),
),
path(
"teams/<uuid:team_id>/posts/",
LatestPostView.as_view(),
),
]
33 changes: 31 additions & 2 deletions apps/obstracts_api/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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()
Expand All @@ -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()
Expand All @@ -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),
Expand All @@ -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)
Expand Down Expand Up @@ -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
23 changes: 22 additions & 1 deletion apps/obstracts_api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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]

Expand Down

0 comments on commit 632a48b

Please sign in to comment.