Skip to content

Commit

Permalink
feat: add admin invoice download
Browse files Browse the repository at this point in the history
  • Loading branch information
nijel committed Oct 23, 2024
1 parent d9d268d commit 1615f16
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 4 deletions.
20 changes: 20 additions & 0 deletions weblate_web/invoices/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,18 @@
# along with this program. If not, see <https://www.gnu.org/licenses/>.
#

from __future__ import annotations

from typing import TYPE_CHECKING, Any

from django.contrib import admin
from django.urls import reverse

from .models import Discount, Invoice, InvoiceItem

if TYPE_CHECKING:
from django.http.request import HttpRequest

Check warning on line 30 in weblate_web/invoices/admin.py

View check run for this annotation

Codecov / codecov/patch

weblate_web/invoices/admin.py#L30

Added line #L30 was not covered by tests


@admin.register(Discount)
class DiscountAdmin(admin.ModelAdmin):
Expand All @@ -30,6 +38,7 @@ class DiscountAdmin(admin.ModelAdmin):

class InvoiceItemAdmin(admin.TabularInline):
model = InvoiceItem
min_num = 1


@admin.register(Invoice)
Expand All @@ -43,3 +52,14 @@ class InvoiceAdmin(admin.ModelAdmin):
"number",
)
inlines = (InvoiceItemAdmin,)

def save_related(
self, request: HttpRequest, form: Any, formsets: Any, change: Any
) -> None:
super().save_related(

Check warning on line 59 in weblate_web/invoices/admin.py

View check run for this annotation

Codecov / codecov/patch

weblate_web/invoices/admin.py#L59

Added line #L59 was not covered by tests
request=request, form=form, formsets=formsets, change=change
)
form.instance.generate_files()

Check warning on line 62 in weblate_web/invoices/admin.py

View check run for this annotation

Codecov / codecov/patch

weblate_web/invoices/admin.py#L62

Added line #L62 was not covered by tests

def view_on_site(self, obj):
return reverse("invoice-pdf", kwargs={"pk": obj.pk})

Check warning on line 65 in weblate_web/invoices/admin.py

View check run for this annotation

Codecov / codecov/patch

weblate_web/invoices/admin.py#L65

Added line #L65 was not covered by tests
21 changes: 20 additions & 1 deletion weblate_web/invoices/views.py
Original file line number Diff line number Diff line change
@@ -1 +1,20 @@
# Create your views here.
from django.contrib.auth.decorators import login_required, user_passes_test
from django.http import FileResponse
from django.shortcuts import get_object_or_404

from weblate_web.views import AuthenticatedHttpRequest

from .models import Invoice


@login_required
@user_passes_test(lambda u: u.is_superuser)
def download_invoice(request: AuthenticatedHttpRequest, pk: int):
invoice = get_object_or_404(Invoice, pk=pk)

Check warning on line 13 in weblate_web/invoices/views.py

View check run for this annotation

Codecov / codecov/patch

weblate_web/invoices/views.py#L13

Added line #L13 was not covered by tests

return FileResponse(

Check warning on line 15 in weblate_web/invoices/views.py

View check run for this annotation

Codecov / codecov/patch

weblate_web/invoices/views.py#L15

Added line #L15 was not covered by tests
invoice.path.open("rb"),
as_attachment=True,
filename=invoice.filename,
content_type="application/pdf",
)
10 changes: 8 additions & 2 deletions weblate_web/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
from django.views.decorators.cache import cache_page
from django.views.generic import RedirectView, TemplateView

from weblate_web.invoices.views import download_invoice
from weblate_web.models import Post
from weblate_web.views import (
AddDiscoveryView,
Expand All @@ -54,7 +55,7 @@
api_user,
disable_repeat,
donate_pay,
download_invoice,
download_payment_invoice,
fetch_vat,
not_found,
process_payment,
Expand Down Expand Up @@ -201,7 +202,11 @@ def lastmod(self, item):
r"^donate/edit/(?P<pk>[0-9]+)/$", EditLinkView.as_view(), name="donate-edit"
),
re_path(r"^donate/pay/(?P<pk>[0-9]+)/$", donate_pay, name="donate-pay"),
re_path(r"^user/invoice/" + UUID + "/$", download_invoice, name="user-invoice"),
re_path(
r"^user/invoice/" + UUID + "/$",
download_payment_invoice,
name="user-invoice",
),
re_path(
r"^donate/disable/(?P<pk>[0-9]+)/$", disable_repeat, name="donate-disable"
),
Expand Down Expand Up @@ -285,6 +290,7 @@ def lastmod(self, item):
CompleteView.as_view(),
name="payment-complete",
),
path("invoice/<int:pk>/pdf/", download_invoice, name="invoice-pdf"),
# FOSDEM short link
re_path(
r"^FOSDEM/|fosdem/$",
Expand Down
2 changes: 1 addition & 1 deletion weblate_web/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -546,7 +546,7 @@ def process_payment(request):


@login_required
def download_invoice(request, pk):
def download_payment_invoice(request, pk):
# Allow downloading own invoices of pending ones (for proforma invoices)
payment = get_object_or_404(Payment, pk=pk)

Expand Down

0 comments on commit 1615f16

Please sign in to comment.