Skip to content

Commit dc727da

Browse files
authored
Merge pull request #42 from 1toldyou/main
Use Constant to configure database name
2 parents 9d6c18f + fe5b457 commit dc727da

File tree

8 files changed

+85
-66
lines changed

8 files changed

+85
-66
lines changed

constant.py

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,30 @@ class ContentLimit:
3232
IMAGE_SIZE = 1024 * 1024 * 4
3333

3434

35+
class MediaAssets:
36+
FAVICON = "https://cdn.jsdelivr.net/gh/Plan-At/static-image/2022/02/17/favicon.ico"
37+
38+
39+
class APITag:
40+
AUTH = ["Authorization"]
41+
HOSTING = ["Content Hosting"]
42+
CALENDAR = ["Calendar Related"]
43+
USER = ["User Related"]
44+
CAPTCHA = ["Captcha"]
45+
EXAMPLE = ["Example Data"]
46+
47+
48+
class DBName:
49+
THIS = "PlanAtDev"
50+
CLUSTER_NAME = "Cluster1"
51+
TOKEN = "TokenV3"
52+
LOGIN = "LoginV2"
53+
USER_PROFILE = "User"
54+
CALENDAR_EVENT_INDEX = "CalendarEventIndex"
55+
CALENDAR_EVENT = "CalendarEventEntry"
56+
IMAGE_HOSTING = "ImageHosting"
57+
58+
3559
class RateLimitConfig:
3660
ENABLE_RL = False
3761
if ENABLE_RL:
@@ -68,16 +92,3 @@ class RateLimitConfig:
6892
MID_SENSITIVITY = "100/second"
6993
HIGH_SENSITIVITY = "100/second"
7094
BURST = "1000/second"
71-
72-
73-
class MediaAssets:
74-
FAVICON = "https://cdn.jsdelivr.net/gh/Plan-At/static-image/2022/02/17/favicon.ico"
75-
76-
77-
class APITag:
78-
AUTH = ["Authorization"]
79-
HOSTING = ["Content Hosting"]
80-
CALENDAR = ["Calendar Related"]
81-
USER = ["User Related"]
82-
CAPTCHA = ["Captcha"]
83-
EXAMPLE = ["Example Data"]

route/v2_auth.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
# Local file
1212
from util import json_body, token_tool
1313
import util.pymongo_wrapper as DocumentDB
14+
from constant import DBName
1415

1516
router = APIRouter()
1617

