-
Notifications
You must be signed in to change notification settings - Fork 0
/
team_base.py
258 lines (209 loc) · 7.17 KB
/
team_base.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
import json
import os
import psycopg2
class TeamBase:
"""
Base interface implementation for API's to manage teams.
For simplicity a single team manages a single project. And there is a separate team per project.
Users can be
"""
def __init__(self):
self.connection = psycopg2.connect(
host=os.getenv('POSTGRES_HOSTNAME'),
port=os.getenv('POSTGRES_PORT'),
user=os.getenv('POSTGRES_USERNAME'),
password=os.getenv('POSTGRES_PASSWORD'),
database=os.getenv('POSTGRES_DBNAME')
)
self.cursor = self.connection.cursor()
self.cursor.execute('''
CREATE TABLE IF NOT EXISTS teams (
id SERIAL PRIMARY KEY,
name VARCHAR(64) NOT NULL UNIQUE,
description VARCHAR(256) NOT NULL,
creation_time TIMESTAMP NOT NULL DEFAULT NOW(),
admin VARCHAR(64) NOT NULL
)
''')
# create a team
def create_team(self, request: str) -> str:
"""
:param request: A json string with the team details
{
"name" : "<team_name>",
"description" : "<some description>",
"admin": "<id of a user>"
}
:return: A json string with the response {"id" : "<team_id>"}
Constraint:
* Team name must be unique
* Name can be max 64 characters
* Description can be max 128 characters
"""
try:
result = ""
json_request = json.loads(request)
assert len(json_request['name']) <= 64
assert len(json_request['description']) <= 128
self.cursor.execute('''
INSERT INTO teams (name, description, admin) VALUES (%s, %s, %s) RETURNING id
''',
(json_request['name'], json_request['description'], json_request['admin']))
result = json.dumps({"id": self.cursor.fetchone()[0]})
return result
except Exception as e:
print(e)
# list all teams
def list_teams(self) -> str:
"""
:return: A json list with the response.
[
{
"name" : "<team_name>",
"description" : "<some description>",
"creation_time" : "<some date:time format>",
"admin": "<id of a user>"
}
]
"""
try:
result = ""
self.cursor.execute('''
SELECT * FROM teams
''')
result = json.dumps(self.cursor.fetchall())
return result
except Exception as e:
print(e)
# describe team
def describe_team(self, request: str) -> str:
"""
:param request: A json string with the team details
{
"id" : "<team_id>"
}
:return: A json string with the response
{
"name" : "<team_name>",
"description" : "<some description>",
"creation_time" : "<some date:time format>",
"admin": "<id of a user>"
}
"""
try:
result = ""
json_request = json.loads(request)
self.cursor.execute('''
SELECT * FROM teams WHERE id = %s
''',
(json_request['id'],))
result = json.dumps(self.cursor.fetchone())
return result
except Exception as e:
print(e)
# update team
def update_team(self, request: str) -> str:
"""
:param request: A json string with the team details
{
"id" : "<team_id>",
"team" : {
"name" : "<team_name>",
"description" : "<team_description>",
"admin": "<id of a user>"
}
}
:return:
Constraint:
* Team name must be unique
* Name can be max 64 characters
* Description can be max 128 characters
"""
try:
result = ""
json_request = json.loads(request)
assert len(json_request['team']['name']) <= 64
assert len(json_request['team']['description']) <= 128
self.cursor.execute('''
UPDATE teams SET description = %s WHERE id = %s
''',
(json_request['description'], json_request['id']))
result = json.dumps({"id": self.cursor.fetchone()[0]})
return result
except Exception as e:
print(e)
# add users to team
def add_users_to_team(self, request: str):
"""
:param request: A json string with the team details
{
"id" : "<team_id>",
"users" : ["user_id 1", "user_id2"]
}
:return:
Constraint:
* Cap the max users that can be added to 50
"""
try:
result = ""
json_request = json.loads(request)
assert len(json_request['users']) <= 50
self.cursor.execute('''
UPDATE teams SET users = %s WHERE id = %s
''',
(json_request['users'], json_request['id']))
result = json.dumps({"id": self.cursor.fetchone()[0]})
return result
except Exception as e:
print(e)
# add users to team
def remove_users_from_team(self, request: str):
"""
:param request: A json string with the team details
{
"id" : "<team_id>",
"users" : ["user_id 1", "user_id2"]
}
:return:
Constraint:
* Cap the max users that can be added to 50
"""
try:
result = ""
json_request = json.loads(request)
assert len(json_request['users']) <= 50
self.cursor.execute('''
UPDATE teams SET users = %s WHERE id = %s
''',
(json_request['users'], json_request['id']))
result = json.dumps({"id": self.cursor.fetchone()[0]})
return result
except Exception as e:
print(e)
# list users of a team
def list_team_users(self, request: str):
"""
:param request: A json string with the team identifier
{
"id" : "<team_id>"
}
:return:
[
{
"id" : "<user_id>",
"name" : "<user_name>",
"display_name" : "<display name>"
}
]
"""
try:
result = ""
json_request = json.loads(request)
self.cursor.execute('''
SELECT * FROM users WHERE id = %s
''',
(json_request['id'],))
result = json.dumps(self.cursor.fetchone())
return result
except Exception as e:
print(e)