feat: Add ItemIssues model

This commit is contained in:
jhynsoo 2023-11-09 19:41:37 +09:00
parent f7f9486237
commit 89c56cc1b6
4 changed files with 78 additions and 33 deletions

View File

@ -1,11 +1,12 @@
from django.contrib import admin from django.contrib import admin
from .models import Brand, Product, Post, Image from .models import Brand, Product, ItemIssues, Post, Image
admin.site.register(Brand) admin.site.register(Brand)
admin.site.register(Product) admin.site.register(Product)
admin.site.register(Image) admin.site.register(Image)
admin.site.register(ItemIssues)
@admin.register(Post) @admin.register(Post)
@ -15,10 +16,11 @@ class PostAdmin(admin.ModelAdmin):
"price", "price",
"text", "text",
"author", "author",
"status", "item_issues",
"done",
"written_at", "written_at",
) )
list_filter = ("status", "written_at") list_filter = ("done", "written_at")
search_fields = ("product", "text") search_fields = ("product", "text")
date_hierarchy = "written_at" date_hierarchy = "written_at"
raw_id_fields = ("author",) raw_id_fields = ("author",)

View File

@ -12,9 +12,7 @@ class Brand(models.Model):
class Product(models.Model): class Product(models.Model):
name = models.CharField(max_length=50) name = models.CharField(max_length=50)
brand = models.ForeignKey( brand = models.ForeignKey(Brand, on_delete=models.CASCADE, related_name="products")
Brand, on_delete=models.CASCADE, related_name="products"
)
release_date = models.DateField(blank=True, null=True) release_date = models.DateField(blank=True, null=True)
class Meta: class Meta:
@ -24,23 +22,31 @@ class Product(models.Model):
return self.name return self.name
class ItemIssues(models.Model):
display = models.BooleanField(default=False)
frame = models.BooleanField(default=False)
button = models.BooleanField(default=False)
biometric = models.BooleanField(default=False)
camera = models.BooleanField(default=False)
speaker = models.BooleanField(default=False)
others = models.BooleanField(default=False)
class Post(models.Model): class Post(models.Model):
STATUS_CHOICES = (
("s", "selling"),
("r", "reserved"),
("d", "done"),
)
product = models.ForeignKey( product = models.ForeignKey(
Product, on_delete=models.CASCADE, related_name="posts" Product,
on_delete=models.CASCADE,
related_name="posts",
) )
price = models.IntegerField() price = models.IntegerField()
text = models.TextField() text = models.TextField()
author = models.ForeignKey( author = models.ForeignKey(
User, on_delete=models.SET_NULL, null=True, related_name="posts" User, on_delete=models.SET_NULL, null=True, related_name="posts"
) )
status = models.CharField( item_issues = models.OneToOneField(
max_length=1, choices=STATUS_CHOICES, default="s" ItemIssues, on_delete=models.CASCADE, related_name="post"
) )
done = models.BooleanField(default=False)
written_at = models.DateTimeField(auto_now_add=True) written_at = models.DateTimeField(auto_now_add=True)
class Meta: class Meta:
@ -52,7 +58,9 @@ class Post(models.Model):
class Image(models.Model): class Image(models.Model):
post = models.ForeignKey( post = models.ForeignKey(
Post, on_delete=models.CASCADE, related_name="images" Post,
on_delete=models.CASCADE,
related_name="images",
) )
image = models.ImageField(upload_to="images", blank=True, null=True) image = models.ImageField(upload_to="images", blank=True, null=True)

View File

