def valid_password(email, password): """ tests if the given password given matches the hash in the db for the given user """ login_qry = "SELECT hash FROM users WHERE email = ?" password_rs = db.get_rs(login_qry, (email, )) print(password_rs) print(type(password_rs)) if password_rs: hashed = db.get_rs(login_qry, (email, ))[0] if secrets.checkpw(password, hashed): return True else: return False else: return False
def is_unique(email): """ tests if the new email exists in the db """ unique_query = "SELECT COUNT(*) FROM users WHERE email = ?" rs = db.get_rs(unique_query, (email, )) if not rs[0]: return True else: return False
def get_cached_place_by_id(place_id: str) -> Place: logging.debug("Getting places by id") cached_qry = "SELECT * FROM cache WHERE place_id = ?" rs = db.get_rs(cached_qry, (place_id, )) location = (rs[4], rs[5]) types = get_types_by_place_id(rs[2]) return Place(rs[3], rs[2], location, rs[6], types)
def get_types_by_place_id(place_id: str) -> [str]: place_types_by_id_qry = """ SELECT DISTINCT pt.place_type FROM place_types as pt JOIN cached_type as ct ON ct.type_id = pt.type_id JOIN cache as c ON c.place_id = ct.place_id WHERE c.place_id = ?""" place_rs = db.get_rs(place_types_by_id_qry, (place_id, )) type_list = [] for r in place_rs: if isinstance(r, tuple): type_list.append(r[0]) else: type_list.append(r) return type_list
def get_favorites(user_id): get_fave_qry = "SELECT place_id FROM favorites WHERE user_id = ?" faves = db.get_rs(get_fave_qry, (user_id, )) favorites = [] print(faves) if len(faves) == 1: for f in faves: print(f) favorites.append(places.get_cached_place_by_id(f)) elif len(faves) > 1: for f in faves: print(f[0]) favorites.append(places.get_cached_place_by_id(f[0])) return favorites
def get_cached_by_radius(location: tuple, search_radius) -> list: """ This function was adapted from: https://stackoverflow.com/questions/21042418/mysql-select-coordinates-within-range """ qry_args = (location[0], location[1], location[0], search_radius / 1000) # sqlite dose not have all the same math functions as mysql # this is a list of python functions tp pass along to sqlite functions = (("ACOS", 1, math.acos), ("COS", 1, math.cos), ("SIN", 1, math.sin), ("RADIANS", 1, math.radians)) # this sql call is what I took from the site, it finds all get_places_in_radius_qry = "SELECT *, ( 3959 * ACOS( COS( RADIANS(?) )" \ " * COS( radians( latitude ) ) * COS( RADIANS( longitude ) " \ "- RADIANS(?) ) + SIN( RADIANS(?) ) * SIN( RADIANS( latitude ) ) ) ) " \ "AS distance FROM cache WHERE distance < ?" search_rs = db.get_rs(get_places_in_radius_qry, qry_args, custom_functions=functions) cached_places = places_from_search_results(search_rs) return cached_places
def get_user(user): """ gets user from db and loads into an object """ user_qry = "SELECT email, email_confirmed, admin, first_name, last_name, user_id FROM users WHERE email = ?" rs = db.get_rs(user_qry, (user, )) db_user = User(db_user=rs) return db_user