|
22 | 22 |
|
23 | 23 |
|
24 | 24 | class UploadView(Resource):
|
25 |
| - def base64_encode_qrcode(self, qrcode): |
| 25 | + def base64_encode(self, qrcode): |
26 | 26 | try:
|
27 | 27 | with open(qrcode, "rb") as image_file:
|
28 | 28 | return base64.b64encode(image_file.read())
|
@@ -58,7 +58,7 @@ def get_ticket_id(self, soup):
|
58 | 58 | ticket_id = re.search("- (.+) -", soup.text)
|
59 | 59 | if not ticket_id:
|
60 | 60 | abort(422)
|
61 |
| - return ticket_id.groups()[0] |
| 61 | + return ticket_id.groups()[0].strip() |
62 | 62 |
|
63 | 63 | def get_route(self, soup):
|
64 | 64 | route = re.search("\d+ / \d+(.+) - (.*)Aikuinen", soup.text)
|
@@ -94,43 +94,49 @@ def is_square(fn):
|
94 | 94 | if 'file' not in args:
|
95 | 95 | abort(422)
|
96 | 96 |
|
97 |
| - # Danger Zone, parsing PDF files is dirty and this one definately ain't |
98 |
| - # the prettiest creature alive. Cleanse yourself with strong liqueur |
99 |
| - # afterwards. |
100 |
| - |
101 | 97 | tmp_dir = tempfile.mkdtemp()
|
102 | 98 | tmp_src_file = tmp_dir + '/in.pdf'
|
103 | 99 | with open(tmp_src_file, 'wb') as fd:
|
104 | 100 | fd.write(args['file'].read())
|
105 |
| - |
106 |
| - pages = self.get_pages(tmp_src_file) |
107 |
| - if not pages: |
108 |
| - return {"message": "Could not parse the uploaded file, is this actually the PDF file with tickets?"}, 422 |
109 | 101 |
|
110 |
| - first_page = pages[0] |
111 |
| - ticket_count = mongo.db.tickets.find({"order_id": self.get_order_id(first_page)}).count() |
| 102 | + qr_codes = [] |
| 103 | + pdf_files = [] |
112 | 104 |
|
113 |
| - qr_codes = iter([]) |
114 |
| - if ticket_count: |
115 |
| - return {"message": "Ticket already uploaded"}, 400 |
116 | 105 | try:
|
| 106 | + # Danger Zone, parsing PDF files is dirty and this one definately ain't |
| 107 | + # the prettiest creature alive. Cleanse yourself with strong liqueur |
| 108 | + # afterwards. |
117 | 109 | os.popen('pdfimages -png ' + tmp_src_file + ' ' + tmp_dir + '/out')
|
118 |
| - ims = os.popen("ls " + tmp_dir + "/out*").read().split() |
119 |
| - qr_codes = iter([fn for fn in ims if is_square(fn)]) |
120 |
| - # qr_codes = iter(ims[1:len(ims):2]) |
| 110 | + ims = os.popen("ls " + tmp_dir + "/out*.png").read().split() |
| 111 | + qr_codes = [fn for fn in ims if is_square(fn)] |
| 112 | + os.popen("pdfseparate " + tmp_src_file + ' ' + tmp_dir + "/pdfout-%d.pdf") |
| 113 | + pdfs = os.popen("ls " + tmp_dir + "/pdfout-*.pdf").read().split() |
| 114 | + pdf_files = [fn for fn in pdfs] |
121 | 115 | except:
|
122 | 116 | print("XXX: No poppler installed, unable to produce 2D barcodes")
|
123 | 117 |
|
| 118 | + if len(qr_codes) != len(pdf_files) != len(pages): |
| 119 | + return {"message": "Invalid ticket file"}, 400 |
| 120 | + |
124 | 121 | tickets = []
|
125 |
| - for page in pages: |
| 122 | + for qr_code, pdf_file in iter(zip(qr_codes, pdf_files)): |
| 123 | + pages = self.get_pages(pdf_file) |
| 124 | + if not pages: |
| 125 | + return {"message": "Could not parse the uploaded file, is this actually the PDF file with tickets?"}, 422 |
| 126 | + page = pages[0] |
| 127 | + ticket_count = mongo.db.tickets.find({"order_id": self.get_order_id(page)}).count() |
| 128 | + if ticket_count: |
| 129 | + return {"message": "Ticket already uploaded"}, 400 |
126 | 130 | route = self.get_route(page)
|
127 | 131 | ticket_type, expires = self.get_ticket_type_and_expiration(page)
|
128 |
| - qr_base64 = self.base64_encode_qrcode(qr_codes.next()) |
| 132 | + qr_base64 = self.base64_encode(qr_code) |
| 133 | + pdf_base64 = self.base64_encode(pdf_file) |
129 | 134 | tickets.append({
|
130 | 135 | 'ticket_id': uuid.uuid4().hex,
|
131 | 136 | 'src': route[0].upper(),
|
132 | 137 | 'dest': route[1].upper(),
|
133 | 138 | 'qr': 'data:image/png;base64,' + qr_base64,
|
| 139 | + 'pdf': 'data:application/pdf;base64,' + pdf_base64, |
134 | 140 | 'order_id': self.get_order_id(page),
|
135 | 141 | 'price': self.get_price(page) / len(pages),
|
136 | 142 | 'ticket_type': ticket_type,
|
|
0 commit comments