def send_push_notification_ride_approved_by_passenger(request): driver = UsersHandler.get_user_from_db(request[driver_type][phone_number]) if is_valid_token(driver): PushNotificationsHandler. \ send_push_notification(driver[token], request[passenger_type][user_name] + ' מצטרף אליך סופית, אל תשכח אותו כשתצא')
def send_push_notification_ride_request_approved_by_driver( passenger_phone_number): passenger = UsersHandler.get_user_from_db(passenger_phone_number) if is_valid_token(passenger): PushNotificationsHandler.send_push_notification( passenger[token], 'אישרו לך טרמפ, לא תכנס לבדוק מי?')
def add_ride_offer(ride_offer): logger.info('Trying to build ride offer object') driver = UsersHandler.get_user_from_db(ride_offer[phone_number]) try: if driver is not None: offer_object = build_ride_offer_object(ride_offer, driver) if is_offer_exists_already(driver, offer_object): logger.warning( 'Offer already exists in DB, not creating a new one') return offer_already_exists else: logger.info('Trying to insert new offer to DB') new_offer = firebase_db.child(rides_collection).child( offers_collection).push(offer_object) firebase_db.child(users_collection).child(driver[phone_number]).child(rides). \ child(offers_collection).push({offer_id: new_offer.key}) logger.info('Offer insert completed successfully') return success else: logger.error('''Driver doesn't exists!''') return failure except RequestException as err: logger.error(str(err)) logger.warning("Offer creation failed") return failure
def send_push_notification_new_ride_request_to_driver(passenger, driver_phone_number): driver = UsersHandler.get_user_from_db(driver_phone_number) if is_valid_token(driver): PushNotificationsHandler.send_push_notification( driver[token], passenger[user_name] + ' רוצה לנסוע איתך, חבל לנסוע לבד')
def send_push_notification_about_ride_canceled(request_to_cancel, canceler_phone_number): phone_number_to_send_notification = \ (request_to_cancel[passenger_type][phone_number], request_to_cancel[driver_type][phone_number])[ request_to_cancel[passenger_type][phone_number] == canceler_phone_number] user = UsersHandler.get_user_from_db(phone_number_to_send_notification) if is_valid_token(user): PushNotificationsHandler.send_push_notification( user[token], get_cancel_message(request_to_cancel))
def find_ride(ride_request): passenger = UsersHandler.get_user_from_db(ride_request[phone_number]) if passenger is None: logger.error('''User doesn't exists''') return None logger.info('Trying to find optional ride offers') optional_offers = get_optional_offers(ride_request) if len(optional_offers) == 0: logger.info('No optional offers') return optional_offers logger.info('%s optional offers founded, sending them to user', len(optional_offers)) return optional_offers
def passenger_offer_accept(accepted_offer_id, passenger_phone_number): logger.info('Passenger trying to accept ride offer') passenger = UsersHandler.get_user_from_db(passenger_phone_number) try: if passenger is not None: accepted_offer = get_offer_from_db(accepted_offer_id) if accepted_offer is not None: if not is_passenger_accepted_this_offer_already( passenger_phone_number, accepted_offer_id): logger.info('Trying to create ride request') new_request = build_ride_request_object( accepted_offer, passenger) new_request_response = firebase_db.child( rides_collection).child(requests_collection).push( new_request) add_request_to_users_rides(passenger_phone_number, accepted_offer[phone_number], new_request_response.key, accepted_offer_id) logger.info( 'Sending push notification to driver about new ride request' ) send_push_notification_new_ride_request_to_driver( passenger, accepted_offer[phone_number]) logger.info('Returning success') return success else: logger.error('''Offer with id=%s doesn't exists''', accepted_offer_id) return failure else: logger.error('''User with phoneNumber=%s doesn't exists''', passenger_phone_number) return failure except RequestException as err: logger.error(str(err)) logger.warning("Offer wasn't accepted") return failure
def get_user_rides(user_phone_number): user = UsersHandler.get_user_from_db(user_phone_number) try: if user is not None: logger.info('Trying to get all rides of user with id=%s', user_phone_number) user_rides_pointers = firebase_db.child(users_collection).child( user_phone_number).child(rides).get() all_user_rides = get_user_rides_from_pointers(user_rides_pointers) logger.info('Found %s rides, returning them to user', len(all_user_rides)) return all_user_rides else: logger.error('No user with id=%s', user_phone_number) return failure except RequestException as err: logger.error(str(err)) logger.warning( '''There was an error while trying to get user rides from DB''') return failure
def auto_login_user(): logger.info('Get request to login: ' + request.url) user_phone_number = request.args.get(phone_number) return UsersHandler.auto_login(user_phone_number)
def login_user(): logger.info('Get request to login: ' + request.url) user_data = create_json_object_from_request_args(request.args) return UsersHandler.login(user_data)
def delete_user(): logger.info('Get request to delete user: ' + request.url) id_of_user_to_delete = request.args.get(phone_number) return UsersHandler.delete_user(id_of_user_to_delete)
def update_user(): logger.info('Get request to update user: ' + request.url) updated_user_data = create_json_object_from_request_args(request.args) return UsersHandler.update_user(updated_user_data)