-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueries.py
More file actions
198 lines (153 loc) · 5.02 KB
/
queries.py
File metadata and controls
198 lines (153 loc) · 5.02 KB
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
"""
Query Resolvers
"""
import json
import os
from typing import List
import httpx
import strawberry
from db import ccdb, docsstoragedb
from models import CCRecruitment, StorageFile
# import all models and types
from otypes import (
CCRecruitmentType,
Info,
SignedURL,
SignedURLInput,
StorageFileType,
)
from utils import get_curr_time_str
inter_communication_secret = os.getenv("INTER_COMMUNICATION_SECRET")
# fetch signed url from the files service
@strawberry.field
async def signedUploadURL(details: SignedURLInput, info: Info) -> SignedURL:
"""
Uploads file to the files service by any user.
Args:
details (otypes.SignedURLInput): contains the details of the file to be
uploaded
info (otypes.Info): contains the user's context information.
Returns:
(otypes.SignedURL): Signed URL for uploading files to the files service
Raises:
Exception: Not logged in!
Exception: If the request failed.
"""
user = info.context.user
if not user:
raise Exception("Not logged in!")
async with httpx.AsyncClient() as client:
# make request to files api
response = await client.get(
"http://files/signed-url",
params={
"user": json.dumps(user),
"static_file": "true" if details.static_file else "false",
"filename": details.filename,
"inter_communication_secret": inter_communication_secret,
"max_sizeMB": details.max_size_mb,
},
)
# error handling
if response.status_code != 200:
raise Exception(response.text)
return SignedURL(url=response.text)
@strawberry.field
async def ccApplications(
info: Info,
year: int = 2024,
) -> List[CCRecruitmentType]:
"""
Returns list of all CC Applications for CC.
Args:
info (otypes.Info): contains the user's context information.
year (int): The year of application. Defaults to 2024.
Returns:
(List[otypes.CCRecruitmentType]): List of all CC Applications
for the given year.
Raises:
Exception: Not logged in!
Exception: Not Authenticated to access this API!!
Exception: Invalid year
"""
user = info.context.user
if not user:
raise Exception("Not logged in!")
if user.get("role", None) not in ["cc"]:
raise Exception("Not Authenticated to access this API!!")
if year < 2024:
raise Exception("Invalid year")
results = await ccdb.find().to_list(length=None)
applications = [
CCRecruitmentType.from_pydantic(CCRecruitment.model_validate(result))
for result in results
if result.get("apply_year", 2024) == year
]
return applications
@strawberry.field
async def haveAppliedForCC(info: Info, year: int | None = None) -> bool:
"""
Finds whether any logged in user has applied for CC.
Args:
info (otypes.Info): contains the user's context information.
year (int): The year of application. Defaults to None.
Returns:
(bool): True if the user has applied for CC, False otherwise.
Raises:
Exception: Not logged in!
Exception: Not Authenticated to access this API!!
Exception: Invalid year
"""
user = info.context.user
if not user:
raise Exception("Not logged in!")
if user.get("role", None) not in ["public"]:
raise Exception("Not Authenticated to access this API!!")
if year is None:
year = int(get_curr_time_str()[:4])
if year < 2024:
raise Exception("Invalid year")
# check if user already applied in the same year
results = await ccdb.find({"uid": user["uid"]}).to_list(length=None)
for result in results:
if result.get("apply_year", 2024) == year:
return True
return False
# Storagefile queries
@strawberry.field
async def storagefiles(filetype: str) -> List[StorageFileType]:
"""
Gets all the storage files, has public access
Args:
filetype (str): The type of file to get.
Returns:
(List[otypes.StorageFileType]): List of storage files of the given type
"""
storage_files = await docsstoragedb.find({"filetype": filetype}).to_list(
length=None
)
return [
StorageFileType.from_pydantic(StorageFile.model_validate(storage_file))
for storage_file in storage_files
]
@strawberry.field
async def storagefile(file_id: str) -> StorageFileType:
"""
Gets a single storage file by id, has public access
Args:
file_id (str): The id of the file to get
Returns:
(otypes.StorageFileType): The storage file with the given id
"""
storage_file = await docsstoragedb.find_one({"_id": file_id})
return StorageFileType.from_pydantic(
StorageFile.model_validate(storage_file)
)
# register all queries
queries = [
signedUploadURL,
ccApplications,
haveAppliedForCC,
storagefiles,
storagefile,
]