def delete(id): user_id = g.user['id'] query = text( "DELETE FROM user_birds WHERE user_id = :user_id AND bird_id = :bird_id" ) query = query.bindparams(user_id=user_id, bird_id=id) get_db().engine.execute(query) return redirect(url_for('favorites.index'))
def test_user_favorites_displayed_in_index(client, auth, app): res = auth.login() with app.app_context(): get_db().engine.execute( "INSERT INTO bird (species_code, common_name, sci_name) VALUES ('bgbrd', 'Big Bird', 'Birdus largus')" ) get_db().engine.execute( "INSERT INTO user_birds (user_id, bird_id) VALUES (1, 1)") response = client.get('/favorites/') assert 'Big Bird' in response.data assert 'Birdus largus' in response.data assert 'bgbrd' in response.data
def app(): db_fd, db_path = tempfile.mkstemp() app = create_app({'TESTING': True, 'DATABASE': db_path}) with app.app_context(): init_db() get_db().engine.execute(text(_data_sql)) yield app os.close(db_fd) os.unlink(db_path)
def test_user_favorites_does_not_store_duplicate_birds(client, auth, app): res = auth.login() with app.app_context(): get_db().engine.execute( "INSERT INTO bird (species_code, common_name, sci_name) VALUES ('bgbrd', 'Big Bird', 'Birdus largus')" ) get_db().engine.execute( 'INSERT INTO user_birds (user_id, bird_id) VALUES (1, 1)') data = {'birds': 'bgbrd/Big Bird/Birdus largus'} client.post('api/v1/favorites', data=data) assert get_db().engine.execute( "SELECT * FROM bird WHERE species_code = 'bgbrd'").fetchall( ).__len__() == 1
def edit(): username = g.user['username'] if request.method == 'GET': email = g.user['email'] return render_template('dashboard/edit.html', username=username, email=email) elif request.method == 'POST': new_username = request.form['username'] new_email = request.form['email'] new_preference = request.form['notify'] query = text('UPDATE birdy_user SET username = :new_username, email = :new_email, notify = :notify WHERE username = :old_username') query = query.bindparams(new_username=new_username, new_email=new_email, old_username=username, notify=new_preference) get_db().engine.execute(query) return redirect(url_for('index'))
def test_user_favorites_delete_removes_bird_user_association( client, auth, app): res = auth.login() with app.app_context(): get_db().engine.execute( "INSERT INTO bird (species_code, common_name, sci_name) VALUES ('bgbrd', 'Big Bird', 'Birdus largus')" ) get_db().engine.execute( 'INSERT INTO user_birds (user_id, bird_id) VALUES (1, 1)') response = client.get('/favorites/') assert 'Big Bird' in response.data client.post('/favorites/1/delete') response = client.get('/favorites/') assert 'Big Bird' not in response.data
def test_user_favorites_show_page_has_recent_sightings(client, auth, app): res = auth.login() with app.app_context(): get_db().engine.execute( "INSERT INTO bird (species_code, common_name, sci_name) VALUES ('cangoo', 'Canada Goose', 'Branta canadensis')" ) get_db().engine.execute( 'INSERT INTO user_birds (user_id, bird_id) VALUES (1, 1)') response = client.get('/favorites/1') assert 'Canada Goose' in response.data assert 'Branta canadensis' in response.data assert 'Scientific Name' in response.data assert 'Observation Date' in response.data assert 'Location' in response.data assert 'How Many' in response.data
def register(): if request.method == 'POST': username = request.form['username'] email = request.form['email'] password = request.form['password'] latitude = str(request.form['latitude']) longitude = str(request.form['longitude']) db = get_db() error = None if not username: error = 'Username is required.' elif not password: error = 'Password is required.' elif db.engine.execute( text('SELECT id FROM birdy_user WHERE username = :username'). bindparams(username=username)).fetchone() is not None: error = 'User {} is already registered.'.format(username) if error is None: query = text( "INSERT INTO birdy_user (username, email, notify, password, latitude, longitude) VALUES (:username, :email, :notify, :password, :latitude, :longitude)" ) query = query.bindparams(username=username, email=email, notify='daily', password=generate_password_hash(password), latitude=latitude, longitude=longitude) db.engine.execute(query) return redirect(url_for('auth.login')) flash(error) return render_template('auth/register.html')
def get_favorite_birds(user_id): query = text("SELECT bird_id FROM user_birds WHERE user_id = :user_id") query = query.bindparams(user_id=user_id) bird_ids = get_db().engine.execute(query).fetchall() if bird_ids == []: return None else: birds = [] for bird_id in bird_ids: query = text("SELECT * FROM bird WHERE id = :bird_id") query = query.bindparams(bird_id=bird_id[0]) bird_info = get_db().engine.execute(query).fetchone() bird = Bird(bird_info[0], bird_info[2], bird_info[3], bird_info[1]) birds.append(bird) return birds
def load_logged_in_user(): user_id = session.get('user_id') if user_id is None: g.user = None else: query = text('SELECT * FROM birdy_user WHERE id = :user_id') query = query.bindparams(user_id=user_id) g.user = get_db().engine.execute(query).fetchone()
def show(id): query = text("SELECT * FROM bird WHERE id = :bird_id") query = query.bindparams(bird_id=id) bird_info = get_db().engine.execute(query).fetchone() bird = Bird(bird_info[0], bird_info[2], bird_info[3], str(bird_info[1])) sightings = EbirdService().get_nearby_sightings_by_species( g.user['latitude'], g.user['longitude'], bird.species_code) return render_template('favorites/show.html', sightings=sightings, bird=bird)
def test_register(client, app): assert client.get('/auth/register').status_code == 200 response = client.post( '/auth/register', data={'username': '******', 'email': 'c', 'password': '******', 'latitude': '33.22', 'longitude': '-55.44'} ) assert 'http://localhost/auth/login' == response.headers['Location'] with app.app_context(): query = text("SELECT * FROM birdy_user WHERE username = :fake") query = query.bindparams(fake='a') assert get_db().engine.execute(query).fetchone() is not None
def create(): bird_info = request.form.getlist('birds[]') user_id = g.user['id'] for info in bird_info: species_code = str(info.split("/")[0]) common_name = info.split("/")[1] sci_name = info.split("/")[2] query = text("SELECT * FROM bird WHERE species_code = :species_code") query = query.bindparams(species_code=species_code) if get_db().engine.execute(query).fetchone() == None: query = text( "INSERT INTO bird (species_code, common_name, sci_name) VALUES (:species_code, :common_name, :sci_name)" ) query = query.bindparams(species_code=species_code, common_name=common_name, sci_name=sci_name) get_db().engine.execute(query) query = text( "SELECT bird.id FROM bird WHERE species_code = :species_code") query = query.bindparams(species_code=species_code) bird_id = get_db().engine.execute(query).fetchone() query = text( "INSERT INTO user_birds (user_id, bird_id) SELECT :user_id, :bird_id WHERE NOT EXISTS (SELECT * FROM user_birds WHERE user_id = :user_id AND bird_id = :bird_id)" ) query = query.bindparams(user_id=int(user_id), bird_id=bird_id[0]) get_db().engine.execute(query) return "favorites added"
def index(): db = get_db() user_id = g.user['id'] latitude = g.user['latitude'] longitude = g.user['longitude'] birds = get_favorite_birds(user_id) if birds == []: return render_template('favorites/index.html', birds=None) else: return render_template('favorites/index.html', birds=birds, lat=float(latitude), lng=float(longitude))
def test_user_can_edit_profile(client, auth, app): res = auth.login() with app.app_context(): query = "SELECT * FROM birdy_user WHERE id = 1" billy = get_db().engine.execute(query).fetchone() assert 1 == billy[0] assert 'daily' == billy[3] assert "billy" == billy[4] assert '*****@*****.**' == billy[5] client.post( '/user/edit', data={'username': '******', 'email': '*****@*****.**', 'notify': 'weekly'} ) query = "SELECT * FROM birdy_user WHERE id = 1" updated_billy = get_db().engine.execute(query).fetchone() assert 1 == updated_billy[0] assert 'weekly' == updated_billy[3] assert "billyRocks" == updated_billy[4] assert "*****@*****.**" == updated_billy[5]
def send_monthly_sightings_emails(): app = create_app() app.config['SQLALCHEMY_DATABASE_URI'] = os.getenv('DATABASE_URL') app.app_context().push() with app.app_context(): query = text( "SELECT id, email, latitude, longitude FROM birdy_user WHERE notify = 'monthly'" ) users = get_db().engine.execute(query).fetchall() for user in users: message = MailGenerator().fav_bird_sightings_message( user[0], user[2], user[3]) if message == "": return "no message to send" else: send_email(user[1], message)
def login(): if request.method == 'POST': username = request.form['username'] password = request.form['password'] db = get_db() error = None query = text('SELECT * FROM birdy_user WHERE username = :username') query = query.bindparams(username=username) user = db.engine.execute(query).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')