def edit_me(email, data): response = {} try: hod = HOD.query.filter_by(email=email).first() except Exception: raise AppException('Internal Server Error', 500) if not hod: response['success'] = False response['message'] = 'HOD not found' return response, 404 try: hod.name = data['name'] if data['password'] != '': hod.password = data['password'] hod.save() db.session.refresh(hod) except Exception: db.session.rollback() raise AppException('Internal Server Error', 500) response['success'] = True response['message'] = 'Details updated successfully' return response, 200
def create_hod(data): response = {} name = data['name'] email = data['email'] department_code = data['department'] password = data['password'] try: dept = Department.query.filter_by(code=department_code).first() if dept and HOD.query.filter_by(department=dept).count() > 0: response['success'] = False response[ 'message'] = f'HOD for {dept.name} has already been signed up' return response, 423 except Exception: raise AppException('Internal Server Error', 500) try: hod = HOD(name=name, email=email, department_code=department_code, password=password) hod.save() db.session.refresh(hod) except Exception: db.session.rollback() raise AppException('Internal Server Error', 500) response['success'] = True response['message'] = 'New HOD registered successsfully' return response, 201
def verify_registered_courses(reg_no, course_code): response = {} try: student = Student.query.filter_by(reg_no=reg_no).first() except Exception: raise AppException('Internal Server Error', 500) if not student: response['success'] = False response['message'] = 'Student Not Found' return response, 404 try: registered_course = student.registered_courses.filter_by( code=course_code).first() except Exception: raise AppException('Internal Server Error', 500) if not registered_course: response['success'] = False response['message'] = 'Student has not registered for this course' return response, 200 response['success'] = True response['message'] = 'Student has registered for this course' return response, 200
def register_fingerprint(reg_no, data): response = {} try: student = Student.query.filter_by(reg_no=reg_no).first() except Exception: raise AppException('Internal Server Error', 500) if not student: response['success'] = False response['message'] = 'Student Not Found' return response, 404 if student.fingerprint_template: response['success'] = False response[ 'message'] = 'Fingerprint registration can only be done once. Contact Admin' return response, 423 try: student.fingerprint_template = data['template'] student.save() except Exception: db.session.rollback() raise AppException('Internal Server Error', 500) response['success'] = True response['message'] = 'Fingerprint registered successfully' return response, 200
def mark_lecture_attendance(email, course_code): response = {} try: lecturer = Lecturer.query.filter_by(email=email).first() course = Course.query.filter_by(code=course_code).first() except Exception: raise AppException('Internal Server Error', 500) if not lecturer: response['success'] = False response['message'] = 'Lecturer Not Found' return response, 404 if not course: response['success'] = False response['message'] = 'Course Not Found' return response, 404 if not lecturer.is_assigned(course): response['success'] = False response['message'] = 'Lecturer is not assigned to this course' return response, 403 try: lecturer.attend_lecture(course) db.session.commit() except Exception: db.session.rollback() raise AppException('Internal Server Error', 500) response['success'] = True response['message'] = 'Attendance taken for lecturer' return response, 200
def get_me_courses(reg_no, semester): response = {} try: student = Student.query.filter_by(reg_no=reg_no).first() except Exception: raise AppException('Internal Server Error', 500) if not student: response['success'] = False response['message'] = 'Student Not Found' return response, 404 # Select courses based on student's level and current semester # If and only if student has completed registration if not student.reg_complete or not student.fingerprint_template: response['success'] = False response[ 'message'] = 'Student registration is incomplete and thus can not access courses' return response, 423 try: courses = list( filter( lambda c: c.semester.semester == semester and c.level.level == student.level.level, student.department.courses)) except Exception: raise AppException('Internal Server Error', 500) response['success'] = True response['data'] = [course.to_dict for course in courses] return response, 200
def edit_me(reg_no, data): response = {} level = data['level'] department = data['department'] try: student = Student.query.filter_by(reg_no=reg_no).first() except Exception: raise AppException('Internal Server Error', 500) if not student: response['success'] = False response['message'] = 'Student Not Found' return response, 404 if student.reg_complete: response['success'] = False response[ 'message'] = "You cannot update your details anymore. Contact Admin" return response, 423 try: student.level = Level.query.filter_by(level=level).first() student.department = Department.query.filter_by( code=department).first() student.reg_complete = True student.save() except Exception: db.session.rollback() raise AppException('Internal Server Error', 500) response['success'] = True response['message'] = 'Details updated successfully' return response, 200
def get_assigned(email, semester): response = {} try: hod = HOD.query.filter_by(email=email).first() except Exception: raise AppException('Internal Server Error', 500) if not hod: response['success'] = False response['message'] = 'HOD not found' return response, 404 # Filter courses in department by semester courses = list( filter(lambda c: c.semester.semester == semester, hod.department.courses)) assigned_courses = [{ 'course': course.to_dict, 'lecturers': [lecturer.to_dict for lecturer in course.lecturers_assigned.all()] } for course in courses] response['success'] = False response['data'] = assigned_courses return response, 200
def login_admin(data): response = {} email = data['email'] password = data['password'] try: admin = Admin.query.filter_by(email=email).first() except Exception: raise AppException('Internal Server Error', 500) if not admin: response['success'] = False response['message'] = 'Invalid email or password' return response, 401 if not admin.verify_password(password): response['success'] = False response['message'] = 'Invalid email or password' return response, 401 encode_data = {'email': admin.email, 'admin': True, 'entity': 'admin'} token = encode_auth_token(data=encode_data, expiry=datetime.utcnow() + timedelta(days=1)) if not isinstance(token, bytes): response['success'] = False response['message'] = token return response, 500 response['success'] = True response['message'] = 'Logged in successfully' response['x-auth-token'] = token.decode() return response, 200
def wrapper(*args, **kwargs): response = {} auth_token = request.headers.get('x-auth-token') if not auth_token or auth_token is None: response = {'success': False, 'message': 'Please provide a token'} return response, 401 decoded_payload = decode_auth_token(auth_token=auth_token) # Error decoding token if isinstance(decoded_payload, str): response['success'] = False response['message'] = decoded_payload return response, 401 # Check revoked token try: if RevokedToken.check(token=auth_token): response['success'] = False response['message'] = 'Revoked token. Please log in again' return response, 403 except Exception: raise AppException('Internal Server Error. Revoke Check Error', 500) return func(*args, **kwargs, decoded_payload=decoded_payload)
def create_student(data=None): response = {} try: student = Student(**data) student.save() except Exception: db.session.rollback() raise AppException('Internal Server Error', 500) response['success'] = True response['message'] = 'New student successfully registered' return response, 201
def register_courses(reg_no, data): response = {} try: student = Student.query.filter_by(reg_no=reg_no).first() except Exception: raise AppException('Internal Server Error', 500) if not student: response['success'] = False response['message'] = 'Student Not Found' return response, 404 if not student.reg_complete or not student.fingerprint_template: response['success'] = False response[ 'message'] = 'You must complete your registration and thumbprint before registering your courses' return response, 423 if student.has_registered_course: response['success'] = False response[ 'message'] = 'Registration can only be done once. Contact Admin' return response, 423 try: course_codes = data['courses'] for course_code in course_codes: course = Course.query.filter_by(code=course_code).first() student.register_course(course) db.session.commit() student.has_registered_course = True student.save() except Exception: db.session.rollback() raise AppException('Internal Server Error', 500) response['success'] = True response['message'] = 'Courses registered successfully' return response, 200
def take_exam_attendance(reg_no, course_code): response = {} try: student = Student.query.filter_by(reg_no=reg_no).first() course = Course.query.filter_by(code=course_code).first() except Exception: raise AppException('Internal Server Error', 500) if not student: response['success'] = False response['message'] = 'Student not found' return response, 404 if not course: response['success'] = False response['message'] = 'Course not found' return response, 404 if not student.is_registered(course): response['success'] = False response['message'] = 'Student is not registered for this course' return response, 403 if student.exam_attendance_taken(course): response['success'] = False response[ 'message'] = 'Student has already taken exam attendance for this course' return response, 403 try: student.take_exam_attendance(course) db.session.commit() except Exception: db.session.rollback() raise AppException('Internal Server Error', 500) response['success'] = True response['message'] = 'Attendance taken' return response, 200
def take_lecture_attendance(reg_no, course_code, lecturer_id): response = {} try: student = Student.query.filter_by(reg_no=reg_no).first() course = Course.query.filter_by(code=course_code).first() lecturer = Lecturer.query.filter_by(id=lecturer_id).first() except Exception: raise AppException('Internal Server Error', 500) if not student: response['success'] = False response['message'] = 'Student not found' return response, 404 if not course: response['success'] = False response['message'] = 'Course not found' return response, 404 if not lecturer: response['success'] = False response['message'] = 'Lecturer not found' return response, 404 if not student.is_registered(course): response['success'] = False response['message'] = 'Student is not registered for this course' return response, 403 try: student.attend_lecture(course, lecturer) db.session.commit() except Exception: db.session.rollback() raise AppException('Internal Server Error', 500) response['success'] = True response['message'] = 'Attendance taken' return response, 200
def assign_courses(email, data): response = {} try: hod = HOD.query.filter_by(email=email).first() except Exception: raise AppException('Internal Server Error', 500) if not hod: response['success'] = False response['message'] = 'HOD not found' return response, 404 # if hod.has_assigned_courses: # response['success'] = False # response['message'] = 'Unable to assign courses more than once. Contact Admin' # return response, 423 try: for datum in data: lecturer_email = datum['email'] course_codes = datum['courses'] lecturer = Lecturer.query.filter_by( email=lecturer_email).first() for course_code in course_codes: course = Course.query.filter_by(code=course_code).first() lecturer.assign_course(course) db.session.commit() db.session.refresh(lecturer) # hod.has_assigned_courses = True # hod.save() except Exception: db.session.rollback() raise AppException('Internal Server Error', 500) response['success'] = True response['message'] = 'Courses assigned successfully' return response, 200
def get_all(): response = {} try: schools = School.query.order_by(School.code).all() except Exception: raise AppException('Internal Server Error', 500) if not schools: response['success'] = False response['message'] = 'Schools Not Found' return response, 404 response['success'] = True response['data'] = [school.code for school in schools] return response, 200
def logout_student(auth_token): response = {} decoded_payload = decode_auth_token(auth_token=auth_token) # Error decoding error if isinstance(decoded_payload, str): response['success'] = False response['message'] = decoded_payload return response, 401 # Ensure this method logs out only students if decoded_payload.get('reg_no') is None: response['success'] = True response['message'] = 'Unathorized to perform action' return response, 403 # Check revoked token try: if RevokedToken.check(token=auth_token): response['success'] = False response['message'] = 'Revoked token. Please log in again' return response, 403 except Exception: raise AppException('Internal Server Error. Revoke check Error', 500) # Mark token as revoked and logout student try: RevokedToken(token=auth_token).save() except Exception: db.session.rollback() raise AppException('Internal Server Error', 500) response['success'] = True response['message'] = 'Logged out successfully' return response, 200
def get_registered_students(course_code, department_code): response = {} try: registered_students = Course.query.filter_by( code=course_code).first().students_registered except Exception: raise AppException('Internal Server Error', 500) if not registered_students: response['success'] = False response['message'] = f'No student is registered for {course_code}' return response, 404 try: department = Department.query.filter_by( code=department_code).first() students_by_dept = registered_students.filter_by( department=department).all() except Exception: raise AppException('Internal Server Error', 500) if not students_by_dept: response['success'] = False response[ 'message'] = f'No student from {department_code.upper()} is registered for {course_code}' return response, 404 response['success'] = True response['message'] = 'Students fetched successfully' response['data'] = [{ 'id': id + 1, 'reg_no': student.reg_no, 'name': f'{student.firstname} {student.lastname}', 'template': student.fingerprint_template } for id, student in enumerate(students_by_dept)] return response, 200
def get_all_courses(): response = {} try: courses = Course.query.all() except Exception: raise AppException('Internal Server Error', 500) if not courses: response['success'] = False response['message'] = 'No courses found' return response, 404 response['success'] = True response['data'] = [course.to_dict for course in courses] return response, 200
def get_all_students(): response = {} try: students = Student.query.all() except Exception: raise AppException('Internal Server Error', 500) if not students: response['success'] = False response['message'] = 'Students Not Found' return response, 404 response['success'] = True response['data'] = [student.to_dict for student in students] return response, 200
def get_me(reg_no): response = {} try: student = Student.query.filter_by(reg_no=reg_no).first() except Exception: raise AppException('Internal Server Error', 500) if not student: response['success'] = False response['message'] = 'Student Not Found' return response, 404 response['success'] = True response['data'] = student.to_dict return response, 200
def get_all(): response = {} try: departments = Department.query.order_by(Department.code).all() except Exception: raise AppException('Internal Server Error', 500) if not departments: response['success'] = False response['message'] = 'Departments Not Found' return response, 404 response['success'] = True response['data'] = [department.to_dict for department in departments] return response, 200
def get_me(email): response = {} try: hod = HOD.query.filter_by(email=email).first() except Exception: raise AppException('Internal Server Error', 500) if not hod: response['success'] = False response['message'] = 'HOD Not Found' return response, 404 response['success'] = True response['data'] = hod.to_dict return response, 200
def get_registered_courses(reg_no): response = {} try: student = Student.query.filter_by(reg_no=reg_no).first() except Exception: raise AppException('Internal Server Error', 500) if not student: response['success'] = False response['message'] = 'Student Not Found' return response, 404 response['success'] = True response['data'] = [ course.to_dict for course in student.registered_courses.all() ] return response, 200
def get_lecturers(email): response = {} try: hod = HOD.query.filter_by(email=email).first() except Exception: raise AppException('Internal Server Error', 500) if not hod: response['success'] = False response['message'] = 'HOD not found' return response, 404 response['success'] = True response['data'] = [ lecturer.to_dict for lecturer in hod.department.lecturers ] return response, 200
def get_me(email): response = {} try: lecturer = Lecturer.query.filter_by(email=email).first() except Exception: raise AppException('Internal Server Error', 500) if not lecturer: response['success'] = False response['message'] = 'Lecturer Not Found' return response, 404 data = { **lecturer.to_dict, 'assigned_courses': [course.to_dict for course in lecturer.assigned_courses.all()] } response['success'] = True response['data'] = data return response, 200
def get_courses(email, semester): response = {} try: hod = HOD.query.filter_by(email=email).first() except Exception: raise AppException('Internal Server Error', 500) if not hod: response['success'] = False response['message'] = 'HOD not found' return response, 404 # Filter courses in department by semester courses = list( filter(lambda c: c.semester.semester == semester, hod.department.courses)) response['success'] = True response['data'] = [course.to_dict for course in courses] return response, 200
def create_lecturer(data): response = {} name = data['name'] email = data['email'] department_code = data['department'] password = data['password'] try: lecturer = Lecturer(name=name, email=email, department_code=department_code, password=password) lecturer.save() db.session.refresh(lecturer) except Exception: db.session.rollback() raise AppException('Internal Server Error', 500) response['success'] = True response['message'] = 'New Lecturer registered successsfully' return response, 201
def get_departments(school_code): response = {} try: school = School.query.filter_by(code=school_code).first() except Exception: raise AppException('Internal Server Error', 500) if not school: response['success'] = False response['message'] = 'School Not Found' return response, 404 departments = school.departments if not departments: response['success'] = False response['message'] = f'No Departments Not Found for this {school_code}' return response, 404 response['success'] = True response['data'] = [dept.code for dept in departments] return response, 200
def verify(auth_token): response = {} decoded_payload = decode_auth_token(auth_token=auth_token) if isinstance(decoded_payload, str): response['success'] = False response['message'] = decoded_payload return response, 401 try: if RevokedToken.check(token=auth_token): response['success'] = False response['message'] = 'Token revoked' return response, 403 except Exception: raise AppException('Internal Server Error. Revoke Check Error', 500) response['success'] = True response['entity'] = decoded_payload['entity'] return response, 200