2023-05-03 14:23:44 +09:00
|
|
|
from rest_framework import serializers
|
|
|
|
from rest_framework.serializers import ModelSerializer
|
|
|
|
|
|
|
|
from market.models import (
|
|
|
|
Brand,
|
|
|
|
Product,
|
|
|
|
Post,
|
|
|
|
Image,
|
2023-11-09 19:41:37 +09:00
|
|
|
ItemIssues,
|
2023-05-03 14:23:44 +09:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2023-05-18 11:02:46 +09:00
|
|
|
class ProductSerializer(ModelSerializer):
|
2023-05-03 14:23:44 +09:00
|
|
|
class Meta:
|
2023-05-18 11:02:46 +09:00
|
|
|
model = Product
|
2023-06-19 03:56:23 +09:00
|
|
|
fields = ("id", "name", "brand")
|
2023-05-18 11:02:46 +09:00
|
|
|
depth = 1
|
2023-05-03 14:23:44 +09:00
|
|
|
|
|
|
|
|
2023-05-18 11:02:46 +09:00
|
|
|
class ProductListSerializer(ModelSerializer):
|
2023-05-03 14:23:44 +09:00
|
|
|
class Meta:
|
2023-05-18 11:02:46 +09:00
|
|
|
model = Product
|
|
|
|
fields = ("id", "name")
|
2023-05-03 14:23:44 +09:00
|
|
|
|
|
|
|
|
2023-05-18 11:02:46 +09:00
|
|
|
class BrandSerializer(ModelSerializer):
|
|
|
|
products = ProductListSerializer(many=True, read_only=True)
|
2023-05-03 14:23:44 +09:00
|
|
|
|
|
|
|
class Meta:
|
2023-05-18 11:02:46 +09:00
|
|
|
model = Brand
|
|
|
|
fields = ["id", "name", "products"]
|
2023-05-03 14:23:44 +09:00
|
|
|
|
|
|
|
|
2023-05-18 11:02:46 +09:00
|
|
|
class BrandListSerializer(ModelSerializer):
|
2023-05-17 16:10:25 +09:00
|
|
|
class Meta:
|
2023-05-18 11:02:46 +09:00
|
|
|
model = Brand
|
|
|
|
fields = "__all__"
|
2023-05-17 16:10:25 +09:00
|
|
|
|
|
|
|
|
2023-05-03 14:23:44 +09:00
|
|
|
class ImageSerializer(ModelSerializer):
|
|
|
|
class Meta:
|
|
|
|
model = Image
|
|
|
|
fields = ("image",)
|
|
|
|
|
|
|
|
|
2023-11-13 19:52:35 +09:00
|
|
|
class ImageCreateSerializer(ModelSerializer):
|
|
|
|
class Meta:
|
|
|
|
model = Image
|
|
|
|
fields = ("post", "image")
|
|
|
|
|
|
|
|
|
2023-11-09 19:41:37 +09:00
|
|
|
class ItemIssuesSerializer(ModelSerializer):
|
|
|
|
class Meta:
|
|
|
|
model = ItemIssues
|
|
|
|
fields = (
|
|
|
|
"display",
|
|
|
|
"frame",
|
|
|
|
"button",
|
|
|
|
"biometric",
|
|
|
|
"camera",
|
|
|
|
"speaker",
|
|
|
|
"others",
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2023-06-16 16:36:27 +09:00
|
|
|
class PostListSerializer(ModelSerializer):
|
|
|
|
product = ProductListSerializer(read_only=True)
|
|
|
|
nickname = serializers.CharField(source="author.nickname")
|
|
|
|
image = serializers.SerializerMethodField()
|
2023-11-09 19:41:37 +09:00
|
|
|
item_issues = ItemIssuesSerializer(read_only=True)
|
2023-06-16 16:36:27 +09:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = Post
|
|
|
|
fields = (
|
|
|
|
"id",
|
|
|
|
"product",
|
|
|
|
"price",
|
|
|
|
"text",
|
|
|
|
"nickname",
|
2023-11-09 19:41:37 +09:00
|
|
|
"item_issues",
|
|
|
|
"done",
|
2023-06-16 16:36:27 +09:00
|
|
|
"written_at",
|
|
|
|
"image",
|
|
|
|
)
|
|
|
|
|
|
|
|
def get_image(self, obj):
|
|
|
|
first_image = obj.images.first()
|
|
|
|
return ImageSerializer(
|
2023-11-09 19:41:37 +09:00
|
|
|
instance=first_image,
|
|
|
|
context=self.context,
|
2023-06-16 16:36:27 +09:00
|
|
|
).data["image"]
|
|
|
|
|
|
|
|
|
2023-06-19 03:56:23 +09:00
|
|
|
class PostCreateSerializer(ModelSerializer):
|
2023-11-09 19:41:37 +09:00
|
|
|
item_issues = ItemIssuesSerializer(write_only=True)
|
2023-11-13 19:52:35 +09:00
|
|
|
# item_issues = serializers.JSONField(write_only=True)
|
|
|
|
|
|
|
|
photos = serializers.ListField(
|
|
|
|
child=serializers.ImageField(),
|
|
|
|
write_only=True,
|
|
|
|
)
|
|
|
|
|
|
|
|
def create(self, validated_data):
|
|
|
|
item_issues_data = validated_data.pop("item_issues")
|
|
|
|
item_issues = ItemIssues.objects.create(**item_issues_data)
|
|
|
|
validated_data["item_issues"] = item_issues
|
|
|
|
photos = validated_data.pop("photos", [])
|
|
|
|
post = super().create(validated_data)
|
|
|
|
for photo in photos:
|
|
|
|
Image.objects.create(post=post, image=photo)
|
|
|
|
return post
|
2023-11-09 19:41:37 +09:00
|
|
|
|
2023-06-19 03:56:23 +09:00
|
|
|
class Meta:
|
|
|
|
model = Post
|
|
|
|
fields = (
|
|
|
|
"product",
|
|
|
|
"price",
|
|
|
|
"text",
|
2023-11-09 19:41:37 +09:00
|
|
|
"item_issues",
|
2023-11-13 19:52:35 +09:00
|
|
|
"photos",
|
2023-06-19 03:56:23 +09:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2023-11-09 19:41:37 +09:00
|
|
|
class PostUpdateSerializer(ModelSerializer):
|
2023-06-23 02:09:45 +09:00
|
|
|
class Meta:
|
|
|
|
model = Post
|
2023-11-09 19:41:37 +09:00
|
|
|
fields = (
|
|
|
|
"price",
|
|
|
|
"text",
|
|
|
|
"done",
|
|
|
|
)
|
2023-06-23 02:09:45 +09:00
|
|
|
|
|
|
|
|
2023-05-03 14:23:44 +09:00
|
|
|
class PostSerializer(ModelSerializer):
|
2023-05-17 16:10:25 +09:00
|
|
|
product = ProductListSerializer(read_only=True)
|
2023-05-03 14:23:44 +09:00
|
|
|
nickname = serializers.CharField(source="author.nickname")
|
|
|
|
images = ImageSerializer(many=True, read_only=True)
|
2023-11-09 19:41:37 +09:00
|
|
|
item_issues = ItemIssuesSerializer(read_only=True)
|
2023-05-03 14:23:44 +09:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = Post
|
|
|
|
fields = (
|
|
|
|
"id",
|
|
|
|
"product",
|
|
|
|
"price",
|
|
|
|
"text",
|
|
|
|
"nickname",
|
2023-11-09 19:41:37 +09:00
|
|
|
"item_issues",
|
|
|
|
"done",
|
2023-05-03 14:23:44 +09:00
|
|
|
"written_at",
|
|
|
|
"images",
|
|
|
|
)
|