feat: order creation on cart detail view

This commit is contained in:
2025-01-03 12:18:08 +01:00
parent 10b411ba33
commit 340c0d0508
9 changed files with 473 additions and 47 deletions
+10 -1
View File
@@ -1,4 +1,4 @@
# Generated by Django 5.1.3 on 2025-01-02 13:36
# Generated by Django 5.1.3 on 2025-01-03 11:17
import django.db.models.deletion
import django.utils.timezone
@@ -275,6 +275,7 @@ class Migration(migrations.Migration):
verbose_name="tipo de dirección",
),
),
("email", models.EmailField(max_length=254, verbose_name="e-mail")),
(
"default",
models.BooleanField(default=True, verbose_name="por defecto"),
@@ -354,6 +355,12 @@ class Migration(migrations.Migration):
verbose_name="total",
),
),
(
"email",
models.EmailField(
blank=True, max_length=254, verbose_name="e-mail"
),
),
(
"billing_address",
models.CharField(
@@ -411,6 +418,8 @@ class Migration(migrations.Migration):
(
"user",
models.ForeignKey(
blank=True,
null=True,
on_delete=django.db.models.deletion.PROTECT,
to=settings.AUTH_USER_MODEL,
verbose_name="cliente",
+4 -2
View File
@@ -356,7 +356,8 @@ class Order(TimestampedModel):
default=Decimal("0"), max_digits=13, decimal_places=2, verbose_name=_("total")
)
user = models.ForeignKey(User, on_delete=models.PROTECT, verbose_name=_("cliente"))
user = models.ForeignKey(User, blank=True, null=True, on_delete=models.PROTECT, verbose_name=_("cliente"))
email = models.EmailField(blank=True, verbose_name=_('e-mail'))
# Datos de facturación
billing_address = models.CharField(
@@ -492,11 +493,12 @@ class CustomerAddress(models.Model):
default=Types.SHIPPING,
verbose_name=_("tipo de dirección"),
)
email = models.EmailField(blank=False, null=False, verbose_name=_("e-mail"))
default = models.BooleanField(default=True, verbose_name=_("por defecto"))
hidden = models.BooleanField(default=False, db_index=True, verbose_name=_("oculto"))
def __str__(self):
return f'{self.full_name} - {self.address}, {self.address_zip}, {self.address_state} - {self.address_country}'
return f"{self.full_name} - {self.address}, {self.address_zip}, {self.address_state} - {self.address_country}"
class Meta:
verbose_name = _("dirección de cliente")
+26 -12
View File
@@ -73,22 +73,36 @@ def delete_product_batch(batch: ProductBatch):
def create_order_from_cart(
cart: Cart,
billing_address: CustomerAddress,
shipping_address: CustomerAddress,
shipping_method: ShippingMethod,
billing_address_full_name="",
billing_address_address="",
billing_address_town="",
billing_address_state="",
billing_address_country="",
billing_address_zip="",
shipping_address_full_name="",
shipping_address_address="",
shipping_address_town="",
shipping_address_state="",
shipping_address_country="",
shipping_address_zip="",
shipping_address_phone="",
email="",
):
order = Order.objects.create(
billing_address=billing_address.address,
billing_city=billing_address.address_town,
billing_state=billing_address.address_state,
billing_country=billing_address.address_country,
billing_zip=billing_address.address_zip,
shipping_address=shipping_address.address,
shipping_city=shipping_address.address_town,
shipping_state=shipping_address.address_state,
shipping_country=shipping_address.address_country,
shipping_zip=shipping_address.address_zip,
billing_address=f'{billing_address_full_name} {billing_address_address}',
billing_city=billing_address_town,
billing_state=billing_address_state,
billing_country=billing_address_country,
billing_zip=billing_address_zip,
contact_phone=shipping_address_phone,
shipping_address=f'{shipping_address_address} {shipping_address_full_name}',
shipping_city=shipping_address_town,
shipping_state=shipping_address_state,
shipping_country=shipping_address_country,
shipping_zip=shipping_address_zip,
user=cart.user,
email=email,
)
for item in cart.items.all():