def update(id): student = get_students(id) if request.method == 'POST': name = request.form['name'] age = request.form['age'] gender = request.form['gender'] error = None if not name: error = 'Student Name is required.' if error is not None: flash(error) else: db = get_db() db.execute( 'UPDATE student SET name = ?, age = ?, gender=? WHERE id = ?', ( name, age, gender, id, )) db.commit() return redirect(url_for('student.index')) return render_template('student/update.html', student=student)
def login(): if request.method == 'POST': username = request.form['username'] password = request.form['password'] db = get_db() error = None user = db.execute('SELECT * FROM user WHERE username = ?', (username, )).fetchone() if user is None: error = 'Invalid Username.' elif user['password'] != password: error = 'Invalid Password.' if error is None: session.clear() session['user_id'] = user['id'] return redirect(url_for('student.index')) flash(error) return render_template('auth/login.html')
def load_logged_in_user(): user_id = session.get('user_id') if user_id is None: g.user = None else: g.user = get_db().execute('SELECT * FROM user WHERE id = ?', (user_id, )).fetchone()
def get_students(id): student = get_db().execute( 'SELECT id, "S0000" || CAST( id AS TEXT) AS roll_number, name, age, gender, add_date ' ' FROM student WHERE id=?', (id, )).fetchone() if student is None: abort(404, "Post id {0} doesn't exist.".format(id)) return student
def create(): if request.method == 'POST': name = request.form['name'] age = request.form['age'] gender = request.form['gender'] error = None if not name: error = 'Name is required.' if error is not None: flash(error) else: db = get_db() db.execute( 'INSERT INTO student (name, age, gender)' ' VALUES (?, ?, ?)', (name, age, gender)) db.commit() return redirect(url_for('student.index')) return render_template('student/create.html')
def search(): students = [] if request.method == 'GET': pattern = '%' + request.args.get('search') + '%' print(pattern) error = None if not pattern: error = 'Search field cannot be empty.' if error is not None: flash(error) else: db = get_db() students = db.execute( 'SELECT id, "S0000" || CAST( id AS TEXT) AS roll_number, name, age, gender, add_date FROM student' ' WHERE name LIKE ?', (pattern, )).fetchall() print(students) return render_template('student/index.html', students=students) return render_template('student/index.html', students=students)
def adduser(): if request.method == 'POST': username = request.form['username'] password = request.form['password'] db = get_db() error = None if not username: error = 'Username is required.' elif not password: error = 'Password is required.' elif db.execute('SELECT id FROM user WHERE username = ?', (username, )).fetchone() is not None: error = 'User {} is already registered.'.format(username) if error is None: db.execute('INSERT INTO user (username, password) VALUES (?, ?)', (username, password)) db.commit() return redirect(url_for('student.index')) flash(error) return render_template('auth/adduser.html')
def delete(id): get_students(id) db = get_db() db.execute('DELETE FROM student WHERE id = ?', (id, )) db.commit() return redirect(url_for('student.index'))
def index(): db = get_db() students = db.execute( 'SELECT id, "S0000" || CAST( id AS TEXT) AS roll_number, name, age, gender, add_date FROM student' ).fetchall() return render_template('student/index.html', students=students)