some cleanup and rewrite

This commit is contained in:
Jan-Philipp Warmers 2014-04-19 14:31:04 +02:00
parent 9a1e944938
commit 0e3dd402b8
1 changed files with 104 additions and 59 deletions

View File

@ -1,12 +1,13 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
__author__ = 'progamler' __author__ = 'progamler'
from os import system
import curses import curses
from sqlalchemy import Table, Column, Integer, String, MetaData, ForeignKey, create_engine import datetime
from sqlalchemy import Table, Column, Integer, String, MetaData, ForeignKey, create_engine, Date, func
from sqlalchemy.sql import select from sqlalchemy.sql import select
# Config foo # Config foo
db_uri = 'sqlite:///./db.sqlite' db_uri = 'sqlite:///./db.sqlite'
@ -24,12 +25,14 @@ def create_db():
Column('barcode', Integer, primary_key=True), Column('barcode', Integer, primary_key=True),
Column('name', String(40)), Column('name', String(40)),
Column('preis', Integer), ) Column('preis', Integer), )
users = Table('user', metadata, user = Table('user', metadata,
Column('barcode', Integer, primary_key=True), Column('barcode', Integer, primary_key=True),
Column('name', String(40)), ) Column('name', String(40)), )
#transaktion = Table('transaktion', metadata, transaktion = Table('transaktion', metadata,
#Column('produkt', Integer, ForeignKey('produkte.barcode')), Column('id', Integer, primary_key=True),
#Column('user', Integer ,ForeignKey('users.barcode'))) Column('produkt', Integer, ForeignKey('produkte.barcode')),
Column('user', Integer, ForeignKey('user.barcode')),
Column('time', Date, server_default=func.now()), sqlite_autoincrement=True)
metadata.create_all(db) metadata.create_all(db)
@ -39,22 +42,22 @@ def create_db():
db.execute(i, name='l33t', preis=1337, barcode='3') db.execute(i, name='l33t', preis=1337, barcode='3')
db.execute(i, name='Clubmate 0.5', preis=1.5, barcode='4029764001807') db.execute(i, name='Clubmate 0.5', preis=1.5, barcode='4029764001807')
u = users.insert() u = user.insert()
db.execute(u, barcode='23', name='proGamler') db.execute(u, barcode='23', name='proGamler')
def return_price(produkt): def return_price(produkt):
db = create_engine(db_uri) db = create_engine(db_uri)
metadata = MetaData(db) metadata = MetaData(db)
metadata.reflect() metadata.reflect()
produkte = Table('produkte', metadata, autoload=True) produkte = Table('produkte', metadata, autoload=True)
s = select(['name', 'preis']).where(produkte.c.barcode == produkt) s = select(['name', 'preis', 'barcode']).where(produkte.c.barcode == produkt)
result = db.execute(s) result = db.execute(s)
return result.fetchone() return result.fetchone()
def isproduct(code): def isproduct(code):
foo = return_price(code) foo = return_price(code)
try: try:
@ -63,32 +66,58 @@ def isproduct(code):
except: except:
return False return False
def user(code): def user(code):
db = create_engine(db_uri) db = create_engine(db_uri)
metadata = MetaData(db) metadata = MetaData(db)
metadata.reflect() metadata.reflect()
user = Table('user', metadata, autoload=True) user = Table('user', metadata, autoload=True)
s = select(['name']).where(user.c.barcode == code) s = select(['name', 'barcode']).where(user.c.barcode == code)
result = db.execute(s) result = db.execute(s)
x = result.fetchone() x = result.fetchone()
return x.name
return x
def isuser(code): def isuser(code):
try:
name = user(code) name = user(code)
try:
print(name.name)
return True return True
except: except:
return False return False
def logout(x): def logout(x):
if x == 666: if x == 666:
return True return True
else: else:
return False return False
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 x = 0
name = '' name = ''
list = [] list = []
userbarcode = ''
while x != int('01'): while x != int('01'):
screen = curses.initscr() screen = curses.initscr()
@ -102,13 +131,29 @@ while x != int('01'):
screen.addstr(4, 4, pro.name) screen.addstr(4, 4, pro.name)
list.append(x) list.append(x)
elif isuser(x): elif isuser(x):
name = user(x) foo = user(x)
name = foo.name
userbarcode = foo.barcode
screen.addstr(4, 4, "Du wurdest eingeloggt") screen.addstr(4, 4, "Du wurdest eingeloggt")
elif logout(x): 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 = '' name = ''
list = [] list = []
screen.addstr(2, 30, name) screen.addstr(2, 30, name)
preis = 0 preis = 0
y = 6 y = 6
@ -123,9 +168,9 @@ while x != int('01'):
screen.addstr(y + 2, 30, str(preis)) screen.addstr(y + 2, 30, str(preis))
k = screen.getstr(3, 3, 60).decode('utf-8').rstrip() k = screen.getstr(3, 3, 60).decode('utf-8').rstrip()
if k != '': try:
x = int(k) x = int(k)
else: except:
x = 0 x = 0
curses.endwin() curses.endwin()