Enhance discount logic and shop configuration models

Refactored discount application logic in OrderItem to support multiple coupons with configurable multiplication and addition rules from ShopConfiguration. Updated DiscountCode model to use PositiveIntegerField for percent and improved validation. Extended ShopConfiguration with new fields for contact, social media, and coupon settings. Ensured ShopConfiguration singleton creation at app startup.
This commit is contained in:
David Bruno Vontor
2025-11-14 15:18:08 +01:00
parent f14c09bf7a
commit 7a715efeda
3 changed files with 127 additions and 31 deletions

View File

@@ -1,6 +1,21 @@
from django.apps import AppConfig
from django.db.utils import OperationalError, ProgrammingError
from .models import ShopConfiguration
class ConfigurationConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'configuration'
def ready(self):
"""Ensure the ShopConfiguration singleton exists at startup.
Wrapped in broad DB error handling so that commands like
makemigrations/migrate don't fail when the table does not yet exist.
"""
try:
ShopConfiguration.get_solo()
except (OperationalError, ProgrammingError):
ShopConfiguration.objects.create()