def random(): db = get_db() definitions = db.execute( 'SELECT d.id, word, definition, created, author_id, username' ' FROM definitions d JOIN user u ON d.author_id = u.id' ' ORDER BY RANDOM() LIMIT 1').fetchall() return render_template('definitions/index.html', definitions_list=definitions)
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 my_words(author_id): db = get_db() definitions = db.execute( 'SELECT d.id, word, definition, created, author_id, username' ' FROM definitions d JOIN user u ON d.author_id = u.id WHERE author_id = ?' ' ORDER BY created DESC', (author_id, )).fetchall() return render_template('definitions/index.html', definitions_list=definitions)
def get_definition(id, check_author=True): definition = get_db().execute( 'SELECT d.id, word, definition, created, author_id, username' ' FROM definitions d JOIN user u ON d.author_id = u.id' ' WHERE d.id = ?', (id, )).fetchone() if definition is None: abort(404, "Definition id {0} doesn't exist.".format(id)) if check_author and definition['author_id'] != g.user['id']: abort(403, "Not permitted to edit this one") return definition
def add(): if request.method == 'POST': word = request.form['word'] definition = request.form['definition'] error = None if not word: error = 'Word is required.' if error is not None: flash(error) else: db = get_db() db.execute( 'INSERT INTO definitions (word, definition, author_id)' 'VALUES (?, ?, ?)', (word, definition, g.user['id'])) db.commit() return redirect(url_for('definitions.index')) return render_template('definitions/add.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 user WHERE username = ?', (username, )).fetchone() if user is None: error = 'Incorrect username.' elif not check_password_hash(user['password'], password): error = 'Incorrect password.' if error is None: session.clear() session['user_id'] = user['id'] return redirect(url_for('index')) flash(error) return render_template('auth/login.html')
def update(id): post = get_definition(id) if request.method == 'POST': word = request.form['word'] definition = request.form['definition'] error = None if not word: error = 'Title is required.' if error is not None: flash(error) else: db = get_db() db.execute( 'UPDATE definitions SET word = ?, definition = ?' ' WHERE id = ?', (word, definition, id)) db.commit() return redirect(url_for('definitions.index')) return render_template('definitions/update.html', post=post)
def register(): if request.method == 'POST': username = request.form['username'] password = request.form['password'] db = get_db() error = None if not username: error = 'Username is required' if not password: error = 'Password is required' elif db.execute('SELECT id FROM user WHERE username = ?', (username, )).fetchone() is not None: error = 'User {} is already registerd.'.format(username) if error is None: db.execute('INSERT INTO user (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 delete(id): get_definition(id) db = get_db() db.execute('DELETE FROM definitions WHERE id = ?', (id, )) db.commit() return redirect(url_for('definitions.index'))