Add: posts api app
This commit is contained in:
parent
141e08f0f4
commit
a5b0580be6
6 changed files with 197 additions and 0 deletions
0
backend/api/posts/__init__.py
Normal file
0
backend/api/posts/__init__.py
Normal file
6
backend/api/posts/apps.py
Normal file
6
backend/api/posts/apps.py
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
from django.apps import AppConfig
|
||||||
|
|
||||||
|
|
||||||
|
class ApiConfig(AppConfig):
|
||||||
|
default_auto_field = "django.db.models.BigAutoField"
|
||||||
|
name = "api.posts"
|
148
backend/api/posts/serializers.py
Normal file
148
backend/api/posts/serializers.py
Normal file
|
@ -0,0 +1,148 @@
|
||||||
|
from rest_framework import serializers
|
||||||
|
|
||||||
|
from django.utils.timezone import localtime
|
||||||
|
|
||||||
|
from apps.archive.models import PostModel
|
||||||
|
|
||||||
|
|
||||||
|
class PostListSerializer(serializers.Serializer):
|
||||||
|
count = serializers.SerializerMethodField()
|
||||||
|
posts = serializers.SerializerMethodField()
|
||||||
|
|
||||||
|
class Meta:
|
||||||
|
model = PostModel
|
||||||
|
fields = ["count", "posts"]
|
||||||
|
|
||||||
|
def get_count(self, obj):
|
||||||
|
return obj.count()
|
||||||
|
|
||||||
|
def get_posts(self, obj):
|
||||||
|
return PostPreviewSerializer(obj.all(), many=True).data
|
||||||
|
|
||||||
|
|
||||||
|
class PostPreviewSerializer(serializers.ModelSerializer):
|
||||||
|
description = serializers.SerializerMethodField()
|
||||||
|
date = serializers.SerializerMethodField()
|
||||||
|
media = serializers.SerializerMethodField()
|
||||||
|
source_site = serializers.SerializerMethodField()
|
||||||
|
category = serializers.SerializerMethodField()
|
||||||
|
creator = serializers.SerializerMethodField()
|
||||||
|
tags = serializers.SerializerMethodField()
|
||||||
|
|
||||||
|
class Meta:
|
||||||
|
model = PostModel
|
||||||
|
fields = [
|
||||||
|
"post_id",
|
||||||
|
"mature",
|
||||||
|
"title",
|
||||||
|
"description",
|
||||||
|
"source_site",
|
||||||
|
"category",
|
||||||
|
"creator",
|
||||||
|
"date",
|
||||||
|
"media",
|
||||||
|
"tags",
|
||||||
|
]
|
||||||
|
|
||||||
|
def get_description(self, obj):
|
||||||
|
return obj.description.first().content
|
||||||
|
|
||||||
|
def get_source_site(self, obj):
|
||||||
|
return {
|
||||||
|
"slug": obj.source_site.slug,
|
||||||
|
"name": obj.source_site.name,
|
||||||
|
}
|
||||||
|
|
||||||
|
def get_category(self, obj):
|
||||||
|
data = []
|
||||||
|
for i in obj.category.all():
|
||||||
|
data.append({"slug": i.slug, "name": i.name})
|
||||||
|
return data
|
||||||
|
|
||||||
|
def get_creator(self, obj):
|
||||||
|
avatar_url = None
|
||||||
|
if obj.creator.avatar:
|
||||||
|
avatar_url = obj.creator.avatar.file.url
|
||||||
|
|
||||||
|
return {
|
||||||
|
"slug": obj.creator.slug,
|
||||||
|
"name": obj.creator.name,
|
||||||
|
"avatar": avatar_url,
|
||||||
|
}
|
||||||
|
|
||||||
|
def get_date(self, obj):
|
||||||
|
return {
|
||||||
|
"created": localtime(obj.date_created).isoformat(),
|
||||||
|
"imported": localtime(obj.date_imported).isoformat(),
|
||||||
|
"last_import": localtime(obj.date_last_import).isoformat(),
|
||||||
|
}
|
||||||
|
|
||||||
|
def get_media(self, obj):
|
||||||
|
data = []
|
||||||
|
for i in obj.files.all():
|
||||||
|
data.append({"type": i.mimetype, "src": i.file.url})
|
||||||
|
return data
|
||||||
|
|
||||||
|
def get_tags(self, obj):
|
||||||
|
return [tag.slug for tag in obj.tags.all()]
|
||||||
|
|
||||||
|
|
||||||
|
class PostSerializer(serializers.ModelSerializer):
|
||||||
|
source_site = serializers.SerializerMethodField()
|
||||||
|
description = serializers.SerializerMethodField()
|
||||||
|
creator = serializers.SerializerMethodField()
|
||||||
|
tags = serializers.SerializerMethodField()
|
||||||
|
date = serializers.SerializerMethodField()
|
||||||
|
media = serializers.SerializerMethodField()
|
||||||
|
|
||||||
|
class Meta:
|
||||||
|
model = PostModel
|
||||||
|
fields = [
|
||||||
|
"post_id",
|
||||||
|
"title",
|
||||||
|
"description",
|
||||||
|
"source_site",
|
||||||
|
"creator",
|
||||||
|
"date",
|
||||||
|
"mature",
|
||||||
|
"media",
|
||||||
|
"tags",
|
||||||
|
]
|
||||||
|
|
||||||
|
def get_source_site(self, obj):
|
||||||
|
return {
|
||||||
|
"slug": obj.source_site.slug,
|
||||||
|
"name": obj.source_site.name,
|
||||||
|
}
|
||||||
|
|
||||||
|
def get_description(self, obj):
|
||||||
|
return {
|
||||||
|
"count": obj.description.count(),
|
||||||
|
"content": obj.description.first().content,
|
||||||
|
}
|
||||||
|
|
||||||
|
def get_creator(self, obj):
|
||||||
|
return {
|
||||||
|
"slug": obj.creator.slug,
|
||||||
|
"name": obj.creator.name,
|
||||||
|
# "source_site": {
|
||||||
|
# "slug": obj.creator.source_site.slug,
|
||||||
|
# "name": obj.creator.source_site.name,
|
||||||
|
# },
|
||||||
|
}
|
||||||
|
|
||||||
|
def get_tags(self, obj):
|
||||||
|
return [tag.slug for tag in obj.tags.all()]
|
||||||
|
|
||||||
|
def get_date(self, obj):
|
||||||
|
return {
|
||||||
|
"created": localtime(obj.date_created).isoformat(),
|
||||||
|
"imported": localtime(obj.date_imported).isoformat(),
|
||||||
|
"last_import": localtime(obj.date_last_import).isoformat(),
|
||||||
|
}
|
||||||
|
|
||||||
|
def get_media(self, obj):
|
||||||
|
data = []
|
||||||
|
for i in obj.files.all():
|
||||||
|
data.append({"type": i.mimetype, "url": i.file.url})
|
||||||
|
return data
|
8
backend/api/posts/urls.py
Normal file
8
backend/api/posts/urls.py
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
from django.urls import path
|
||||||
|
|
||||||
|
from api.posts.views import PostView, PostListView
|
||||||
|
|
||||||
|
urlpatterns = [
|
||||||
|
path("<str:post_id>/", PostView.as_view(), name="post_details"),
|
||||||
|
path("", PostListView.as_view(), name="post_list"),
|
||||||
|
]
|
34
backend/api/posts/views.py
Normal file
34
backend/api/posts/views.py
Normal file
|
@ -0,0 +1,34 @@
|
||||||
|
from rest_framework.response import Response
|
||||||
|
from rest_framework.views import APIView
|
||||||
|
from rest_framework.permissions import IsAuthenticated
|
||||||
|
|
||||||
|
from apps.archive.models import PostModel
|
||||||
|
|
||||||
|
from .serializers import PostListSerializer, PostSerializer
|
||||||
|
|
||||||
|
|
||||||
|
class PostListView(APIView):
|
||||||
|
permission_classes = [IsAuthenticated]
|
||||||
|
|
||||||
|
def get(self, request):
|
||||||
|
user = request.user.userprofile
|
||||||
|
|
||||||
|
if user.show_mature:
|
||||||
|
posts = PostModel.objects.all().order_by("-date_created")
|
||||||
|
else:
|
||||||
|
posts = PostModel.objects.filter(mature=False).order_by("-date_created")
|
||||||
|
serializer = PostListSerializer(posts)
|
||||||
|
return Response(serializer.data)
|
||||||
|
|
||||||
|
|
||||||
|
class PostView(APIView):
|
||||||
|
permission_classes = [IsAuthenticated]
|
||||||
|
|
||||||
|
def get(self, request, post_id):
|
||||||
|
try:
|
||||||
|
post_instance = PostModel.objects.get(post_id=post_id)
|
||||||
|
except PostModel.DoesNotExist:
|
||||||
|
raise NotFound(detail="Post not found.")
|
||||||
|
|
||||||
|
serializer = PostSerializer(post_instance)
|
||||||
|
return Response(serializer.data)
|
|
@ -4,4 +4,5 @@ from django.urls import path, include
|
||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
path("auth/", include("api.authentication.urls")),
|
path("auth/", include("api.authentication.urls")),
|
||||||
path("user/", include("api.user.urls")),
|
path("user/", include("api.user.urls")),
|
||||||
|
path("posts/", include("api.posts.urls")),
|
||||||
]
|
]
|
||||||
|
|
Loading…
Add table
Reference in a new issue