def update_honor(id): honor = get_honor(id) if request.method == 'POST': title = request.form['title'] givenby = request.form['givenby'] 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( 'UPDATE honors SET title = ?, givenby = ?, description = ?' ' WHERE id = ?', (title, givenby, description, id)) db.commit() return redirect(url_for('resume.index')) return render_template('resume/honors/update.html', honor=honor)
def update_workexp(id): experience = get_experience(id) if request.method == 'POST': title = request.form['title'] who = request.form['who'] what = request.form['what'] whenexp = request.form['whenexp'] error = None if not title: error = 'Title is required.' if error is not None: flash(error) else: db = get_db() db.execute( 'UPDATE workexperience SET title = ?, who = ?, what = ?, whenexp = ?' ' WHERE id = ?', (title, who, what, whenexp, id)) db.commit() return redirect(url_for('resume.index')) return render_template('resume/workexp/update.html', experience=experience)
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 index(): db = get_db() posts = db.execute( 'SELECT p.id, title, body, created, author_id, username' ' FROM post p JOIN user u ON p.author_id = u.id' ' ORDER BY created DESC' ).fetchall() return render_template('blog/index.html', posts=posts)
def get_post(id, check_author=True): post = get_db().execute( 'SELECT p.id, title, body, created, author_id, username' ' FROM post p JOIN user u ON p.author_id = u.id' ' WHERE p.id = ?', (id,) ).fetchone() if post is None: abort(404, "Post id {0} doesn't exist.".format(id)) if check_author and post['author_id'] != g.user['id']: abort(403) return post
def get_skill(id, check_author=True): db = get_db() skill = db.execute( 'SELECT s.id, title, description, author_id, username' ' FROM skills s JOIN user u ON s.author_id = u.id' ' WHERE s.id = ?', (id, )).fetchone() if skill is None: abort(404, "Post id {0} doesn't exist.".format(id)) if check_author and skill['author_id'] != g.user['id']: abort(403) return skill
def get_experience(id, check_author=True): db = get_db() experience = db.execute( 'SELECT w.id, title, who, what, whenexp, author_id, username' ' FROM workexperience w JOIN user u ON w.author_id = u.id' ' WHERE w.id = ?', (id, )).fetchone() if experience is None: abort(404, "Post id {0} doesn't exist.".format(id)) if check_author and experience['author_id'] != g.user['id']: abort(403) return experience
def get_honor(id, check_author=True): db = get_db() honor = db.execute( 'SELECT h.id, title, givenby, description, author_id, username' ' FROM honors h JOIN user u ON h.author_id = u.id' ' WHERE h.id = ?', (id, )).fetchone() if honor is None: abort(404, "Post id {0} doesn't exist.".format(id)) if check_author and honor['author_id'] != g.user['id']: abort(403) return honor
def create_skill(): if request.method == 'POST': title = request.form['title'] 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 skills (title, description, author_id)' ' VALUES (?, ?, ?)', (title, description, g.user['id'])) db.commit() return redirect(url_for('resume.index')) return render_template('resume/skills/create.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 create(): if request.method == 'POST': title = request.form['title'] 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( 'INSERT INTO post (title, body, author_id)' ' VALUES (?, ?, ?)', (title, body, g.user['id']) ) db.commit() return redirect(url_for('blog.index')) return render_template('blog/create.html')
def index(): db = get_db() experiences = db.execute( 'SELECT id, created, title, who, what, whenexp, author_id' ' FROM workexperience' ' ORDER BY created DESC').fetchall() skills = db.execute( # 'SELECT id, title, description, author_id' 'SELECT *' ' FROM skills' ' ORDER BY created DESC').fetchall() honors = db.execute('SELECT h.id, title, givenby, description, created' ' FROM honors h' ' ORDER BY created DESC') return render_template('resume/index.html', experiences=experiences, skills=skills, honors=honors)
def update_skill(id): skill = get_skill(id) if request.method == 'POST': title = request.form['title'] 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( 'UPDATE skills SET title = ?, description = ?' ' WHERE id = ?', (title, description, id)) db.commit() return redirect(url_for('resume.index')) return render_template('resume/skills/update.html', skill=skill)
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.' 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, generate_password_hash(password))) db.commit() return redirect(url_for('auth.login')) flash(error) return render_template('auth/register.html')
def create_workexp(): if request.method == 'POST': title = request.form['title'] who = request.form['who'] what = request.form['what'] whenexp = request.form['whenexp'] error = None if not title: error = 'Title is required.' if error is not None: flash(error) else: db = get_db() db.execute( 'INSERT INTO workexperience (title, who, what, whenexp, author_id)' ' VALUES (?, ?, ?, ?, ?)', (title, who, what, whenexp, g.user['id'])) db.commit() return redirect(url_for('resume.index')) return render_template('resume/workexp/create.html')
def update(id): post = get_post(id) if request.method == 'POST': title = request.form['title'] 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 post SET title = ?, body = ?' ' WHERE id = ?', (title, body, id) ) db.commit() return redirect(url_for('blog.index')) return render_template('blog/update.html', post=post)
def delete_honor(id): get_honor(id) db = get_db() db.execute('DELETE FROM honors WHERE id = ?', (id, )) db.commit() return redirect(url_for('resume.index'))
def delete_workexp(id): get_experience(id) db = get_db() db.execute('DELETE FROM post WHERE id = ?', (id, )) db.commit() return redirect(url_for('resume.index'))
def delete(id): get_post(id) db = get_db() db.execute('DELETE FROM post WHERE id = ?', (id,)) db.commit() return redirect(url_for('blog.index'))