From a68e5275fd7bd86698cbf84e19727f5c0723c13b Mon Sep 17 00:00:00 2001 From: Parsa Nazer Date: Tue, 4 Feb 2025 21:02:00 +0330 Subject: [PATCH] product price refactor , admin display name, product models verbose names --- backend/product/admin.py | 16 +++++++++++++--- backend/product/models.py | 23 +++++++++++++---------- backend/product/serializers.py | 12 +----------- 3 files changed, 27 insertions(+), 24 deletions(-) diff --git a/backend/product/admin.py b/backend/product/admin.py index 69733a9..620457d 100644 --- a/backend/product/admin.py +++ b/backend/product/admin.py @@ -28,7 +28,7 @@ class ProductModelAdmin(ModelAdmin, ImportExportModelAdmin): autocomplete_fields = ['related_products'] # compressed_fields = True warn_unsaved_form = True - list_display = ['display_image', 'price',] + list_display = ['display_image', 'display_price', 'view', 'show', 'rating', 'category', 'discount', 'sell'] fieldsets = ( ('Main Fileds', {'fields': ('name', 'description', 'price', 'min_price', 'currency', 'discount', 'category', 'related_products', 'show',), "classes": ["tab"],}), ('SEO Fileds', {'fields': ('meta_description', 'meta_keywords', 'meta_rating', 'slug'), "classes": ["tab"],}), @@ -43,6 +43,11 @@ class ProductModelAdmin(ModelAdmin, ImportExportModelAdmin): "widget": ArrayWidget, } } + + def display_price(self, obj): + return obj.get_toman_price() + display_price.short_description = 'قیمت تومانی' + @display(description='محصول', header=True) def display_image(self, instance): if instance.image1: @@ -74,7 +79,7 @@ class ProductModelAdmin(ModelAdmin, ImportExportModelAdmin): class MainCategoryModelAdmin(ModelAdmin, ImportExportModelAdmin): import_form_class = ImportForm export_form_class = ExportForm - + list_display = ['name', ] readonly_fields = ('slug', ) compressed_fields = True @@ -88,6 +93,8 @@ class MainCategoryModelAdmin(ModelAdmin, ImportExportModelAdmin): @admin.register(SubCategoryModel) class SubCategoryModelAdmin(ModelAdmin, ImportExportModelAdmin): + list_display = ['name', 'parent', 'show'] + import_form_class = ImportForm export_form_class = ExportForm @@ -106,7 +113,7 @@ class SubCategoryModelAdmin(ModelAdmin, ImportExportModelAdmin): class CommentAdmin(ModelAdmin, ImportExportModelAdmin): import_form_class = ImportForm export_form_class = ExportForm - + list_display = ['user', 'product', 'display_content','show'] compressed_fields = True warn_unsaved_form = True @@ -116,6 +123,9 @@ class CommentAdmin(ModelAdmin, ImportExportModelAdmin): "widget": ArrayWidget, } } + def display_content(self, obj): + return obj.display_content[0:20] + '...' + display_content.short_description = 'محتوای کامنت' @admin.register(DollorModel) class DollorAdmin(ModelAdmin, ImportExportModelAdmin): diff --git a/backend/product/models.py b/backend/product/models.py index 28f74a2..d0dacdb 100644 --- a/backend/product/models.py +++ b/backend/product/models.py @@ -6,7 +6,7 @@ import requests from django.utils.translation import gettext_lazy as _ class MainCategoryModel(models.Model): - name = models.CharField(max_length=50, verbose_name='نام') + name = models.CharField(max_length=50, verbose_name='نام دسته بندی') slug = models.SlugField(max_length=50, unique=True, help_text="اسم دسته را برای مسیر به انگلیسی و بدون فاصله وارد کنید") icon = models.ImageField(upload_to='category_model/',verbose_name='آیکون', blank=True, null=True) meta_title = models.CharField(max_length=60, verbose_name="عنوان متا", help_text="عنوان متا برای SEO", blank=True, null=True) @@ -26,7 +26,7 @@ class MainCategoryModel(models.Model): class SubCategoryModel(models.Model): - name = models.CharField(max_length=50, verbose_name='نام') + name = models.CharField(max_length=50, verbose_name='نام دسته بندی') slug = models.SlugField(max_length=50, unique=True, help_text="اسم دسته را برای مسیر به انگلیسی و بدون فاصله وارد کنید") image = models.ImageField(upload_to='category_model/',verbose_name='عکس', blank=True, null=True) icon = models.ImageField(upload_to='category_model/',verbose_name='آیکون', blank=True, null=True) @@ -107,12 +107,12 @@ class ProductModel(models.Model): discount = models.SmallIntegerField(default=0, verbose_name='تخفیف') slug = models.SlugField(max_length=50, unique=True, blank=True, null=True, allow_unicode=True, verbose_name='نام یکتا', help_text="این فیلد را خالی بگذارید") - meta_description = models.CharField(max_length=300, blank=True, null=True, help_text='این فیلد را حتما پر کنید') - meta_keywords = models.CharField(max_length=300, blank=True, null=True, help_text='این فیلد را حتما پر کنید') - meta_rating = models.FloatField(default=5, help_text='امتیاز محصول') + meta_description = models.CharField(max_length=300, blank=True, null=True, help_text='این فیلد را حتما پر کنید', verbose_name='متا دیسکریپشن') + meta_keywords = models.CharField(max_length=300, blank=True, null=True, help_text='این فیلد را حتما پر کنید', verbose_name='متا کیورد') + meta_rating = models.FloatField(default=5, help_text='امتیاز محصول', verbose_name='متا ریتینگ') created_at = models.DateTimeField(auto_now_add=True, verbose_name='زمان ثبت محصول') category = models.ForeignKey(SubCategoryModel, null=True, on_delete=models.SET_NULL, related_name='products', verbose_name='دسته بندی محصول') - related_products = models.ManyToManyField('self', blank=True,) + related_products = models.ManyToManyField('self', blank=True, verbose_name='محصولات مرتبط') def format_discount_price(self): discount_price = int(self.price * (100 - self.discount) / 100) formatted_num = "{:,.0f}".format(discount_price) @@ -122,9 +122,11 @@ class ProductModel(models.Model): def __str__(self): return self.name - def get_toman_price(self): - dollor_object, _ = DollorModel.objects.get_or_create(unique_filed='unique') - dollor_price = dollor_object.price + def get_toman_price(self, dollor_price=None): + if not dollor_price: + dollor_object, _ = DollorModel.objects.get_or_create(unique_filed='unique') + dollor_price = dollor_object.price + dollar_to_dirham = 0.27 if dollor_price is None: raise ValidationError({"dollor_price": "The 'dollor_price' must be provided in the context for dollar pricing."}) @@ -134,6 +136,7 @@ class ProductModel(models.Model): toman_price = self.price * dollor_price elif self.currency == 'derham': toman_price = self.price * dollor_price * dollar_to_dirham + toman_price = toman_price if toman_price > self.min_price else self.min_price return toman_price def get_toman_price_after_discount(self): @@ -152,7 +155,7 @@ class ProductModel(models.Model): class InStuckColors(models.Model): color = models.CharField(_("رنگ"), null=True, blank=True, max_length=255) in_stuck = models.PositiveIntegerField(default=0, verbose_name="تعداد موجود") - product = models.ForeignKey(ProductModel, on_delete=models.CASCADE, related_name='colors') + product = models.ForeignKey(ProductModel, on_delete=models.CASCADE, related_name='colors', verbose_name='محصول') class Meta: verbose_name = 'تعداد موجود رنگ' verbose_name_plural = 'تعداد موجود رنگ ها' diff --git a/backend/product/serializers.py b/backend/product/serializers.py index 560cec1..7a4b60f 100644 --- a/backend/product/serializers.py +++ b/backend/product/serializers.py @@ -38,17 +38,7 @@ class DynamicProductSerializer(serializers.ModelSerializer): def get_price(self, obj): dollor_price = self.context.get('dollor_price') - dollar_to_dirham = 0.27 - if dollor_price is None: - raise ValidationError({"dollor_price": "The 'dollor_price' must be provided in the context for dollar pricing."}) - if obj.currency == 'toman': - toman_price = obj.price - elif obj.currency == 'dollor': - toman_price = obj.price * dollor_price - elif obj.currency == 'derham': - toman_price = obj.price * dollor_price * dollar_to_dirham - # min price implmentaion - toman_price = toman_price if toman_price > obj.min_price else obj.min_price + toman_price = obj.get_toman_price(dollor_price=dollor_price) return "{:,.0f} تومان".format(toman_price) def get_is_new(self, obj):