diff --git a/counter/forms.py b/counter/forms.py index 10109b4d..27dc74d7 100644 --- a/counter/forms.py +++ b/counter/forms.py @@ -43,7 +43,6 @@ class BillingInfoForm(forms.ModelForm): ] widgets = { "phone_number": RegionalPhoneNumberWidget, - "country": AutoCompleteSelect, } diff --git a/eboutic/models.py b/eboutic/models.py index 60dddb91..b6daaf34 100644 --- a/eboutic/models.py +++ b/eboutic/models.py @@ -51,7 +51,9 @@ class BillingInfoState(Enum): MISSING_PHONE_NUMBER = 3 @classmethod - def from_model(cls, info: BillingInfo) -> BillingInfoState: + def from_model(cls, info: BillingInfo | None) -> BillingInfoState: + if info is None: + return cls.EMPTY for attr in [ "first_name", "last_name", diff --git a/eboutic/templates/eboutic/eboutic_billing_info.jinja b/eboutic/templates/eboutic/eboutic_billing_info.jinja index b498e234..4a1d0739 100644 --- a/eboutic/templates/eboutic/eboutic_billing_info.jinja +++ b/eboutic/templates/eboutic/eboutic_billing_info.jinja @@ -16,7 +16,7 @@ class="collapse-body" hx-trigger="submit" hx-post="{{ action }}" - hx-swap="outerHTML settle:100" + hx-swap="outerHTML" hx-target="closest span" x-show="collapsed" > diff --git a/eboutic/templates/eboutic/eboutic_main.jinja b/eboutic/templates/eboutic/eboutic_main.jinja index ee563dd0..1e070b95 100644 --- a/eboutic/templates/eboutic/eboutic_main.jinja +++ b/eboutic/templates/eboutic/eboutic_main.jinja @@ -66,7 +66,6 @@ {% trans %}Clear{% endtrans %}
@@ -84,7 +85,7 @@ {% endif %} diff --git a/eboutic/views.py b/eboutic/views.py index 1bf0d12f..e9dba8b8 100644 --- a/eboutic/views.py +++ b/eboutic/views.py @@ -38,6 +38,7 @@ from django.urls import reverse from django.utils.decorators import method_decorator from django.views.decorators.http import require_GET, require_POST from django.views.generic import TemplateView, UpdateView, View +from django_countries.fields import Country from core.views.mixins import FragmentMixin, UseFragmentsMixin from counter.forms import BillingInfoForm @@ -55,6 +56,7 @@ from eboutic.schemas import PurchaseItemList, PurchaseItemSchema if TYPE_CHECKING: from cryptography.hazmat.primitives.asymmetric.rsa import RSAPublicKey + from django.utils.html import SafeString @login_required @@ -99,15 +101,28 @@ class BillingInfoFormFragment(LoginRequiredMixin, FragmentMixin, UpdateView): form_class = BillingInfoForm template_name = "eboutic/eboutic_billing_info.jinja" + def get_initial(self): + if self.object is None: + return { + "country": Country(code="FR"), + } + return {} + + def render_fragment(self, request, **kwargs) -> SafeString: + self.object = self.get_object() + return super().render_fragment(request, **kwargs) + + def get_customer(self) -> Customer: + return Customer.get_or_create(self.request.user)[0] + + def form_valid(self, form: BillingInfoForm): + form.instance.customer = self.get_customer() + return super().form_valid(form) + def get_object(self, *args, **kwargs): - customer, _ = Customer.get_or_create(self.request.user) - if not hasattr(customer, "billing_infos"): - customer.billing_infos = BillingInfo() - return customer.billing_infos + return getattr(self.get_customer(), "billing_infos", None) def get_context_data(self, **kwargs): - if not hasattr(self, "object"): - self.object = self.get_object() kwargs = super().get_context_data(**kwargs) kwargs["action"] = reverse("eboutic:billing_infos") kwargs["BillingInfoState"] = BillingInfoState