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 save(): body_height = request.args.get('body_height', default=None, type=int) body_weight = request.args.get('body_weight', default=None, type=int) skin_tone = request.args.get('skin_tone', default=None, type=str) sex = request.args.get('sex', default=None, type=str) error = None if not body_height: error = 'body_height is required.' if not body_weight: error = 'body_weight is required.' if not skin_tone: error = 'skin_tone is required.' if not sex: error = 'sex is required.' if error is not None: flash(error) else: db = get_db() db.execute( 'INSERT INTO avatar (body_height, body_weight, skin_tone, sex, owner_id)' ' VALUES (?, ?, ?, ?, ?)', (body_height, body_weight, skin_tone, sex, g.user['id'])) db.commit() return redirect(url_for('avatar.index'))
def update(id): avatar = get_avatar(id) body_height = request.form['body_height'] body_weight = request.form['body_weight'] skin_tone = request.form['skin_tone'] sex = request.form['sex'] error = None if not body_height: error = 'body_height is required.' if not body_weight: error = 'body_weight is required.' if not skin_tone: error = 'skin_tone is required.' if not sex: error = 'sex is required.' if error is not None: flash(error) else: db = get_db() db.execute( 'UPDATE avatar SET body_height = ?, body_weight = ?, skin_tone = ?,' 'sex = ? WHERE id = ?', (body_height, body_weight, skin_tone, sex, id) ) db.commit() return redirect(url_for('avatar.index'))
def create(): if request.method == 'POST': body_height = request.form['body_height'] body_weight = request.form['body_weight'] skin_tone = request.form['skin_tone'] sex = request.form['sex'] error = None if not body_height: error = 'body_height is required.' if not body_weight: error = 'body_weight is required.' if not skin_tone: error = 'skin_tone is required.' if not sex: error = 'sex is required.' if error is not None: flash(error) else: db = get_db() db.execute( 'INSERT INTO avatar (body_height, body_weight, skin_tone, sex, owner_id)' ' VALUES (?, ?, ?, ?, ?)', (body_height, body_weight, skin_tone, sex, g.user['id']) ) db.commit() return redirect(url_for('avatar.index')) return render_template('avatar/create.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 index(): db = get_db() avatar = db.execute( 'SELECT p.id, body_height, body_weight, skin_tone, sex, created, owner_id, username' ' FROM avatar p JOIN user u ON p.owner_id = u.id' ' ORDER BY created DESC' ).fetchone() if avatar: return render_template('avatar/index.html', avatar=avatar) else: return render_template('avatar/create.html')
def get_avatar(id, check_author=True): avatar = get_db().execute( 'SELECT p.id, body_height, body_weight, created, owner_id, username' ' FROM avatar p JOIN user u ON p.owner_id = u.id' ' WHERE p.id = ?', (id,) ).fetchone() if avatar is None: abort(404, "avatar id {0} doesn't exist.".format(id)) if check_author and avatar['owner_id'] != g.user['id']: abort(403) return avatar
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 delete(id): get_avatar(id) db = get_db() db.execute('DELETE FROM avatar WHERE id = ?', (id,)) db.commit() return redirect(url_for('avatar.index'))