def get_module_students(): #Not Yet implemented. Looks incomplete modulecode = request.form.get('modulecode') session = get_db_session() module = attendance.ModuleAttendance(modulecode, session) mod_students = module.students resp = dict() for student in mod_students: resp[student.id] = dict() resp[student.id]['Name'] = student.name return jsonify(resp) #TODO: Have this returned by a pretty template
def get_student_module_attendance(): req = request.get_json() studentID = req['studentID'] modulecode = req['modulecode'] isoDate = req['SessionDate'] sessiondate = datetime.datetime.fromisoformat(isoDate) session = get_db_session() student_attendance = StudentAttendance(studentID, session) session_attendance = student_attendance.get_session_attendance( modulecode, sessiondate) resp = session_attendance return jsonify( resp ) #TODO: Have this returned by a pretty render_template TODO: MAke the datetime in this response ISO8601 format
def get_student_attendance(): #For all modules studentID = request.form.get('studentID') session = get_db_session() student = attendance.StudentAttendance(studentID, session) student_modules = student.modules student_attendance = dict() #TODO: Include student name and ID in this response for module in student_modules: student_attendance[module.code] = student.get_module_attendance( module.code) return jsonify( student_attendance ) #TODO: Have this returned by a pretty dashboard-like template
def login(): if current_user.is_authenticated: return redirect(url_for('landing.landing')) form = LoginForm() if form.validate_on_submit(): username = form.username.data password = form.password.data session = get_db_session() user = session.query(schema.User).\ filter(schema.User.username == username.lower()).\ one_or_none() if user is None or not user.check_password(password): flash('Invalid username or password') return redirect(url_for('auth.login')) login_user(user, remember=form.remember_me.data) #Not sure if it is necessary to commit/persist after every login next_page = request.args.get('next') if not next_page or url_parse(next_page).netloc != '': next_page = url_for('landing.landing') return redirect(next_page) return render_template('login.html', title='Sign In', form=form)
def set_student_module_attendance(): if request.method == 'POST': req = request.get_json() studentID = req['studentID'] modulecode = req['modulecode'] status = req['status'] sess_date = req[ 'SessionDate'] #Expects YYYY-MM-DDTHH:MM Format (Iso 8601) try: studentID = int(studentID) except ValueError: return "The student ID {} must be a number (integer)".format( studentID) try: sess_date = datetime.datetime.strptime(sess_date, '%Y-%m-%dT%H:%M') except ValueError: return "The date {} is not in the required YYYY-MM-DDTHH:mm format".format( sess_date) if status.lower() == "present": status = True else: status = False session = get_db_session() mod_attendance = attendance.ModuleAttendance(modulecode, session) try: mod_attendance.updateAttendance(studentID, sess_date, present=status) except ValueError: return "The date >>{}<< does not have a class session for the module with code >>{}<<".format( sess_date.isoformat(), modulecode) #redirect to show student's attendance for the module. Preserve the POST parameters return redirect(url_for('api.get_student_module_attendance'), code=307)
def load_user(user_id): session = get_db_session() return session.query( schema.User).filter(schema.User.id == int(user_id)).one_or_none()
def landing(): '''Display landing page for student, staff(instructor) or admin''' session = get_db_session() if current_user.privilege == schema.PrivilegeLevel.student.name: modules = get_module_list(session) #TODO: This function currently gets all modules in the system. Need to modify so that it only gets the modules a student is enrolled to. Nipo challenge?? return render_template('student_dashboard.html',modules = modules) courses = get_course_list(session) return render_template('list_courses.html', courses = courses) elif current_user.privilege == schema.PrivilegeLevel.staff.name: #Use query strings to pass name of module in url and refresh whenever module is changed #The refreshed page will contain dates of the selected module (from query string) #Remember to check that staff member has access to module. #Optionally, use post request and have javascript refresh the page (too much work!!!) #TODO: For each module, only show valid dates. Nipo challenge? modules = current_user.modules dates = [] formatted_dates = [] mod_code = request.args.get('mod_code') session_date = request.args.get('session_date') #Each of the options below needs to set: # 1. selectedModule # 2. dates # 3. selectedDate if mod_code is not None and session_date is None: # module changed mod_attendance = ModuleAttendance(mod_code, session) selectedModule = mod_attendance.module if selectedModule not in current_user.modules: # User not enrolled in this module flash("Invalid module selection") return redirect(url_for('landing.landing')) dates += mod_attendance.dates if len(dates) == 0: flash("selected module has no registered class sessions") return redirect(url_for('landing.landing')) selectedDate = dates[0] elif mod_code is not None and session_date is not None: # Date changed mod_attendance = ModuleAttendance(mod_code, session) selectedModule = mod_attendance.module dates += mod_attendance.dates selectedDate = datetime.datetime.fromisoformat(session_date) if selectedModule not in current_user.modules or selectedDate not in dates: # User not enrolled in this module or module has no session on this date flash("Invalid module or date selection") return redirect(url_for('landing.landing')) else: #Default scenario, fresh login if len(modules) == 0: flash("You are currently not assigned to any modules. Contact admin to correct this. Showing dummy module.") # TODO: Create test module for showing when # staff is not assigned to any modules # modules = [test_module] modules = get_module_list(session) selectedModule = modules[0] dates += ModuleAttendance(selectedModule.code, session).dates selectedDate = dates[0] #move selected options to the top of list to make them #the ones selected by default if selectedDate != dates[0]: dates.remove(selectedDate) dates.insert(0, selectedDate) if selectedModule != modules[0]: modules.remove(selectedModule) modules.insert(0, selectedModule) # raise for date in dates: human_friendly_format = date.strftime('%a %d %b %H:%M') iso_format = date.strftime('%Y-%m-%dT%H:%M') combined_format = {"human friendly format": human_friendly_format, "iso format": iso_format} formatted_dates.append(combined_format) students = selectedModule.students for student in students: student_attendance = StudentAttendance(student.id, session) session_attendance = student_attendance.get_session_attendance(selectedModule.code, selectedDate) if session_attendance['attendance']: student.status= "student-present" else: student.status= "student-absent" return render_template('staff_dashboard.html',\ dates=formatted_dates,students=students,modules=modules) elif current_user.privilege == schema.PrivilegeLevel.admin.name: tables = db.utils.get_tables_data(session) return render_template('admin_dashboard/admin_dashboard.html',\ tables=tables, form=add_entity_forms(), attach_to_module_forms=attach_to_module_forms())
def shutdown_session(exception=None): session = get_db_session() session.close()