Skip to content

Commit 0ecc4f3

Browse files
authored
Uncomment post_answers function for answer submission
1 parent 0d9236f commit 0ecc4f3

File tree

1 file changed

+102
-102
lines changed

1 file changed

+102
-102
lines changed

routes/answer.py

Lines changed: 102 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -42,109 +42,109 @@ class AnswerStruct(BaseModel):
4242
# "BACKEND":"web",
4343
}
4444

45-
# @ans_app.post("/submit")
46-
# async def post_answers(answerReq: AnswerStruct, idToken: str = Depends(get_access_token)):
47-
# # async def post_answers(answerReq: AnswerStruct):
48-
# try:
49-
# decoded_token = auth.verify_id_token(idToken, app=resources["firebase_app"])
50-
# email = decoded_token.get("email")
51-
# # email='aryanramesh.jain2023@vitstudent.ac.in'
52-
53-
# if not email:
54-
# return JSONResponse(status_code=401, content="Invalid or missing email in token.")
55-
56-
# response = user_table.get_item(Key={"uid": email})
57-
# user = response.get("Item")
58-
59-
# if not user:
60-
# return JSONResponse(status_code=404, content="User not found.")
61-
62-
# flat_domains = list(chain.from_iterable(user.get("domain", []).values()))
63-
# # Validate domain selection
64-
# if answerReq.domain not in flat_domains:
65-
# return JSONResponse(status_code=408, content="Domain was not selected")
45+
@ans_app.post("/submit")
46+
async def post_answers(answerReq: AnswerStruct, idToken: str = Depends(get_access_token)):
47+
# async def post_answers(answerReq: AnswerStruct):
48+
try:
49+
decoded_token = auth.verify_id_token(idToken, app=resources["firebase_app"])
50+
email = decoded_token.get("email")
51+
# email='aryanramesh.jain2023@vitstudent.ac.in'
52+
53+
if not email:
54+
return JSONResponse(status_code=401, content="Invalid or missing email in token.")
55+
56+
response = user_table.get_item(Key={"uid": email})
57+
user = response.get("Item")
58+
59+
if not user:
60+
return JSONResponse(status_code=404, content="User not found.")
61+
62+
flat_domains = list(chain.from_iterable(user.get("domain", []).values()))
63+
# Validate domain selection
64+
if answerReq.domain not in flat_domains:
65+
return JSONResponse(status_code=408, content="Domain was not selected")
6666

67-
# # Validate subcategory for WEB domain
68-
# if answerReq.subcategory and answerReq.domain != "WEB":
69-
# return JSONResponse(status_code=400, content="Subcategory only allowed for WEB domain")
67+
# Validate subcategory for WEB domain
68+
if answerReq.subcategory and answerReq.domain != "WEB":
69+
return JSONResponse(status_code=400, content="Subcategory only allowed for WEB domain")
7070

71-
# if answerReq.domain == "WEB" and answerReq.round == 2 and not answerReq.subcategory:
72-
# return JSONResponse(status_code=400, content="Subcategory required for WEB domain in Round 2")
73-
# mapped_domain = domain_mapping.get(answerReq.domain)
74-
# # if answerReq.domain in ["GRAPHIC DESIGN", "CC", "AI/ML", "UI/UX", "VIDEO EDITING"]:
75-
# # raise HTTPException(status_code=401, detail=f"Deadline for '{answerReq.domain}' is over.")
71+
if answerReq.domain == "WEB" and answerReq.round == 2 and not answerReq.subcategory:
72+
return JSONResponse(status_code=400, content="Subcategory required for WEB domain in Round 2")
73+
mapped_domain = domain_mapping.get(answerReq.domain)
74+
# if answerReq.domain in ["GRAPHIC DESIGN", "CC", "AI/ML", "UI/UX", "VIDEO EDITING"]:
75+
# raise HTTPException(status_code=401, detail=f"Deadline for '{answerReq.domain}' is over.")
7676

