diff --git a/weblate_web/static/custom.js b/weblate_web/static/custom.js index 5a3668eb2f..09f244dbbd 100644 --- a/weblate_web/static/custom.js +++ b/weblate_web/static/custom.js @@ -171,10 +171,6 @@ ready(() => { if (country && code) { const payload = new FormData(); payload.append("vat", country + code); - payload.append( - "payment", - document.querySelector('input[name="payment"]').value, - ); payload.append( "csrfmiddlewaretoken", document.querySelector('input[name="csrfmiddlewaretoken"]').value, diff --git a/weblate_web/templates/payment/customer.html b/weblate_web/templates/payment/customer.html index 3b5b17016f..694542ae46 100644 --- a/weblate_web/templates/payment/customer.html +++ b/weblate_web/templates/payment/customer.html @@ -16,7 +16,6 @@

{% trans "Your billing information" %}

{% csrf_token %} - {% for field in form %}
{{ field.label }}
diff --git a/weblate_web/templates/payment/payment.html b/weblate_web/templates/payment/payment.html index d0d9c81edc..3002805a03 100644 --- a/weblate_web/templates/payment/payment.html +++ b/weblate_web/templates/payment/payment.html @@ -57,22 +57,7 @@

{% trans "Payment Summary" %}

{% trans "Billing information" %}
{% trans "Edit" %} - {{ object.customer.name }} -
- {{ object.customer.address }} -
- {% if object.customer.address_2 %} - {{ object.customer.address_2 }} -
- {% endif %} - {% if object.customer.postcode %}{{ object.customer.postcode }}{% endif %} - {{ object.customer.city }} -
- {{ object.customer.country.name }} -
- {{ object.customer.tax }} -
- {{ object.customer.vat }} + {% include "snippets/customer_address.html" with customer=object.customer %}
diff --git a/weblate_web/templates/snippets/customer_address.html b/weblate_web/templates/snippets/customer_address.html new file mode 100644 index 0000000000..42bc3f22f7 --- /dev/null +++ b/weblate_web/templates/snippets/customer_address.html @@ -0,0 +1,24 @@ +{{ customer.name }} +
+{{ customer.address }} +
+{% if customer.address_2 %} + {{ customer.address_2 }} +
+{% endif %} +{% if customer.postcode %}{{ customer.postcode }}{% endif %} +{{ customer.city }} +
+{{ customer.country.name }} +{% if customer.tax %} +
+ {{ customer.tax }} +{% endif %} +{% if customer.vat %} +
+ {{ customer.vat }} +{% endif %} +{% if customer.email %} +
+ {{ customer.email }} +{% endif %} diff --git a/weblate_web/templates/snippets/service.html b/weblate_web/templates/snippets/service.html index 15b3a85bdb..3b7958da16 100644 --- a/weblate_web/templates/snippets/service.html +++ b/weblate_web/templates/snippets/service.html @@ -265,4 +265,19 @@
{% endwith %} + {% if service.customer %} +
+
{% trans "Billing information" %}
+
+ {% include "snippets/customer_address.html" with customer=service.customer %} +
+ + + +
+
+
+ {% endif %} diff --git a/weblate_web/templates/user.html b/weblate_web/templates/user.html index 7b3d5458f6..0aff690dd4 100644 --- a/weblate_web/templates/user.html +++ b/weblate_web/templates/user.html @@ -135,6 +135,23 @@

{% trans "My donations" %}

+ {% if donation.customer %} +
+
+
{% trans "Billing information" %}
+
+ {% include "snippets/customer_address.html" with customer=donation.customer %} +
+
+ +
+
+
+
+
+ {% endif %} {% empty %} diff --git a/weblate_web/test-data/fakturace b/weblate_web/test-data/fakturace index 54b6bee6fd..535da425c9 160000 --- a/weblate_web/test-data/fakturace +++ b/weblate_web/test-data/fakturace @@ -1 +1 @@ -Subproject commit 54b6bee6fd59e00876e3ba11c4db9187caa3432e +Subproject commit 535da425c9038002717fcff026c14b30a4a2123e diff --git a/weblate_web/urls.py b/weblate_web/urls.py index eaeab00afa..fc2d926e4a 100644 --- a/weblate_web/urls.py +++ b/weblate_web/urls.py @@ -39,6 +39,7 @@ CustomerView, DiscoverView, DonateView, + EditCustomerView, EditDiscoveryView, EditLinkView, MilestoneArchiveView, @@ -290,6 +291,7 @@ def lastmod(self, item): CompleteView.as_view(), name="payment-complete", ), + path("customer//", EditCustomerView.as_view(), name="edit-customer"), path("invoice//pdf/", download_invoice, name="invoice-pdf"), # FOSDEM short link re_path( diff --git a/weblate_web/views.py b/weblate_web/views.py index eef161baa1..ed54bb0efe 100644 --- a/weblate_web/views.py +++ b/weblate_web/views.py @@ -134,8 +134,18 @@ def get_page_range(page_obj): def get_customer( request: AuthenticatedHttpRequest, obj: Service | Donation | None = None ) -> Customer: + # Get from Service / Donation objects if obj and obj.customer: return obj.customer + + # Use existing customer for user + customers = Customer.objects.filter( + Q(donation__user=request.user) | Q(service__users=request.user) + ) + if len(customers) == 1: + return customers[0] + + # Create new customer object for an user return Customer.objects.get_or_create( origin=PAYMENTS_ORIGIN, user_id=request.user.id, @@ -320,12 +330,10 @@ def api_support(request): @require_POST -def fetch_vat(request): - if "payment" not in request.POST or "vat" not in request.POST: +@login_required +def fetch_vat(request: AuthenticatedHttpRequest): + if "vat" not in request.POST: raise SuspiciousOperation("Missing needed parameters") - payment = Payment.objects.filter(pk=request.POST["payment"], state=Payment.NEW) - if not payment.exists(): - raise SuspiciousOperation("Already processed payment") vat = cache_vies_data(request.POST["vat"]) return JsonResponse(data=getattr(vat, "vies_data", {"valid": False})) @@ -448,6 +456,29 @@ def get_form_kwargs(self): return kwargs +@method_decorator(login_required, name="dispatch") +class EditCustomerView(UpdateView): + # Unlike CustomerView, this is not bound to payment (allows editing all + # user customer contacts) and returns to /user + template_name = "payment/customer.html" + success_url = "/user/" + form_class = CustomerForm + model = Customer + request: AuthenticatedHttpRequest + + def get_queryset(self): + return ( + super() + .get_queryset() + .filter( + Q(donation__user=self.request.user) + | Q(service__users=self.request.user) + | (Q(origin=PAYMENTS_ORIGIN) & Q(user_id=self.request.user.id)) + ) + .distinct() + ) + + class CompleteView(PaymentView): def dispatch(self, request, *args, **kwargs): with transaction.atomic():