def update(id): well = get_well(id) if request.method == "POST": well_id = request.form['well_id'] on_prd_date = request.form['on_prd_date'] tvd = request.form['tvd'] frac_len = request.form['frac_len'] cum_frac_propp = request.form['cum_frac_propp'] bh_long = request.form['bh_long'] bh_lat = request.form['bh_lat'] latrl_len = request.form['latrl_len'] date_days = ( datetime.datetime.strptime(str(on_prd_date), '%Y-%m-%d') - datetime.datetime.strptime(str("2013-5-1"), "%Y-%m-%d")).days + 1 error = None if not well_id: error = 'Well ID is required.' if error is not None: flash(error) else: db = get_db() db.execute( 'UPDATE well SET well_id = ?, on_prd_date = ?, date_days=?, tvd = ?, frac_len=?, cum_frac_propp=?, bh_long=?, bh_lat=?, latrl_len=? WHERE id = ?', (well_id, on_prd_date, date_days, tvd, frac_len, cum_frac_propp, bh_long, bh_lat, latrl_len, id)) db.commit() return redirect(url_for('wells.index')) return render_template('wells/update.html', well=well)
def index(): db = get_db() wells = db.execute( 'SELECT w.id, well_id, on_prd_date, tvd, frac_len, cum_frac_propp, bh_long, bh_lat, latrl_len, entered, author_id, email' ' FROM well w JOIN user u ON w.author_id = u.id ORDER BY entered DESC' ).fetchall() return render_template('wells/index.html', wells=wells)
def register(): if request.method == 'POST': email = request.form['email'] password =request.form['password'] db = get_db() error = None if not email: error = 'Email is required.' elif not password: error = 'Passwork is required.' elif db.execute( 'SELECT id FROM user WHERE email = ?', (email,) ).fetchone() is not None: error = 'User {} is already registered.'.format(email) if error is None: db.execute( 'INSERT INTO user (email, password) VALUES (?, ?)', (email, generate_password_hash(password)) ) db.commit() return redirect(url_for('auth.login')) flash(error) return render_template('auth/register.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 get_well(id, check_author=True): well = get_db().execute( 'SELECT w.id, well_id, on_prd_date, date_days, tvd, frac_len, cum_frac_propp, bh_long, bh_lat, latrl_len, entered, author_id, email' ' FROM well w JOIN user u ON w.author_id = u.id' ' WHERE w.id = ?', (id, )).fetchone() if well is None: abort(404, "well id {0} doesn't exist.".format(id)) if check_author and well['author_id'] != g.user['id']: abort(403) return well
def create(): if request.method == "POST": well_id = request.form['well_id'] on_prd_date = request.form['on_prd_date'] tvd = request.form['tvd'] frac_len = request.form['frac_len'] cum_frac_propp = request.form['cum_frac_propp'] bh_long = request.form['bh_long'] bh_lat = request.form['bh_lat'] latrl_len = request.form['latrl_len'] date_days = ( datetime.datetime.strptime(str(on_prd_date), '%Y-%m-%d') - datetime.datetime.strptime(str("2013-5-1"), "%Y-%m-%d")).days + 1 error = None if not well_id: error = 'Well ID is required.' if not on_prd_date: error = '(Planned) on-production date is required.' if not tvd: error = 'Well TVD is required.' if not frac_len: error = 'Total Frac length is required.' if not cum_frac_propp: error = '(Planned) total frac proppant tonnage is required.' if not bh_long: error = 'Well bottom hole longitude is required.' if not bh_lat: error = 'Well bottom hole latitude is required.' if error is not None: flash(error) else: db = get_db() db.execute( 'INSERT INTO well (well_id, on_prd_date, date_days, tvd, frac_len, cum_frac_propp, bh_long, bh_lat, latrl_len, author_id)' ' VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)', (well_id, on_prd_date, date_days, tvd, frac_len, cum_frac_propp, bh_long, bh_lat, latrl_len, g.user['id'])) db.commit() return redirect(url_for('wells.index')) return render_template('wells/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 email.' 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('wells.index')) flash(error) return render_template('auth/login.html')
def delete(id): get_well(id) db = get_db() db.execute('DELETE FROM well WHERE id = ?', (id, )) db.commit() return redirect(url_for('wells.index'))