def update(id): article = get_article(id) if request.method == 'POST': title = request.form['title'] description = request.form['description'] body = request.form['body'] error = None if not title: error = 'Title is required.' if error is not None: flash(error) else: db = get_db() db.execute( 'UPDATE article SET title = ?, description = ?, body = ?' ' WHERE id = ?', (title, description, body, id) ) db.commit() return redirect(url_for('blog.index')) return render_template('blog/update.html', post=article)
def register(): if request.method == 'POST': email = request.form['email'] password = request.form['password'] name = request.form['name'] last_name = request.form['last_name'] institution = request.form['institution'] db = get_db() error = None if not email: error = 'E-mail address is required' elif not password: error = 'Password is required' elif not email or not name or not last_name: error = 'Fill required fields' elif not institution: institution = '' elif db.execute( 'SELECT id FROM user WHERE email = ?', (email,) ).fetchone() is not None: error = 'E-mail address {} is already registered'.format(email) if error is None: db.execute( 'INSERT INTO user (email, password, name, last_name, institution) ' 'VALUES (?,?,?,?,?)', (email, generate_password_hash(password), name, last_name, institution) ) db.commit() return redirect(url_for('auth.login')) flash(error) return render_template('auth/register.html')
def index(): db = get_db() articles = db.execute( 'SELECT article.id, title, description, body, article.created, author_id, name, last_name' ' FROM article JOIN user u ON article.author_id = u.id' ' ORDER BY created DESC' ).fetchall() return render_template('blog/index.html', posts=articles)
def get_users_articles(author_id): articles = get_db().execute( 'SELECT p.id, title, description, body, created, author_id' ' FROM article p JOIN user u ON p.author_id = u.id' ' WHERE p.author_id = ?', (author_id,) ).fetchall() return articles
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_user(id): user = get_db().execute( 'SELECT id, email, name, last_name, institution' ' FROM user' ' WHERE id = ?', (id,) ).fetchone() if user is None: abort(404, "User id {0} doesn't exist.".format(id)) return user
def get_article(id, check_author=True): article = get_db().execute( 'SELECT p.id, title, description, body, created, author_id, name, last_name' ' FROM article p JOIN user u ON p.author_id = u.id' ' WHERE p.id = ?', (id,) ).fetchone() if article is None: abort(404, "Article id {0} doesn't exist.".format(id)) if check_author and article['author_id'] != g.user['id']: abort(403) return article
def create(): if request.method == 'POST': title = request.form['title'] body = request.form['body'] description = request.form['description'] error = None if not title: error = 'Title is required.' if error is not None: flash(error) else: db = get_db() db.execute( 'INSERT INTO article (title, description, body, author_id)' ' VALUES (?, ?, ?, ?)', (title, description, body, g.user['id']) ) db.commit() return redirect(url_for('blog.index')) return render_template('blog/create.html')
def login(): if request.method == 'POST': email = request.form['email'] password = request.form['password'] db = get_db() error = None user = db.execute( 'SELECT * FROM user WHERE email = ?', (email,) ).fetchone() if user is None: error = 'Incorrect e-mail address.' 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 delete(id): get_article(id) db = get_db() db.execute('DELETE FROM article WHERE id = ?', (id,)) db.commit() return redirect(url_for('blog.index'))