2022-04-23 22:55:51 +02:00
|
|
|
#!/usr/bin/python3
|
|
|
|
|
2022-08-07 16:35:28 +02:00
|
|
|
from sqlalchemy import Column, Integer, Float, String, Text, Date, ForeignKey, BOOLEAN
|
2022-04-23 22:55:51 +02:00
|
|
|
from sqlalchemy.ext.declarative import declarative_base
|
2022-08-07 16:35:28 +02:00
|
|
|
from sqlalchemy import create_engine
|
2022-06-14 14:47:12 +02:00
|
|
|
from sqlalchemy.orm import sessionmaker
|
2022-04-23 22:55:51 +02:00
|
|
|
from config import CONFIG
|
|
|
|
|
|
|
|
Base = declarative_base()
|
|
|
|
|
|
|
|
|
2022-05-02 14:17:05 +02:00
|
|
|
class User(Base):
|
2022-04-25 00:32:17 +02:00
|
|
|
__tablename__ = 'user'
|
|
|
|
id = Column(Integer, primary_key=True, autoincrement=True)
|
2022-05-13 18:22:36 +02:00
|
|
|
username = Column(String(64))
|
|
|
|
password = Column(String(64))
|
2022-06-15 11:21:31 +02:00
|
|
|
email = Column(Text)
|
|
|
|
settings = Column(Text) #neki json...
|
2022-04-23 22:55:51 +02:00
|
|
|
|
2022-05-02 14:17:05 +02:00
|
|
|
class Card(Base):
|
|
|
|
__tablename__ = 'card'
|
2022-04-25 00:32:17 +02:00
|
|
|
id = Column(Integer, primary_key=True, autoincrement=True)
|
2022-05-02 14:17:05 +02:00
|
|
|
title = Column(String(1024))
|
|
|
|
owner_id = Column(Integer, ForeignKey("user.id"), nullable=False)
|
2022-06-02 22:36:08 +02:00
|
|
|
interest_rate = Column(Float)
|
2022-06-03 19:12:14 +02:00
|
|
|
item_location = Column(String(1024))
|
2022-06-05 21:19:20 +02:00
|
|
|
last_review = Column(Text)
|
2022-06-13 14:27:14 +02:00
|
|
|
share_id = Column(Text) #explain: 0 means not shared, otherwise increment from db and generate new cards for all relevant(tbd) users
|
2023-04-08 23:40:52 +02:00
|
|
|
card_type = Column(String(1024))
|
2022-06-04 22:18:21 +02:00
|
|
|
|
|
|
|
class Deck(Base):
|
|
|
|
__tablename__ = 'deck'
|
|
|
|
id = Column(Integer, primary_key=True, autoincrement=True)
|
2022-06-05 18:54:48 +02:00
|
|
|
owner_id = Column(Integer, ForeignKey("user.id"), nullable=False)
|
2022-06-04 22:18:21 +02:00
|
|
|
cards_by_id = Column(Text)
|
|
|
|
number_of_cards = Column(Integer)
|
|
|
|
current_card = Column(Integer)
|
|
|
|
completed = Column(BOOLEAN)
|
|
|
|
|
2022-08-07 16:35:28 +02:00
|
|
|
class Rating(Base):
|
|
|
|
__tablename__ = 'rating'
|
2022-06-05 21:19:20 +02:00
|
|
|
id = Column(Integer, primary_key=True, autoincrement=True)
|
2022-08-07 16:35:28 +02:00
|
|
|
user_id = Column(Integer, ForeignKey("user.id"), nullable=False)
|
|
|
|
card_id = Column(Integer, ForeignKey("card.id"), nullable=False)
|
|
|
|
rating_value = Column(Text) #Yes, Maybe, No
|
|
|
|
rating_time = Column(Date)
|
2022-04-25 00:32:17 +02:00
|
|
|
|
2022-04-23 22:55:51 +02:00
|
|
|
engine = create_engine(CONFIG['DB_CONNECTION'])
|
|
|
|
|
2023-08-27 18:52:59 +02:00
|
|
|
|
|
|
|
def create_all():
|
|
|
|
Base.metadata.create_all(Engine)
|
|
|
|
|
|
|
|
Engine = create_engine(CONFIG['DB_CONNECTION'])
|
|
|
|
Session = sessionmaker(Engine)
|
2022-06-14 14:47:12 +02:00
|
|
|
|
|
|
|
def get_session():
|
2023-08-27 18:52:59 +02:00
|
|
|
return Session
|
|
|
|
|
|
|
|
def get_engine():
|
|
|
|
return Engine
|
|
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
NAVODILA TODO
|
|
|
|
|
|
|
|
Upravljanje klicev na bazo
|
|
|
|
Base = declarative_base()
|
|
|
|
class ImeTabele(Base):
|
|
|
|
__tablename__ = 'imetabele'
|
|
|
|
stolpec = Column(Tip, druge lastnosti stolpca)
|
|
|
|
|
|
|
|
def create_all():
|
|
|
|
Base.metadata.create_all(Engine)
|
|
|
|
|
|
|
|
Ustvarimo engine in session objekt ter funkciji, ki ju vračata
|
|
|
|
Engine = create_engine(CONFIG['DB_CONNECTION'])
|
|
|
|
Session = sessionmaker(Engine)
|
|
|
|
|
|
|
|
def get_session():
|
|
|
|
return Session
|
|
|
|
def get_engine():
|
|
|
|
return Engine
|
|
|
|
|
|
|
|
V skripti, ki želi dostopati pa do baze pa klic postavimo v naslednji okvir:
|
|
|
|
Session = get_session()
|
|
|
|
with Session() as session:
|
|
|
|
<vsebina klica>
|
|
|
|
get_engine().dispose()
|
|
|
|
"""
|