153 lines
3.5 KiB
Python
153 lines
3.5 KiB
Python
from rest_framework import serializers
|
|
from rest_framework.serializers import ModelSerializer
|
|
|
|
from market.models import (
|
|
Brand,
|
|
Product,
|
|
Post,
|
|
Image,
|
|
ItemIssues,
|
|
)
|
|
|
|
|
|
class ProductSerializer(ModelSerializer):
|
|
class Meta:
|
|
model = Product
|
|
fields = ("id", "name", "brand")
|
|
depth = 1
|
|
|
|
|
|
class ProductListSerializer(ModelSerializer):
|
|
class Meta:
|
|
model = Product
|
|
fields = ("id", "name")
|
|
|
|
|
|
class BrandSerializer(ModelSerializer):
|
|
products = ProductListSerializer(many=True, read_only=True)
|
|
|
|
class Meta:
|
|
model = Brand
|
|
fields = ["id", "name", "products"]
|
|
|
|
|
|
class BrandListSerializer(ModelSerializer):
|
|
class Meta:
|
|
model = Brand
|
|
fields = "__all__"
|
|
|
|
|
|
class ImageSerializer(ModelSerializer):
|
|
class Meta:
|
|
model = Image
|
|
fields = ("image",)
|
|
|
|
|
|
class ImageCreateSerializer(ModelSerializer):
|
|
class Meta:
|
|
model = Image
|
|
fields = ("post", "image")
|
|
|
|
|
|
class ItemIssuesSerializer(ModelSerializer):
|
|
class Meta:
|
|
model = ItemIssues
|
|
fields = (
|
|
"display",
|
|
"frame",
|
|
"button",
|
|
"biometric",
|
|
"camera",
|
|
"speaker",
|
|
"others",
|
|
)
|
|
|
|
|
|
class PostListSerializer(ModelSerializer):
|
|
product = ProductListSerializer(read_only=True)
|
|
nickname = serializers.CharField(source="author.nickname")
|
|
image = serializers.SerializerMethodField()
|
|
item_issues = ItemIssuesSerializer(read_only=True)
|
|
|
|
class Meta:
|
|
model = Post
|
|
fields = (
|
|
"id",
|
|
"product",
|
|
"price",
|
|
"text",
|
|
"nickname",
|
|
"item_issues",
|
|
"done",
|
|
"written_at",
|
|
"image",
|
|
)
|
|
|
|
def get_image(self, obj):
|
|
first_image = obj.images.first()
|
|
return ImageSerializer(
|
|
instance=first_image,
|
|
context=self.context,
|
|
).data["image"]
|
|
|
|
|
|
class PostCreateSerializer(ModelSerializer):
|
|
item_issues = ItemIssuesSerializer(write_only=True)
|
|
# 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
|
|
|
|
class Meta:
|
|
model = Post
|
|
fields = (
|
|
"product",
|
|
"price",
|
|
"text",
|
|
"item_issues",
|
|
"photos",
|
|
)
|
|
|
|
|
|
class PostUpdateSerializer(ModelSerializer):
|
|
class Meta:
|
|
model = Post
|
|
fields = (
|
|
"price",
|
|
"text",
|
|
"done",
|
|
)
|
|
|
|
|
|
class PostSerializer(ModelSerializer):
|
|
product = ProductListSerializer(read_only=True)
|
|
nickname = serializers.CharField(source="author.nickname")
|
|
images = ImageSerializer(many=True, read_only=True)
|
|
item_issues = ItemIssuesSerializer(read_only=True)
|
|
|
|
class Meta:
|
|
model = Post
|
|
fields = (
|
|
"id",
|
|
"product",
|
|
"price",
|
|
"text",
|
|
"nickname",
|
|
"item_issues",
|
|
"done",
|
|
"written_at",
|
|
"images",
|
|
)
|