Propagate sql_session through constructors
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
from math import ceil
|
||||
|
||||
import sqlalchemy
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from dibbler.models import (
|
||||
Product,
|
||||
@@ -13,8 +14,8 @@ from .helpermenus import Menu
|
||||
|
||||
|
||||
class AddStockMenu(Menu):
|
||||
def __init__(self):
|
||||
Menu.__init__(self, "Add stock and adjust credit", uses_db=True)
|
||||
def __init__(self, sql_session: Session):
|
||||
Menu.__init__(self, "Add stock and adjust credit", sql_session=sql_session, uses_db=True)
|
||||
self.help_text = """
|
||||
Enter what you have bought for PVVVV here, along with your user name and how
|
||||
much money you're due in credits for the purchase when prompted.\n"""
|
||||
@@ -151,10 +152,10 @@ much money you're due in credits for the purchase when prompted.\n"""
|
||||
PurchaseEntry(purchase, product, -self.products[product][0])
|
||||
|
||||
purchase.perform_soft_purchase(-self.price, round_up=False)
|
||||
self.session.add(purchase)
|
||||
self.sql_session.add(purchase)
|
||||
|
||||
try:
|
||||
self.session.commit()
|
||||
self.sql_session.commit()
|
||||
print("Success! Transaction performed:")
|
||||
# self.print_info()
|
||||
for user in self.users:
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import sqlalchemy
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from dibbler.conf import config
|
||||
from dibbler.models import (
|
||||
@@ -13,10 +14,8 @@ from .helpermenus import Menu
|
||||
|
||||
|
||||
class BuyMenu(Menu):
|
||||
def __init__(self, session=None):
|
||||
Menu.__init__(self, "Buy", uses_db=True)
|
||||
if session:
|
||||
self.session = session
|
||||
def __init__(self, sql_session: Session):
|
||||
Menu.__init__(self, "Buy", sql_session=sql_session, uses_db=True)
|
||||
self.superfast_mode = False
|
||||
self.help_text = """
|
||||
Each purchase may contain one or more products and one or more buyers.
|
||||
@@ -167,9 +166,9 @@ When finished, write an empty line to confirm the purchase.\n"""
|
||||
break
|
||||
|
||||
self.purchase.perform_purchase()
|
||||
self.session.add(self.purchase)
|
||||
self.sql_session.add(self.purchase)
|
||||
try:
|
||||
self.session.commit()
|
||||
self.sql_session.commit()
|
||||
except sqlalchemy.exc.SQLAlchemyError as e:
|
||||
print(f"Could not store purchase: {e}")
|
||||
else:
|
||||
|
||||
+24
-22
@@ -1,5 +1,7 @@
|
||||
import sqlalchemy
|
||||
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from dibbler.models import User, Product
|
||||
from .helpermenus import Menu, Selector
|
||||
|
||||
@@ -14,8 +16,8 @@ __all__ = [
|
||||
|
||||
|
||||
class AddUserMenu(Menu):
|
||||
def __init__(self):
|
||||
Menu.__init__(self, "Add user", uses_db=True)
|
||||
def __init__(self, sql_session: Session):
|
||||
Menu.__init__(self, "Add user", sql_session=sql_session, uses_db=True)
|
||||
|
||||
def _execute(self):
|
||||
self.print_header()
|
||||
@@ -28,9 +30,9 @@ class AddUserMenu(Menu):
|
||||
cardnum = cardnum.lower()
|
||||
rfid = self.input_str("RFID (optional)", regex=User.rfid_re, length_range=(0, 10))
|
||||
user = User(username, cardnum, rfid)
|
||||
self.session.add(user)
|
||||
self.sql_session.add(user)
|
||||
try:
|
||||
self.session.commit()
|
||||
self.sql_session.commit()
|
||||
print(f"User {username} stored")
|
||||
except sqlalchemy.exc.IntegrityError as e:
|
||||
print(f"Could not store user {username}: {e}")
|
||||
@@ -38,8 +40,8 @@ class AddUserMenu(Menu):
|
||||
|
||||
|
||||
class EditUserMenu(Menu):
|
||||
def __init__(self):
|
||||
Menu.__init__(self, "Edit user", uses_db=True)
|
||||
def __init__(self, sql_session: Session):
|
||||
Menu.__init__(self, "Edit user", sql_session=sql_session, uses_db=True)
|
||||
self.help_text = """
|
||||
The only editable part of a user is its card number and rfid.
|
||||
|
||||
@@ -69,7 +71,7 @@ user, then rfid (write an empty line to remove the card number or rfid).
|
||||
empty_string_is_none=True,
|
||||
)
|
||||
try:
|
||||
self.session.commit()
|
||||
self.sql_session.commit()
|
||||
print(f"User {user.name} stored")
|
||||
except sqlalchemy.exc.SQLAlchemyError as e:
|
||||
print(f"Could not store user {user.name}: {e}")
|
||||
@@ -77,8 +79,8 @@ user, then rfid (write an empty line to remove the card number or rfid).
|
||||
|
||||
|
||||
class AddProductMenu(Menu):
|
||||
def __init__(self):
|
||||
Menu.__init__(self, "Add product", uses_db=True)
|
||||
def __init__(self, sql_session: Session):
|
||||
Menu.__init__(self, "Add product", sql_session=sql_session, uses_db=True)
|
||||
|
||||
def _execute(self):
|
||||
self.print_header()
|
||||
@@ -86,9 +88,9 @@ class AddProductMenu(Menu):
|
||||
name = self.input_str("Name", regex=Product.name_re, length_range=(1, Product.name_length))
|
||||
price = self.input_int("Price", 1, 100000)
|
||||
product = Product(bar_code, name, price)
|
||||
self.session.add(product)
|
||||
self.sql_session.add(product)
|
||||
try:
|
||||
self.session.commit()
|
||||
self.sql_session.commit()
|
||||
print(f"Product {name} stored")
|
||||
except sqlalchemy.exc.SQLAlchemyError as e:
|
||||
print(f"Could not store product {name}: {e}")
|
||||
@@ -96,8 +98,8 @@ class AddProductMenu(Menu):
|
||||
|
||||
|
||||
class EditProductMenu(Menu):
|
||||
def __init__(self):
|
||||
Menu.__init__(self, "Edit product", uses_db=True)
|
||||
def __init__(self, sql_session: Session):
|
||||
Menu.__init__(self, "Edit product", sql_session=sql_session, uses_db=True)
|
||||
|
||||
def _execute(self):
|
||||
self.print_header()
|
||||
@@ -135,7 +137,7 @@ class EditProductMenu(Menu):
|
||||
product.hidden = self.confirm(f"Hidden(currently {product.hidden})", default=False)
|
||||
elif what == "store":
|
||||
try:
|
||||
self.session.commit()
|
||||
self.sql_session.commit()
|
||||
print(f"Product {product.name} stored")
|
||||
except sqlalchemy.exc.SQLAlchemyError as e:
|
||||
print(f"Could not store product {product.name}: {e}")
|
||||
@@ -149,8 +151,8 @@ class EditProductMenu(Menu):
|
||||
|
||||
|
||||
class AdjustStockMenu(Menu):
|
||||
def __init__(self):
|
||||
Menu.__init__(self, "Adjust stock", uses_db=True)
|
||||
def __init__(self, sql_session: Session):
|
||||
Menu.__init__(self, "Adjust stock", sql_session=sql_session, uses_db=True)
|
||||
|
||||
def _execute(self):
|
||||
self.print_header()
|
||||
@@ -168,7 +170,7 @@ class AdjustStockMenu(Menu):
|
||||
product.stock += add_stock
|
||||
|
||||
try:
|
||||
self.session.commit()
|
||||
self.sql_session.commit()
|
||||
print("Stock is now stored")
|
||||
self.pause()
|
||||
except sqlalchemy.exc.SQLAlchemyError as e:
|
||||
@@ -179,13 +181,13 @@ class AdjustStockMenu(Menu):
|
||||
|
||||
|
||||
class CleanupStockMenu(Menu):
|
||||
def __init__(self):
|
||||
Menu.__init__(self, "Stock Cleanup", uses_db=True)
|
||||
def __init__(self, sql_session: Session):
|
||||
Menu.__init__(self, "Stock Cleanup", sql_session=sql_session, uses_db=True)
|
||||
|
||||
def _execute(self):
|
||||
self.print_header()
|
||||
|
||||
products = self.session.query(Product).filter(Product.stock != 0).all()
|
||||
products = self.sql_session.query(Product).filter(Product.stock != 0).all()
|
||||
|
||||
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.")
|
||||
@@ -199,12 +201,12 @@ class CleanupStockMenu(Menu):
|
||||
for product in products:
|
||||
oldstock = product.stock
|
||||
product.stock = self.input_int(product.name, 0, 10000, default=max(0, oldstock))
|
||||
self.session.add(product)
|
||||
self.sql_session.add(product)
|
||||
if oldstock != product.stock:
|
||||
changed_products.append((product, oldstock))
|
||||
|
||||
try:
|
||||
self.session.commit()
|
||||
self.sql_session.commit()
|
||||
print("New stocks are now stored.")
|
||||
self.pause()
|
||||
except sqlalchemy.exc.SQLAlchemyError as e:
|
||||
|
||||
@@ -5,7 +5,8 @@ import re
|
||||
import sys
|
||||
from select import select
|
||||
|
||||
from dibbler.db import session as create_session
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from dibbler.models import User
|
||||
from dibbler.lib.helpers import (
|
||||
search_user,
|
||||
@@ -37,6 +38,7 @@ class Menu(object):
|
||||
exit_disallowed_msg=None,
|
||||
help_text=None,
|
||||
uses_db=False,
|
||||
sql_session: Session | None=None,
|
||||
):
|
||||
self.name = name
|
||||
self.items = items if items is not None else []
|
||||
@@ -49,7 +51,9 @@ class Menu(object):
|
||||
self.help_text = help_text
|
||||
self.context = None
|
||||
self.uses_db = uses_db
|
||||
self.session = None
|
||||
self.sql_session: Session | None = sql_session
|
||||
|
||||
assert not (self.uses_db and self.sql_session is None)
|
||||
|
||||
def exit_menu(self):
|
||||
if self.exit_disallowed_msg is not None:
|
||||
@@ -338,7 +342,7 @@ class Menu(object):
|
||||
results = {}
|
||||
result_values = {}
|
||||
for thing in permitted_things:
|
||||
results[thing] = search_fun[thing](search_str, self.session, find_hidden_products)
|
||||
results[thing] = search_fun[thing](search_str, self.sql_session, find_hidden_products)
|
||||
result_values[thing] = self.search_result_value(results[thing])
|
||||
selected_thing = argmax(result_values)
|
||||
if not results[selected_thing]:
|
||||
@@ -373,7 +377,7 @@ class Menu(object):
|
||||
print(f'"{string}" looks like a username, but no such user exists.')
|
||||
if self.confirm(f"Create user {string}?"):
|
||||
user = User(string, None)
|
||||
self.session.add(user)
|
||||
self.sql_session.add(user)
|
||||
return user
|
||||
return None
|
||||
if type_guess == "card":
|
||||
@@ -392,7 +396,7 @@ class Menu(object):
|
||||
(1, 10),
|
||||
)
|
||||
user = User(username, string)
|
||||
self.session.add(user)
|
||||
self.sql_session.add(user)
|
||||
return user
|
||||
if selection == "set":
|
||||
user = self.input_user("User to set card number for")
|
||||
@@ -406,7 +410,7 @@ class Menu(object):
|
||||
return None
|
||||
|
||||
def search_ui(self, search_fun, search_str, thing):
|
||||
result = search_fun(search_str, self.session)
|
||||
result = search_fun(search_str, self.sql_session)
|
||||
return self.search_ui2(search_str, result, thing)
|
||||
|
||||
def search_ui2(self, search_str, result, thing):
|
||||
@@ -484,16 +488,13 @@ class Menu(object):
|
||||
def execute(self, **kwargs):
|
||||
self.set_context(None)
|
||||
try:
|
||||
if self.uses_db and not self.session:
|
||||
self.session = create_session()
|
||||
return self._execute(**kwargs)
|
||||
except ExitMenu:
|
||||
self.at_exit()
|
||||
return None
|
||||
finally:
|
||||
if self.session is not None:
|
||||
self.session.close()
|
||||
self.session = None
|
||||
if self.sql_session is not None:
|
||||
self.sql_session = None
|
||||
|
||||
def _execute(self, **kwargs):
|
||||
while True:
|
||||
|
||||
@@ -3,7 +3,6 @@ import os
|
||||
import random
|
||||
import sys
|
||||
|
||||
from dibbler.db import session as create_session
|
||||
|
||||
from .buymenu import BuyMenu
|
||||
from .faq import FAQMenu
|
||||
@@ -28,7 +27,7 @@ class MainMenu(Menu):
|
||||
else:
|
||||
num = 1
|
||||
item_name = in_str
|
||||
buy_menu = BuyMenu(create_session())
|
||||
buy_menu = BuyMenu(self.sql_session)
|
||||
thing = buy_menu.search_for_thing(item_name, find_hidden_products=False)
|
||||
if thing:
|
||||
buy_menu.execute(initial_contents=[(thing, num)])
|
||||
|
||||
+21
-20
@@ -1,4 +1,5 @@
|
||||
import sqlalchemy
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from dibbler.conf import config
|
||||
from dibbler.models import Transaction, Product, User
|
||||
@@ -8,8 +9,8 @@ from .helpermenus import Menu, Selector
|
||||
|
||||
|
||||
class TransferMenu(Menu):
|
||||
def __init__(self):
|
||||
Menu.__init__(self, "Transfer credit between users", uses_db=True)
|
||||
def __init__(self, sql_session: Session):
|
||||
Menu.__init__(self, "Transfer credit between users", sql_session=sql_session, uses_db=True)
|
||||
|
||||
def _execute(self):
|
||||
self.print_header()
|
||||
@@ -26,10 +27,10 @@ class TransferMenu(Menu):
|
||||
t2 = Transaction(user2, -amount, f'transfer from {user1.name} "{comment}"')
|
||||
t1.perform_transaction()
|
||||
t2.perform_transaction()
|
||||
self.session.add(t1)
|
||||
self.session.add(t2)
|
||||
self.sql_session.add(t1)
|
||||
self.sql_session.add(t2)
|
||||
try:
|
||||
self.session.commit()
|
||||
self.sql_session.commit()
|
||||
print(f"Transferred {amount:d} kr from {user1} to {user2}")
|
||||
print(f"User {user1}'s credit is now {user1.credit:d} kr")
|
||||
print(f"User {user2}'s credit is now {user2.credit:d} kr")
|
||||
@@ -40,8 +41,8 @@ class TransferMenu(Menu):
|
||||
|
||||
|
||||
class ShowUserMenu(Menu):
|
||||
def __init__(self):
|
||||
Menu.__init__(self, "Show user", uses_db=True)
|
||||
def __init__(self, sql_session: Session):
|
||||
Menu.__init__(self, "Show user", sql_session=sql_session, uses_db=True)
|
||||
|
||||
def _execute(self):
|
||||
self.print_header()
|
||||
@@ -123,13 +124,13 @@ class ShowUserMenu(Menu):
|
||||
|
||||
|
||||
class UserListMenu(Menu):
|
||||
def __init__(self):
|
||||
Menu.__init__(self, "User list", uses_db=True)
|
||||
def __init__(self, sql_session: Session):
|
||||
Menu.__init__(self, "User list", sql_session=sql_session, uses_db=True)
|
||||
|
||||
def _execute(self):
|
||||
self.print_header()
|
||||
user_list = self.session.query(User).all()
|
||||
total_credit = self.session.query(sqlalchemy.func.sum(User.credit)).first()[0]
|
||||
user_list = self.sql_session.query(User).all()
|
||||
total_credit = self.sql_session.query(sqlalchemy.func.sum(User.credit)).first()[0]
|
||||
|
||||
line_format = "%-12s | %6s\n"
|
||||
hline = "---------------------\n"
|
||||
@@ -144,8 +145,8 @@ class UserListMenu(Menu):
|
||||
|
||||
|
||||
class AdjustCreditMenu(Menu):
|
||||
def __init__(self):
|
||||
Menu.__init__(self, "Adjust credit", uses_db=True)
|
||||
def __init__(self, sql_session: Session):
|
||||
Menu.__init__(self, "Adjust credit", sql_session=sql_session, uses_db=True)
|
||||
|
||||
def _execute(self):
|
||||
self.print_header()
|
||||
@@ -164,9 +165,9 @@ class AdjustCreditMenu(Menu):
|
||||
description = "manually adjusted credit"
|
||||
transaction = Transaction(user, -amount, description)
|
||||
transaction.perform_transaction()
|
||||
self.session.add(transaction)
|
||||
self.sql_session.add(transaction)
|
||||
try:
|
||||
self.session.commit()
|
||||
self.sql_session.commit()
|
||||
print(f"User {user.name}'s credit is now {user.credit:d} kr")
|
||||
except sqlalchemy.exc.SQLAlchemyError as e:
|
||||
print(f"Could not store transaction: {e}")
|
||||
@@ -174,14 +175,14 @@ class AdjustCreditMenu(Menu):
|
||||
|
||||
|
||||
class ProductListMenu(Menu):
|
||||
def __init__(self):
|
||||
Menu.__init__(self, "Product list", uses_db=True)
|
||||
def __init__(self, sql_session: Session):
|
||||
Menu.__init__(self, "Product list", sql_session=sql_session, uses_db=True)
|
||||
|
||||
def _execute(self):
|
||||
self.print_header()
|
||||
text = ""
|
||||
product_list = (
|
||||
self.session.query(Product)
|
||||
self.sql_session.query(Product)
|
||||
.filter(Product.hidden.is_(False))
|
||||
.order_by(Product.stock.desc())
|
||||
)
|
||||
@@ -204,8 +205,8 @@ class ProductListMenu(Menu):
|
||||
|
||||
|
||||
class ProductSearchMenu(Menu):
|
||||
def __init__(self):
|
||||
Menu.__init__(self, "Product search", uses_db=True)
|
||||
def __init__(self, sql_session: Session):
|
||||
Menu.__init__(self, "Product search", sql_session=sql_session, uses_db=True)
|
||||
|
||||
def _execute(self):
|
||||
self.print_header()
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
import re
|
||||
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from dibbler.conf import config
|
||||
from dibbler.models import Product, User
|
||||
from dibbler.lib.printer_helpers import print_bar_code, print_name_label
|
||||
@@ -8,8 +10,8 @@ from .helpermenus import Menu
|
||||
|
||||
|
||||
class PrintLabelMenu(Menu):
|
||||
def __init__(self):
|
||||
Menu.__init__(self, "Print a label", uses_db=True)
|
||||
def __init__(self, sql_session: Session):
|
||||
Menu.__init__(self, "Print a label", sql_session=sql_session, uses_db=True)
|
||||
self.help_text = """
|
||||
Prints out a product bar code on the printer
|
||||
|
||||
|
||||
+17
-16
@@ -1,4 +1,5 @@
|
||||
from sqlalchemy import desc, func
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from dibbler.lib.helpers import less
|
||||
from dibbler.models import PurchaseEntry, Product, User
|
||||
@@ -15,14 +16,14 @@ __all__ = [
|
||||
|
||||
|
||||
class ProductPopularityMenu(Menu):
|
||||
def __init__(self):
|
||||
Menu.__init__(self, "Products by popularity", uses_db=True)
|
||||
def __init__(self, sql_session: Session):
|
||||
Menu.__init__(self, "Products by popularity", sql_session=sql_session, uses_db=True)
|
||||
|
||||
def _execute(self):
|
||||
self.print_header()
|
||||
text = ""
|
||||
sub = (
|
||||
self.session.query(
|
||||
self.sql_session.query(
|
||||
PurchaseEntry.product_id,
|
||||
func.sum(PurchaseEntry.amount).label("purchase_count"),
|
||||
)
|
||||
@@ -31,7 +32,7 @@ class ProductPopularityMenu(Menu):
|
||||
.subquery()
|
||||
)
|
||||
product_list = (
|
||||
self.session.query(Product, sub.c.purchase_count)
|
||||
self.sql_session.query(Product, sub.c.purchase_count)
|
||||
.outerjoin((sub, Product.product_id == sub.c.product_id))
|
||||
.order_by(desc(sub.c.purchase_count))
|
||||
.filter(sub.c.purchase_count is not None)
|
||||
@@ -48,14 +49,14 @@ class ProductPopularityMenu(Menu):
|
||||
|
||||
|
||||
class ProductRevenueMenu(Menu):
|
||||
def __init__(self):
|
||||
Menu.__init__(self, "Products by revenue", uses_db=True)
|
||||
def __init__(self, sql_session: Session):
|
||||
Menu.__init__(self, "Products by revenue", sql_session=sql_session, uses_db=True)
|
||||
|
||||
def _execute(self):
|
||||
self.print_header()
|
||||
text = ""
|
||||
sub = (
|
||||
self.session.query(
|
||||
self.sql_session.query(
|
||||
PurchaseEntry.product_id,
|
||||
func.sum(PurchaseEntry.amount).label("purchase_count"),
|
||||
)
|
||||
@@ -64,7 +65,7 @@ class ProductRevenueMenu(Menu):
|
||||
.subquery()
|
||||
)
|
||||
product_list = (
|
||||
self.session.query(Product, sub.c.purchase_count)
|
||||
self.sql_session.query(Product, sub.c.purchase_count)
|
||||
.outerjoin((sub, Product.product_id == sub.c.product_id))
|
||||
.order_by(desc(sub.c.purchase_count * Product.price))
|
||||
.filter(sub.c.purchase_count is not None)
|
||||
@@ -86,22 +87,22 @@ class ProductRevenueMenu(Menu):
|
||||
|
||||
|
||||
class BalanceMenu(Menu):
|
||||
def __init__(self):
|
||||
Menu.__init__(self, "Total balance of PVVVV", uses_db=True)
|
||||
def __init__(self, sql_session: Session):
|
||||
Menu.__init__(self, "Total balance of PVVVV", sql_session=sql_session, uses_db=True)
|
||||
|
||||
def _execute(self):
|
||||
self.print_header()
|
||||
text = ""
|
||||
total_value = 0
|
||||
product_list = self.session.query(Product).filter(Product.stock > 0).all()
|
||||
product_list = self.sql_session.query(Product).filter(Product.stock > 0).all()
|
||||
for p in product_list:
|
||||
total_value += p.stock * p.price
|
||||
|
||||
total_positive_credit = (
|
||||
self.session.query(func.sum(User.credit)).filter(User.credit > 0).first()[0]
|
||||
self.sql_session.query(func.sum(User.credit)).filter(User.credit > 0).first()[0]
|
||||
)
|
||||
total_negative_credit = (
|
||||
self.session.query(func.sum(User.credit)).filter(User.credit < 0).first()[0]
|
||||
self.sql_session.query(func.sum(User.credit)).filter(User.credit < 0).first()[0]
|
||||
)
|
||||
|
||||
total_credit = total_positive_credit + total_negative_credit
|
||||
@@ -119,8 +120,8 @@ class BalanceMenu(Menu):
|
||||
|
||||
|
||||
class LoggedStatisticsMenu(Menu):
|
||||
def __init__(self):
|
||||
Menu.__init__(self, "Statistics from log", uses_db=True)
|
||||
def __init__(self, sql_session: Session):
|
||||
Menu.__init__(self, "Statistics from log", sql_session=sql_session, uses_db=True)
|
||||
|
||||
def _execute(self):
|
||||
statisticsTextOnly()
|
||||
statisticsTextOnly(self.sql_session)
|
||||
|
||||
Reference in New Issue
Block a user