mirror of
https://github.com/Steffo99/greed.git
synced 2024-11-25 15:24:17 +00:00
Start work on the database code
The code isn't finished yet and isn't ready for use!
This commit is contained in:
parent
e540fa09b2
commit
c1e1684efd
3 changed files with 104 additions and 22 deletions
|
@ -3,7 +3,7 @@
|
||||||
# Config file parameters
|
# Config file parameters
|
||||||
[Config]
|
[Config]
|
||||||
; Config file version. DO NOT EDIT THIS!
|
; Config file version. DO NOT EDIT THIS!
|
||||||
version = 3
|
version = 4
|
||||||
; Set this to no when you are done editing the file
|
; Set this to no when you are done editing the file
|
||||||
is_template = yes
|
is_template = yes
|
||||||
|
|
||||||
|
@ -20,3 +20,4 @@ long_polling_timeout = 30
|
||||||
# Database parameters
|
# Database parameters
|
||||||
[Database]
|
[Database]
|
||||||
; The database engine you want to use. Refer to http://docs.sqlalchemy.org/en/latest/core/engines.html for the possible settings.
|
; The database engine you want to use. Refer to http://docs.sqlalchemy.org/en/latest/core/engines.html for the possible settings.
|
||||||
|
engine = sqlite://
|
114
database.py
114
database.py
|
@ -1,27 +1,99 @@
|
||||||
from sqlalchemy import Table, Column, BigInteger, Numeric, String, Text, MetaData, ForeignKey
|
from sqlalchemy import create_engine, Column, ForeignKey
|
||||||
|
from sqlalchemy import Integer, BigInteger, String, Numeric, Text
|
||||||
|
from sqlalchemy.ext.declarative import declarative_base
|
||||||
|
import configloader
|
||||||
|
import telegram
|
||||||
import decimal
|
import decimal
|
||||||
|
import strings
|
||||||
|
from html import escape
|
||||||
|
|
||||||
# Create a metadata object containing info about the tables in the database
|
# Create a (lazy) database engine
|
||||||
metadata = MetaData()
|
engine = create_engine(configloader.config["Database"]["engine"])
|
||||||
|
|
||||||
# Metadata for the users table
|
# Create a base class to define all the database subclasses
|
||||||
# TODO: maybe add a 'credit' column
|
TableDeclarativeBase = declarative_base(bind=engine)
|
||||||
users = Table("users", metadata,
|
|
||||||
Column("telegram_user_id", BigInteger, primary_key=True),
|
|
||||||
Column("first_name", String, nullable=False),
|
|
||||||
Column("last_name", String),
|
|
||||||
Column("username", String))
|
|
||||||
|
|
||||||
# Metadata for the admins table
|
# Define all the database tables using the sqlalchemy declarative base
|
||||||
# TODO: add columns for all the possible permissions
|
class User(TableDeclarativeBase):
|
||||||
admins = Table("admins", metadata,
|
"""A Telegram user who used the bot at least once."""
|
||||||
Column("telegram_user_id", ForeignKey("users.telegram_user_id"), primary_key=True))
|
|
||||||
|
|
||||||
# Metadata for the products table
|
# Telegram data
|
||||||
products = Table("products", metadata,
|
user_id = Column(BigInteger, primary_key=True)
|
||||||
Column("product_name", String, primary_key=True),
|
first_name = Column(String, nullable=False)
|
||||||
Column("description", Text),
|
last_name = Column(String)
|
||||||
Column("price", Numeric(...)))
|
username = Column(String)
|
||||||
|
|
||||||
#TODO: many things are still missing...
|
# Current wallet credit
|
||||||
raise NotImplementedError()
|
credit = Column(Numeric, nullable=False)
|
||||||
|
|
||||||
|
# Extra table parameters
|
||||||
|
__tablename__ = "users"
|
||||||
|
|
||||||
|
def __init__(self, telegram_user: telegram.User):
|
||||||
|
# Get the data from telegram
|
||||||
|
self.id = telegram_user.id
|
||||||
|
self.first_name = telegram_user.first_name
|
||||||
|
self.last_name = telegram_user.last_name
|
||||||
|
self.username = telegram_user.username
|
||||||
|
# The starting wallet value is 0
|
||||||
|
self.credit = decimal.Decimal("0")
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
"""Describe the user in the best way possible given the available data."""
|
||||||
|
if self.username is not None:
|
||||||
|
return f"@{self.username}"
|
||||||
|
elif self.last_name is not None:
|
||||||
|
return f"{self.first_name} {self.last_name}"
|
||||||
|
else:
|
||||||
|
return self.first_name
|
||||||
|
|
||||||
|
def __repr__(self):
|
||||||
|
return f"<User {self} having {self.credit} credit>"
|
||||||
|
|
||||||
|
|
||||||
|
class Product(TableDeclarativeBase):
|
||||||
|
"""A purchasable product."""
|
||||||
|
|
||||||
|
# Product name
|
||||||
|
name = Column(String, primary_key=True)
|
||||||
|
# Product description
|
||||||
|
description = Column(Text)
|
||||||
|
# Product price, if null product is not for sale
|
||||||
|
price = Column(Numeric)
|
||||||
|
# Image filename
|
||||||
|
image = Column(String)
|
||||||
|
# Stock quantity, if null product has infinite stock
|
||||||
|
stock = Column(Integer)
|
||||||
|
|
||||||
|
# Extra table parameters
|
||||||
|
__tablename__ = "product"
|
||||||
|
|
||||||
|
# No __init__ is needed, the default one is sufficient
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
"""Return the product details formatted with Telegram HTML. The image is omitted."""
|
||||||
|
return f"<b>{escape(self.name)}</b>\n" \
|
||||||
|
f"{escape(self.description)}\n" \
|
||||||
|
f"<i>{format(strings.in_stock_format_string, quantity=self.stock) if self.stock is not None else ''}</i>\n" \
|
||||||
|
f"{format(strings.currency_format_string, symbol=strings.currency_symbol, value=self.price)}"
|
||||||
|
|
||||||
|
def __repr__(self):
|
||||||
|
return f"<Product {self.name}>"
|
||||||
|
|
||||||
|
# TODO: add get image (and set image?) method(s)
|
||||||
|
|
||||||
|
|
||||||
|
class Transactions(TableDeclarativeBase):
|
||||||
|
"""A greed wallet transaction.
|
||||||
|
Wallet credit ISN'T calculated from these, but they can be used to recalculate it."""
|
||||||
|
|
||||||
|
# The internal transaction ID
|
||||||
|
transaction_id = Column(BigInteger, primary_key=True)
|
||||||
|
# The user whose credit is affected by this transaction
|
||||||
|
user_id = Column(BigInteger, ForeignKey("users.id"), nullable=False)
|
||||||
|
# The value of this transaction. Can be both negative and positive.
|
||||||
|
value = Column(Numeric, nullable=False)
|
||||||
|
# Extra notes on the transaction
|
||||||
|
notes = Column(Text)
|
||||||
|
|
||||||
|
# TODO: there still are some missing fields
|
|
@ -2,6 +2,15 @@
|
||||||
# Can be edited, but DON'T REMOVE THE REPLACEMENT FIELDS (words surrounded by {curly braces})
|
# Can be edited, but DON'T REMOVE THE REPLACEMENT FIELDS (words surrounded by {curly braces})
|
||||||
# TODO: maybe add a preformat to all strings in this file
|
# TODO: maybe add a preformat to all strings in this file
|
||||||
|
|
||||||
|
# Currency symbol
|
||||||
|
currency_symbol = "€"
|
||||||
|
|
||||||
|
# Positioning of the currency symbol
|
||||||
|
currency_format_string = "{symbol} {value}"
|
||||||
|
|
||||||
|
# Quantity of a product in stock
|
||||||
|
in_stock_format_string = "{quantity} disponibili"
|
||||||
|
|
||||||
# Answer: the start command was sent and the bot should welcome the user
|
# Answer: the start command was sent and the bot should welcome the user
|
||||||
conversation_after_start = "Ciao!\n" \
|
conversation_after_start = "Ciao!\n" \
|
||||||
"Benvenuto su greed!"
|
"Benvenuto su greed!"
|
||||||
|
|
Loading…
Reference in a new issue