Skip to content

Commit 4c5965a

Browse files
committed
Improve code quality
1 parent 38f964b commit 4c5965a

File tree

3 files changed

+38
-18
lines changed

3 files changed

+38
-18
lines changed

insalan/tickets/models.py

+31-14
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"""
44
import uuid
55
from io import BytesIO
6+
from os import path
67

78
from django.db import models
89
from django.utils.translation import gettext_lazy as _
@@ -14,7 +15,7 @@
1415

1516
from insalan import settings
1617
from insalan.user.models import User
17-
18+
from insalan.cms.models import Content
1819

1920
class Ticket(models.Model):
2021
"""
@@ -73,7 +74,7 @@ def generate_ticket_pdf(ticket):
7374
p = canvas.Canvas(buffer, pagesize=(page_width, page_height))
7475

7576
# get and resize (to reduce the size of the pdf) the tournament image
76-
image = Image.open(settings.MEDIA_ROOT + "/" + str(ticket.tournament.logo))
77+
image = Image.open(path.join(settings.MEDIA_ROOT, str(ticket.tournament.logo)))
7778
image.thumbnail((page_width*1.5, page_height*1.5), Image.BILINEAR)
7879
img = utils.ImageReader(image)
7980
iw, ih = img.getSize()
@@ -109,7 +110,7 @@ def generate_ticket_pdf(ticket):
109110
p.drawImage(qr, page_width/4, 0.117 * page_height, qr_size, qr_size)
110111

111112
# add the logo from static
112-
logo = Image.open(settings.STATIC_ROOT + "/images/logo.png")
113+
logo = Image.open(path.join(settings.STATIC_ROOT, "images/logo.png"))
113114
img = utils.ImageReader(logo)
114115
im_size = 150/850 * page_height
115116
p.drawImage(img, 0.08 * page_width, 0.588 * page_height, im_size, im_size, mask='auto')
@@ -203,18 +204,34 @@ def generate_ticket_pdf(ticket):
203204

204205
# split the string in multiple lines
205206
n = 105
206-
CGV = "En faisant l'acquisition de ce billet, le détenteur reconnaît et accepte les conditions suivantes : aucun remboursement ne sera effectué, sauf en cas d'annulation de l'événement par l'insalan. L'insalan décline toute responsabilité en cas de perte, vol ou dommage des billets. L'admission à l'événement est conditionnée au respect des règles en vigueur sur le site. En participant, le détenteur autorise l'utilisation de son image à des fins promotionnelles. Pour de plus amples informations, veuillez consulter le site web de l'insalan."
207-
parts = []
208-
for i in CGV.split(" "):
209-
if len(parts) == 0 or len(parts[-1]) + 1 + len(i) > n:
210-
parts.append(i)
211-
else:
212-
parts[-1] += " " + i
213-
214-
# write the lines
215-
for i, part in enumerate(parts):
216-
p.drawCentredString(page_width/2, 0.094 * page_height - i * 0.0141 * page_height, part)
207+
CGV = Content.objects.filter(name="ticket_CGV")
208+
if CGV:
209+
parts = []
210+
for i in CGV.first().content.split(" "):
211+
if len(parts) == 0 or len(parts[-1]) + 1 + len(i) > n:
212+
parts.append(i)
213+
else:
214+
parts[-1] += " " + i
215+
216+
# write the lines
217+
for i, part in enumerate(parts):
218+
p.drawCentredString(page_width/2, 0.094 * page_height - i * 0.0141 * page_height, part)
217219

218220
p.showPage()
219221
p.save()
220222
return buffer.getvalue()
223+
224+
@staticmethod
225+
def create_pdf_name(ticket):
226+
"""
227+
Create the name of the pdf file for a ticket.
228+
"""
229+
# we only keep alphanumeric characters and spaces
230+
username = ''.join(
231+
c for c in ticket.user.username if c.isalnum() or c == " "
232+
).replace(' ', '-')
233+
event_name = ''.join(
234+
c for c in ticket.tournament.event.name if c.isalnum() or c == " "
235+
).replace(' ', '-')
236+
237+
return f"billet-{username}-{event_name}.pdf"

insalan/tickets/views.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,12 @@
1818
from rest_framework.permissions import IsAuthenticated, IsAdminUser
1919

2020
from insalan.user.models import User
21-
from .models import Ticket, TicketManager
22-
from django.shortcuts import render
21+
from .models import Ticket
2322

2423
@api_view(["GET"])
2524
@permission_classes([IsAdminUser])
2625
def get(request: HttpRequest, id: str, token: str) -> JsonResponse:
27-
"""Get ticket details for the given id and token."""
26+
"""Get ticket details for the given user id and token."""
2827
try:
2928
uuid.UUID(hex=token)
3029
except ValueError:

insalan/user/models.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,11 @@ def send_ticket_mail(user_object: User, ticket: str):
227227
[user_object.email],
228228
connection=connection,
229229
)
230-
email.attach(f"billet-{user_object.username.replace(' ', '-')}-{ticket.tournament.event.name.replace(' ', '-')}.pdf", ticket_pdf, "application/pdf")
230+
email.attach(
231+
TicketManager.create_pdf_name(ticket),
232+
ticket_pdf,
233+
"application/pdf"
234+
)
231235
email.send()
232236

233237
# vim: set tw=80 cc=80:

0 commit comments

Comments
 (0)