From c51480330028b0717a4ac48dd2658cc8ea3c8df7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EB=B0=8D?= <79096808+0321minji@users.noreply.github.com> Date: Sat, 20 Jan 2024 14:40:44 +0900 Subject: [PATCH] #8 fix : remove category field from products (#15) --- products/admin.py | 7 ----- products/migrations/0003_product_like_cnt.py | 18 +++++++++++ .../migrations/0004_product_likeuser_set.py | 20 +++++++++++++ ...tegory_remove_product_like_cnt_and_more.py | 28 +++++++++++++++++ products/models.py | 5 +--- products/services.py | 30 +++++++++++++++---- products/views.py | 4 +-- 7 files changed, 92 insertions(+), 20 deletions(-) create mode 100644 products/migrations/0003_product_like_cnt.py create mode 100644 products/migrations/0004_product_likeuser_set.py create mode 100644 products/migrations/0005_remove_product_category_remove_product_like_cnt_and_more.py diff --git a/products/admin.py b/products/admin.py index 6c2c17b..09b66c5 100644 --- a/products/admin.py +++ b/products/admin.py @@ -2,13 +2,6 @@ from . import models -@admin.register(models.Category) -class Category(admin.ModelAdmin): - - """ """ - - pass - @admin.register(models.Product) class Product(admin.ModelAdmin): diff --git a/products/migrations/0003_product_like_cnt.py b/products/migrations/0003_product_like_cnt.py new file mode 100644 index 0000000..06f4b91 --- /dev/null +++ b/products/migrations/0003_product_like_cnt.py @@ -0,0 +1,18 @@ +# Generated by Django 4.0 on 2024-01-13 05:49 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('products', '0002_alter_productkeyword_product_and_more'), + ] + + operations = [ + migrations.AddField( + model_name='product', + name='like_cnt', + field=models.PositiveIntegerField(default=0), + ), + ] diff --git a/products/migrations/0004_product_likeuser_set.py b/products/migrations/0004_product_likeuser_set.py new file mode 100644 index 0000000..4b36f3e --- /dev/null +++ b/products/migrations/0004_product_likeuser_set.py @@ -0,0 +1,20 @@ +# Generated by Django 4.0 on 2024-01-13 05:59 + +from django.conf import settings +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + migrations.swappable_dependency(settings.AUTH_USER_MODEL), + ('products', '0003_product_like_cnt'), + ] + + operations = [ + migrations.AddField( + model_name='product', + name='likeuser_set', + field=models.ManyToManyField(blank=True, related_name='liked_products', to=settings.AUTH_USER_MODEL), + ), + ] diff --git a/products/migrations/0005_remove_product_category_remove_product_like_cnt_and_more.py b/products/migrations/0005_remove_product_category_remove_product_like_cnt_and_more.py new file mode 100644 index 0000000..b21c9a6 --- /dev/null +++ b/products/migrations/0005_remove_product_category_remove_product_like_cnt_and_more.py @@ -0,0 +1,28 @@ +# Generated by Django 4.0 on 2024-01-20 05:23 + +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('products', '0004_product_likeuser_set'), + ] + + operations = [ + migrations.RemoveField( + model_name='product', + name='category', + ), + migrations.RemoveField( + model_name='product', + name='like_cnt', + ), + migrations.RemoveField( + model_name='product', + name='likeuser_set', + ), + migrations.DeleteModel( + name='Category', + ), + ] diff --git a/products/models.py b/products/models.py index 3b1d0fc..6d5f903 100644 --- a/products/models.py +++ b/products/models.py @@ -2,12 +2,9 @@ from core.models import TimeStampedModel # Create your models here. -class Category(models.Model): - name = models.CharField(max_length=100, blank= False) - class Product(TimeStampedModel): name = models.CharField(max_length=100) - category = models.ForeignKey('Category', related_name='products', on_delete=models.CASCADE, null=False, blank=False) + #category = models.ForeignKey('Category', related_name='products', on_delete=models.CASCADE, null=False, blank=False) basic_price = models.CharField(max_length=500, blank = False) option = models.TextField(blank = True) info = models.TextField(blank = True) diff --git a/products/services.py b/products/services.py index e6867bb..9295072 100644 --- a/products/services.py +++ b/products/services.py @@ -9,8 +9,9 @@ from django.core.files.uploadedfile import InMemoryUploadedFile from django.conf import settings -from products.models import Category, Product, ProductKeyword, ProductPhoto +from products.models import Product, ProductKeyword, ProductPhoto from users.models import User +from .selectors import ProductSelector #from core.exceptions import ApplicationError class ProductCoordinatorService: @@ -18,7 +19,7 @@ def __init__(self, user:User): self.user=user #pass @transaction.atomic - def create(self, category: str, name : str,keywords : list[str],basic_price : str,option : str, + def create(self, name : str,keywords : list[str],basic_price : str,option : str, product_photos : list[str],info : str,notice : str,period : str,transaction_direct : bool, transaction_package : bool,refund : str, ) -> Product: @@ -26,7 +27,6 @@ def create(self, category: str, name : str,keywords : list[str],basic_price : st product= product_service.create( reformer=self.user, - category=category, name=name, basic_price=basic_price, option=option, @@ -50,13 +50,31 @@ def __init__(self): pass @staticmethod - def create(category: str,name : str,basic_price : str,option : str,info : str,notice : str, + def like_or_dislike(product:Product, user: User)-> bool: + if ProductSelector.likes(product=product, user=user): + product.likeuser_set.remove(user) + product.like_cnt-=1 + + product.full_clean() + product.save() + + return False + else: + product.likeuser_set.add(user) + product.like_cnt +=1 + + product.full_clean() + product.save() + + return True + + + @staticmethod + def create(name : str,basic_price : str,option : str,info : str,notice : str, period : str,transaction_direct : bool,transaction_package : bool,refund : str, reformer : User): - category = get_object_or_404(Category, id=category) product = Product( name = name, - category = category, basic_price = basic_price, option = option, info = info, diff --git a/products/views.py b/products/views.py index e2806fd..3a321d3 100644 --- a/products/views.py +++ b/products/views.py @@ -7,7 +7,7 @@ from users.models import User -from .models import Category, Product +from .models import Product from .services import ProductCoordinatorService, ProductPhotoService, ProductKeywordService, ProductService @@ -16,7 +16,6 @@ class ProductCreateApi(APIView): class ProductCreateInputSerializer(serializers.Serializer): name = serializers.CharField() - category = serializers.CharField() keywords = serializers.ListField(required=False) basic_price = serializers.CharField() option = serializers.CharField() @@ -37,7 +36,6 @@ def post(self,request): product = service.create( name=data.get('name'), - category=data.get('category'), keywords=data.get('keywords', []), basic_price=data.get('basic_price'), option=data.get('option'),