dibbler/text_interface/editing.py

191 lines
7.0 KiB
Python
Raw Normal View History

2017-02-20 13:19:03 +01:00
import sqlalchemy
from db import User, Product
from text_interface.helpermenus import Menu, Selector
__all__ = ["AddUserMenu", "AddProductMenu", "EditProductMenu", "AdjustStockMenu", "CleanupStockMenu", "EditUserMenu"]
class AddUserMenu(Menu):
def __init__(self):
Menu.__init__(self, 'Add user', uses_db=True)
def _execute(self):
self.print_header()
username = self.input_str('Username (should be same as PVV username)> ', User.name_re, (1, 10))
cardnum = self.input_str('Card number (optional)> ', User.card_re, (0, 10))
cardnum = cardnum.lower()
rfid = self.input_str('RFID (optional)> ', User.rfid_re, (0, 10))
user = User(username, cardnum, rfid)
self.session.add(user)
try:
self.session.commit()
print(f'User {username} stored')
2018-08-19 16:38:45 +02:00
except sqlalchemy.exc.IntegrityError as e:
print(f'Could not store user {username}: {e}')
2017-02-20 13:19:03 +01:00
self.pause()
class EditUserMenu(Menu):
def __init__(self):
Menu.__init__(self, 'Edit user', uses_db=True)
self.help_text = '''
The only editable part of a user is its card number and rfid.
First select an existing user, then enter a new card number for that
user, then rfid (write an empty line to remove the card number or rfid).
'''
def _execute(self):
self.print_header()
user = self.input_user('User> ')
self.printc(f'Editing user {user.name}')
card_str = f'"{user.card}"'
2017-02-20 13:19:03 +01:00
if user.card is None:
card_str = 'empty'
# TODO: Inconsistent with other defaulted strings. Redo.
2017-02-20 13:19:03 +01:00
user.card = self.input_str('Card number (currently %s)> ' % card_str,
User.card_re, (0, 10),
empty_string_is_none=True)
if user.card:
user.card = user.card.lower()
rfid_str = f'"{user.rfid}"'
2017-02-20 13:19:03 +01:00
if user.rfid is None:
rfid_str = 'empty'
# TODO: Inconsistent with other defaulted strings. Redo.
user.rfid = self.input_str(f'RFID (currently {rfid_str})> ',
2017-02-20 13:19:03 +01:00
User.rfid_re, (0, 10),
empty_string_is_none=True)
try:
self.session.commit()
print(f'User {user.name} stored')
2018-08-19 16:38:45 +02:00
except sqlalchemy.exc.SQLAlchemyError as e:
print(f'Could not store user {user.name}: {e}')
2017-02-20 13:19:03 +01:00
self.pause()
class AddProductMenu(Menu):
def __init__(self):
Menu.__init__(self, 'Add product', uses_db=True)
def _execute(self):
self.print_header()
bar_code = self.input_str('Bar code> ', Product.bar_code_re, (8, 13))
name = self.input_str('Name> ', Product.name_re, (1, Product.name_length))
price = self.input_int('Price> ', (1, 100000))
product = Product(bar_code, name, price)
self.session.add(product)
try:
self.session.commit()
print(f'Product {name} stored')
2018-08-19 16:38:45 +02:00
except sqlalchemy.exc.SQLAlchemyError as e:
print(f'Could not store product {name}: {e}')
2017-02-20 13:19:03 +01:00
self.pause()
class EditProductMenu(Menu):
def __init__(self):
Menu.__init__(self, 'Edit product', uses_db=True)
def _execute(self):
self.print_header()
product = self.input_product('Product> ')
self.printc(f'Editing product {product.name}')
2017-02-20 13:19:03 +01:00
while True:
selector = Selector(f'Do what with {product.name}?',
2017-02-20 13:19:03 +01:00
items=[('name', 'Edit name'),
('price', 'Edit price'),
('barcode', 'Edit barcode'),
('hidden', 'Edit hidden status'),
('store', 'Store')])
what = selector.execute()
if what == 'name':
product.name = self.input_str(f'Name[{product.name}]> ', Product.name_re, (1, product.name_length))
2017-02-20 13:19:03 +01:00
elif what == 'price':
product.price = self.input_int(f'Price[{product.price}]> ', (1, 100000))
2017-02-20 13:19:03 +01:00
elif what == 'barcode':
product.bar_code = self.input_str(f'Bar code[{product.bar_code}]> ', Product.bar_code_re, (8, 13))
2017-02-20 13:19:03 +01:00
elif what == 'hidden':
product.hidden = self.confirm('Hidden[%s]' % ("Y" if product.hidden else "N"), False)
elif what == 'store':
try:
self.session.commit()
print(f'Product {product.name} stored')
2018-08-19 16:38:45 +02:00
except sqlalchemy.exc.SQLAlchemyError as e:
print(f'Could not store product {product.name}: {e}')
2017-02-20 13:19:03 +01:00
self.pause()
return
elif what is None:
2018-08-19 16:38:45 +02:00
print('Edit aborted')
2017-02-20 13:19:03 +01:00
return
else:
2018-08-19 16:38:45 +02:00
print('What what?')
2017-02-20 13:19:03 +01:00
class AdjustStockMenu(Menu):
def __init__(self):
Menu.__init__(self, 'Adjust stock', uses_db=True)
def _execute(self):
self.print_header()
product = self.input_product('Product> ')
print(f'The stock of this product is: {product.stock:d} ')
2018-08-19 16:38:45 +02:00
print('Write the number of products you have added to the stock')
print('Alternatively, correct the stock for any mistakes')
2017-02-20 13:19:03 +01:00
add_stock = self.input_int('Added stock> ', (-1000, 1000))
# TODO: Print something else when adding negative stock?
print(f'You added {add_stock:d} to the stock of {product}')
2017-02-20 13:19:03 +01:00
product.stock += add_stock
print(f'The stock is now {product.stock:d}')
2017-02-20 13:19:03 +01:00
try:
self.session.commit()
2018-08-19 16:38:45 +02:00
print('Stock is now stored')
2017-02-20 13:19:03 +01:00
self.pause()
2018-08-19 16:38:45 +02:00
except sqlalchemy.exc.SQLAlchemyError as e:
print(f'Could not store stock: {e}')
2017-02-20 13:19:03 +01:00
self.pause()
return
print(f'The stock is now {product.stock:d}')
2017-02-20 13:19:03 +01:00
class CleanupStockMenu(Menu):
def __init__(self):
Menu.__init__(self, 'Stock Cleanup', uses_db=True)
def _execute(self):
self.print_header()
products = self.session.query(Product).filter(Product.stock != 0).all()
2018-08-19 16:38:45 +02:00
print("Every product in stock will be printed.")
print("Entering no value will keep current stock or set it to 0 if it is negative.")
print("Entering a value will set current stock to that value.")
print("Press enter to begin.")
2017-02-20 13:19:03 +01:00
self.pause()
changed_products = []
for product in products:
oldstock = product.stock
product.stock = self.input_int(product.name, (0, 10000), default=max(0, oldstock))
self.session.add(product)
if oldstock != product.stock:
changed_products.append((product, oldstock))
try:
self.session.commit()
2018-08-19 16:38:45 +02:00
print('New stocks are now stored.')
2017-02-20 13:19:03 +01:00
self.pause()
2018-08-19 16:38:45 +02:00
except sqlalchemy.exc.SQLAlchemyError as e:
print(f'Could not store stock: {e}')
2017-02-20 13:19:03 +01:00
self.pause()
return
for p in changed_products:
2018-08-19 16:38:45 +02:00
print(p[0].name, ".", p[1], "->", p[0].stock)