def test_validate_number(): # Number input should be considered by value even if passed as a string assert validate_number('12', 15, min=3) == True assert validate_number('12', 3) == False # String input that cannot be coverted to a number should return false assert validate_number('Eleven', 15) == False
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 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)