def test_student_registration(): students_model.Student(uni='one').destroy() students_model.Student(uni='two').destroy() students_model.Student( **student_user_data).get_or_create().register_as_student(uni='one') ta = students_model.Student(**ta_user_data).get_or_create() with pytest.raises(students_model.DuplicateUNIException): ta.register_as_student(uni='one') with pytest.raises(ValueError, message='Students must have UNIs'): ta.register_as_student(uni='') with pytest.raises(ValueError, message='Students must have UNIs'): ta.register_as_student() ta.register_as_student(uni='two')
def register(): register_as = request.form['register_as'] user = request.user_models['user'] if register_as == 'teacher': teachers_model.Teacher(id=user.get_id()).register_as_teacher() elif register_as == 'student': uni = request.form['uni'] students_model.Student(id=user.get_id()).register_as_student(uni=uni) return flask.redirect(url_for('home'))
def add_student_to_course(course, **kwargs): if 'unis' in request.form and request.form['unis'] != '': unis = request.form['unis'].split('\n') bulk_add_student_to_course(unis, course) elif 'uni' not in request.form or not request.form['uni']: raise ValueError('Must include UNI') else: uni = request.form['uni'] student = students_model.Student(uni=uni) course.add_student(student) return flask.redirect(request.referrer or url_for('home'))
def manage_session(): # want to go through oauth flow for this route specifically # not get stuck in redirect loop if request.path == '/oauth/callback': return request.user_models = {} user_id = flask.session.get('user_id', None) if user_id is not None: user = users_model.User(id=user_id) request.user_models['user'] = user if user.is_teacher(): request.user_models['teacher'] = teachers_model.Teacher(id=user_id) if user.is_student(): request.user_models['student'] = students_model.Student(id=user_id) request.user_models['ta'] = request.user_models['student'].as_TA()
def create_common_context(): # register student and TA as students student = students_model.Student( **student_user_data).get_or_create().register_as_student( uni='student1') ta = tas_model.TA(**ta_user_data).get_or_create().register_as_student( uni='student2') # register teacher as teacher teacher = teachers_model.Teacher( **teacher_user_data).get_or_create().register_as_teacher() # create course course = teacher.add_course('Course 1') return {'teacher': teacher, 'student': student, 'ta': ta, 'course': course}
def convert_params(endpoint, values): if not values: return if 'course_id' in values: course = courses_model.Course(id=values['course_id']) if not course.fetched: raise ValueError('Course does not exist') values['course'] = course if 'student_id' in values: student = students_model.Student(id=values['student_id']) if not student.fetched: raise ValueError('Student does not exist') values['student'] = student if 'ta_id' in values: ta = tas_model.TA(id=values['ta_id']) if not ta.fetched: raise ValueError('TA does not exist') values['ta'] = ta
def bulk_add_student_to_course(unis, course): """Add unis to course. Unis should already be split into a list. Will gracefully issue warning on invalid unis and continue processing rest. Args: unis (list of strings): List of unis of students to add. course (Course): Course to add students to. """ for uni in unis: uni = uni.strip('\r') if not uni: continue student = students_model.Student(uni=uni) if not student.fetched: flask.session['messages'].append({ 'type': 'warning', 'message': 'No student with UNI ' + uni + ' exists' }) else: course.add_student(student)
def test_attendance_taking(): with common_context() as context: context['student_ta'] = students_model.Student( **other_user_data).get_or_create().register_as_student(uni='oooh') student_ta = context['student_ta'] course = context['course'] ta = context['ta'] student = context['student'] course.add_TA(ta) course.add_student(student) course.add_TA(student_ta) course.add_student(student_ta) secret = course.open_session() assert course.sign_student_in(student, secret), ( 'Sign in failed for student with secret gleaned from course.open_session()' ) assert len(course.get_attendance_records()) == 1, ( 'Signed in student but got {} course attendance records'.format( len(course.get_attendance_records()))) assert len(course.get_attendance_records(student=student)) == 1, ( 'Signed in student but got {} student attendance records'.format( len(course.get_attendance_records(student=student)))) assert course.currently_signed_in(student) and student.is_signed_into( course), ('Student not signed into course after signing in') assert ta.sign_in(course, secret), ( 'Sign in failed for TA with secret gleaned from course.open_session()' ) assert len(course.get_attendance_records()) == 2, ( 'Signed in TA but got {} course attendance records'.format( len(course.get_attendance_records()))) assert len(course.get_attendance_records(ta=ta)) == 1, ( 'Signed in TA but got {} TA attendance records'.format( len(course.get_attendance_records(ta=ta)))) assert course.currently_signed_in(ta) and ta.is_signed_into(course), ( 'TA not signed into course after signing in') assert student_ta.sign_in(course, secret), ( 'Sign in failed for Student-TA with secret gleaned from course.open_session()' ) assert len(course.get_attendance_records()) == 3, ( 'Signed in Student-TA but got {} course attendance records'.format( len(course.get_attendance_records()))) assert len(course.get_attendance_records(ta=student_ta)) == 1, ( 'Signed in Student-TA but got {} Student-TA attendance records'. format(len(course.get_attendance_records(student=student_ta)))) assert course.currently_signed_in( student_ta) and student_ta.is_signed_into(course), ( 'Student-TA not signed into course after signing in') course.close_session() with pytest.raises(courses_model.CourseNotTakingAttendance): student.sign_in(course, secret) with pytest.raises(courses_model.CourseNotTakingAttendance): course.sign_student_in(ta, student) with pytest.raises(courses_model.CourseNotTakingAttendance): course.sign_student_in(student_ta, student) assert len(course.get_attendance_records()) == 3, ( 'Closing attendance window changed number of attendance records to {}' .format(len(course.get_attendance_records()))) assert len(course.get_attendance_records(ta=ta)) == 1, ( 'Closing attendance window changed number of TA attendance records to {}' .format(len(course.get_attendance_records(ta=ta)))) assert len(course.get_attendance_records(student=student)) == 1, ( 'Closing attendance window changed number of Student attendance records to {}' .format(len(course.get_attendance_records(student=student)))) assert len(course.get_attendance_records( student=student_ta)) == 1, 'Closing attendance \ window changed number of Student-TA attendance records to {}'.format( len(course.get_attendance_records(student=student_ta))) course.open_session() assert len(course.get_attendance_records()) == 3, ( 'Reopening attendance window changed number of attendance records to {}' .format(len(course.get_attendance_records()))) assert len(course.get_attendance_records(ta=ta)) == 1, ( 'Reopening attendance window changed number of TA attendance records to {}' .format(len(course.get_attendance_records(ta=ta)))) assert len(course.get_attendance_records( student=student)) == 1, 'Reopening attendance \ window changed number of Student attendance records to {}'.format( len(course.get_attendance_records(student=student))) assert len(course.get_attendance_records( student=student_ta)) == 1, 'Reopening attendance \ window changed number of Student-TA attendance records to {}'.format( len(course.get_attendance_records(student=student_ta))) assert ta.sign_in(course), 'Sign in failed for TA with no secret' assert len(course.get_attendance_records()) == 4, ( 'TA sign in without secret did not add to course attendance records' ) assert len(course.get_attendance_records(ta=ta)) == 2, ( 'TA sign in without secret did not add to TA attendance records') assert not student.sign_in( course), 'Sign in succeeded for Student with no secret' assert len(course.get_attendance_records()) == 4, ( 'Student sign in without secret changed course attendance records to' .format(len(course.get_attendance_records()))) assert len(course.get_attendance_records(student=student)) == 1, ( 'Student sign in without secret changed student attendance records to {}' .format(len(course.get_attendance_records(student=student)))) assert not students_model.Student(**student_user_data).fetched, ( "User with id \'student1\' still exists after context destruction.") assert not students_model.Student(**other_user_data).fetched, ( "User with id \'oooh\' still exists after context destruction.")