2020-02-21 15:33:37 +00:00
|
|
|
from django.contrib import admin
|
2020-06-21 21:46:26 +00:00
|
|
|
from django.template.response import TemplateResponse
|
|
|
|
from django.urls import path
|
|
|
|
from django.conf.urls import url
|
|
|
|
|
|
|
|
from uncloud_pay.views import BillViewSet
|
|
|
|
from hardcopy import bytestring_to_pdf
|
|
|
|
from django.core.files.temp import NamedTemporaryFile
|
|
|
|
from django.http import FileResponse
|
|
|
|
from django.template.loader import render_to_string
|
|
|
|
|
2020-02-21 15:33:37 +00:00
|
|
|
|
2020-06-21 11:46:54 +00:00
|
|
|
from uncloud_pay.models import Bill, Order, BillRecord, BillingAddress
|
|
|
|
|
2020-08-04 16:56:36 +00:00
|
|
|
|
2020-06-21 14:08:00 +00:00
|
|
|
class BillRecordInline(admin.TabularInline):
|
2020-08-08 20:20:49 +00:00
|
|
|
# model = Bill.bill_records.through
|
|
|
|
model = BillRecord
|
2020-06-21 11:46:54 +00:00
|
|
|
|
2020-08-04 16:56:36 +00:00
|
|
|
# AT some point in the future: expose REPLACED and orders that depend on us
|
|
|
|
# class OrderInline(admin.TabularInline):
|
|
|
|
# model = Order
|
|
|
|
# fk_name = "replaces"
|
|
|
|
# class OrderAdmin(admin.ModelAdmin):
|
|
|
|
# inlines = [ OrderInline ]
|
|
|
|
|
2020-06-21 14:08:00 +00:00
|
|
|
class BillAdmin(admin.ModelAdmin):
|
|
|
|
inlines = [ BillRecordInline ]
|
2020-06-21 14:42:55 +00:00
|
|
|
|
2020-06-21 21:46:26 +00:00
|
|
|
def get_urls(self):
|
2020-08-08 20:20:49 +00:00
|
|
|
"""
|
|
|
|
Create URLs for PDF view
|
|
|
|
"""
|
|
|
|
|
2020-06-21 21:46:26 +00:00
|
|
|
info = "%s_%s" % (self.model._meta.app_label, self.model._meta.model_name)
|
|
|
|
pat = lambda regex, fn: url(regex, self.admin_site.admin_view(fn), name='%s_%s' % (info, fn.__name__))
|
|
|
|
|
|
|
|
url_patterns = [
|
|
|
|
pat(r'^([0-9]+)/as_pdf/$', self.my_view),
|
|
|
|
] + super().get_urls()
|
|
|
|
|
|
|
|
return url_patterns
|
|
|
|
|
|
|
|
def my_view(self, request, object_id):
|
|
|
|
bill = self.get_object(request, object_id=object_id)
|
|
|
|
print(bill)
|
|
|
|
|
|
|
|
if bill is None:
|
|
|
|
raise self._get_404_exception(object_id)
|
|
|
|
|
|
|
|
output_file = NamedTemporaryFile()
|
2020-06-21 21:54:57 +00:00
|
|
|
bill_html = render_to_string("bill.html.j2", {'bill': bill,
|
2020-08-08 22:37:27 +00:00
|
|
|
'bill_records': bill.billrecord_set.all()
|
2020-06-21 21:54:57 +00:00
|
|
|
})
|
2020-06-21 21:46:26 +00:00
|
|
|
|
|
|
|
bytestring_to_pdf(bill_html.encode('utf-8'), output_file)
|
|
|
|
response = FileResponse(output_file, content_type="application/pdf")
|
|
|
|
response['Content-Disposition'] = f'filename="bill_{bill}.pdf"'
|
|
|
|
|
|
|
|
return response
|
|
|
|
|
|
|
|
# ...
|
|
|
|
context = dict(
|
|
|
|
# Include common variables for rendering the admin template.
|
|
|
|
self.admin_site.each_context(request),
|
|
|
|
# Anything else you want in the context...
|
|
|
|
# key=value,
|
|
|
|
)
|
|
|
|
|
|
|
|
#return TemplateResponse(request, "admin/change_list.html", context)
|
|
|
|
|
|
|
|
|
2020-06-21 11:46:54 +00:00
|
|
|
admin.site.register(Bill, BillAdmin)
|
|
|
|
admin.site.register(Order)
|
|
|
|
admin.site.register(BillRecord)
|
|
|
|
admin.site.register(BillingAddress)
|
2020-08-08 20:20:49 +00:00
|
|
|
|
|
|
|
|
|
|
|
#admin.site.register(Order, OrderAdmin)
|