|
3 | 3 | """
|
4 | 4 | import uuid
|
5 | 5 | from io import BytesIO
|
| 6 | +from os import path |
6 | 7 |
|
7 | 8 | from django.db import models
|
8 | 9 | from django.utils.translation import gettext_lazy as _
|
|
14 | 15 |
|
15 | 16 | from insalan import settings
|
16 | 17 | from insalan.user.models import User
|
17 |
| - |
| 18 | +from insalan.cms.models import Content |
18 | 19 |
|
19 | 20 | class Ticket(models.Model):
|
20 | 21 | """
|
@@ -73,7 +74,7 @@ def generate_ticket_pdf(ticket):
|
73 | 74 | p = canvas.Canvas(buffer, pagesize=(page_width, page_height))
|
74 | 75 |
|
75 | 76 | # 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))) |
77 | 78 | image.thumbnail((page_width*1.5, page_height*1.5), Image.BILINEAR)
|
78 | 79 | img = utils.ImageReader(image)
|
79 | 80 | iw, ih = img.getSize()
|
@@ -109,7 +110,7 @@ def generate_ticket_pdf(ticket):
|
109 | 110 | p.drawImage(qr, page_width/4, 0.117 * page_height, qr_size, qr_size)
|
110 | 111 |
|
111 | 112 | # 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")) |
113 | 114 | img = utils.ImageReader(logo)
|
114 | 115 | im_size = 150/850 * page_height
|
115 | 116 | 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):
|
203 | 204 |
|
204 | 205 | # split the string in multiple lines
|
205 | 206 | 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) |
217 | 219 |
|
218 | 220 | p.showPage()
|
219 | 221 | p.save()
|
220 | 222 | 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" |
0 commit comments