def update_attendance(meeting_doc: MeetingDocument, attendance: StudentMeetingAttendance): st_query = StudentDocument.objects(id=attendance.student_id) if len(st_query) < 1: print( "ERROR: update_student_attendance, could not find Student Document" ) else: student = st_query[0] student.meetings_registered[str( attendance.meeting_id)] = attendance.attended update_count = 1 if attendance.attended else -1 update_meeting_count(student, meeting_doc.session_level, attended=update_count) student.save() index = student_meeting_index(meeting_doc, attendance.student_id) if index is not None: meeting_doc.students[index]["attended"] = attendance.attended meeting_doc.save() else: print( "ERROR: update_student_attendance, student not registered for meeting" )
def remove_student_from_meeting( meeting_doc: MeetingDocument, student_id: PydanticObjectId ): index = student_meeting_index(meeting_doc, student_id) if index is not None: del meeting_doc.students[index] meeting_doc.save() else: print("ERROR: remove_student_from_meeting student not found in MeetingDocument")
def test_delete_meeting(mock_database): pre_save_meetings() meeting = MeetingDocument.objects()[0] id = meeting.uuid response = client.delete("/admin/delete_meeting", json={"meeting_id": f"{id}"}) assert response.status_code == 200 meeting = MeetingDocument.objects(uuid=id) assert len(meeting) == 0
async def get_meetings_by_filter( search: MeetingSearchModel, token_data: TokenData = Depends(get_student_token_data) ): current_user = get_current_user_doc(token_data) if not current_user: print("This account has no profile!") raise HTTPException( status_code=status.HTTP_400_BAD_REQUEST, detail="This account has no profile", ) current_students = current_user.students # TODO: Use the dates field from MeetingSearchModel meetings = [] try: for level in search.session_levels: for meeting in MeetingDocument.objects(session_level=level): registrations = meeting.students meeting_registrations = generate_meeting_registrations( registrations, current_students ) meeting_info = meeting.student_dict() meeting_info["registrations"] = [ meeting_registrations[sid] for sid in meeting_registrations.keys() ] meetings.append(meeting_info) except Exception as e: print("Exception type:", type(e)) return {"details": "Problem in get_meetings_by_filter"} return meetings
def send_new_meeting_email( background_task: BackgroundTasks, meeting_id: str, token_data: TokenData = Depends(get_admin_token_data), ): meeting = MeetingDocument.objects(id=meeting_id).first() students = StudentProfileDocument.objects( mailing_lists__in=meeting.session_level) emails = [] for student in students: emails.append(student.email) body = ( """ <html> <body> <p>A new meeting has been posted to the Tucson Math Circle website</p> """ + f"<p>Date: {meeting.date_and_time.date}" + f"<br>Time: {meeting.date_and_time.time}-{create_meeting.duration.time}" + f"<br>Topic: {meeting.topic}" + f"<br>Zoom Link: {meeting.zoom_link}" + f"<br>Zoom Password: {meeting.password}" + f"<br>Miro Link: {meeting.miro_link}" + f"<br>Session Level: {meeting.session_level}</p>" + """ </body> </html> """) new_email = EmailSchema(receivers=emails, subject="New meeting Published", body=body) background_task.add_task(email_handler, new_email) return JSONResponse(status_code=200, content={"message": "email has been sent"})
async def update_student_meeting( meeting_id: UUID4, student_first: str, student_last: str, token_data: TokenData = Depends(get_current_token_data), ): current_user = get_current_user_doc(token_data) try: meeting = MeetingDocument.objects(uuid=meeting_id)[0] except Exception: return {"details": "could not find meeting form id"} add_student = {} for student in current_user.students: if student_first == student.first_name and student_last == student.last_name: add_student = student add = StudentMeetingInfo( first_name=add_student["first_name"], last_name=add_student["last_name"], email=current_user.email, guardians=current_user.guardians, ) meeting.students.append(add.dict()) meeting.save() return {"details": "Student added to meeting list"}
async def update_student( registration: StudentMeetingRegistration, token_data: TokenData = Depends(get_student_token_data), ): current_user = get_current_user_doc(token_data) # FIXME: This assumes that the student id given in `registration` # is a valid student id for this account try: student = StudentDocument.objects(id=registration.student_id)[0] except Exception: raise HTTPException( status_code=status.HTTP_400_BAD_REQUEST, detail="Invalid student id", ) try: meeting_doc = MeetingDocument.objects(uuid=registration.meeting_id)[0] except Exception: raise HTTPException( status_code=status.HTTP_400_BAD_REQUEST, detail="Invalid meeting id", ) meeting_id = str(registration.meeting_id) if not registration.registered: remove_student_from_meeting(meeting_doc, registration.student_id) # Update StudentDocument `meetings_registered` to reflect changes if meeting_id in student.meetings_registered: # remove registration del student.meetings_registered[meeting_id] # decrement registered count update_meeting_count(student, meeting_doc.session_level, registered=-1) student.save() return { "details": f"Student with id {registration.student_id} removed from meeting list" } # otherwise add student to meeting student_info = StudentMeetingInfo( student_id=registration.student_id, email=current_user.email, guardians=current_user.guardians, account_uuid=token_data.id, ) meeting_doc.students.append(student_info.dict()) meeting_doc.save() # Update StudentDocument `meetings_registered` if meeting_id not in student.meetings_registered: # add the registration student.meetings_registered[meeting_id] = False # increment registered count update_meeting_count(student, meeting_doc.session_level, registered=1) student.save() return { "details": f"Student with id {registration.student_id} added to meeting list" }
async def get_meeting_by_id( meeting_id: UUID4, token_data: TokenData = Depends(get_current_token_data)): try: meeting = MeetingDocument.objects(uuid=meeting_id)[0] except Exception: return {"details": "Error getting meeting"} if meeting is None: return {"details": "could not find meeting form id"} return meeting.dict()
async def get_all_meetings( token_data: TokenData = Depends(get_current_token_data)): try: meetings = MeetingDocument.objects() except Exception: return {"details": "Error finding meeting"} ret_meetings = [] for meeting in meetings: ret_meetings.append(meeting.dict()) return ret_meetings
async def get_meetings_by_filter( filters: list, token_data: TokenData = Depends(get_current_token_data)): meeting_list = [] try: for filter in filters: meetings = MeetingDocument.objects(session_level=filter) for meeting in meetings: meeting_list.append(meeting.dict()) except Exception: return {"details": "Error finding meeting"} return meeting_list
def test_update_student_for_meeting(mock_database): pre_save_user_auths() pre_save_profile_docs() pre_save_meetings() response = client2.post( "/token", data={ "username": "******", "password": "******" }, ) student_doc = StudentProfileDocument.objects( email="*****@*****.**")[0] student_id = student_doc.students[0].id meeting = MeetingDocument.objects()[0] json = response.json() access_token = json["access_token"] client.post( "/student/update_student_for_meeting", json=StudentMeetingRegistration( meeting_id=meeting.uuid, student_id=student_id, registered=True, ).dict(), headers={"Authorization": f"Bearer {access_token}"}, ) meetings = MeetingDocument.objects(uuid=meeting.uuid)[0] assert len(meetings.students) == 1 assert meetings.students[0]["student_id"] == str(student_id) client.post( "/student/update_student_for_meeting", json=StudentMeetingRegistration( meeting_id=meeting.uuid, student_id=student_id, registered=False, ).dict(), headers={"Authorization": f"Bearer {access_token}"}, ) meetings = MeetingDocument.objects(uuid=meeting.uuid)[0] assert len(meetings.students) == 0
def test_update_meeting(mock_database): pre_save_meetings() meeting = MeetingDocument.objects()[0] id = meeting.uuid response = client2.post("/token", data={ "username": "******", "password": "******" }) json = response.json() access_token = json["access_token"] update = UpdateMeeting(**meeting1.dict(), meeting_id=id) response = client.put( "/admin/update_meeting", json=update.dict(), headers={"Authorization": f"Bearer {access_token}"}, ) assert response.status_code == 200 meeting = MeetingDocument.objects(uuid=id)[0] assert meeting.miro_link == "test_miro_link" assert meeting.topic == "test_topic" assert meeting.zoom_link == "test_zoom_link"
async def update_student_attendance( attendance: StudentMeetingAttendance, token_data: TokenData = Depends(get_admin_token_data), ): try: meeting_doc = MeetingDocument.objects(uuid=attendance.meeting_id)[0] except Exception: return "Could not find the meeting" update_attendance(meeting_doc, attendance) return { "details": f"Updated attendance for student id {attendance.student_id}" }
async def get_meetings_by_filter( search: MeetingSearchModel, token_data: TokenData = Depends(get_admin_token_data)): # TODO: Use the dates field from MeetingSearchModel meetings = [] try: for level in search.session_levels: for meeting in MeetingDocument.objects(session_level=level): meetings.append(meeting.admin_dict()) except Exception as e: print(e) return {"details": "Error finding meeting"} return meetings
def test_create_meeting(mock_database): response = client2.post("/token", data={ "username": "******", "password": "******" }) json = response.json() access_token = json["access_token"] response = client.post( "/admin/create_meeting", json=test_meeting_model.dict(), headers={"Authorization": f"Bearer {access_token}"}, ) meeting = MeetingDocument.objects() json = response.json() assert len(meeting) == 1 assert json["topic"] == "test_topic"
def test_get_meetings(mock_database): pre_save_user_auths() pre_save_profile_docs() pre_save_meetings() response = client2.post( "/token", data={ "username": "******", "password": "******" }, ) json = response.json() access_token = json["access_token"] client.post( "/student/get_meetings", json=MeetingSearchModel( session_levels=[SessionLevel("junior_a")]).dict(), headers={"Authorization": f"Bearer {access_token}"}, ) meetings = MeetingDocument.objects() assert len(meetings) == 3
async def update_meeting( update_meeting_model: UpdateMeeting, token_data: TokenData = Depends(get_admin_token_data), ): meeting_doc = None try: meeting_doc = MeetingDocument.objects( uuid=update_meeting_model.meeting_id)[0] except Exception: print("Could not find the meeting") meeting_doc.date_and_time = update_meeting_model.date_and_time meeting_doc.duration = update_meeting_model.duration meeting_doc.zoom_link = update_meeting_model.zoom_link meeting_doc.session_level = update_meeting_model.session_level meeting_doc.topic = update_meeting_model.topic meeting_doc.miro_link = update_meeting_model.miro_link meeting_doc.coordinator_notes = update_meeting_model.coordinator_notes meeting_doc.student_notes = update_meeting_model.student_notes meeting_doc.materials_uploaded = update_meeting_model.materials_uploaded meeting_doc.materials_object_name = update_meeting_model.materials_object_name meeting_doc.save() return meeting_doc.admin_dict()
def test_update_student_attendance(mock_database): pre_save_user_auths() pre_save_profile_docs() pre_save_meetings() response = client2.post("/token", data={ "username": "******", "password": "******" }) meeting_id = MeetingDocument.objects().first().uuid json = response.json() access_token = json["access_token"] student_id = add_student_to_meeting() response = client.put( "/admin/update_student_attendance", json=StudentMeetingAttendance(meeting_id=meeting_id, student_id=student_id, attended=True).dict(), headers={"Authorization": f"Bearer {access_token}"}, ) assert response.status_code == 200 student = StudentDocument.objects(id=student_id).first() assert student.meeting_counts["junior_a"]["attended"] == 1
async def get_meeting_material_url( meeting_uuid: UUID4, token_data: TokenData = Depends(get_student_token_data) ): meeting_query = MeetingDocument.objects(uuid=meeting_uuid) if len(meeting_query) == 0: raise HTTPException( status_code=status.HTTP_400_BAD_REQUEST, detail=f"Could not find meeting with uuid {meeting_uuid}", ) meeting = meeting_query[0] object_name = meeting.materials_object_name if object_name is None: raise HTTPException( status_code=status.HTTP_400_BAD_REQUEST, detail="Meeting does not have a materials_object_name", ) response = create_presigned_url(object_name) if response is not None: return response return {"details": "Could not generate presigned url"}
def add_student_to_meeting(): response = client2.post( "/token", data={ "username": "******", "password": "******" }, ) student_doc = StudentProfileDocument.objects( email="*****@*****.**")[0] student_id = student_doc.students[0].id meeting = MeetingDocument.objects()[0] json = response.json() access_token = json["access_token"] client.post( "/student/update_student_for_meeting", json=StudentMeetingRegistration( meeting_id=meeting.uuid, student_id=student_id, registered=True, ).dict(), headers={"Authorization": f"Bearer {access_token}"}, ) return student_id
async def delete_meeting(meeting: MeetingIdModel): try: meeting_doc = MeetingDocument.objects(uuid=meeting.meeting_id)[0] except Exception: return "Could not find the meeting" meeting_doc.delete()