uncloud-mravi/uncloud_pay/admin.py

103 lines
3.1 KiB
Python
Raw Normal View History

from django.contrib import admin
from django.template.response import TemplateResponse
from django.urls import path
2020-08-09 08:14:49 +00:00
from django.shortcuts import render
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-10-08 17:54:04 +00:00
from uncloud_pay.models import *
2020-06-21 11:46:54 +00:00
2020-06-21 14:08:00 +00:00
class BillRecordInline(admin.TabularInline):
model = BillRecord
2020-06-21 11:46:54 +00:00
class RecurringPeriodInline(admin.TabularInline):
model = ProductToRecurringPeriod
class ProductAdmin(admin.ModelAdmin):
inlines = [ RecurringPeriodInline ]
2020-08-04 16:56:36 +00:00
2021-12-26 19:36:31 +00:00
# class BillAdmin(admin.ModelAdmin):
# inlines = [ BillRecordInline ]
2020-06-21 14:42:55 +00:00
2021-12-26 19:36:31 +00:00
# def get_urls(self):
# """
# Create URLs for PDF view
# """
2021-12-26 19:36:31 +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__))
2021-12-26 19:36:31 +00:00
# url_patterns = [
# pat(r'^([0-9]+)/as_pdf/$', self.as_pdf),
# pat(r'^([0-9]+)/as_html/$', self.as_html),
# ] + super().get_urls()
2021-12-26 19:36:31 +00:00
# return url_patterns
2021-12-26 19:36:31 +00:00
# def as_pdf(self, request, object_id):
# bill = self.get_object(request, object_id=object_id)
# print(bill)
2021-12-26 19:36:31 +00:00
# if bill is None:
# raise self._get_404_exception(object_id)
2021-12-26 19:36:31 +00:00
# output_file = NamedTemporaryFile()
# bill_html = render_to_string(
# "uncloud_pay/bill.html.j2",
# {
# 'bill': bill,
# 'bill_records': bill.bill_records.all()
# }
# )
2021-12-26 19:36:31 +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"'
2021-12-26 19:36:31 +00:00
# return response
2021-12-26 19:36:31 +00:00
# def as_html(self, request, object_id):
# bill = self.get_object(request, object_id=object_id)
2020-08-09 08:14:49 +00:00
2021-12-26 19:36:31 +00:00
# if bill is None:
# raise self._get_404_exception(object_id)
2020-08-09 08:14:49 +00:00
2021-12-26 19:36:31 +00:00
# return render(request, 'uncloud_pay/bill.html.j2',
# {'bill': bill,
# 'bill_records': bill.bill_records.all()
# })
2020-08-09 08:14:49 +00:00
2021-12-26 19:36:31 +00:00
# bill_html = render_to_string("bill.html.j2", {'bill': bill,
# 'bill_records': bill.bill_records.all()
# })
2020-08-09 08:14:49 +00:00
2021-12-26 19:36:31 +00:00
# bytestring_to_pdf(bill_html.encode('utf-8'), output_file)
# response = FileResponse(output_file, content_type="application/pdf")
2020-08-09 08:14:49 +00:00
2021-12-26 19:36:31 +00:00
# response['Content-Disposition'] = f'filename="bill_{bill}.pdf"'
2021-12-26 19:36:31 +00:00
# return HttpResponse(template.render(context, request))
# return response
2021-12-26 19:36:31 +00:00
#admin.site.register(Bill, BillAdmin)
admin.site.register(Product, ProductAdmin)
2020-12-29 00:43:33 +00:00
for m in [
BillingAddress,
Order,
BillRecord,
2020-12-29 00:43:33 +00:00
Payment,
ProductToRecurringPeriod,
RecurringPeriod,
StripeCreditCard,
StripeCustomer,
PricingPlan,
VATRate
2020-12-29 00:43:33 +00:00
]:
2020-10-08 17:54:04 +00:00
admin.site.register(m)