From 768453fae711eec9817a6afd1cc4ce9020d0248d Mon Sep 17 00:00:00 2001 From: Alexandr Abramov Date: Wed, 27 Sep 2023 20:40:45 +0300 Subject: [PATCH] =?UTF-8?q?=D0=A0=D0=B5=D0=B0=D0=BB=D0=B8=D0=B7=D0=BE?= =?UTF-8?q?=D0=B2=D0=B0=D0=BB=20=D0=BC=D0=B5=D1=85=D0=B0=D0=BD=D0=B8=D0=B7?= =?UTF-8?q?=D0=BC=20=D1=80=D0=B0=D1=81=D1=87=D0=B5=D1=82=D0=B0=20=D1=84?= =?UTF-8?q?=D0=B8=D0=BD=D0=B0=D0=BB=D1=8C=D0=BD=D0=BE=D0=B9=20=D1=81=D1=82?= =?UTF-8?q?=D0=BE=D0=B8=D0=BC=D0=BE=D1=81=D1=82=D0=B8=20=D0=BF=D1=80=D0=BE?= =?UTF-8?q?=D0=B4=D1=83=D0=BA=D1=82=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- products/models.py | 15 +++++++++++++++ products/views.py | 3 +++ 2 files changed, 18 insertions(+) diff --git a/products/models.py b/products/models.py index 9fa9a5c..686760f 100644 --- a/products/models.py +++ b/products/models.py @@ -1,5 +1,6 @@ from django.contrib.auth import get_user_model from django.db import models +from decimal import Decimal class Product(models.Model): @@ -23,6 +24,20 @@ class Product(models.Model): def shop_name(self): return self.seller.shop_name + def calculate_final_price(self): + """ + Расчет финальной стоимости товара + """ + + base_price = self.price + tax = base_price * Decimal('0.06') #: Налог + bank_fee = base_price * Decimal('0.02') #: Комиссия банку + author_commission = base_price * Decimal('0.02') #: Комиссия за транзакцию продавца + marketplace_fee = base_price * Decimal('0.20') #: Выручка маркетплейса + + final_price = base_price + tax + bank_fee + author_commission + marketplace_fee + return final_price + def __str__(self): return f'{self.product_title} {self.shop_name} {self.seller}' diff --git a/products/views.py b/products/views.py index de50c0e..cae9933 100644 --- a/products/views.py +++ b/products/views.py @@ -18,6 +18,9 @@ def perform_create(self, serializer): new_product = serializer.save(seller=self.request.user) new_product.seller = self.request.user + + total_price = new_product.calculate_final_price() + new_product.price = total_price new_product.save()