-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgroup.py
69 lines (57 loc) · 1.34 KB
/
group.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
from database import get_db
class GroupMembership:
def __init__(self, group_id, user_id, role):
self.group_id = group_id
self.user_id = user_id
self.role = role
@staticmethod
def get(user_id):
db = get_db()
group_member = db.execute(
"SELECT * FROM group_members WHERE user_id = ?", (user_id,)
).fetchone()
if not group_member:
return None
group_id = group_member[0]
user_id = group_member[1]
role = db.execute(
"SELECT * FROM groups WHERE group_id = ?", (group_id,)
).fetchone()[1]
group_member = GroupMembership(
group_id=group_id,
user_id=user_id,
role=role
)
return group_member
@staticmethod
def get_all():
db = get_db()
group_members = db.execute(
"SELECT * FROM group_members"
).fetchall()
if not group_members:
return None
group_member_list = []
for group_member in group_members:
group_id = group_member[0]
user_id = group_member[1]
role = db.execute(
"SELECT * FROM groups WHERE group_id = ?", (group_id,)
).fetchone()[1]
group_member_list.append(
GroupMembership(
group_id=group_id,
user_id=user_id,
role=role
)
)
return group_member_list
@staticmethod
def create(user_id, group_id):
db = get_db()
db.execute(
"INSERT INTO group_members (user_id, group_id) "
"VALUES (?, ?)",
(user_id, group_id)
)
db.commit()