1
Fork 0
mirror of https://github.com/Steffo99/greed.git synced 2024-11-22 05:54:18 +00:00

🔧 Improve SQLalchemy code

This commit is contained in:
Steffo 2021-05-07 00:43:22 +02:00
parent 6cf8a3d43e
commit d3200b170a
Signed by: steffo
GPG key ID: 6965406171929D01
2 changed files with 11 additions and 11 deletions

View file

@ -20,7 +20,7 @@ TableDeclarativeBase = declarative_base()
# Define all the database tables using the sqlalchemy declarative base # Define all the database tables using the sqlalchemy declarative base
class User(DeferredReflection, TableDeclarativeBase): class User(TableDeclarativeBase):
"""A Telegram user who used the bot at least once.""" """A Telegram user who used the bot at least once."""
# Telegram data # Telegram data
@ -87,7 +87,7 @@ class User(DeferredReflection, TableDeclarativeBase):
return f"<User {self.mention()} having {self.credit} credit>" return f"<User {self.mention()} having {self.credit} credit>"
class Product(DeferredReflection, TableDeclarativeBase): class Product(TableDeclarativeBase):
"""A purchasable product.""" """A purchasable product."""
# Product id # Product id
@ -151,7 +151,7 @@ class Product(DeferredReflection, TableDeclarativeBase):
self.image = r.content self.image = r.content
class Transaction(DeferredReflection, TableDeclarativeBase): class Transaction(TableDeclarativeBase):
"""A greed wallet transaction. """A greed wallet transaction.
Wallet credit ISN'T calculated from these, but they can be used to recalculate it.""" Wallet credit ISN'T calculated from these, but they can be used to recalculate it."""
# TODO: split this into multiple tables # TODO: split this into multiple tables
@ -201,7 +201,7 @@ class Transaction(DeferredReflection, TableDeclarativeBase):
return f"<Transaction {self.transaction_id} for User {self.user_id}>" return f"<Transaction {self.transaction_id} for User {self.user_id}>"
class Admin(DeferredReflection, TableDeclarativeBase): class Admin(TableDeclarativeBase):
"""A greed administrator with his permissions.""" """A greed administrator with his permissions."""
# The telegram id # The telegram id
@ -223,7 +223,7 @@ class Admin(DeferredReflection, TableDeclarativeBase):
return f"<Admin {self.user_id}>" return f"<Admin {self.user_id}>"
class Order(DeferredReflection, TableDeclarativeBase): class Order(TableDeclarativeBase):
"""An order which has been placed by an user. """An order which has been placed by an user.
It may include multiple products, available in the OrderItem table.""" It may include multiple products, available in the OrderItem table."""
@ -286,7 +286,7 @@ class Order(DeferredReflection, TableDeclarativeBase):
(w.loc.get("refund_reason", reason=self.refund_reason) if self.refund_date is not None else "") (w.loc.get("refund_reason", reason=self.refund_reason) if self.refund_date is not None else "")
class OrderItem(DeferredReflection, TableDeclarativeBase): class OrderItem(TableDeclarativeBase):
"""A product that has been purchased as part of an order.""" """A product that has been purchased as part of an order."""
# The unique item id # The unique item id

View file

@ -176,7 +176,7 @@ class Worker(threading.Thread):
# If the will be owner flag is set # If the will be owner flag is set
if will_be_owner: if will_be_owner:
# Become owner # Become owner
self.admin = db.Admin(user_id=self.user.user_id, self.admin = db.Admin(user=self.user.user,
edit_products=True, edit_products=True,
receive_orders=True, receive_orders=True,
create_transactions=True, create_transactions=True,
@ -695,7 +695,7 @@ class Worker(threading.Thread):
# Create a new transaction and add it to the session # Create a new transaction and add it to the session
transaction = db.Transaction(user=self.user, transaction = db.Transaction(user=self.user,
value=value, value=value,
order_id=order.order_id) order=order)
self.session.add(transaction) self.session.add(transaction)
# Commit all the changes # Commit all the changes
self.session.commit() self.session.commit()
@ -1151,7 +1151,7 @@ class Worker(threading.Thread):
break break
# Find the order # Find the order
order_id = re.search(self.loc.get("order_number").replace("{id}", "([0-9]+)"), update.message.text).group(1) order_id = re.search(self.loc.get("order_number").replace("{id}", "([0-9]+)"), update.message.text).group(1)
order = self.session.query(db.Order).filter(db.Order.order_id == order_id).one() order = self.session.query(db.Order).get(order_id)
# Check if the order hasn't been already cleared # Check if the order hasn't been already cleared
if order.delivery_date is not None or order.refund_date is not None: if order.delivery_date is not None or order.refund_date is not None:
# Notify the admin and skip that order # Notify the admin and skip that order
@ -1337,7 +1337,7 @@ class Worker(threading.Thread):
"""Generate a .csv file containing the list of all transactions.""" """Generate a .csv file containing the list of all transactions."""
log.debug("Generating __transaction_file") log.debug("Generating __transaction_file")
# Retrieve all the transactions # Retrieve all the transactions
transactions = self.session.query(db.Transaction).order_by(db.Transaction.transaction_id.asc()).all() transactions = self.session.query(db.Transaction).order_by(db.Transaction.transaction_id).all()
# Create the file if it doesn't exists # Create the file if it doesn't exists
try: try:
with open(f"transactions_{self.chat.id}.csv", "x"): with open(f"transactions_{self.chat.id}.csv", "x"):
@ -1388,7 +1388,7 @@ class Worker(threading.Thread):
if isinstance(user, CancelSignal): if isinstance(user, CancelSignal):
return return
# Check if the user is already an administrator # Check if the user is already an administrator
admin = self.session.query(db.Admin).filter_by(user_id=user.user_id).one_or_none() admin = self.session.query(db.Admin).filter_by(user=user).one_or_none()
if admin is None: if admin is None:
# Create the keyboard to be sent # Create the keyboard to be sent
keyboard = telegram.ReplyKeyboardMarkup([[self.loc.get("emoji_yes"), self.loc.get("emoji_no")]], keyboard = telegram.ReplyKeyboardMarkup([[self.loc.get("emoji_yes"), self.loc.get("emoji_no")]],