Refactor: to properly use DRFs view functions
This commit is contained in:
parent
3d7e25e20c
commit
3657b4423f
3 changed files with 47 additions and 66 deletions
|
@ -1,3 +1,4 @@
|
|||
from typing import Any, Dict, List, Optional
|
||||
from rest_framework import serializers
|
||||
|
||||
from django.utils.timezone import localtime
|
||||
|
@ -5,21 +6,6 @@ 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()
|
||||
|
@ -44,46 +30,37 @@ class PostPreviewSerializer(serializers.ModelSerializer):
|
|||
"tags",
|
||||
]
|
||||
|
||||
def get_description(self, obj):
|
||||
return obj.description.first().content
|
||||
def get_description(self, obj: PostModel) -> Optional[str]:
|
||||
return obj.description.first().content if obj.description.exists() else None
|
||||
|
||||
def get_source_site(self, obj):
|
||||
def get_source_site(self, obj: PostModel) -> Dict[str, str]:
|
||||
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
|
||||
def get_category(self, obj: PostModel) -> List[Dict[str, str]]:
|
||||
return [{"slug": i.slug, "name": i.name} for i in obj.category.all()]
|
||||
|
||||
def get_creator(self, obj: PostModel) -> Dict[str, Optional[str]]:
|
||||
avatar_url = obj.creator.avatar.file.url if obj.creator.avatar else None
|
||||
return {
|
||||
"slug": obj.creator.slug,
|
||||
"name": obj.creator.name,
|
||||
"avatar": avatar_url,
|
||||
}
|
||||
|
||||
def get_date(self, obj):
|
||||
def get_date(self, obj: PostModel) -> Dict[str, str]:
|
||||
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_media(self, obj: PostModel) -> List[Dict[str, str]]:
|
||||
return [{"type": i.mimetype, "src": i.file.url} for i in obj.files.all()]
|
||||
|
||||
def get_tags(self, obj):
|
||||
def get_tags(self, obj: PostModel) -> List[str]:
|
||||
return [tag.slug for tag in obj.tags.all()]
|
||||
|
||||
|
||||
|
@ -109,39 +86,35 @@ class PostSerializer(serializers.ModelSerializer):
|
|||
"tags",
|
||||
]
|
||||
|
||||
def get_source_site(self, obj):
|
||||
def get_source_site(self, obj) -> Dict[str, str]:
|
||||
return {
|
||||
"slug": obj.source_site.slug,
|
||||
"name": obj.source_site.name,
|
||||
}
|
||||
|
||||
def get_description(self, obj):
|
||||
def get_description(self, obj) -> Dict[str, str]:
|
||||
return {
|
||||
"count": obj.description.count(),
|
||||
"content": obj.description.first().content,
|
||||
}
|
||||
|
||||
def get_creator(self, obj):
|
||||
def get_creator(self, obj) -> Dict[str, str]:
|
||||
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):
|
||||
def get_tags(self, obj) -> List[str]:
|
||||
return [tag.slug for tag in obj.tags.all()]
|
||||
|
||||
def get_date(self, obj):
|
||||
def get_date(self, obj) -> Dict[str, str]:
|
||||
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):
|
||||
def get_media(self, obj) -> List[Dict[str, str]]:
|
||||
data = []
|
||||
for i in obj.files.all():
|
||||
data.append({"type": i.mimetype, "url": i.file.url})
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue