Add: post endpoints for singel post py user, source_site
This commit is contained in:
parent
91e7ea2e81
commit
e02935eac8
2 changed files with 80 additions and 6 deletions
|
@ -1,8 +1,30 @@
|
||||||
from django.urls import path
|
from django.urls import path
|
||||||
|
from api.posts.views import (
|
||||||
from api.posts.views import PostDetailView, PostListView
|
PostDetailView,
|
||||||
|
PostDetailSiteCreatorView,
|
||||||
|
PostListView,
|
||||||
|
PostListSourceView,
|
||||||
|
PostListSourceCreatorView,
|
||||||
|
PostListSourceCategoryView,
|
||||||
|
)
|
||||||
|
|
||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
path("<str:post_id>/", PostDetailView.as_view(), name="post_detail"),
|
path("<str:post_id>/", PostDetailView.as_view(), name="post_detail"),
|
||||||
|
path(
|
||||||
|
"<str:source_site>/<str:creator_slug_or_id>/<str:post_id>/",
|
||||||
|
PostDetailSiteCreatorView.as_view(),
|
||||||
|
name="post_detail_with_site_creator",
|
||||||
|
),
|
||||||
path("", PostListView.as_view(), name="post_list"),
|
path("", PostListView.as_view(), name="post_list"),
|
||||||
|
path("<str:source_site>/", PostListSourceView.as_view(), name="post_list_source"),
|
||||||
|
path(
|
||||||
|
"<str:source_site>/<str:creator_slug_or_id>/",
|
||||||
|
PostListSourceCreatorView.as_view(),
|
||||||
|
name="post_list_source_creator",
|
||||||
|
),
|
||||||
|
path(
|
||||||
|
"<str:source_site>/<str:creator_slug_or_id>/<str:category>",
|
||||||
|
PostListSourceCategoryView.as_view(),
|
||||||
|
name="post_list_source_creator_category",
|
||||||
|
),
|
||||||
]
|
]
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
from rest_framework.generics import ListAPIView, RetrieveAPIView
|
from rest_framework.generics import ListAPIView, RetrieveAPIView
|
||||||
from rest_framework.response import Response
|
from rest_framework.response import Response
|
||||||
from rest_framework.permissions import IsAuthenticated
|
from rest_framework.permissions import IsAuthenticated
|
||||||
|
from drf_spectacular.utils import extend_schema
|
||||||
|
|
||||||
from apps.archive.models import PostModel
|
from apps.archive.models import PostModel
|
||||||
|
|
||||||
|
@ -35,12 +36,53 @@ class PostListView(ListAPIView):
|
||||||
|
|
||||||
def get_queryset(self):
|
def get_queryset(self):
|
||||||
user = self.request.user.userprofile
|
user = self.request.user.userprofile
|
||||||
if user.show_mature:
|
queryset = PostModel.objects.all()
|
||||||
queryset = PostModel.objects.all()
|
|
||||||
else:
|
# Apply mature filtering
|
||||||
queryset = PostModel.objects.filter(mature=False)
|
if not user.show_mature:
|
||||||
|
queryset = queryset.filter(mature=False)
|
||||||
|
|
||||||
|
# Extract optional parameters
|
||||||
|
source_site = self.kwargs.get("source_site")
|
||||||
|
creator_slug_or_id = self.kwargs.get("creator_slug_or_id")
|
||||||
|
category = self.kwargs.get("category")
|
||||||
|
|
||||||
|
# Filter by source_site if provided
|
||||||
|
if source_site:
|
||||||
|
queryset = queryset.filter(source_site__slug=source_site)
|
||||||
|
|
||||||
|
# Filter by creator (either by slug or id)
|
||||||
|
if creator_slug_or_id:
|
||||||
|
queryset = queryset.filter(
|
||||||
|
creator__slug=creator_slug_or_id
|
||||||
|
) | queryset.filter(creator__creator_id=creator_slug_or_id)
|
||||||
|
|
||||||
|
if category:
|
||||||
|
queryset = queryset.filter(category__slug=category)
|
||||||
|
|
||||||
return queryset.order_by("-date_created")
|
return queryset.order_by("-date_created")
|
||||||
|
|
||||||
|
@extend_schema(operation_id="posts_list_all")
|
||||||
|
def get(self, request, *args, **kwargs):
|
||||||
|
return super().get(request, *args, **kwargs)
|
||||||
|
|
||||||
|
|
||||||
|
class PostListSourceView(PostListView):
|
||||||
|
@extend_schema(operation_id="posts_list_by_source")
|
||||||
|
def get(self, request, *args, **kwargs):
|
||||||
|
return super().get(request, *args, **kwargs)
|
||||||
|
|
||||||
|
|
||||||
|
class PostListSourceCreatorView(PostListView):
|
||||||
|
@extend_schema(operation_id="posts_list_by_source_and_creator")
|
||||||
|
def get(self, request, *args, **kwargs):
|
||||||
|
return super().get(request, *args, **kwargs)
|
||||||
|
|
||||||
|
|
||||||
|
class PostListSourceCategoryView(PostListView):
|
||||||
|
@extend_schema(operation_id="posts_list_by_source_creator_and_category")
|
||||||
|
def get(self, request, *args, **kwargs):
|
||||||
|
return super().get(request, *args, **kwargs)
|
||||||
|
|
||||||
|
|
||||||
class PostDetailView(RetrieveAPIView):
|
class PostDetailView(RetrieveAPIView):
|
||||||
|
@ -50,3 +92,13 @@ class PostDetailView(RetrieveAPIView):
|
||||||
"post_id" # This tells DRF to use the "post_id" URL kwarg for lookups.
|
"post_id" # This tells DRF to use the "post_id" URL kwarg for lookups.
|
||||||
)
|
)
|
||||||
queryset = PostModel.objects.all()
|
queryset = PostModel.objects.all()
|
||||||
|
|
||||||
|
@extend_schema(operation_id="posts_retrieve_by_id")
|
||||||
|
def get(self, request, *args, **kwargs):
|
||||||
|
return super().get(request, *args, **kwargs)
|
||||||
|
|
||||||
|
|
||||||
|
class PostDetailSiteCreatorView(PostDetailView):
|
||||||
|
@extend_schema(operation_id="posts_retrieve_by_site_creator_id")
|
||||||
|
def get(self, request, *args, **kwargs):
|
||||||
|
return super().get(request, *args, **kwargs)
|
||||||
|
|
Loading…
Add table
Reference in a new issue