begin to add pdf view of bill into admin
This commit is contained in:
		
					parent
					
						
							
								126d9da764
							
						
					
				
			
			
				commit
				
					
						11de455d23
					
				
			
		
					 2 changed files with 61 additions and 2 deletions
				
			
		| 
						 | 
				
			
			@ -1,4 +1,14 @@
 | 
			
		|||
from django.contrib import admin
 | 
			
		||||
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
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
from uncloud_pay.models import Bill, Order, BillRecord, BillingAddress
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -8,6 +18,54 @@ class BillRecordInline(admin.TabularInline):
 | 
			
		|||
class BillAdmin(admin.ModelAdmin):
 | 
			
		||||
    inlines = [ BillRecordInline ]
 | 
			
		||||
 | 
			
		||||
#    change_list_template = "uncloud_pay/change_list.html"
 | 
			
		||||
 | 
			
		||||
    def get_urls(self):
 | 
			
		||||
        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 changelist_view(self, request, extra_context=None):
 | 
			
		||||
    #     extra_context = extra_context or {}
 | 
			
		||||
 | 
			
		||||
    #     print("view exec")
 | 
			
		||||
    #     return super().changelist_view(
 | 
			
		||||
    #         request, extra_context=extra_context,
 | 
			
		||||
    #     )
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    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()
 | 
			
		||||
        bill_html = render_to_string("bill.html.j2", {'bill': bill})
 | 
			
		||||
 | 
			
		||||
        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)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
admin.site.register(Bill, BillAdmin)
 | 
			
		||||
admin.site.register(Order)
 | 
			
		||||
admin.site.register(BillRecord)
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -26,7 +26,7 @@
 | 
			
		|||
 | 
			
		||||
<head>
 | 
			
		||||
    <meta http-equiv="content-type" content="text/html; charset=utf-8" />
 | 
			
		||||
    <title>{{ bill.reference }} | {{ bill.uuid }}</title>
 | 
			
		||||
    <title>{{ bill }}</title>
 | 
			
		||||
 | 
			
		||||
    <style>
 | 
			
		||||
      body {
 | 
			
		||||
| 
						 | 
				
			
			@ -693,7 +693,8 @@ oAsAAAAAAACGQNAFAAAAAAAAQyDoAgAAAAAAgCEQdAEAAAAAAMAQCLoAAAAAAABgCP83AL6WQ1Y7
 | 
			
		|||
        <div class="b2">
 | 
			
		||||
          {{ bill.creation_date.date }}<br>
 | 
			
		||||
          {% if bill.billing_address.vat_number != "" %}
 | 
			
		||||
          {{ bill.billing_address.vat_number %}<br>
 | 
			
		||||
          {{ bill.billing_address.vat_number
 | 
			
		||||
          }}<br>
 | 
			
		||||
          {% else %}
 | 
			
		||||
          None<br>
 | 
			
		||||
          {% endif %}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue