patch: deal with items not inserted to database
This commit is contained in:
parent
df150b531d
commit
1ff1108ba3
@ -39,6 +39,7 @@ def send_scan(login: str, scanned: dict[int: int], date:str = None):
|
|||||||
try:
|
try:
|
||||||
response = put(url=":".join([SERVER, str(
|
response = put(url=":".join([SERVER, str(
|
||||||
PORT)]) + '/scan2kasse/insert', json=infos)
|
PORT)]) + '/scan2kasse/insert', json=infos)
|
||||||
return True if response.status_code == 201 else False
|
return True if response.status_code == 201 else response.json
|
||||||
except:
|
except Exception as e:
|
||||||
|
LOGGER.exception(e)
|
||||||
return False
|
return False
|
||||||
|
|||||||
@ -1,4 +1,7 @@
|
|||||||
from datetime import date
|
from datetime import date
|
||||||
|
from json import dump as jdump, load as jload
|
||||||
|
from os import remove
|
||||||
|
from os.path import exists
|
||||||
from select import select as timedInput
|
from select import select as timedInput
|
||||||
from sys import stdin
|
from sys import stdin
|
||||||
import connection
|
import connection
|
||||||
@ -20,6 +23,7 @@ consoleHandler.setLevel(logging.DEBUG)
|
|||||||
LOGGER.addHandler(consoleHandler)
|
LOGGER.addHandler(consoleHandler)
|
||||||
|
|
||||||
TEMP = []
|
TEMP = []
|
||||||
|
TEMPFILE = "scans.json"
|
||||||
|
|
||||||
TIMEOUT = 60 # Number of seconds for a timeout after being logged in
|
TIMEOUT = 60 # Number of seconds for a timeout after being logged in
|
||||||
|
|
||||||
@ -27,16 +31,26 @@ TIMEOUT = 60 # Number of seconds for a timeout after being logged in
|
|||||||
def main() -> None:
|
def main() -> None:
|
||||||
while True:
|
while True:
|
||||||
login = input("Enter Login: ")
|
login = input("Enter Login: ")
|
||||||
|
if login == "quit":
|
||||||
|
break
|
||||||
if not connection.check_login(login):
|
if not connection.check_login(login):
|
||||||
continue # Send Error that login wasn't possible
|
continue # Send Error that login wasn't possible
|
||||||
scanned = scanning()
|
scanned = scanning()
|
||||||
scanned = group_scanning(scanned)
|
scanned = group_scanning(scanned)
|
||||||
if not connection.send_scan(login, scanned):
|
result = connection.send_scan(login, scanned)
|
||||||
TEMP.append({'login': login, 'items': scanned, 'date': str(date.today())})
|
if result != True:
|
||||||
|
result['date'] = str(date.now())
|
||||||
|
TEMP.append(result)
|
||||||
if TEMP:
|
if TEMP:
|
||||||
for bought in TEMP:
|
for bought in TEMP:
|
||||||
if connection.send_scan(bought['login'], bought['items'], bought['date']):
|
result = connection.send_scan(bought['login'], bought['items'], bought['date'])
|
||||||
TEMP.remove(bought)
|
TEMP.remove(bought)
|
||||||
|
if result != True:
|
||||||
|
TEMP.append(result)
|
||||||
|
with open(TEMPFILE, "w") as file:
|
||||||
|
jdump(TEMP, file)
|
||||||
|
elif exists(TEMPFILE):
|
||||||
|
remove(TEMPFILE)
|
||||||
|
|
||||||
|
|
||||||
def scanning() -> list:
|
def scanning() -> list:
|
||||||
@ -74,4 +88,7 @@ def group_scanning(scanned: list[int]) -> dict[int: int]:
|
|||||||
return scan_dict
|
return scan_dict
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
if exists(TEMPFILE):
|
||||||
|
with open(TEMPFILE, "r") as file:
|
||||||
|
TEMP = jload(file)
|
||||||
main()
|
main()
|
||||||
@ -1,4 +1,4 @@
|
|||||||
from psycopg2 import connect as psyconn, ProgrammingError
|
from psycopg2 import connect as psyconn, ProgrammingError, errors
|
||||||
from yaml import safe_load
|
from yaml import safe_load
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
@ -33,6 +33,7 @@ class Database:
|
|||||||
LOGGER.info('Connecting to Database.')
|
LOGGER.info('Connecting to Database.')
|
||||||
self.conn = psyconn(host=kwargs["host"], port=kwargs["port"], dbname=kwargs["database"],
|
self.conn = psyconn(host=kwargs["host"], port=kwargs["port"], dbname=kwargs["database"],
|
||||||
user=kwargs["user"], password=kwargs["password"])
|
user=kwargs["user"], password=kwargs["password"])
|
||||||
|
self.conn.autocommit = True
|
||||||
|
|
||||||
def test_connection(self):
|
def test_connection(self):
|
||||||
if not hasattr(self, "conn"):
|
if not hasattr(self, "conn"):
|
||||||
@ -72,23 +73,23 @@ class Database:
|
|||||||
|
|
||||||
@connectionpersistence
|
@connectionpersistence
|
||||||
def get_report(self, **kwargs) -> list:
|
def get_report(self, **kwargs) -> list:
|
||||||
query = "SELECT u.name, bp.buy_date, i.name, bp.amount, bp.buy_price FROM bought_with_prices bp INNER JOIN items i ON bp.item = i.id INNER JOIN users u ON bp.user = u.login"
|
query = "SELECT u.name, bp.date, i.name, bp.amount, bp.price FROM bought_with_prices bp INNER JOIN items i ON bp.item = i.id INNER JOIN users u ON bp.user = u.login"
|
||||||
if kwargs:
|
if kwargs:
|
||||||
query += " WHERE "
|
query += " WHERE "
|
||||||
tempquery = []
|
tempquery = []
|
||||||
if "user" in kwargs and kwargs['user']:
|
if "user" in kwargs and kwargs['user']:
|
||||||
tempquery.append(f"bp.user = '{kwargs['user']}'")
|
tempquery.append(f"bp.user = '{kwargs['user']}'")
|
||||||
if "year" in kwargs and kwargs['year']:
|
if "year" in kwargs and kwargs['year']:
|
||||||
tempstring = ""
|
tempstring = "bp.date BETWEEN "
|
||||||
if "month" in kwargs and kwargs['month']:
|
if "month" in kwargs and kwargs['month']:
|
||||||
tempstring += f"bp.buy_date BETWEEN '{kwargs['year']}-{kwargs['month']}-01' AND "
|
tempstring += f"'{kwargs['year']}-{kwargs['month']}-01' AND "
|
||||||
tempstring += f"'{kwargs['year']+1}-01-01'" if kwargs['month'] == 12 else f"'{kwargs['year']}-{kwargs['month']+1}-01'"
|
tempstring += f"'{kwargs['year']+1}-01-01'" if kwargs['month'] == 12 else f"'{kwargs['year']}-{kwargs['month']+1}-01'"
|
||||||
else:
|
else:
|
||||||
tempstring += f"bp.buy_date BETWEEN '{kwargs['year']}-01-01' AND '{kwargs['year']+1}-01-01'"
|
tempstring += f"'{kwargs['year']}-01-01' AND '{kwargs['year']+1}-01-01'"
|
||||||
tempstring += "::date - INTERVAL '1' DAY"
|
tempstring += "::date - INTERVAL '1' DAY"
|
||||||
tempquery.append(tempstring)
|
tempquery.append(tempstring)
|
||||||
query += " AND ".join(tempquery)
|
query += " AND ".join(tempquery)
|
||||||
query += " ORDER BY u.name, bp.buy_date, i.name ASC;"
|
query += " ORDER BY u.name, bp.date, i.name ASC;"
|
||||||
LOGGER.debug(f"Executing query: {query}")
|
LOGGER.debug(f"Executing query: {query}")
|
||||||
result = []
|
result = []
|
||||||
with self.conn.cursor() as cursor:
|
with self.conn.cursor() as cursor:
|
||||||
@ -103,23 +104,29 @@ class Database:
|
|||||||
|
|
||||||
@connectionpersistence
|
@connectionpersistence
|
||||||
def insert_bought_items(self, user: str, items: dict, date: str = None):
|
def insert_bought_items(self, user: str, items: dict, date: str = None):
|
||||||
temp = ["user, item, amount", "%(user)s, %(item)s, %(amount)s",
|
temp = ['"user", item, amount', "%(user)s, %(item)s, %(amount)s",
|
||||||
"bought.user = %(user)s AND bought.item = %(item)s AND bought.date = " + ("%(date)s" if date else "NOW()")]
|
"bought.user = %(user)s AND bought.item = %(item)s AND bought.date = " + ("%(date)s" if date else "NOW()")]
|
||||||
if date:
|
if date:
|
||||||
temp[0] += ", date"
|
temp[0] += ", date"
|
||||||
temp[1] += ", %(date)s"
|
temp[1] += ", %(date)s"
|
||||||
values = [{'user': user, 'item': key, 'amount': value, 'date': date} for key, value in items.items()]
|
values = [{'user': user, 'item': int(key), 'amount': value, 'date': date} for key, value in items.items()]
|
||||||
else:
|
else:
|
||||||
values = [{'user': user, 'item': key, 'amount': value} for key, value in items.items()]
|
values = [{'user': user, 'item': int(key), 'amount': value} for key, value in items.items()]
|
||||||
query = f"INSERT INTO bought({temp[0]}) VALUES({temp[1]}) ON CONFLICT ON CONSTRAINT bought_user_item_buy_date DO UPDATE SET amount = bought.amount + %(amount)s WHERE {temp[2]};"
|
query = f"INSERT INTO bought({temp[0]}) VALUES({temp[1]}) ON CONFLICT ON CONSTRAINT bought_user_item_date DO UPDATE SET amount = bought.amount + %(amount)s WHERE {temp[2]};"
|
||||||
with self.conn.cursor() as cursor:
|
with self.conn.cursor() as cursor:
|
||||||
try:
|
failed = {}
|
||||||
cursor.executemany(query, values)
|
for value in values:
|
||||||
self.conn.commit()
|
try:
|
||||||
return True
|
cursor.execute(query, value)
|
||||||
except Exception as e:
|
except errors.ForeignKeyViolation as e:
|
||||||
LOGGER.debug(e)
|
if failed:
|
||||||
return False
|
failed['items'][value['item']] = value['amount']
|
||||||
|
else:
|
||||||
|
failed = dict(value)
|
||||||
|
LOGGER.exception(e)
|
||||||
|
except Exception as e:
|
||||||
|
LOGGER.exception(e)
|
||||||
|
return failed
|
||||||
|
|
||||||
def __delete__(self):
|
def __delete__(self):
|
||||||
self.conn.close()
|
self.conn.close()
|
||||||
|
|||||||
@ -41,13 +41,15 @@ def login():
|
|||||||
def insert():
|
def insert():
|
||||||
match request.json:
|
match request.json:
|
||||||
case {'login': login, 'items': items, 'date': date}:
|
case {'login': login, 'items': items, 'date': date}:
|
||||||
if DATABASE.insert_bought_items(login, items, date):
|
failed = DATABASE.insert_bought_items(login, items, date)
|
||||||
return jsonify({'insert': True}), 201
|
if failed:
|
||||||
return jsonify({'insert': False}), 400
|
return jsonify(failed), 400
|
||||||
|
return jsonify({'inserted': True}), 201
|
||||||
case {'login': login, 'items': items}:
|
case {'login': login, 'items': items}:
|
||||||
if DATABASE.insert_bought_items(login, items):
|
failed = DATABASE.insert_bought_items(login, items)
|
||||||
return jsonify({'insert': True}), 201
|
if failed:
|
||||||
return jsonify({'insert': False}), 400
|
return jsonify(failed), 400
|
||||||
|
return jsonify({'inserted': True}), 201
|
||||||
case _:
|
case _:
|
||||||
abort(400)
|
abort(400)
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user