Skip to content

Commit

Permalink
feat: allow to edit billing info without payment
Browse files Browse the repository at this point in the history
Add editing link to user overview.
  • Loading branch information
nijel committed Oct 24, 2024
1 parent 2347b3d commit 8e684f9
Show file tree
Hide file tree
Showing 9 changed files with 96 additions and 27 deletions.
4 changes: 0 additions & 4 deletions weblate_web/static/custom.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 0 additions & 1 deletion weblate_web/templates/payment/customer.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ <h1 class="section-title min-m">{% trans "Your billing information" %}</h1>
<div class="payment-form">
<form method="post">
{% csrf_token %}
<input type="hidden" name="payment" value="{{ payment.pk }}">
{% for field in form %}
<div class="form-line">
<div class="line-left">{{ field.label }}</div>
Expand Down
17 changes: 1 addition & 16 deletions weblate_web/templates/payment/payment.html
Original file line number Diff line number Diff line change
Expand Up @@ -57,22 +57,7 @@ <h1 class="section-title min-m">{% trans "Payment Summary" %}</h1>
<div class="line-left">{% trans "Billing information" %}</div>
<div class="line-right">
<a href="{% url "payment-customer" pk=object.pk %}" class="button small right">{% trans "Edit" %}</a>
{{ object.customer.name }}
<br>
{{ object.customer.address }}
<br>
{% if object.customer.address_2 %}
{{ object.customer.address_2 }}
<br>
{% endif %}
{% if object.customer.postcode %}{{ object.customer.postcode }}{% endif %}
{{ object.customer.city }}
<br>
{{ object.customer.country.name }}
<br>
{{ object.customer.tax }}
<br>
{{ object.customer.vat }}
{% include "snippets/customer_address.html" with customer=object.customer %}
</div>
<div class="clear"></div>
</div>
Expand Down
24 changes: 24 additions & 0 deletions weblate_web/templates/snippets/customer_address.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{{ customer.name }}
<br>
{{ customer.address }}
<br>
{% if customer.address_2 %}
{{ customer.address_2 }}
<br>
{% endif %}
{% if customer.postcode %}{{ customer.postcode }}{% endif %}
{{ customer.city }}
<br>
{{ customer.country.name }}
{% if customer.tax %}
<br>
{{ customer.tax }}
{% endif %}
{% if customer.vat %}
<br>
{{ customer.vat }}
{% endif %}
{% if customer.email %}
<br>
{{ customer.email }}
{% endif %}
15 changes: 15 additions & 0 deletions weblate_web/templates/snippets/service.html
Original file line number Diff line number Diff line change
Expand Up @@ -265,4 +265,19 @@
<div class="clear"></div>
</div>
{% endwith %}
{% if service.customer %}
<div class="form-line service-text-formatting">
<div class="line-left">{% trans "Billing information" %}</div>
<div class="line-right">
{% include "snippets/customer_address.html" with customer=service.customer %}
<br />
<form class="form-inline"
method="get"
action="{% url "edit-customer" pk=service.customer.pk %}">
<input type="submit" class="button small inline" value="{% trans "Edit" %}">
</form>
</div>
<div class="clear"></div>
</div>
{% endif %}
</div>
17 changes: 17 additions & 0 deletions weblate_web/templates/user.html
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,23 @@ <h1>{% trans "My donations" %}</h1>
</div>
</div>
</div>
{% if donation.customer %}
<div class="payment-updated">
<div class="form-line user-text-formatting user-text-formatting-payment">
<div class="line-left">{% trans "Billing information" %}</div>
<div class="line-right">
{% include "snippets/customer_address.html" with customer=donation.customer %}
<br />
<form class="form-inline"
method="get"
action="{% url "edit-customer" pk=donation.customer.pk %}">
<input type="submit" class="button small inline" value="{% trans "Edit" %}">
</form>
</div>
<div class="clear"></div>
</div>
</div>
{% endif %}
</div>
{% empty %}

Expand Down
2 changes: 1 addition & 1 deletion weblate_web/test-data/fakturace
2 changes: 2 additions & 0 deletions weblate_web/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
CustomerView,
DiscoverView,
DonateView,
EditCustomerView,
EditDiscoveryView,
EditLinkView,
MilestoneArchiveView,
Expand Down Expand Up @@ -290,6 +291,7 @@ def lastmod(self, item):
CompleteView.as_view(),
name="payment-complete",
),
path("customer/<int:pk>/", EditCustomerView.as_view(), name="edit-customer"),
path("invoice/<uuid:pk>/pdf/", download_invoice, name="invoice-pdf"),
# FOSDEM short link
re_path(
Expand Down
41 changes: 36 additions & 5 deletions weblate_web/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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}))

Expand Down Expand Up @@ -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():
Expand Down

0 comments on commit 8e684f9

Please sign in to comment.