def submit_assignments(): """Finalize edits on an assignment and update the database""" if request.method == 'POST': # Grab all the necessary form data name = request.form['name'] desc = request.form['description'] points = request.form['points'] id = request.form['submit'] # Validate all of the submitted data if (validate(id, 'assignments') and validate_text(name, 50) and validate_text(desc, 300) and validate_number(points, 100000)): # If validation passes, update the database with db.get_db() as con: with con.cursor() as cur: cur.execute( """ UPDATE assignments SET name = %s, description = %s, points = %s WHERE id = %s """, (name, desc, points, id)) else: # If validation fails, prepare an error to be shown to the user flash('Something went wrong.') return redirect(url_for('teacher.assignments'))
def create(): """Create a class with user input as data""" if request.method == "POST": # Requests tags with 'code', 'name', 'major', and 'description' in form class_code = request.form['code'] class_name = request.form['name'] class_subject = request.form['major'] class_description = request.form['description'] if ( validate_number(class_code, 999) and validate_text(class_name, 50) and validate_text(class_subject, 5) and validate_text(class_description, 150) ): # Inserts data into database if all validation passes with db.get_db() as con: with con.cursor() as cur: cur.execute("INSERT INTO courses(course_code, course_name, major, description, teacher_id ) VALUES(%s, %s, %s, %s, %s)", (class_code, class_name, class_subject, class_description, g.user['id'], ) ) return redirect(url_for('teacher.courses')) else: # If validation fails, prepare an error to be shown to the user flash("Something went wrong.") return render_template('layouts/teacher/courses/course-creation.html')
def session_submit(): """Finalize session creation with extra data and end session creation state""" if request.method == 'POST': if session.get('class_session'): session_name = request.form['session_name'] meeting_days = request.form['meeting_days'] meeting_place = request.form['meeting_place'] meeting_time = request.form['meeting_time'] # validate length of text input before proceeding if (validate_text(session_name, 1) and validate_text(meeting_days, 6) and validate_text(meeting_place, 10) and validate_text(meeting_time, 11)): # if all validation passes, open a DB connection with db.get_db() as con: with con.cursor() as cur: cur.execute( """ UPDATE sessions SET session_name = %s, meeting_days = %s, meeting_place = %s, meeting_time = %s WHERE id = %s """, (session_name, meeting_days, meeting_place, meeting_time, session['class_session'])) return redirect(url_for('teacher.sessions')) # if any validation fails, store an error and continue to redirect else: flash('Something went wrong. Editing canceled.') # Remove keys related to session creation state from the global session session.pop('class_session', None) session.pop('course_id', None) session.pop('edit', None) if not session.get('edit'): return redirect(url_for('teacher.make_session')) else: return redirect(url_for('teacher.session_edit'))
def course_edit(id): """Update course information using user input as data""" if validate(id, 'courses'): if request.method == "POST": class_code = request.form['code'] class_name = request.form['name'] class_subject = request.form['major'] class_description = request.form['description'] if ( validate_text(class_code, 10) and validate_text(class_name, 50) and validate_text(class_subject, 5) and validate_text(class_description, 150) ): # If all data is successfully validated, update the DB with db.get_db() as con: with con.cursor() as cur: cur.execute(""" UPDATE courses SET course_code = %s, course_name = %s , major= %s, description= %s, teacher_id= %s WHERE id = %s """, (class_code, class_name, class_subject, class_description, g.user['id'], id) ) return redirect(url_for('teacher.courses')) else: # If validation fails, prepare an error to be shown to the user flash("Something went wrong.") # Grab the current course information so the user knows what they're changing with db.get_db() as con: with con.cursor() as cur: cur.execute(""" SELECT * FROM courses WHERE id = %s """, (id,)) course = cur.fetchone() return render_template('layouts/teacher/courses/edit-course.html', course=course) return redirect(url_for('teacher.courses'))
def create_assignments(): """Display a form for creating new assignments and create them using user input""" if request.method == 'POST': # Collect the necessary form data name = request.form['name'] desc = request.form['description'] points = request.form['points'] course = request.form['course'] # Validate the collected data if (validate_text(name, 50) and validate_text(desc, 300) and validate_number(points, 100000) and validate(course, 'courses')): # If validation succeeds, create a new record in the database with db.get_db() as con: with con.cursor() as cur: cur.execute( """ INSERT INTO assignments (name, description, points, course_id) VALUES (%s, %s, %s, %s) """, (name, desc, points, course)) return redirect(url_for('teacher.assignments')) else: # If validation fails, prepare an error to be shown to the user flash("Something went wrong.") # Grab all of the logged in teacher's course information for select input with db.get_db() as con: with con.cursor() as cur: cur.execute( """ SELECT * FROM courses WHERE teacher_id = %s """, (g.user['id'], )) courses = cur.fetchall() return render_template( 'layouts/teacher/assignments/create-assignments.html', courses=courses)
def test_validate_text(): # String input should be considered based on given max length length assert validate_text('Some text', 20) == True assert validate_text('Some text', 4) == False