Envoie du billet au paiement validé

This commit is contained in:
Jonas 12t 2021-10-31 15:25:00 +04:00
parent 9fe53bd339
commit d56366ca21
8 changed files with 36 additions and 18 deletions

View File

@ -1,4 +1,4 @@
<html> <html lang="fr">
<head> <head>
{% load static %} {% load static %}
@ -11,7 +11,7 @@
<body> <body>
<section id="informations"> <section id="informations">
<h1 id="name">Théodore Marcelin</h1> <h1 id="name">{{ ticket.first_name }} {{ ticket.last_name }}</h1>
<h1 id="destination">CDG ✈ LFLL</h1> <h1 id="destination">CDG ✈ LFLL</h1>
<dl> <dl>
<dt>Flight</dt> <dt>Flight</dt>

View File

@ -1,8 +1,10 @@
import os import os
import threading import threading
from django.core.mail import send_mail, EmailMessage, EmailMultiAlternatives from django.core.mail import send_mail, EmailMessage, EmailMultiAlternatives
from django.db import connection
from django.template.loader import render_to_string from django.template.loader import render_to_string
from django.utils import timezone from django.utils import timezone
from stripe.http_client import requests
from weasyprint import HTML from weasyprint import HTML
from BaseBillet.models import Configuration, Reservation, Ticket from BaseBillet.models import Configuration, Reservation, Ticket
@ -31,18 +33,24 @@ class ThreadMaileur():
if template and context : if template and context :
self.html = render_to_string(template, context=context) self.html = render_to_string(template, context=context)
self.context = context self.context = context
self.attached_file = self._attached_file() self.tickets_uuid = self._tickets_uuid()
self.url = self._url()
def _attached_file(self): def _url(self):
attached_file = [] url = f"http://{connection.tenant.domains.all()[0].domain}:8002/api/ticket/"
return url
def _tickets_uuid(self):
tickets_uuid = []
if self.context : if self.context :
if self.context.get('reservation'): if self.context.get('reservation'):
reservation: Reservation = self.context.get('reservation') reservation: Reservation = self.context.get('reservation')
tickets = reservation.tickets.filter(status=Ticket.NOT_SCANNED) tickets = reservation.tickets.filter(status=Ticket.NOT_SCANNED)
if len(tickets) > 0: if len(tickets) > 0:
for ticket in tickets : for ticket in tickets :
attached_file.append(render_to_string('ticket/ticket.html', context={'context': 'context'})) tickets_uuid.append(f"{ticket.uuid}")
return attached_file
return tickets_uuid
def config_valid(self): def config_valid(self):
EMAIL_HOST = os.environ.get('EMAIL_HOST') EMAIL_HOST = os.environ.get('EMAIL_HOST')
@ -66,15 +74,24 @@ class ThreadMaileur():
) )
mail.attach_alternative(self.html, "text/html") mail.attach_alternative(self.html, "text/html")
attached_file = []
for ticket in self.tickets_uuid :
response = requests.get(f"{self.url}{ticket}")
if response.status_code == 200:
attached_file.append(response.content)
# attached_file.append(render_to_string('ticket/ticket.html', context={'context': 'context'}))
# msg = EmailMessage(subject, html_content, from_email, [to]) # msg = EmailMessage(subject, html_content, from_email, [to])
# msg.content_subtype = "html" # Main content is now text/html # msg.content_subtype = "html" # Main content is now text/html
# msg.send() # msg.send()
# import ipdb; ipdb.set_trace() # import ipdb; ipdb.set_trace()
i=1 i=1
for file in self.attached_file: for file in attached_file:
html_before_pdf = HTML(string=file) # html_before_pdf = HTML(string=file)
mail.attach(f'ticket_{i}.pdf', html_before_pdf.write_pdf(), 'application/pdf') # mail.attach(f'ticket_{i}.pdf', html_before_pdf.write_pdf(), 'application/pdf')
mail.attach(f'ticket_{i}.pdf', file, 'application/pdf')
i += 1 i += 1
@ -91,8 +108,8 @@ class ThreadMaileur():
def send_with_tread(self): def send_with_tread(self):
self.send() # self.send()
# logger.info(f'{timezone.now()} on lance le thread email {self.email}') logger.info(f'{timezone.now()} on lance le thread email {self.email}')
# thread_email = threading.Thread(target=self.send) thread_email = threading.Thread(target=self.send)
# thread_email.start() thread_email.start()
# logger.info(f'{timezone.now()} Thread email lancé') logger.info(f'{timezone.now()} Thread email lancé')

View File

@ -15,6 +15,6 @@ router.register(r'reservations', api_view.ReservationViewset, basename='reservat
urlpatterns = [ urlpatterns = [
path('', include(router.urls)), path('', include(router.urls)),
path('ticket/<uuid:pk_uuid>', TicketPdf.as_view()), path('ticket/<uuid:pk_uuid>', TicketPdf.as_view(), name='ticket_uuid'),
] ]

View File

@ -25,14 +25,15 @@
html { html {
align-content: center; align-content: center;
align-items: center; align-items: center;
background: #eef1f5; background: #fff;
display: flex; display: flex;
font-family: Barlow Condensed, sans-serif; font-family: Barlow Condensed, sans-serif;
height: 100%; height: 100%;
justify-content: center; justify-content: center;
} }
body { body {
background: #fff; background: #eef1f5;
box-sizing: border-box; box-sizing: border-box;
color: #2A3239; color: #2A3239;
display: flex; display: flex;