Sith/counter/views/eticket.py

141 lines
4.8 KiB
Python
Raw Normal View History

#
# Copyright 2023 © AE UTBM
# ae@utbm.fr / ae.info@utbm.fr
#
# This file is part of the website of the UTBM Student Association (AE UTBM),
# https://ae.utbm.fr.
#
# You can find the source code of the website at https://github.com/ae-utbm/sith
#
# LICENSED UNDER THE GNU GENERAL PUBLIC LICENSE VERSION 3 (GPLv3)
2024-09-23 08:25:27 +00:00
# SEE : https://raw.githubusercontent.com/ae-utbm/sith/master/LICENSE
# OR WITHIN THE LOCAL FILE "LICENSE"
#
#
2024-11-27 17:47:18 +00:00
from django.http import Http404, HttpResponse
2024-06-24 11:07:36 +00:00
from django.utils.translation import gettext_lazy as _
2024-11-27 17:47:18 +00:00
from django.views.generic import DetailView, ListView
from django.views.generic.edit import CreateView, UpdateView
2016-03-28 12:54:35 +00:00
2024-11-27 17:47:18 +00:00
from core.views import CanViewMixin
from counter.forms import EticketForm
from counter.models import Eticket, Selling
2016-09-29 16:17:44 +00:00
2017-06-12 07:47:24 +00:00
2017-04-04 13:45:02 +00:00
class EticketListView(CounterAdminTabsMixin, CounterAdminMixin, ListView):
2024-07-12 07:34:16 +00:00
"""A list view for the admins."""
2018-10-04 19:29:19 +00:00
2016-10-03 17:30:05 +00:00
model = Eticket
2018-10-04 19:29:19 +00:00
template_name = "counter/eticket_list.jinja"
ordering = ["id"]
2016-10-03 17:30:05 +00:00
current_tab = "etickets"
2017-06-12 07:47:24 +00:00
2017-04-04 13:45:02 +00:00
class EticketCreateView(CounterAdminTabsMixin, CounterAdminMixin, CreateView):
2024-07-12 07:34:16 +00:00
"""Create an eticket."""
2018-10-04 19:29:19 +00:00
2016-10-03 17:30:05 +00:00
model = Eticket
2018-10-04 19:29:19 +00:00
template_name = "core/create.jinja"
2016-10-03 17:30:05 +00:00
form_class = EticketForm
current_tab = "etickets"
2017-06-12 07:47:24 +00:00
2017-04-04 13:45:02 +00:00
class EticketEditView(CounterAdminTabsMixin, CounterAdminMixin, UpdateView):
2024-07-12 07:34:16 +00:00
"""Edit an eticket."""
2018-10-04 19:29:19 +00:00
2016-10-03 17:30:05 +00:00
model = Eticket
2018-10-04 19:29:19 +00:00
template_name = "core/edit.jinja"
2016-10-03 17:30:05 +00:00
form_class = EticketForm
pk_url_kwarg = "eticket_id"
current_tab = "etickets"
2017-06-12 07:47:24 +00:00
2016-10-03 17:30:05 +00:00
class EticketPDFView(CanViewMixin, DetailView):
2024-07-12 07:34:16 +00:00
"""Display the PDF of an eticket."""
2018-10-04 19:29:19 +00:00
2016-10-03 17:30:05 +00:00
model = Selling
pk_url_kwarg = "selling_id"
def get(self, request, *args, **kwargs):
from reportlab.graphics import renderPDF
2024-06-24 11:07:36 +00:00
from reportlab.graphics.barcode.qr import QrCodeWidget
from reportlab.graphics.shapes import Drawing
from reportlab.lib.units import cm
from reportlab.lib.utils import ImageReader
from reportlab.pdfgen import canvas
2018-10-04 19:29:19 +00:00
if not (
hasattr(self.object, "product") and hasattr(self.object.product, "eticket")
):
raise Http404
2016-10-03 17:30:05 +00:00
eticket = self.object.product.eticket
user = self.object.customer.user
2018-10-04 19:29:19 +00:00
code = "%s %s %s %s" % (
self.object.customer.user.id,
self.object.product.id,
self.object.id,
self.object.quantity,
)
2016-10-03 17:30:05 +00:00
code += " " + eticket.get_hash(code)[:8].upper()
2018-10-04 19:29:19 +00:00
response = HttpResponse(content_type="application/pdf")
response["Content-Disposition"] = 'filename="eticket.pdf"'
2016-10-03 17:30:05 +00:00
p = canvas.Canvas(response)
p.setTitle("Eticket")
im = ImageReader("core/static/core/img/eticket.jpg")
width, height = im.getSize()
size = max(width, height)
width = 8 * cm * width / size
height = 8 * cm * height / size
p.drawImage(im, 10 * cm, 25 * cm, width, height)
if eticket.banner:
im = ImageReader(eticket.banner)
width, height = im.getSize()
size = max(width, height)
width = 6 * cm * width / size
height = 6 * cm * height / size
p.drawImage(im, 1 * cm, 25 * cm, width, height)
if user.profile_pict:
im = ImageReader(user.profile_pict.file)
width, height = im.getSize()
size = max(width, height)
width = 150 * width / size
height = 150 * height / size
p.drawImage(im, 10.5 * cm - width / 2, 16 * cm, width, height)
if eticket.event_title:
p.setFont("Helvetica-Bold", 20)
p.drawCentredString(10.5 * cm, 23.6 * cm, eticket.event_title)
if eticket.event_date:
p.setFont("Helvetica-Bold", 16)
2018-10-04 19:29:19 +00:00
p.drawCentredString(
10.5 * cm, 22.6 * cm, eticket.event_date.strftime("%d %b %Y")
) # FIXME with a locale
2016-10-03 17:30:05 +00:00
p.setFont("Helvetica-Bold", 14)
2018-10-04 19:29:19 +00:00
p.drawCentredString(
10.5 * cm,
15 * cm,
"%s : %d %s"
% (user.get_display_name(), self.object.quantity, str(_("people(s)"))),
)
2016-10-03 17:30:05 +00:00
p.setFont("Courier-Bold", 14)
qrcode = QrCodeWidget(code)
bounds = qrcode.getBounds()
width = bounds[2] - bounds[0]
height = bounds[3] - bounds[1]
2018-10-04 19:29:19 +00:00
d = Drawing(260, 260, transform=[260.0 / width, 0, 0, 260.0 / height, 0, 0])
2016-10-03 17:30:05 +00:00
d.add(qrcode)
renderPDF.draw(d, p, 10.5 * cm - 130, 6.1 * cm)
p.drawCentredString(10.5 * cm, 6 * cm, code)
2016-12-18 23:08:17 +00:00
partners = ImageReader("core/static/core/img/partners.png")
width, height = partners.getSize()
size = max(width, height)
width = width * 2 / 3
height = height * 2 / 3
p.drawImage(partners, 0 * cm, 0 * cm, width, height)
2016-10-03 17:30:05 +00:00
p.showPage()
p.save()
return response