Commit 86a5475c authored by MichiK's avatar MichiK Committed by anthraxx
Browse files

Add generation of customer tokens as PDF

parent 73872100
#!/usr/bin/env python
#!/usr/bin/env python3
import logging
from sys import exit
......@@ -11,6 +11,7 @@ from foobarpay.display import Display
from foobarpay.scanner import EvdevScanner, FifoScanner
from foobarpay.logic import Logic
from foobarpay.model.product import Product
from foobarpay.tokens import TokenGenerator
class FooBarPay:
DEFAULT_SCANNER = '/dev/input/by-id/usb-©_Symbol_Technologies__Inc__2000_Symbol_Bar_Code_Scanner_S_N:ac08a7010000_Rev:NBRXUAAQ3-event-kbd'
......@@ -48,6 +49,12 @@ if __name__ == "__main__":
parser.add_argument('--scanner', dest='scanner', default=FooBarPay.DEFAULT_SCANNER, help='specify scanner device')
parser.add_argument('--scanner-driver', dest='scanner_driver', choices=['evdev', 'fifo'], default='evdev', help='specify scanner driver')
parser.add_argument('--db', dest='database', default=FooBarPay.DEFAULT_DATABASE, help='specify database file')
parser.add_argument('--gen-tokens', dest='gen_tokens', action='store_true', help='generate customer tokens')
parser.set_defaults(debug=False, debug_sql=False)
FooBarPay(parser.parse_args()).start()
args = parser.parse_args()
if args.gen_tokens:
TokenGenerator(Database(args.database, debug=args.debug_sql)).generate()
else:
FooBarPay(args).start()
......@@ -10,7 +10,7 @@ class Logic(object):
Idle = 0
Started = 1
USER_ID_PREFIX = 'U-'
USER_ID_PREFIX = '999'
LOAD_PREFIX = '980'
def __init__(self, display, database):
......
import base64
from barcode import EAN13
from cairosvg import svg2pdf
from io import BytesIO
from jinja2 import Template
from random import randrange
from time import time
from foobarpay.model.customer import Customer
from foobarpay.logic import Logic
class TokenGenerator(object):
def __init__(self, db):
self.db = db
def make_barcode(self):
while True:
id = randrange(999999999)
if not self.db.get(Customer, id=id):
self.db.get_or_create(Customer, id=id)
self.db.commit()
break
ean = EAN13(Logic.USER_ID_PREFIX + str(id).zfill(9))
f = BytesIO()
ean.write(f, {'write_text': False})
return base64.encodestring(f.getvalue()).decode('utf-8')
def generate(self):
with open('resources/customer_tokens_template.svg') as f:
t = Template(f.read())
with open('customer_tokens_%i.pdf' % time(), 'wb') as f:
f.write(svg2pdf(t.render(make_barcode=self.make_barcode)))
This diff is collapsed.
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment