matemat/preisanzeige/main.py

196 lines
5.9 KiB
Python
Raw Normal View History

2014-04-18 16:35:47 +00:00
#!/usr/bin/env python3
__author__ = 'progamler'
import curses
2014-04-19 12:31:04 +00:00
import datetime
from sqlalchemy import Table, Column, Integer, String, MetaData, ForeignKey, create_engine, Date, func
2014-04-18 16:35:47 +00:00
from sqlalchemy.sql import select
2014-04-18 16:42:17 +00:00
2014-04-19 12:31:04 +00:00
2014-04-18 16:42:17 +00:00
# Config foo
db_uri = 'sqlite:///./db.sqlite'
2014-04-18 16:35:47 +00:00
#produkte = Table('produkte', metadata,
2014-04-19 12:31:04 +00:00
# Column('barcode', Integer, primary_key=True),
# Column('name', String(40)),
# Column('preis', Integer),)
2014-04-18 16:35:47 +00:00
def create_db():
2014-04-18 16:42:17 +00:00
db = create_engine(db_uri)
2014-04-18 16:35:47 +00:00
metadata = MetaData()
produkte = Table('produkte', metadata,
2014-04-19 12:31:04 +00:00
Column('barcode', Integer, primary_key=True),
Column('name', String(40)),
Column('preis', Integer), )
user = Table('user', metadata,
Column('barcode', Integer, primary_key=True),
Column('name', String(40)), )
transaktion = Table('transaktion', metadata,
Column('id', Integer, primary_key=True),
Column('produkt', Integer, ForeignKey('produkte.barcode')),
Column('user', Integer, ForeignKey('user.barcode')),
Column('time', Date, server_default=func.now()), sqlite_autoincrement=True)
2014-04-18 16:35:47 +00:00
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')
2014-04-29 19:37:05 +00:00
db.execute(i, name='Florapower 0.5', preis=1.5, barcode='4260031874056')
db.execute(i, name='Hermann Brause Orange 0.33', preis=1, barcode='1094922076228')
db.execute(i, name='Hermann Brause Alienblut 0.33', preis=1, barcode='1094922076235')
db.execute(i, name='Hermann Brause Apfel-Kirsch 0.33', preis=1, barcode='1094922076259')
db.execute(i, name='Spezi 0.33', preis=1, barcode='4008340003381')
db.execute(i, name='Clubmate Cola 0.33', preis=1, barcode='4029764001883')
db.execute(i, name='Hermann Brause Rhabarber-Erdbeer 0.33', preis=1, barcode='1094922075450')
db.execute(i, name='Mars', preis=1, barcode='5000159407236')
db.execute(i, name='Twix', preis=1, barcode='5000159459228')
db.execute(i, name='KitKat', preis=1, barcode='40114606')
db.execute(i, name='Wunderbar', preis=1, barcode='5034660522768')
db.execute(i, name='Riesen Karamell', preis=1, barcode='40144238')
db.execute(i, name='Banane-Mandel', preis=1, barcode='4016249300401')
db.execute(i, name='Nuss-Schnitte Cashew', preis=1, barcode='4016249013455')
db.execute(i, name='Krunchy Zartbitter-Nuss', preis=1, barcode='4021234103639')
db.execute(i, name='Snickers', preis=1, barcode='5000159461122')
2014-04-18 16:35:47 +00:00
2014-04-19 12:31:04 +00:00
u = user.insert()
2014-04-18 16:35:47 +00:00
db.execute(u, barcode='23', name='proGamler')
def return_price(produkt):
2014-04-18 16:42:17 +00:00
db = create_engine(db_uri)
2014-04-18 16:35:47 +00:00
metadata = MetaData(db)
metadata.reflect()
produkte = Table('produkte', metadata, autoload=True)
2014-04-19 12:31:04 +00:00
s = select(['name', 'preis', 'barcode']).where(produkte.c.barcode == produkt)
2014-04-18 16:35:47 +00:00
result = db.execute(s)
return result.fetchone()
2014-04-19 12:31:04 +00:00
2014-04-18 16:35:47 +00:00
def isproduct(code):
foo = return_price(code)
2014-04-19 12:31:04 +00:00
try:
2014-04-18 16:35:47 +00:00
print(foo.name)
return True
except:
return False
2014-04-19 12:31:04 +00:00
2014-04-18 16:35:47 +00:00
def user(code):
2014-04-18 16:42:17 +00:00
db = create_engine(db_uri)
2014-04-18 16:35:47 +00:00
metadata = MetaData(db)
metadata.reflect()
user = Table('user', metadata, autoload=True)
2014-04-19 12:31:04 +00:00
s = select(['name', 'barcode']).where(user.c.barcode == code)
2014-04-18 16:35:47 +00:00
result = db.execute(s)
x = result.fetchone()
2014-04-19 12:31:04 +00:00
return x
2014-04-18 16:35:47 +00:00
def isuser(code):
2014-04-19 12:31:04 +00:00
name = user(code)
2014-04-18 16:35:47 +00:00
try:
2014-04-19 12:31:04 +00:00
print(name.name)
2014-04-18 16:35:47 +00:00
return True
except:
return False
2014-04-19 12:31:04 +00:00
2014-04-18 16:35:47 +00:00
def logout(x):
if x == 666:
return True
else:
return False
2014-04-19 12:31:04 +00:00
def count(date):
db = create_engine(db_uri)
metadata = MetaData(db)
metadata.reflect()
trans = Table('transaktion', metadata, autoload=True)
user = Table('transaktion', metadata, autoload=True)
current_time = datetime.timedelta(weeks=date)
s = select(['id','user']).where(trans.c.time > current_time)
result = db.execute(s)
for a in result:
print(a)
if __name__ == "__main__":
x = 0
name = ''
list = []
userbarcode = ''
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):
foo = user(x)
name = foo.name
userbarcode = foo.barcode
screen.addstr(4, 4, "Du wurdest eingeloggt")
elif logout(x):
if userbarcode:
db = create_engine(db_uri)
metadata = MetaData(db)
metadata.reflect()
trans = Table('transaktion', metadata, autoload=True)
trans_o = trans.insert()
for a in list:
foo = return_price(a)
db.execute(trans_o,
produkt=foo.barcode,
user=userbarcode
)
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()
try:
x = int(k)
except:
x = 0
curses.endwin()
2014-04-18 16:35:47 +00:00