def register(): if request.method == 'POST': username = request.form['username'] password = request.form['password'] person_name = request.form['name'] email = request.form['name'] 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, person_name, email) VALUES (?, ?, ?, ?)', (username, generate_password_hash(password), person_name, email)) 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_user_id_from_email(email): ids = get_db().execute('SELECT id FROM user ' ' WHERE email = ? ', (email, )).fetchone() if ids: return ids['id'] else: return None
def get_location(id, check_user=True): location = get_db().execute( 'SELECT l.location_id, location, lat, lon, person_id, created, person_name' ' FROM location l JOIN user u ON l.person_id = u.id' ' WHERE l.location_id = ?', (id, )).fetchone() if location is None: abort(404, "Location id {0} doesn't exist.".format(id)) if check_user and location['person_id'] != g.user['id']: abort(403) return location
def index(): db = get_db() if g.user and g.user['person_name'] == 'admin': locations = db.execute( 'SELECT l.location_id, location, lat, lon, person_id, created, person_name' ' FROM location l JOIN user u ON l.person_id = u.id ').fetchall() else: locations = db.execute( ' SELECT l.location_id, location, lat, lon, person_id, created, person_name' ' FROM location l JOIN user u ON l.person_id = u.id ' ' WHERE ' 'l.off_the_grid == 0 ' ' AND ' '(person_id, created) in (SELECT person_id, max(created)' 'FROM location GROUP BY person_id)').fetchall() return render_template('locations/index.html', locations=locations)
def update_location(city, country, user_id, location_id, off_the_grid=0, user_id_type='id'): G = Geo(city, country, user_id, edit=True) location = G.geolocate() db = get_db() db.execute( 'UPDATE location SET location = ?, lat = ?, lon = ? ' ' WHERE location_id = ?', (f'{location["city"]}, {location["country"]}', location['lat'], location['lng'], location_id)) db.commit()
def push_location(city, country, user_id, off_the_grid, user_id_type='id'): G = Geo(city, country, user_id) location = G.geolocate() try: db = get_db() db.execute( 'INSERT INTO location (location, lat, lon, person_id, off_the_grid)' ' VALUES (?, ?, ?, ?, ?)', (f'{location["city"]}, {location["country"]}', location['lat'], location['lng'], g.user['id'], off_the_grid)) db.commit() return 1 except: return 0
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('locations.index')) flash(error) return render_template('auth/login.html')
def email_push_location(city, country, user_id, off_the_grid=0, user_id_type='email'): if user_id_type == 'email': user_id = get_user_id_from_email(user_id) if not user_id: return 0, 'no user with that email' G = Geo(city, country, user_id) location = G.geolocate() #try: db = get_db() db.execute( 'INSERT INTO location (location, lat, lon, person_id, off_the_grid)' ' VALUES (?, ?, ?, ?, ?)', (f'{location["city"]}, {location["country"]}', location['lat'], location['lng'], user_id, off_the_grid)) db.commit() return 1