-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
226 lines (191 loc) · 6.57 KB
/
utils.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
import os
import requests
inter_communication_secret = os.getenv("INTER_COMMUNICATION_SECRET")
def update_role(uid, cookies=None, role="club") -> dict | None:
"""
Function to call the updateRole mutation
Makes a mutation resolved by the `updateRole` method from Users
Microservice.
Used to change a user's role field.
Args:
uid (str): User ID.
cookies (dict): Cookies from the request. Defaults to None.
role (str): Role of the user to be updated to. Defaults to 'club'.
Returns:
dict: Response from the mutation.
"""
try:
query = """
mutation UpdateRole($roleInput: RoleInput!) {
updateRole(roleInput: $roleInput)
}
"""
variables = {
"roleInput": {
"role": role,
"uid": uid,
"interCommunicationSecret": inter_communication_secret,
}
}
if cookies:
result = requests.post(
"http://gateway/graphql",
json={"query": query, "variables": variables},
cookies=cookies,
)
else:
result = requests.post(
"http://gateway/graphql",
json={"query": query, "variables": variables},
)
return result.json()
except Exception:
return None
def update_events_members_cid(old_cid, new_cid, cookies=None) -> bool:
"""
Function to call the updateEventsCid & updateMembersCid mutation
Makes a mutation resolved by the `updateEventsCid` method from Events
Microservice.
Makes another mutation resolved by the `updateMembersCid` method from
Members Microservice.
Used when club changes its cid to change its members and events data
accordingly.
Args:
old_cid (str): Old CID of the club.
new_cid (str): New CID of the club.
cookies (dict): Cookies from the request. Defaults to None.
Returns:
bool: True if both mutations are successful, False otherwise.
"""
return1, return2 = None, None
# Update Events CID
try:
query = """
mutation UpdateEventsCid($oldCid: String!, $newCid: String!, $interCommunicationSecret: String) {
updateEventsCid(oldCid: $oldCid, newCid: $newCid, interCommunicationSecret: $interCommunicationSecret)
}
""" # noqa: E501
variables = {
"oldCid": old_cid,
"newCid": new_cid,
"interCommunicationSecret": inter_communication_secret,
}
if cookies:
result = requests.post(
"http://gateway/graphql",
json={"query": query, "variables": variables},
cookies=cookies,
)
else:
result = requests.post(
"http://gateway/graphql",
json={"query": query, "variables": variables},
)
return1 = result.json()
except Exception:
return False
# Update Members CID
try:
query = """
mutation UpdateMembersCid($oldCid: String!, $newCid: String!, $interCommunicationSecret: String) {
updateMembersCid(oldCid: $oldCid, newCid: $newCid, interCommunicationSecret: $interCommunicationSecret)
}
""" # noqa: E501
variables = {
"oldCid": old_cid,
"newCid": new_cid,
"interCommunicationSecret": inter_communication_secret,
}
if cookies:
result = requests.post(
"http://gateway/graphql",
json={"query": query, "variables": variables},
cookies=cookies,
)
else:
result = requests.post(
"http://gateway/graphql",
json={"query": query, "variables": variables},
)
return2 = result.json()
except Exception:
return False
if return1 and return2:
return True
else:
return False
def getUser(uid, cookies=None) -> dict | None:
"""
Function to get a particular user details
Makes a query resolved by the `userProfile` method from Users Microservice.
Used to get a users details.
Args:
uid (str): User ID of the user to be fetched.
cookies (dict): Cookies from the request. Defaults to None.
Returns:
dict: User details as a result of the query.
"""
try:
query = """
query GetUserProfile($userInput: UserInput!) {
userProfile(userInput: $userInput) {
firstName
lastName
email
rollno
}
}
"""
variable = {"userInput": {"uid": uid}}
if cookies:
request = requests.post(
"http://gateway/graphql",
json={"query": query, "variables": variable},
cookies=cookies,
)
else:
request = requests.post(
"http://gateway/graphql",
json={"query": query, "variables": variable},
)
return request.json()["data"]["userProfile"]
except Exception:
return None
def delete_file(filename) -> str:
"""
Method for deleting a file from the files microservice
Args:
filename (str): Name of the file to be deleted
Returns:
str: Response from the files microservice
"""
response = requests.post(
"http://files/delete-file",
params={
"filename": filename,
"inter_communication_secret": inter_communication_secret,
},
)
if response.status_code != 200:
raise Exception(response.text)
return response.text
def check_remove_old_file(old_obj, new_obj, name="logo") -> bool:
"""
Method to remove old files.
Args:
old_obj (dict): Old object containing the old file
new_obj (dict): New object containing the new file
name (str): Name of the file to be removed. Defaults to "logo" as
mostly they are images of club logo's.
Returns:
bool: True if the old file is removed, False otherwise
"""
old_file = old_obj.get(name)
new_file = new_obj.get(name)
if old_file and new_file and old_file != new_file:
try:
delete_file(old_file)
except Exception as e:
print(f"Error in deleting old {name} file: {e}")
return False
return True