@@ -20,7 +21,7 @@ async def v2_revoke_auth_token(request: Request, pa_token: str = Header(None)):
2021
mongo_client = DocumentDB.get_client()
2122
db_client = mongo_client.get_database(DocumentDB.DB)
2223
token_deletion_query = DocumentDB.delete_one(
23-
collection="TokenV3",
24+
collection=DBName.TOKEN,
2425
find_filter={"token_value": pa_token},
2526
db_client=db_client)
2627
mongo_client.close()
@@ -36,7 +37,7 @@ async def v2_revoke_auth_token(request: Request, pa_token: str = Header(None)):
3637
async def v2_verify_auth_password(request: Request, cred: json_body.PasswordLoginBody):
3738
mongo_client = DocumentDB.get_client()
3839
db_client = mongo_client.get_database(DocumentDB.DB)
39-
credential_verify_query = DocumentDB.find_one(collection="LoginV2",
40+
credential_verify_query = DocumentDB.find_one(collection=DBName.LOGIN,
4041
find_filter={"person_id": cred.person_id},
4142
db_client=db_client)
4243
print(credential_verify_query)
@@ -61,7 +62,7 @@ async def v2_verify_auth_password(request: Request, cred: json_body.PasswordLogi
6162
async def v2_update_auth_password(request: Request, old_cred: json_body.PasswordLoginBody, new_cred: json_body.PasswordLoginBody):
6263
mongo_client = DocumentDB.get_client()
6364
db_client = mongo_client.get_database(DocumentDB.DB)
64-
credential_verify_query = DocumentDB.find_one(collection="LoginV2",
65+
credential_verify_query = DocumentDB.find_one(collection=DBName.LOGIN,
6566
find_filter={"person_id": old_cred.person_id},
6667
db_client=db_client)
6768
print(credential_verify_query)
@@ -79,7 +80,7 @@ async def v2_update_auth_password(request: Request, old_cred: json_body.Password
7980
"password_hash": hashlib.sha512(new_cred.password.encode("utf-8")).hexdigest(),
8081
"password_length": len(new_cred.password),
8182
}
82-
credential_update_query = DocumentDB.replace_one(collection="LoginV2",
83+
credential_update_query = DocumentDB.replace_one(collection=DBName.LOGIN,
8384
find_filter={"person_id": old_cred.person_id},
8485
document_body=new_credential_entry,
8586
db_client=db_client)
@@ -98,7 +99,7 @@ async def v2_enable_auth_totp(request: Request, cred: json_body.PasswordLoginBod
9899
mongo_client = DocumentDB.get_client()
99100
db_client = mongo_client.get_database(DocumentDB.DB)
100101
# same as the traditional plain-password login
101-
credential_verify_query = DocumentDB.find_one(collection="LoginV2",
102+
credential_verify_query = DocumentDB.find_one(collection=DBName.LOGIN,
102103
find_filter={"person_id": cred.person_id},
103104
db_client=db_client)
104105
print(credential_verify_query)
@@ -118,7 +119,7 @@ async def v2_enable_auth_totp(request: Request, cred: json_body.PasswordLoginBod
118119
authenticator_url = pyotp.totp.TOTP(new_secret_key).provisioning_uri(name=cred.person_id,
119120
issuer_name='Plan-At')
120121
credential_modify_query = DocumentDB.update_one(db_client=db_client,
121-
collection="LoginV2",
122+
collection=DBName.LOGIN,
122123
find_filter={"person_id": cred.person_id},
123124
changes={"$set": {"totp_status": "enabled",
124125
"totp_secret_key": new_secret_key}})
@@ -140,7 +141,7 @@ async def v2_disable_auth_totp(request: Request, cred: json_body.PasswordLoginBo
140141
mongo_client = DocumentDB.get_client()
141142
db_client = mongo_client.get_database(DocumentDB.DB)
142143
# same as the traditional plain-password login
143-
credential_verify_query = DocumentDB.find_one(collection="LoginV2",
144+
credential_verify_query = DocumentDB.find_one(collection=DBName.LOGIN,
144145
find_filter={"person_id": cred.person_id},
145146
db_client=db_client)
146147
print(credential_verify_query)
@@ -158,7 +159,7 @@ async def v2_disable_auth_totp(request: Request, cred: json_body.PasswordLoginBo
158159
content={"status": "Time-based OTP not enabled for this user",
159160
"person_id": cred.person_id})
160161
credential_modify_query = DocumentDB.update_one(db_client=db_client,
161-
collection="LoginV2",
162+
collection=DBName.LOGIN,
162163
find_filter={"person_id": cred.person_id},
163164
changes={"$set": {"totp_status": "disabled",
164165
"totp_secret_key": ""}})
@@ -184,7 +185,7 @@ async def v2_verify_auth_totp(request: Request, person_id: str, totp_code: str):
184185
content={"status": "totp_code malformed",
185186
"totp_code": totp_code})
186187
# same as the traditional plain-password login
187-
credential_verify_query = DocumentDB.find_one(collection="LoginV2",
188+
credential_verify_query = DocumentDB.find_one(collection=DBName.LOGIN,
188189
find_filter={"person_id": person_id},
189190
db_client=db_client)
190191
print(credential_verify_query)

route/v2_calendar.py

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from util.token_tool import get_person_id_with_token
1212
import util.pymongo_wrapper as DocumentDB
1313
import util.json_filter as JSONFilter
14+
from constant import DBName
1415

1516
router = APIRouter()
1617

@@ -64,13 +65,13 @@ async def v2_create_calendar_event(request: Request, req_body: json_body.Calenda
6465
if not least_one_access_control:
6566
return JSONResponse(status_code=400, content={"status": "person_id or canonical_name in access_control_list is required"})
6667
print(new_event_entry)
67-
insert_query = DocumentDB.insert_one(collection="CalendarEventEntry",
68+
insert_query = DocumentDB.insert_one(collection=DBName.CALENDAR_EVENT,
6869
document_body=new_event_entry,
6970
db_client=db_client)
7071
print(insert_query.inserted_id)
7172
"""add record to the index"""
7273
index_update_query = DocumentDB.update_one(
73-
collection="CalendarEventIndex",
74+
collection=DBName.CALENDAR_EVENT_INDEX,
7475
find_filter={"person_id": person_id},
7576
changes={"$push": {"event_id_list": new_event_id}},
7677
db_client=db_client)
@@ -97,7 +98,7 @@ async def v2_edit_calendar_event(request: Request,
9798
if person_id == "":
9899
return JSONResponse(status_code=403, content={"status": "user not found"})
99100
# Check is have sufficient permission to modify the event
100-
find_query = DocumentDB.find_one(collection="CalendarEventEntry",
101+
find_query = DocumentDB.find_one(collection=DBName.CALENDAR_EVENT,
101102
find_filter={"event_id": event_id},
102103
db_client=db_client)
103104
print(find_query)
@@ -141,7 +142,7 @@ async def v2_edit_calendar_event(request: Request,
141142
least_one_access_control = False
142143
for each_access_control in req_body.access_control_list:
143144
print(each_access_control)
144-
if (each_access_control.canonical_name != None) or (each_access_control.person_id != None):
145+
if (each_access_control.canonical_name is not None) or (each_access_control.person_id is not None):
145146
updated_event_entry["access_control_list"].append({
146147
"canonical_name": each_access_control.canonical_name,
147148
"person_id": each_access_control.person_id,
@@ -152,8 +153,10 @@ async def v2_edit_calendar_event(request: Request,
152153
return JSONResponse(status_code=400,
153154
content={"status": "person_id or canonical_name in access_control_list is required"})
154155
print(updated_event_entry)
155-
insert_query = DocumentDB.replace_one(collection="CalendarEventEntry", find_filter={"event_id": event_id},
156-
document_body=updated_event_entry, db_client=db_client)
156+
insert_query = DocumentDB.replace_one(collection=DBName.CALENDAR_EVENT,
157+
find_filter={"event_id": event_id},
158+
document_body=updated_event_entry,
159+
db_client=db_client)
157160
print(insert_query)
158161
mongo_client.close()
159162
return JSONResponse(status_code=200, content={"status": "success", "event_id": event_id})
@@ -179,7 +182,7 @@ async def v2_delete_calendar_event(request: Request, event_id: int, pa_token: st
179182
mongo_client.close()
180183
return JSONResponse(status_code=403,
181184
content={"status": f"unable to delete calendar_event {event_id} with current token"})
182-
deletion_query = DocumentDB.delete_one(collection="CalendarEventEntry",
185+
deletion_query = DocumentDB.delete_one(collection=DBName.CALENDAR_EVENT,
183186
find_filter={"event_id": event_id},
184187
db_client=db_client)
185188
print(deletion_query)
@@ -190,7 +193,7 @@ async def v2_delete_calendar_event(request: Request, event_id: int, pa_token: st
190193
return JSONResponse(status_code=404,
191194
content={"status": "calendar_event deleted but some error occurred", "event_id": event_id})
192195
"""remove from the index"""
193-
update_query = DocumentDB.update_one(collection="CalendarEventIndex",
196+
update_query = DocumentDB.update_one(collection=DBName.CALENDAR_EVENT_INDEX,
194197
find_filter={"person_id": person_id},
195198
changes={"$pull": {"event_id_list": event_id}},
196199
db_client=db_client)
@@ -219,15 +222,14 @@ async def v2_get_calendar_event(request: Request,
219222
if len(str(event_id)) != 16:
220223
result_calendar_event.append({"status": "malformed event_id", "event_id": event_id})
221224
else:
222-
find_query = DocumentDB.find_one(collection="CalendarEventEntry",
225+
find_query = DocumentDB.find_one(collection=DBName.CALENDAR_EVENT,
223226
find_filter={"event_id": event_id},
224227
db_client=db_client)
225228
if find_query is None:
226229
result_calendar_event.append({"status": "calendar_event not found", "event_id": event_id})
227-
processed_find_query = JSONFilter.universal_calendar_event(
228-
input_json=find_query,
229-
person_id=person_id,
230-
required_permission_list=["read_full"])
230+
processed_find_query = JSONFilter.universal_calendar_event(input_json=find_query,
231+
person_id=person_id,
232+
required_permission_list=["read_full"])
231233
if processed_find_query:
232234
result_calendar_event.append(processed_find_query)
233235
except (Exception, OSError, IOError) as e:
@@ -245,7 +247,7 @@ async def v2_get_calendar_event_index(request: Request, pa_token: str = Header(N
245247
if person_id == "":
246248
mongo_client.close()
247249
return JSONResponse(status_code=403, content={"status": "user not found with this token", "pa_token": pa_token})
248-
db_query = DocumentDB.find_one(collection="CalendarEventIndex", find_filter={"person_id": person_id}, db_client=db_client)
250+
db_query = DocumentDB.find_one(collection=DBName.CALENDAR_EVENT_INDEX, find_filter={"person_id": person_id}, db_client=db_client)
249251
if db_query is None:
250252
return JSONResponse(status_code=403, content={"status": "CalendarEvent index for this user not found", "person_id": person_id})
251253
mongo_client.close()

route/v2_hosting.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import util.pymongo_wrapper as DocumentDB
88
from util.token_tool import get_person_id_with_token
99
from util import image4io
10-
from constant import ServerConfig, ContentLimit
10+
from constant import ServerConfig, ContentLimit, DBName
1111

1212
router = APIRouter()
1313

@@ -43,7 +43,7 @@ async def v2_upload_image(request: Request, image_file_bytes: bytes = File(...,
4343
"image_height": image_info["uploadedFiles"][0]["height"],
4444
"hosting_provider": "image4io"
4545
}
46-
db_action_result = DocumentDB.insert_one(collection="ImageHosting", document_body=report_card, db_client=db_client)
46+
db_action_result = DocumentDB.insert_one(collection=DBName.IMAGE_HOSTING, document_body=report_card, db_client=db_client)
4747
print(db_action_result)
4848
mongo_client.close()
4949
return JSONResponse(status_code=201,
@@ -57,7 +57,7 @@ async def v2_delete_image(request: Request, image_id: str, pa_token: str = Heade
5757
person_id = get_person_id_with_token(pa_token, db_client)
5858
if person_id == "":
5959
return JSONResponse(status_code=403, content={"status": "you need to upload an image", "pa_token": pa_token})
60-
image_info_query = DocumentDB.find_one(collection="ImageHosting", find_filter={"image_id": image_id}, db_client=db_client)
60+
image_info_query = DocumentDB.find_one(collection=DBName.IMAGE_HOSTING, find_filter={"image_id": image_id}, db_client=db_client)
6161
print(image_info_query)
6262
resp = image4io.deleteImage(
6363
authorization=image4io.calculate_basic_auth(
@@ -69,7 +69,7 @@ async def v2_delete_image(request: Request, image_id: str, pa_token: str = Heade
6969
return JSONResponse(status_code=500, content={"status": "image deletion failed", "reason": resp.json()["errors"]})
7070
image_info = resp.json()
7171
print(image_info)
72-
db_action_result = DocumentDB.delete_one(collection="ImageHosting", find_filter={"image_id": image_id}, db_client=db_client)
72+
db_action_result = DocumentDB.delete_one(collection=DBName.IMAGE_HOSTING, find_filter={"image_id": image_id}, db_client=db_client)
7373
if db_action_result.deleted_count != 1:
7474
return JSONResponse(status_code=500,
7575
content={"status": "image deleted from hosting service but failed to remove relevant record from our database", "image_id": image_id})

0 commit comments

Comments
 (0)