@ -6,6 +6,7 @@ from market.models import (
Product, Product,
Post, Post,
Image, Image,
ItemIssues,
) )
@ -42,10 +43,25 @@ class ImageSerializer(ModelSerializer):
fields = ("image",) fields = ("image",)
class ItemIssuesSerializer(ModelSerializer):
class Meta:
model = ItemIssues
fields = (
"display",
"frame",
"button",
"biometric",
"camera",
"speaker",
"others",
)
class PostListSerializer(ModelSerializer): class PostListSerializer(ModelSerializer):
product = ProductListSerializer(read_only=True) product = ProductListSerializer(read_only=True)
nickname = serializers.CharField(source="author.nickname") nickname = serializers.CharField(source="author.nickname")
image = serializers.SerializerMethodField() image = serializers.SerializerMethodField()
item_issues = ItemIssuesSerializer(read_only=True)
class Meta: class Meta:
model = Post model = Post
@ -55,7 +71,8 @@ class PostListSerializer(ModelSerializer):
"price", "price",
"text", "text",
"nickname", "nickname",
"status", "item_issues",
"done",
"written_at", "written_at",
"image", "image",
) )
@ -63,30 +80,39 @@ class PostListSerializer(ModelSerializer):
def get_image(self, obj): def get_image(self, obj):
first_image = obj.images.first() first_image = obj.images.first()
return ImageSerializer( return ImageSerializer(
instance=first_image, context=self.context instance=first_image,
context=self.context,
).data["image"] ).data["image"]
class PostCreateSerializer(ModelSerializer): class PostCreateSerializer(ModelSerializer):
item_issues = ItemIssuesSerializer(write_only=True)
class Meta: class Meta:
model = Post model = Post
fields = ( fields = (
"product", "product",
"price", "price",
"text", "text",
"item_issues",
) )
class PostStatusUpdateSerializer(ModelSerializer): class PostUpdateSerializer(ModelSerializer):
class Meta: class Meta:
model = Post model = Post
fields = ("status",) fields = (
"price",
"text",
"done",
)
class PostSerializer(ModelSerializer): class PostSerializer(ModelSerializer):
product = ProductListSerializer(read_only=True) product = ProductListSerializer(read_only=True)
nickname = serializers.CharField(source="author.nickname") nickname = serializers.CharField(source="author.nickname")
images = ImageSerializer(many=True, read_only=True) images = ImageSerializer(many=True, read_only=True)
item_issues = ItemIssuesSerializer(read_only=True)
class Meta: class Meta:
model = Post model = Post
@ -96,7 +122,8 @@ class PostSerializer(ModelSerializer):
"price", "price",
"text", "text",
"nickname", "nickname",
"status", "item_issues",
"done",
"written_at", "written_at",
"images", "images",
) )

View File

@ -5,14 +5,14 @@ from rest_framework.viewsets import ModelViewSet
from core.mixins import ActionBasedMixin from core.mixins import ActionBasedMixin
from core.permissions import IsAuthorOrReadOnly, IsAdminUserOrReadOnly from core.permissions import IsAuthorOrReadOnly, IsAdminUserOrReadOnly
from market.models import Brand, Product, Post from market.models import Brand, Product, Post, ItemIssues
from market.serializers import ( from market.serializers import (
BrandSerializer, BrandSerializer,
ProductSerializer, ProductSerializer,
PostSerializer, PostSerializer,
PostCreateSerializer, PostCreateSerializer,
PostListSerializer, PostListSerializer,
PostStatusUpdateSerializer, PostUpdateSerializer,
) )
from graph.models import Transaction from graph.models import Transaction
from graph.methods import create_transaction from graph.methods import create_transaction
@ -62,7 +62,7 @@ class PostViewset(ActionBasedMixin, ModelViewSet):
serializer_class_map = { serializer_class_map = {
"list": PostListSerializer, "list": PostListSerializer,
"create": PostCreateSerializer, "create": PostCreateSerializer,
"update": PostStatusUpdateSerializer, "update": PostUpdateSerializer,
} }
permission_classes = [IsAuthenticated, IsAuthorOrReadOnly] permission_classes = [IsAuthenticated, IsAuthorOrReadOnly]
permission_classes_map = { permission_classes_map = {
@ -85,19 +85,27 @@ class PostViewset(ActionBasedMixin, ModelViewSet):
return Response(serializer.data) return Response(serializer.data)
def perform_create(self, serializer): def perform_create(self, serializer):
serializer.save(author=self.request.user) item_issues = ItemIssues.objects.create()
serializer.save(author=self.request.user, item_issues=item_issues)
def partial_update(self, request, *args, **kwargs):
object = self.get_object()
done = request.data.get("done")
if not object.done and done:
create_transaction(object)
elif object.done and not done:
Transaction.objects.filter(post=object).delete()
if object.done != done:
object.done = done
object.save()
return Response(status=200)
def perform_update(self, serializer): def perform_update(self, serializer):
object = self.get_object() object = self.get_object()
object_unsold = object.status == "s" or object.status == "r"
instance = serializer.instance instance = serializer.instance
if object_unsold and serializer.validated_data.get("status") == "d": done = serializer.validated_data.get("done")
if not object.done and done:
create_transaction(instance) create_transaction(instance)
elif ( elif object.done and not done:
not object_unsold Transaction.objects.filter(post=object).delete()
and serializer.validated_data.get("status") == "s"
):
Transaction.objects.filter(
price=instance.price, product=instance.product
).delete()
return super().perform_update(serializer) return super().perform_update(serializer)