* available_count for Gift
This commit is contained in:
parent
9f29e66d12
commit
dd39a0d2d5
18
store/migrations/0049_gift_available_count.py
Normal file
18
store/migrations/0049_gift_available_count.py
Normal file
|
|
@ -0,0 +1,18 @@
|
||||||
|
# Generated by Django 4.2.2 on 2023-11-22 22:28
|
||||||
|
|
||||||
|
from django.db import migrations, models
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
|
dependencies = [
|
||||||
|
('store', '0048_globalsettings_yuan_rate_commission_and_more'),
|
||||||
|
]
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.AddField(
|
||||||
|
model_name='gift',
|
||||||
|
name='available_count',
|
||||||
|
field=models.PositiveSmallIntegerField(default=0, verbose_name='Доступное количество'),
|
||||||
|
),
|
||||||
|
]
|
||||||
|
|
@ -260,6 +260,7 @@ class Gift(models.Model):
|
||||||
name = models.CharField('Название', max_length=100)
|
name = models.CharField('Название', max_length=100)
|
||||||
image = models.ImageField('Фото', upload_to=Image.TYPE_TO_UPLOAD_PATH[Image.GIFT], null=True, blank=True)
|
image = models.ImageField('Фото', upload_to=Image.TYPE_TO_UPLOAD_PATH[Image.GIFT], null=True, blank=True)
|
||||||
min_price = models.DecimalField('Минимальная цена в юанях', help_text='от какой суммы доступен подарок', max_digits=10, decimal_places=2, default=0)
|
min_price = models.DecimalField('Минимальная цена в юанях', help_text='от какой суммы доступен подарок', max_digits=10, decimal_places=2, default=0)
|
||||||
|
available_count = models.PositiveSmallIntegerField('Доступное количество', default=0)
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return self.name
|
return self.name
|
||||||
|
|
@ -636,6 +637,18 @@ class Checklist(models.Model):
|
||||||
if not self.preview_image:
|
if not self.preview_image:
|
||||||
self.generate_preview()
|
self.generate_preview()
|
||||||
|
|
||||||
|
# Update available gifts count
|
||||||
|
if self.gift != old_obj.gift:
|
||||||
|
# Decrement new gift
|
||||||
|
if self.gift:
|
||||||
|
self.gift.available_count = max(self.gift.available_count - 1, 0)
|
||||||
|
self.gift.save()
|
||||||
|
|
||||||
|
# Increment new gift
|
||||||
|
if old_obj.gift:
|
||||||
|
old_obj.gift.available_count = max(old_obj.gift.available_count + 1, 0)
|
||||||
|
old_obj.gift.save()
|
||||||
|
|
||||||
# Save price details to snapshot
|
# Save price details to snapshot
|
||||||
if self.price_snapshot_id:
|
if self.price_snapshot_id:
|
||||||
# Status updated from other statuses back to DRAFT
|
# Status updated from other statuses back to DRAFT
|
||||||
|
|
|
||||||
|
|
@ -200,8 +200,13 @@ class AnonymousUserChecklistSerializer(ChecklistSerializer):
|
||||||
|
|
||||||
def validate(self, attrs):
|
def validate(self, attrs):
|
||||||
gift = attrs.get('gift')
|
gift = attrs.get('gift')
|
||||||
if gift is not None and self.instance.price_yuan < gift.min_price:
|
if gift is not None:
|
||||||
raise CRMException("Can't add gift: price of order < min_price of gift")
|
if self.instance.price_yuan < gift.min_price:
|
||||||
|
raise CRMException("Can't add gift: price of order < min_price of gift")
|
||||||
|
|
||||||
|
if gift.available_count == 0:
|
||||||
|
raise CRMException("Gift is not available")
|
||||||
|
|
||||||
return attrs
|
return attrs
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user