-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathorm.py
99 lines (78 loc) · 2.8 KB
/
orm.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
89
90
91
92
93
94
95
96
97
98
from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Table, Column, Integer, String, MetaData, ForeignKey
from sqlalchemy.orm import relationship, sessionmaker
import config
import nonebot
engine = create_engine(config.DB_ENGINE['sqlite'])
Base = declarative_base()
class User(Base):
__tablename__ = "Users"
UserId = Column(Integer, primary_key=True)
Name = Column(String)
class Group(Base):
__tablename__ = "Groups"
GroupId = Column(Integer, primary_key=True)
Name = Column(String)
class UserInGroup(Base):
__tablename__ = "UserInGroup"
ROLE_ADMIN = "admin"
ROLE_MEMBER = "member"
ROLE_SERVER = "server"
Id = Column(Integer, primary_key=True)
GroupId = Column(Integer, ForeignKey("Groups.GroupId"), nullable=False)
UserId = Column(Integer, ForeignKey("Users.UserId"), nullable=False)
Role = Column(String, nullable=False)
Name = Column(String, nullable=False)
Session = sessionmaker(bind=engine)
_ses = Session()
# Updating Tables
Base.metadata.create_all(engine)
def getSession() -> Session:
return _ses
def getUserByidOrCreate(ses: Session, Id : Integer):
created = False
usr = ses.query(User).filter(User.UserId == Id).one_or_none()
if not usr:
nonebot.logger.debug(f"Creating new user {Id}")
usr = User(UserId = Id, Name = "Unknown")
ses.add(usr)
created = True
return usr, created
def getGroupByIdOrCreate(ses: Session, Id : Integer):
created = False
group = ses.query(Group).filter(Group.GroupId == Id).one_or_none()
if not group:
nonebot.logger.debug(f"Creating new Group {Id}")
group = Group(GroupId = Id, Name = "Unknown")
ses.add(group)
created = True
return group, created
def getUserInGroupOrCreate(ses : Session, user : User, group : Group):
created = False
affiliation = ses.query(UserInGroup).filter(
UserInGroup.UserId == user.UserId,
UserInGroup.GroupId == group.GroupId
).one_or_none()
if not affiliation:
nonebot.logger.debug(f"Creating new affilication {user.UserId} in {group.GroupId}")
affiliation = UserInGroup(UserId=user.UserId, GroupId=group.GroupId, \
Name="UnknownG", Role=UserInGroup.ROLE_MEMBER)
ses.add(affiliation)
created = True
return affiliation, created
def is_admin(role):
if role == ROLE_ADMIN:
return True
elif role == ROLE_SERVER:
return True
elif role == ROLE_MEMBER:
return False
else:
raise Exception
if __name__ == "__main__" :
# Initializes and creates the tables
print("Dropping all tables.\n")
Base.metadata.drop_all(engine)
print("Initializing all tables.\n")
Base.metadata.create_all(engine)