-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodels.py
88 lines (63 loc) · 3.2 KB
/
models.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import os
from dotenv import load_dotenv
from sqlalchemy import ForeignKey, Column, String
from sqlalchemy.dialects.mysql import DATETIME, INTEGER, BIGINT, FLOAT
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import relationship
from common import utilities
load_dotenv("dev.env")
database_name = os.getenv("database")
varchar_length = int(os.getenv("varchar_length"))
DATETIME = DATETIME(fsp=int(os.getenv("time_fsp")))
Base = declarative_base()
action_categories = [
"start channel", "end channel", "start stream", "end stream", "start video", "end video", "start voice",
"end voice",
# Currently timer logs are not implemented
# "start timer", "end timer"
]
class User(Base):
# Question - How to make it just use the class name instead of hard coding the table name?
__tablename__ = 'user'
id = Column(BIGINT, primary_key=True)
longest_streak = Column(INTEGER, server_default="0")
current_streak = Column(INTEGER, server_default="0")
class Action(Base):
__tablename__ = 'action'
id = Column(INTEGER, primary_key=True)
user_id = Column(BIGINT, ForeignKey('user.id', onupdate="CASCADE"), nullable=False, index=True)
category = Column(String(varchar_length), nullable=False)
detail = Column(BIGINT) # Currently, detail is the id of the channel where actions happen
creation_time = Column(DATETIME, default=utilities.get_time)
user = relationship("User", back_populates="action")
class DailyStudyTime(Base):
__tablename__ = "dailystudytime"
id = Column(INTEGER, primary_key=True)
user_id = Column(BIGINT, ForeignKey('user.id', onupdate="CASCADE"), nullable=False, index=True)
timestamp = Column(DATETIME, nullable=False)
study_time = Column(FLOAT, nullable=False)
rank = Column(INTEGER, nullable=False)
user = relationship("User", back_populates="dailystudytime")
class WeeklyStudyTime(Base):
__tablename__ = "weeklystudytime"
id = Column(INTEGER, primary_key=True)
user_id = Column(BIGINT, ForeignKey('user.id', onupdate="CASCADE"), nullable=False, index=True)
timestamp = Column(DATETIME, nullable=False)
study_time = Column(FLOAT, nullable=False)
rank = Column(INTEGER, nullable=False)
user = relationship("User", back_populates="weeklystudytime")
class MonthlyStudyTime(Base):
__tablename__ = "monthlystudytime"
id = Column(INTEGER, primary_key=True)
user_id = Column(BIGINT, ForeignKey('user.id', onupdate="CASCADE"), nullable=False, index=True)
timestamp = Column(DATETIME, nullable=False)
study_time = Column(FLOAT, nullable=False)
rank = Column(INTEGER, nullable=False)
user = relationship("User", back_populates="monthlystudytime")
# This must be in global scope for correct models
User.action = relationship("Action", order_by=Action.id, back_populates="user")
User.dailystudytime = relationship("DailyStudyTime", order_by=DailyStudyTime.timestamp, back_populates="user")
User.weeklystudytime = relationship("WeeklyStudyTime", order_by=WeeklyStudyTime.timestamp, back_populates="user")
User.monthlystudytime = relationship("MonthlyStudyTime", order_by=MonthlyStudyTime.timestamp, back_populates="user")
if __name__ == '__main__':
utilities.recreate_db(Base)