mirror of
https://github.com/Steffo99/greed.git
synced 2024-11-21 13:34:18 +00:00
🔧 Improve SQLalchemy code
This commit is contained in:
parent
6cf8a3d43e
commit
d3200b170a
2 changed files with 11 additions and 11 deletions
12
database.py
12
database.py
|
@ -20,7 +20,7 @@ TableDeclarativeBase = 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."""
|
||||
|
||||
# Telegram data
|
||||
|
@ -87,7 +87,7 @@ class User(DeferredReflection, TableDeclarativeBase):
|
|||
return f"<User {self.mention()} having {self.credit} credit>"
|
||||
|
||||
|
||||
class Product(DeferredReflection, TableDeclarativeBase):
|
||||
class Product(TableDeclarativeBase):
|
||||
"""A purchasable product."""
|
||||
|
||||
# Product id
|
||||
|
@ -151,7 +151,7 @@ class Product(DeferredReflection, TableDeclarativeBase):
|
|||
self.image = r.content
|
||||
|
||||
|
||||
class Transaction(DeferredReflection, TableDeclarativeBase):
|
||||
class Transaction(TableDeclarativeBase):
|
||||
"""A greed wallet transaction.
|
||||
Wallet credit ISN'T calculated from these, but they can be used to recalculate it."""
|
||||
# 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}>"
|
||||
|
||||
|
||||
class Admin(DeferredReflection, TableDeclarativeBase):
|
||||
class Admin(TableDeclarativeBase):
|
||||
"""A greed administrator with his permissions."""
|
||||
|
||||
# The telegram id
|
||||
|
@ -223,7 +223,7 @@ class Admin(DeferredReflection, TableDeclarativeBase):
|
|||
return f"<Admin {self.user_id}>"
|
||||
|
||||
|
||||
class Order(DeferredReflection, TableDeclarativeBase):
|
||||
class Order(TableDeclarativeBase):
|
||||
"""An order which has been placed by an user.
|
||||
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 "")
|
||||
|
||||
|
||||
class OrderItem(DeferredReflection, TableDeclarativeBase):
|
||||
class OrderItem(TableDeclarativeBase):
|
||||
"""A product that has been purchased as part of an order."""
|
||||
|
||||
# The unique item id
|
||||
|
|
10
worker.py
10
worker.py
|
@ -176,7 +176,7 @@ class Worker(threading.Thread):
|
|||
# If the will be owner flag is set
|
||||
if will_be_owner:
|
||||
# Become owner
|
||||
self.admin = db.Admin(user_id=self.user.user_id,
|
||||
self.admin = db.Admin(user=self.user.user,
|
||||
edit_products=True,
|
||||
receive_orders=True,
|
||||
create_transactions=True,
|
||||
|
@ -695,7 +695,7 @@ class Worker(threading.Thread):
|
|||
# Create a new transaction and add it to the session
|
||||
transaction = db.Transaction(user=self.user,
|
||||
value=value,
|
||||
order_id=order.order_id)
|
||||
order=order)
|
||||
self.session.add(transaction)
|
||||
# Commit all the changes
|
||||
self.session.commit()
|
||||
|
@ -1151,7 +1151,7 @@ class Worker(threading.Thread):
|
|||
break
|
||||
# Find the order
|
||||
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
|
||||
if order.delivery_date is not None or order.refund_date is not None:
|
||||
# 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."""
|
||||
log.debug("Generating __transaction_file")
|
||||
# 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
|
||||
try:
|
||||
with open(f"transactions_{self.chat.id}.csv", "x"):
|
||||
|
@ -1388,7 +1388,7 @@ class Worker(threading.Thread):
|
|||
if isinstance(user, CancelSignal):
|
||||
return
|
||||
# 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:
|
||||
# Create the keyboard to be sent
|
||||
keyboard = telegram.ReplyKeyboardMarkup([[self.loc.get("emoji_yes"), self.loc.get("emoji_no")]],
|
||||
|
|
Loading…
Reference in a new issue