feat: Add ItemIssues model
This commit is contained in:
parent
f7f9486237
commit
89c56cc1b6
@ -1,11 +1,12 @@
|
||||
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(Product)
|
||||
admin.site.register(Image)
|
||||
admin.site.register(ItemIssues)
|
||||
|
||||
|
||||
@admin.register(Post)
|
||||
@ -15,10 +16,11 @@ class PostAdmin(admin.ModelAdmin):
|
||||
"price",
|
||||
"text",
|
||||
"author",
|
||||
"status",
|
||||
"item_issues",
|
||||
"done",
|
||||
"written_at",
|
||||
)
|
||||
list_filter = ("status", "written_at")
|
||||
list_filter = ("done", "written_at")
|
||||
search_fields = ("product", "text")
|
||||
date_hierarchy = "written_at"
|
||||
raw_id_fields = ("author",)
|
||||
|
@ -12,9 +12,7 @@ class Brand(models.Model):
|
||||
|
||||
class Product(models.Model):
|
||||
name = models.CharField(max_length=50)
|
||||
brand = models.ForeignKey(
|
||||
Brand, on_delete=models.CASCADE, related_name="products"
|
||||
)
|
||||
brand = models.ForeignKey(Brand, on_delete=models.CASCADE, related_name="products")
|
||||
release_date = models.DateField(blank=True, null=True)
|
||||
|
||||
class Meta:
|
||||
@ -24,23 +22,31 @@ class Product(models.Model):
|
||||
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):
|
||||
STATUS_CHOICES = (
|
||||
("s", "selling"),
|
||||
("r", "reserved"),
|
||||
("d", "done"),
|
||||
)
|
||||
product = models.ForeignKey(
|
||||
Product, on_delete=models.CASCADE, related_name="posts"
|
||||
Product,
|
||||
on_delete=models.CASCADE,
|
||||
related_name="posts",
|
||||
)
|
||||
price = models.IntegerField()
|
||||
text = models.TextField()
|
||||
author = models.ForeignKey(
|
||||
User, on_delete=models.SET_NULL, null=True, related_name="posts"
|
||||
)
|
||||
status = models.CharField(
|
||||
max_length=1, choices=STATUS_CHOICES, default="s"
|
||||
item_issues = models.OneToOneField(
|
||||
ItemIssues, on_delete=models.CASCADE, related_name="post"
|
||||
)
|
||||
done = models.BooleanField(default=False)
|
||||
written_at = models.DateTimeField(auto_now_add=True)
|
||||
|
||||
class Meta:
|
||||
@ -52,7 +58,9 @@ class Post(models.Model):
|
||||
|
||||
class Image(models.Model):
|
||||
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)
|
||||
|
||||
|
@ -6,6 +6,7 @@ from market.models import (
|
||||
Product,
|
||||
Post,
|
||||
Image,
|
||||
ItemIssues,
|
||||
)
|
||||
|
||||
|
||||
@ -42,10 +43,25 @@ class ImageSerializer(ModelSerializer):
|
||||
fields = ("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
|
||||
@ -55,7 +71,8 @@ class PostListSerializer(ModelSerializer):
|
||||
"price",
|
||||
"text",
|
||||
"nickname",
|
||||
"status",
|
||||
"item_issues",
|
||||
"done",
|
||||
"written_at",
|
||||
"image",
|
||||
)
|
||||
@ -63,30 +80,39 @@ class PostListSerializer(ModelSerializer):
|
||||
def get_image(self, obj):
|
||||
first_image = obj.images.first()
|
||||
return ImageSerializer(
|
||||
instance=first_image, context=self.context
|
||||
instance=first_image,
|
||||
context=self.context,
|
||||
).data["image"]
|
||||
|
||||
|
||||
class PostCreateSerializer(ModelSerializer):
|
||||
item_issues = ItemIssuesSerializer(write_only=True)
|
||||
|
||||
class Meta:
|
||||
model = Post
|
||||
fields = (
|
||||
"product",
|
||||
"price",
|
||||
"text",
|
||||
"item_issues",
|
||||
)
|
||||
|
||||
|
||||
class PostStatusUpdateSerializer(ModelSerializer):
|
||||
class PostUpdateSerializer(ModelSerializer):
|
||||
class Meta:
|
||||
model = Post
|
||||
fields = ("status",)
|
||||
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
|
||||
@ -96,7 +122,8 @@ class PostSerializer(ModelSerializer):
|
||||
"price",
|
||||
"text",
|
||||
"nickname",
|
||||
"status",
|
||||
"item_issues",
|
||||
"done",
|
||||
"written_at",
|
||||
"images",
|
||||
)
|
||||
|
@ -5,14 +5,14 @@ from rest_framework.viewsets import ModelViewSet
|
||||
|
||||
from core.mixins import ActionBasedMixin
|
||||
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 (
|
||||
BrandSerializer,
|
||||
ProductSerializer,
|
||||
PostSerializer,
|
||||
PostCreateSerializer,
|
||||
PostListSerializer,
|
||||
PostStatusUpdateSerializer,
|
||||
PostUpdateSerializer,
|
||||
)
|
||||
from graph.models import Transaction
|
||||
from graph.methods import create_transaction
|
||||
@ -62,7 +62,7 @@ class PostViewset(ActionBasedMixin, ModelViewSet):
|
||||
serializer_class_map = {
|
||||
"list": PostListSerializer,
|
||||
"create": PostCreateSerializer,
|
||||
"update": PostStatusUpdateSerializer,
|
||||
"update": PostUpdateSerializer,
|
||||
}
|
||||
permission_classes = [IsAuthenticated, IsAuthorOrReadOnly]
|
||||
permission_classes_map = {
|
||||
@ -85,19 +85,27 @@ class PostViewset(ActionBasedMixin, ModelViewSet):
|
||||
return Response(serializer.data)
|
||||
|
||||
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):
|
||||
object = self.get_object()
|
||||
object_unsold = object.status == "s" or object.status == "r"
|
||||
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)
|
||||
elif (
|
||||
not object_unsold
|
||||
and serializer.validated_data.get("status") == "s"
|
||||
):
|
||||
Transaction.objects.filter(
|
||||
price=instance.price, product=instance.product
|
||||
).delete()
|
||||
elif object.done and not done:
|
||||
Transaction.objects.filter(post=object).delete()
|
||||
return super().perform_update(serializer)
|
||||
|
Loading…
Reference in New Issue
Block a user