77-
# domain_tables = resources['domain_tables']
78-
# domain_table = domain_tables.get(mapped_domain)
79-
80-
# if not domain_table:
81-
# raise HTTPException(status_code=400, detail=f"Domain '{answerReq.domain}' not recognized.")
82-
83-
# # if len(answerReq.questions) != len(answerReq.answers):
84-
# # raise HTTPException(status_code=400, detail="Questions and answers lists must have the same length.")
85-
86-
# # answers_dict = [{"question": q, "answer": a} for q, a in zip(answerReq.questions, answerReq.answers)]
87-
# response = domain_table.get_item(Key={'email': email})
88-
# domain_response = response.get('Item')
89-
90-
# # Convert Pydantic models to dict for DynamoDB
91-
# # For Round 1: answers is List[AnswerItem], convert to dict
92-
# # For Round 2: answers is List[str], use as-is
93-
# if answerReq.round == 1:
94-
# answers_data = [item.dict() for item in answerReq.answers]
95-
# else: # Round 2
96-
# answers_data = answerReq.answers # Already a list of strings
97-
98-
# if answerReq.round == 1:
99-
# if 'Item' in response:
100-
# return JSONResponse(status_code=409, content="Answers already submitted")
101-
102-
# domain_table.put_item(
103-
# Item={
104-
# "email": email,
105-
# # f"round{answerReq.round}": answers_dict,
106-
# # f"score{answerReq.round}": answerReq.score
107-
# "score1" : answerReq.score,
108-
# "round1": answers_data
109-
# }
110-
# )
111-
# if answerReq.round == 2:
112-
# # if not domain_response.get("round1"):
113-
# # return JSONResponse(status_code=201, content=f"Did not attempt round 1")
114-
115-
116-
# if domain_response.get('qualification_status1') != "qualified":
117-
# return JSONResponse(status_code=202, content=f"did not qualify round 1")
118-
119-
# # Determine field name based on subcategory or default to round2
120-
# if answerReq.subcategory == "FRONTEND":
121-
# name = "frontend"
122-
# elif answerReq.subcategory == "BACKEND":
123-
# name = "backend"
124-
# else:
125-
# name = "round2"
77+
domain_tables = resources['domain_tables']
78+
domain_table = domain_tables.get(mapped_domain)
79+
80+
if not domain_table:
81+
raise HTTPException(status_code=400, detail=f"Domain '{answerReq.domain}' not recognized.")
82+
83+
# if len(answerReq.questions) != len(answerReq.answers):
84+
# raise HTTPException(status_code=400, detail="Questions and answers lists must have the same length.")
85+
86+
# answers_dict = [{"question": q, "answer": a} for q, a in zip(answerReq.questions, answerReq.answers)]
87+
response = domain_table.get_item(Key={'email': email})
88+
domain_response = response.get('Item')
89+
90+
# Convert Pydantic models to dict for DynamoDB
91+
# For Round 1: answers is List[AnswerItem], convert to dict
92+
# For Round 2: answers is List[str], use as-is
93+
if answerReq.round == 1:
94+
answers_data = [item.dict() for item in answerReq.answers]
95+
else: # Round 2
96+
answers_data = answerReq.answers # Already a list of strings
97+
98+
if answerReq.round == 1:
99+
if 'Item' in response:
100+
return JSONResponse(status_code=409, content="Answers already submitted")
101+
102+
domain_table.put_item(
103+
Item={
104+
"email": email,
105+
# f"round{answerReq.round}": answers_dict,
106+
# f"score{answerReq.round}": answerReq.score
107+
"score1" : answerReq.score,
108+
"round1": answers_data
109+
}
110+
)
111+
if answerReq.round == 2:
112+
# if not domain_response.get("round1"):
113+
# return JSONResponse(status_code=201, content=f"Did not attempt round 1")
114+
115+
116+
if domain_response.get('qualification_status1') != "qualified":
117+
return JSONResponse(status_code=202, content=f"did not qualify round 1")
118+
119+
# Determine field name based on subcategory or default to round2
120+
if answerReq.subcategory == "FRONTEND":
121+
name = "frontend"
122+
elif answerReq.subcategory == "BACKEND":
123+
name = "backend"
124+
else:
125+
name = "round2"
126126

127-
# domain_table.update_item(
128-
# Key={"email": email},
129-
# UpdateExpression=f"SET {name} = :answers",
130-
# ExpressionAttributeValues={":answers": answers_data}
131-
# )
132-
133-
# existing_rounds = user.get(f"round{answerReq.round}", [])
134-
135-
# if answerReq.domain not in existing_rounds:
136-
# user_table.update_item(
137-
# Key={'uid': email},
138-
# UpdateExpression=f"SET round{answerReq.round} = list_append(if_not_exists(round{answerReq.round}, :empty_list), :new_value)",
139-
# ExpressionAttributeValues={
140-
# ':new_value': [answerReq.domain],
141-
# ':empty_list': []
142-
# }
143-
# )
144-
145-
# return JSONResponse(status_code=200,content=f"Answers for domain '{answerReq.domain}' submitted successfully for round {answerReq.round}.")
146-
147-
# except ClientError as e:
148-
# raise HTTPException(status_code=500, detail=f"DynamoDB error: {str(e)}")
149-
# except Exception as e:
150-
# raise HTTPException(status_code=400, detail=f"Error posting answers: {str(e)}")
127+
domain_table.update_item(
128+
Key={"email": email},
129+
UpdateExpression=f"SET {name} = :answers",
130+
ExpressionAttributeValues={":answers": answers_data}
131+
)
132+
133+
existing_rounds = user.get(f"round{answerReq.round}", [])
134+
135+
if answerReq.domain not in existing_rounds:
136+
user_table.update_item(
137+
Key={'uid': email},
138+
UpdateExpression=f"SET round{answerReq.round} = list_append(if_not_exists(round{answerReq.round}, :empty_list), :new_value)",
139+
ExpressionAttributeValues={
140+
':new_value': [answerReq.domain],
141+
':empty_list': []
142+
}
143+
)
144+
145+
return JSONResponse(status_code=200,content=f"Answers for domain '{answerReq.domain}' submitted successfully for round {answerReq.round}.")
146+
147+
except ClientError as e:
148+
raise HTTPException(status_code=500, detail=f"DynamoDB error: {str(e)}")
149+
except Exception as e:
150+
raise HTTPException(status_code=400, detail=f"Error posting answers: {str(e)}")

0 commit comments

Comments
 (0)