async def get_forum_chat(classroom_uid, token): user_enrolled_status = await classroom_controllers.if_user_enrolled( classroom_uid=classroom_uid, user_id=token.user_id) if_user_is_creator = await classroom_controllers.check_user_if_creator( classroom_id=classroom_uid, user_id=token.user_id) if user_enrolled_status or if_user_is_creator: if_forum_exists = await forum_mongo.check_if_forum_exists( classroom_uid=classroom_uid) if if_forum_exists['forum_exists'] == True: get_all_messages_response_dict = await forum_controllers.get_all_messages( classroom_uid=classroom_uid) if 'status' in get_all_messages_response_dict.keys(): '''if status exists then mongo failed''' return StandardResponseBody( get_all_messages_response_dict['status'], get_all_messages_response_dict['message'], token.token_value) else: return StandardResponseBody( True, 'Forum messages have been acquired', token.token_value, {'forum_stuff': get_all_messages_response_dict}) else: return StandardResponseBody(False, 'Forum does not exist', token.token_value) else: return StandardResponseBody(False, 'You are not part of this classroom', token.token_value)
async def unenroll_user(classroom_uid, user_id, token): ''' To check if person unenrolling a student is the owner ''' if_creator_status = await classroom_controllers.check_user_if_creator( classroom_id=classroom_uid, user_id=token.user_id) if if_creator_status == False: return NotOwnerResponseBody(token=token.token_value) ''' To check if the user to be unenrolled by the teacher is enrolled ''' if_user_enrolled_status = await classroom_controllers.if_user_enrolled( classroom_uid=classroom_uid, user_id=user_id) if if_user_enrolled_status == False: return StandardResponseBody( False, 'The user you are trying to unenroll does not exist in this classroom', token.token_value) if if_creator_status == True and if_user_enrolled_status == True: resp = await classroom_controllers.unenroll_user( classroom_uid=classroom_uid, user_id=user_id) if resp == True: return StandardResponseBody( True, 'The user has been unenrolled from the classroom', token.token_value) else: return StandardResponseBody( False, 'The user could not be unenrolled from the classroom', token.token_value) else: return StandardResponseBody( False, 'You shouldnt see this error, but you are not the creator and not enrolled in this classroom', token.token_value)
async def unenroll(classroom_uid, token): ''' To check if the user trying to unenroll, is enrolled first ''' if_user_enrolled_status = await classroom_controllers.if_user_enrolled( classroom_uid=classroom_uid, user_id=token.user_id) if if_user_enrolled_status == False: return StandardResponseBody(False, 'You do not belong to this classroom', token.token_value) if if_user_enrolled_status == True: resp = await classroom_controllers.unenroll_user( classroom_uid=classroom_uid, user_id=token.user_id) if resp == True: return StandardResponseBody( True, 'You have been unenrolled from the classroom', token.token_value) else: return StandardResponseBody( False, 'You could not be unenrolled from the classroom', token.token_value) else: return StandardResponseBody( False, 'You shouldnt see this error, but you are not part of the class, and then not unenrolled, obv', token.token_value)
async def create_forum(classroom_uid, token): classroom_exist_status = await classroom_models.get_classroom_by_uid( uid=classroom_uid) if not classroom_exist_status: return StandardResponseBody(False, 'Create classroom before creating forum', token.token_value) if_user_creator = await classroom_controllers.check_user_if_creator( classroom_id=classroom_uid, user_id=token.user_id) if if_user_creator == True: if_forum_exists = await forum_mongo.check_if_forum_exists(classroom_uid ) if if_forum_exists['forum_exists'] == True: return StandardResponseBody(False, 'Forum already exists', token.token_value) else: forum_creation_status = await forum_controllers.create_forum( classroom_uid) if forum_creation_status == True: return StandardResponseBody(True, 'Forum has been created', token.token_value) else: return StandardResponseBody(True, 'Forum could not be created', token.token_value) else: return NotOwnerResponseBody(token.token_value)
async def delete_forum(classroom_uid, token): classroom_exist_status = await classroom_models.get_classroom_by_uid( uid=classroom_uid) if not classroom_exist_status: return StandardResponseBody(False, 'Classroom does not exist', token.token_value) if_user_creator = await classroom_controllers.check_user_if_creator( classroom_id=classroom_uid, user_id=token.user_id) if if_user_creator: if_forum_exists = await forum_mongo.check_if_forum_exists(classroom_uid ) if if_forum_exists['forum_exists'] != True: return StandardResponseBody(False, 'Forum does not exist', token.token_value) else: forum_deletion_status = await forum_controllers.delete_forum( classroom_uid) if forum_deletion_status: return StandardResponseBody(True, 'Forum has been deleted', token.token_value) else: return StandardResponseBody(True, 'Forum could not be deleted', token.token_value) else: return NotOwnerResponseBody(token.token_value)
async def get_all_announcements(classroom_uid, token): user_enrolled_status = await classroom_controllers.if_user_enrolled( classroom_uid=classroom_uid, user_id=token.user_id) if_user_is_creator = await classroom_controllers.check_user_if_creator( classroom_id=classroom_uid, user_id=token.user_id) if user_enrolled_status == True or if_user_is_creator == True: if_forum_exists = await forum_mongo.check_if_forum_exists( classroom_uid=classroom_uid) if if_forum_exists['forum_exists'] == True: get_all_announcements_response_dict = await announcement_controllers.get_all_announcements( classroom_uid=classroom_uid) if 'status' in get_all_announcements_response_dict.keys(): '''if status exists then mongo failed''' return StandardResponseBody( False, get_all_announcements_response_dict['message'], token.token_value) else: return StandardResponseBody( True, 'Announcements have been acquired', token.token_value, {'forum_stuff': get_all_announcements_response_dict}) else: return StandardResponseBody(False, 'Announcements do not exist', token.token_value) else: return NotOwnerResponseBody(token.token_value)
async def search(token, query, filter): resp = await discover_controllers.search(query, filter) if resp: return StandardResponseBody(True, "Search results", token.token_value, {"results": resp}) return StandardResponseBody(False, "No search results available", token.token_value)
async def create_announcement_pane(classroom_uid, token): classroom_exist_status = await classroom_models.get_classroom_by_uid( uid=classroom_uid) if not classroom_exist_status: return StandardResponseBody( False, 'Create classroom before creating announcement pane', token.token_value) if_user_creator = await classroom_controllers.check_user_if_creator( classroom_id=classroom_uid, user_id=token.user_id) if if_user_creator == True: ''' forum and announcements are in the same mongo database, just separate threads''' if_announcement_pane_exists = await forum_mongo.check_if_forum_exists( classroom_uid) if if_announcement_pane_exists['forum_exists'] == True: return StandardResponseBody(True, 'Announcement Pane already exists', token.token_value) else: announcement_pane_creation_status = await announcement_controllers.create_announcement_pane( classroom_uid) if announcement_pane_creation_status == True: return StandardResponseBody( True, 'Announcement Pane has been created', token.token_value) else: return StandardResponseBody( False, 'Announcement Pane could not be created', token.token_value) else: return NotOwnerResponseBody(token.token_value)
async def update_profile(token, details): res = await user_controllers.update_profile(token.user_id, details) if res: return StandardResponseBody( True, "Profile updated", token.token_value, res ) return StandardResponseBody(False, "Profile not updated")
async def get_user_dashboard(token): user_data = await user_controllers.get_user_dashboard(token.user_id) if user_data: return StandardResponseBody( True, "Details fetched", token.token_value, user_data ) return StandardResponseBody(False, "Details not fetched")
async def post_announcement(classroom_uid, announcement, background_tasks, token): if_user_creator = await classroom_controllers.check_user_if_creator( classroom_id=classroom_uid, user_id=token.user_id) if if_user_creator == True: if_announcement_pane_exists = await forum_mongo.check_if_forum_exists( classroom_uid) if if_announcement_pane_exists['forum_exists'] == True: announcement_email_notif_status = await announcement_controllers.send_notif( classroom_uid=classroom_uid, announcement=announcement, tasks=background_tasks) if announcement_email_notif_status == True: return StandardResponseBody(True, 'Announcement has been posted', token.token_value) else: return StandardResponseBody( False, 'Announcement could not be posted', token.token_value) else: return StandardResponseBody(False, 'Create Announcement Pane', token.token_value) else: return NotOwnerResponseBody(token.token_value)
async def delete_attendance(token, attendance_token, classroom_uid): if_creator_bool = await classroom_controllers.check_user_if_creator( classroom_id=classroom_uid, user_id=token.user_id) if if_creator_bool == True: ''' 1. Delete attendance token document from Mongo 2. Send response ''' response = await attendance_controllers.delete_attendance_token_redis( token=attendance_token) if response: response = await attendance_controllers.delete_attendance_mongo( classroom_uid, attendance_token) if response == True: return StandardResponseBody(True, 'Attendance has been deleted', token.token_value) else: return StandardResponseBody(False, 'Could not delete attendance', token.token_value) else: return NotOwnerResponseBody(token)
async def generate_classroom_entry_code(uid, token): res = await classroom_controllers.generate_classroom_entry_code( token.user_id, uid) if res: return StandardResponseBody(True, "Entry code generated", token.token_value, res) return StandardResponseBody(False, "Sorry! Couldn't generate entry code", token.token_value)
async def get_classroom_enrolled(classroom_uid, token): res = await classroom_controllers.get_classroom_enrolled(classroom_uid) # print(res) if res: return StandardResponseBody(True, "Enrolled students", token.token_value, {"enrolled": res}) else: return StandardResponseBody(False, "Could not retrieve data")
async def update_password(token, details): res = await user_controllers.update_password(token.user_id, details) if res == True: return StandardResponseBody( True, "Password updated", token.token_value ) elif res == "invalid_password": return StandardResponseBody(False, "Invalid current password") return StandardResponseBody(False, "Profile not updated")
async def get_classroom_details(uid, token): res = await classroom_controllers.get_classroom_details(token.user_id, uid) # print(res) if res: return StandardResponseBody(True, "Classroom details retrieved", token.token_value, res) else: return StandardResponseBody(False, "Could not retrieve data", token.token_value)
async def reset_password(reset): res = await user_controllers.reset_password(reset) if res: return StandardResponseBody( True, "Your password has been changed.", token = None ) return StandardResponseBody( False, "Could not reset password.", token = None )
async def refresh_token(token): res = await token_controllers.refresh_token_by_token(token) #print(res) if res: return StandardResponseBody( True, "Token refreshed", res.token_value ) return StandardResponseBody( False, "Token not refreshed" )
async def create_classroom(classroom, token): res, cls = await classroom_controllers.create_class( token, classroom.class_name) if res == True: return StandardResponseBody(True, "Classroom created", token.token_value, cls) elif res == "exists": return StandardResponseBody(False, "Classroom already exists", token.token_value, cls) return StandardResponseBody(False, "Classroom not created")
async def sign_in(user): token = await user_controllers.sign_in(user) if token: #print(res) return StandardResponseBody( True, "Successfully logged in", token.token_value ) return StandardResponseBody( False, "Invalid username or password" )
async def get_any_user_profile_from_username(username, token): if username == '': return StandardResponseBody( False, 'Username cannot be left empty', token.token_value ) user_id = await user_controllers.get_userid_from_username(username = username) if user_id: return await get_any_user_profile_from_user_id(user_id = user_id, token = token) return StandardResponseBody( False, 'Could not get userid from username', token.token_value )
async def get_user_classrooms(token): res = await classroom_controllers.get_user_classrooms(token.user_id) if res == []: return StandardResponseBody(True, "You haven't created any classrooms", token.token_value) elif res: return StandardResponseBody(True, "User classrooms", token.token_value, res) else: return StandardResponseBody(False, "Could not retrieve data", token.token_value)
async def delete_account(password, token, bg): #print(password.password) try: res = await user_controllers.delete_account( password.password, token ) if res: return StandardResponseBody(True, "Your account has been deleted, along with all your data, and classrooms") except Exception as e: print(e) return StandardResponseBody(False, "Error. Could not delete account")
async def change_profile_picture(token, picture): resp = await user_controllers.change_profile_picture(token.user_id, picture) if resp == "deleted": return StandardResponseBody( True, "Profile picture deleted", token.token_value ) elif resp: return StandardResponseBody( True, "Profile picture updated", token.token_value ) return StandardResponseBody(False, "Could not update profile picture")
async def get_classroom_uid_by_entry_code(entry_code, token): res = await classroom_controllers.getClassroomUid(entry_code) if res['status'] == True: return StandardResponseBody( True, "Classroom ID aquired", token.token_value, { "classroom_uid": res['classroom_uid'], "classroom_name": res['classroom_name'] }) else: return StandardResponseBody(False, "Could not get classroom ID", token.token_value)
async def get_user_enrolled(token): res = await classroom_controllers.get_user_enrolled(token.user_id) # print(res) if res == []: return StandardResponseBody(True, "You aren't enrolled in any classroom", token.token_value) elif res: return StandardResponseBody(True, "User Enrolled in", token.token_value, res) else: return StandardResponseBody(False, "Could not retrieve data")
async def sign_up(user, url, bg): res = await user_controllers.sign_up(user, url, bg) if res == True: return StandardResponseBody( True, "Your account has been created" ) elif res == "exists": return StandardResponseBody( False, "An account with that username already exists" ) return StandardResponseBody( False, "Account not created" )
async def get_username_from_user_id(user_uid, token): resp = await user_controllers.get_user_username(uid=user_uid) if resp: return StandardResponseBody( True, 'Username acquired from given user uid', token.token_value, { 'user_uid': user_uid, 'username': resp } ) else: return StandardResponseBody( False, 'Username could not be aquired from given user uid', token.token_value )
async def upload_file(token, classroom_uid, path, file): res = await classroom_controllers.check_user_if_creator(classroom_uid, token.user_id) if res: res2 = await storage_controllers.upload_file(path, file) if res2: return StandardResponseBody( True, "File uploaded", token.token_value ) return StandardResponseBody( False, "File could not be uploaded", token.token_value ) return StandardResponseBody( False, "User not authorized." )
async def delete_file(classroom_uid, path, token): res = await classroom_controllers.check_user_if_creator(classroom_uid, token.user_id) if res: res = await storage_controllers.delete_file(path) if res == True: return StandardResponseBody( True, "File / folder deleted", token.token_value ) return StandardResponseBody( False, "File / folder could not be deleted", token.token_value ) return StandardResponseBody( False, "User not authorized" )