def delete(): # reading the id to be deleted from request username = request.form['delete_user'] # getting the db connection and # deleting the user db = get_db() db.execute('DELETE FROM users WHERE username = ?', (username,)) db.commit() return redirect(url_for('admin.index'))
def load_logged_in_user(): """ loading the user details if the user is logged""" user_id = session.get('user_id') if not user_id: g.user = None else: g.user = get_db().execute('SELECT * FROM users WHERE id = ?', (user_id, )).fetchone()
def index(): db = get_db() if g.user is None: return redirect(url_for('auth.login')) else: users = db.execute( 'SELECT id, username FROM users' ).fetchall() return render_template('home/index.html', users=users)
def register(): if request.method == 'POST': username = request.form['username'] password = request.form['password'] db = get_db() error = None if db.execute('SELECT id FROM users WHERE username = ?', (username, )).fetchone() is not None: error = f'User {username} already exists.' if error is None: db.execute('INSERT INTO users (username, password) VALUES (?, ?)', (username, generate_password_hash(password))) db.commit() return redirect(url_for('auth.login')) flash(error) return render_template('auth/register.html')
def login(): if request.method == 'POST': username = request.form['username'] password = request.form['password'] db = get_db() error = None user = db.execute('SELECT * FROM users WHERE username = ? ', (username, )).fetchone() # verifying the user and password if user is None or not check_password_hash(user['password'], password): error = 'Incorrect username or password.' flash(error) else: session.clear() session['user_id'] = user['id'] # returning userid to verify login return redirect(url_for('index')) return render_template('auth/login.html')