matemat/preisanzeige/main.py

132 lines
3.2 KiB
Python
Executable File

#!/usr/bin/env python3
__author__ = 'progamler'
from os import system
import curses
from sqlalchemy import Table, Column, Integer, String, MetaData, ForeignKey, create_engine
from sqlalchemy.sql import select
#produkte = Table('produkte', metadata,
# Column('barcode', Integer, primary_key=True),
# Column('name', String(40)),
# Column('preis', Integer),)
def create_db():
db = create_engine('sqlite:///./db.sqlite')
metadata = MetaData()
produkte = Table('produkte', metadata,
Column('barcode', Integer, primary_key=True),
Column('name', String(40)),
Column('preis', Integer),)
users = Table('user', metadata,
Column('barcode', Integer, primary_key=True),
Column('name', String(40)),)
#transaktion = Table('transaktion', metadata,
#Column('produkt', Integer, ForeignKey('produkte.barcode')),
#Column('user', Integer ,ForeignKey('users.barcode')))
metadata.create_all(db)
i = produkte.insert()
db.execute(i, name='Wasser', preis=0.5, barcode='42600370288')
db.execute(i, name='Over 9000', preis=9000.01, barcode='2')
db.execute(i, name='l33t', preis=1337, barcode='3')
db.execute(i, name='Clubmate 0.5', preis=1.5, barcode='4029764001807')
u = users.insert()
db.execute(u, barcode='23', name='proGamler')
def return_price(produkt):
db = create_engine('sqlite:///./db.sqlite')
metadata = MetaData(db)
metadata.reflect()
produkte = Table('produkte', metadata, autoload=True)
s = select(['name', 'preis']).where(produkte.c.barcode == produkt)
result = db.execute(s)
return result.fetchone()
def isproduct(code):
foo = return_price(code)
try :
print(foo.name)
return True
except:
return False
def user(code):
db = create_engine('sqlite:///./db.sqlite')
metadata = MetaData(db)
metadata.reflect()
user = Table('user', metadata, autoload=True)
s = select(['name']).where(user.c.barcode == code)
result = db.execute(s)
x = result.fetchone()
return x.name
def isuser(code):
try:
name = user(code)
return True
except:
return False
def logout(x):
if x == 666:
return True
else:
return False
x = 0
name = ''
list = []
while x != int('01'):
screen = curses.initscr()
screen.clear()
screen.border(0)
screen.addstr(2, 2, "Please Scan Code")
screen.refresh()
if x != 0:
if isproduct(x):
pro = return_price(x)
screen.addstr(4, 4, pro.name)
list.append(x)
elif isuser(x):
name = user(x)
screen.addstr(4, 4, "Du wurdest eingeloggt")
elif logout(x):
name = ''
list = []
screen.addstr(2,30, name)
preis = 0
y = 6
for a in list:
foo = return_price(a)
screen.addstr(y,30, str(foo.preis))
screen.addstr(y,4, foo.name)
y = y+1
preis = preis+foo.preis
screen.addstr(y+1,4, "----------------------------------------------")
screen.addstr(y+2,4, "Gesamtsumme:")
screen.addstr(y+2,30, str(preis))
k = screen.getstr(3,3,60).decode('utf-8').rstrip()
if k != '':
x = int(k)
else:
x = 0
curses.endwin()