-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetSubject.py
49 lines (41 loc) · 1.7 KB
/
getSubject.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
import functions_framework
from flask import jsonify
from pymongo import MongoClient
@functions_framework.http
def hello_http(request):
"""HTTP Cloud Function.
Args:
request (flask.Request): The request object.
<https://flask.palletsprojects.com/en/1.1.x/api/#incoming-request-data>
Returns:
The response text, or any set of values that can be turned into a
Response object using `make_response`
<https://flask.palletsprojects.com/en/1.1.x/api/#flask.make_response>.
"""
request_json = request.get_json(silent=True)
request_args = request.args
uri = "mongodb+srv://tanisha:tanisha@cluster0.pxmdv.mongodb.net/?retryWrites=true&w=majority"
try:
client = MongoClient(uri)
db = client.demo # Access the 'demo' database
collection = db.collec # Access the 'collec' collection
if request_json and 'email' in request_json:
email = request_json['email']
elif request_args and 'email' in request_args:
email = request_args['email']
else:
return "No email provided"
# Aggregate distinct subjects from the saved_quizzes field
# Query the collection based on the email field
query = {"email": email}
result = collection.find_one(query)
if result:
# Extract subjects from saved quizzes
saved_quizzes = result.get('saved_quizzes', [])
subjects = [quiz['subject'] for quiz in saved_quizzes]
return jsonify(subjects)
else:
return "No saved quizzes found for email: {}".format(email)
except Exception as e:
print(e)
return "Error occurred: {}".format(e)