Archived
1
0
Fork 0
This repository has been archived on 2024-02-17. You can view files and clone it, but cannot push or open issues or pull requests.
teleexit/callbackQuery/create.py

108 lines
3.7 KiB
Python
Raw Normal View History

2020-11-07 13:18:53 +01:00
from datetime import datetime
from io import BytesIO
2020-11-07 14:41:31 +01:00
from re import compile
2020-11-07 13:18:53 +01:00
2020-11-07 14:27:06 +01:00
from PyPDF2 import PdfFileReader, PdfFileWriter
2020-11-07 13:18:53 +01:00
from qrcode import make
2020-11-07 14:27:06 +01:00
from reportlab.lib.pagesizes import letter
from reportlab.lib.utils import ImageReader
from reportlab.pdfgen import canvas
from telegram import Update, InlineKeyboardMarkup, InlineKeyboardButton
2020-11-07 12:16:52 +01:00
from telegram.ext import CallbackContext
2020-11-07 15:15:44 +01:00
import db
from main import reasons
2020-11-07 13:18:53 +01:00
local = {
"work": "travail",
"shopping": "achats",
"health": "sante",
"family": "famille",
"handicap": "handicap",
"sport_animal": "sport_animaux",
"injunction": "convocation",
"missions": "missions",
"children": "enfants"
}
2020-11-07 14:41:31 +01:00
address_re = compile(r"^(.*),? ([0-9]{5}) ([a-zA-Z]+(?:[\s-][a-zA-Z]+)*)$")
2020-11-07 12:16:52 +01:00
def create(update: Update, context: CallbackContext):
2020-11-07 14:27:06 +01:00
reason = reasons[update.effective_chat.id][update["_effective_user"]["id"]]
2020-11-07 12:16:52 +01:00
del reasons[update.effective_chat.id][update["_effective_user"]["id"]]
2020-11-07 14:27:06 +01:00
date = datetime.now()
2020-11-07 15:15:44 +01:00
s = db.Session()
u = s.query(db.User).get(update["_effective_user"]["id"])
s.close()
first_name = u.first_name
last_name = u.last_name
birth_date = u.birth_date
birth_city = u.birth_city
address = address_re.fullmatch(u.address).groups()
2020-11-07 14:27:06 +01:00
img = make(f"Cree le: {date.strftime('%d/%m/%Y a %Hh%M')};\n"
f"Nom: {first_name};\n"
f"Prenom: {last_name};\n"
f"Naissance: {birth_date} a "
f"{birth_city};\n"
2020-11-07 14:41:31 +01:00
f"Adresse: {address[0]} {address[1]} {address[2]};\n"
2020-11-07 14:27:06 +01:00
f"Sortie: {date.strftime('%d/%m/%Y a %Hh%M')}\n"
f"Motifs: {', '.join(map(lambda r: local[r], reason))};")
2020-11-07 13:18:53 +01:00
photo = BytesIO()
photo.name = "QRCode.jpeg"
img.save(photo, "JPEG")
photo.seek(0)
context.bot.send_photo(chat_id=update.effective_chat.id, photo=photo)
2020-11-07 12:16:52 +01:00
2020-11-07 14:27:06 +01:00
packet = BytesIO()
can = canvas.Canvas(packet, pagesize=letter)
can.setFont("Helvetica", 11)
can.drawString(119, 696, f"{first_name} {last_name}")
2020-11-07 15:15:44 +01:00
can.drawString(119, 674, birth_date.strftime("%d/%m/%Y"))
2020-11-07 14:27:06 +01:00
can.drawString(297, 674, birth_city)
2020-11-07 14:41:31 +01:00
can.drawString(133, 652, f"{address[0]} {address[1]} {address[2]}")
2020-11-07 14:27:06 +01:00
can.setFontSize(18)
for r in reason:
y = 0
if r == "work":
y = 578
elif r == "shopping":
y = 533
elif r == "health":
y = 477
elif r == "family":
y = 435
elif r == "handicap":
y = 396
elif r == "sport_animal":
y = 358
elif r == "injunction":
y = 295
elif r == "missions":
y = 255
elif r == "children":
y = 211
can.drawString(78, y, "x")
can.setFontSize(11)
2020-11-07 14:41:31 +01:00
can.drawString(105, 177, address[2])
2020-11-07 14:27:06 +01:00
can.drawString(91, 153, date.strftime("%d/%m/%Y"))
can.drawString(264, 153, date.strftime("%Hh%M"))
existing_pdf = PdfFileReader(open("certificate.pdf", "rb"))
photo.seek(0)
can.drawImage(ImageReader(photo), existing_pdf.getPage(0).mediaBox[2] - 156, 100, 92, 92)
can.save()
packet.seek(0)
new_pdf = PdfFileReader(packet)
output = PdfFileWriter()
page = existing_pdf.getPage(0)
page.mergePage(new_pdf.getPage(0))
output.addPage(page)
output_stream = BytesIO()
output_stream.name = "Exit certificate.pdf"
output.write(output_stream)
output_stream.seek(0)
context.bot.send_document(chat_id=update.effective_chat.id, document=output_stream,
2020-11-07 15:34:03 +01:00
reply_markup=InlineKeyboardMarkup([[InlineKeyboardButton("🏘 Home", callback_data="home")],
[InlineKeyboardButton("📝 New", callback_data="new")]]))