-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetQuizzes.py
52 lines (45 loc) · 1.85 KB
/
getQuizzes.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
import functions_framework
from flask import jsonify, request
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)
uri = "mongodb+srv://tanisha:[email protected]/?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']
else:
return "No email provided"
if request_json and 'subject' in request_json:
subject_to_find = request_json['subject']
else:
return "No subject provided"
# Query the collection based on the email field
query = {"email": email}
result = collection.find_one(query)
if result:
# Extract topics from saved quizzes for the specified subject
saved_quizzes = result.get('saved_quizzes', [])
topics = []
for quiz in saved_quizzes:
if quiz['subject'] == subject_to_find:
for topic in quiz['topics']:
topics.append(topic['topic_name'])
return jsonify(topics)
else:
return "No data found for email: {}".format(email)
except Exception as e:
print(e)
return "Error occurred: {}".format(e)