Refactor Stripe payment handling and order status
Refactored Stripe payment integration to use a dedicated Stripe model for session data, updating the PaymentSerializer accordingly. Renamed Order.Status to Order.OrderStatus for clarity. Updated configuration app to ensure SiteConfiguration singleton is created post-migration. Removed obsolete seed_app_config command from docker-compose. Adjusted frontend API generated files and moved orval config.
This commit is contained in:
@@ -104,7 +104,7 @@ class ProductImage(models.Model):
|
||||
# ------------------ OBJEDNÁVKY ------------------
|
||||
|
||||
class Order(models.Model):
|
||||
class Status(models.TextChoices):
|
||||
class OrderStatus(models.TextChoices):
|
||||
CREATED = "created", "cz#Vytvořeno"
|
||||
CANCELLED = "cancelled", "cz#Zrušeno"
|
||||
COMPLETED = "completed", "cz#Dokončeno"
|
||||
@@ -113,7 +113,7 @@ class Order(models.Model):
|
||||
REFUNDED = "refunded", "cz#Vráceno"
|
||||
|
||||
status = models.CharField(
|
||||
max_length=20, choices=Status.choices, null=True, blank=True, default=Status.CREATED
|
||||
max_length=20, choices=OrderStatus.choices, null=True, blank=True, default=OrderStatus.CREATED
|
||||
)
|
||||
|
||||
# Stored order grand total; recalculated on save
|
||||
|
||||
@@ -143,6 +143,9 @@ class OrderItemCreateSerializer(serializers.Serializer):
|
||||
|
||||
# -- PAYMENT --
|
||||
class PaymentSerializer(serializers.ModelSerializer):
|
||||
stripe_session_id = serializers.CharField(source='stripe.stripe_session_id', read_only=True)
|
||||
stripe_payment_intent = serializers.CharField(source='stripe.stripe_payment_intent', read_only=True)
|
||||
stripe_session_url = serializers.URLField(source='stripe.stripe_session_url', read_only=True)
|
||||
|
||||
class Meta:
|
||||
model = Payment
|
||||
@@ -184,15 +187,15 @@ class PaymentSerializer(serializers.ModelSerializer):
|
||||
if payment.payment_method == Payment.PAYMENT.STRIPE:
|
||||
session = StripeClient.create_checkout_session(order)
|
||||
|
||||
payment.stripe_session_id = session.id
|
||||
payment.stripe_payment_intent = session.payment_intent
|
||||
payment.stripe_session_url = session.url
|
||||
stripe_instance = StripeModel.objects.create(
|
||||
stripe_session_id=session.id,
|
||||
stripe_payment_intent=session.payment_intent,
|
||||
stripe_session_url=session.url,
|
||||
status=StripeModel.STATUS_CHOICES.PENDING
|
||||
)
|
||||
|
||||
payment.save(update_fields=[
|
||||
"stripe_session_id",
|
||||
"stripe_payment_intent",
|
||||
"stripe_session_url",
|
||||
])
|
||||
payment.stripe = stripe_instance
|
||||
payment.save(update_fields=["stripe"])
|
||||
|
||||
return payment
|
||||
|
||||
|
||||
@@ -1,21 +1,22 @@
|
||||
from django.apps import AppConfig
|
||||
from django.db.utils import OperationalError, ProgrammingError
|
||||
from django.db.models.signals import post_migrate
|
||||
|
||||
def create_site_config(sender, **kwargs):
|
||||
"""
|
||||
Ensure the SiteConfiguration singleton exists after migrations.
|
||||
"""
|
||||
from .models import SiteConfiguration
|
||||
try:
|
||||
SiteConfiguration.get_solo()
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
class ConfigurationConfig(AppConfig):
|
||||
default_auto_field = 'django.db.models.BigAutoField'
|
||||
name = 'configuration'
|
||||
|
||||
def ready(self):
|
||||
"""Ensure the SiteConfiguration 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:
|
||||
from .models import SiteConfiguration # local import to avoid premature app registry access
|
||||
SiteConfiguration.get_solo() # creates if missing
|
||||
|
||||
except (OperationalError, ProgrammingError):
|
||||
# DB not ready (e.g., before initial migrate); ignore silently
|
||||
pass
|
||||
# Spustí create_site_config po dokončení migrací
|
||||
post_migrate.connect(create_site_config, sender=self)
|
||||
|
||||
|
||||
|
||||
1
backend/globalstaticfiles/filler
Normal file
1
backend/globalstaticfiles/filler
Normal file
@@ -0,0 +1 @@
|
||||
filler
|
||||
Reference in New Issue
Block a user