def update_coordination(): token, coordination = Utils.check_json_vaild(request.get_json(), "Token", "Coordination") decoded_token = Utils.decode_token(token=token) Database.update(DB_collection_current_driver_shift, {'Email': decoded_token['Email']}, {"Current location": coordination}, False)
def login(): phone_number, password = Utils.check_json_vaild(request.get_json(), "PhoneNumber", "Password") manager_data = Manager.check_phone_number_validation(phone_number=phone_number) Manager.check_password_validation(password, manager_data) wanted_keys = {'Name', 'PhoneNumber'} token_data = {key: value for key, value in manager_data.items() if key in wanted_keys} token = Utils.create_token(token_data, life_time_hours=TOKEN_LIFETIME) # image = Manager.get_image({"PhoneNumber": phone_number}) return token
def delete(collection): token, phone_number = Utils.check_json_vaild(request.get_json(), "Token", 'PhoneNumber') Utils.decode_token(token=token) if collection == 'Drivers': Database.delete(collection, {"PhoneNumber": phone_number}) Database.delete_image(DB_COLLECION_IMAGES, {"PhoneNumber": phone_number}) else: Database.delete(collection, {"PhoneNumber": phone_number}) Database.delete(collection, {"phone_number": phone_number})
def change_password(): token, new_password = Utils.check_json_vaild(request.get_json(), 'Token', 'NewPassword') Utils.password_isvalid(new_password, PASSWORD_MIN_LENGTH, PASSWORD_PATTERN) decoded_token = Utils.decode_token(token) hashed_password = Utils.hash_password(new_password) Driver.update_db({'PhoneNumber': decoded_token['PhoneNumber']}, {"Password": hashed_password})
def check_code_number_validation(): restoration_code, token = Utils.check_json_vaild( request.get_json(), 'Restoration code', "Token") decoded_token = Utils.decode_token(token) driver_data = Driver.find_driver( query={'PhoneNumber': decoded_token['PhoneNumber']}, options={'Password restoration code': 1}) if driver_data['Password restoration code'] != int(restoration_code): raise CodeNumberIsInValid("the code number is invalid!") token_data = {"PhoneNumber": decoded_token['PhoneNumber']} return Utils.create_token(token_data, life_time_minutes=CHANGING_PASSWORD_DURATION)
def logout(): token, = Utils.check_json_vaild(request.get_json(), "Token") decoded_token = Utils.decode_token(token=token) current_shift = Database.find_one_and_delete( collection=DB_collection_current_driver_shift, query={'Email': decoded_token['Email']}) current_shift['Finished at'] = ( datetime.datetime.now() + datetime.timedelta(hours=3)).strftime("%H:%M") current_shift['_id'] = uuid.uuid4( ).hex # here i changed the id of the document for not happening contradiction of the documents (which same driver can exist many times in this collection) current_shift['End location'] = current_shift['Current location'] current_shift.pop('Current location') Database.save_to_db(collection=DB_collection_previous_driver_shift, query=current_shift)
def edit_details(): token, = Utils.check_json_vaild(request.get_json(), "Token") decoded_token = Utils.decode_token(token=token) Driver.delete_driver(decoded_token['PhoneNumber']) Driver.registration('edit_mode') name, phone_number, email, birthday = Utils.check_json_vaild( request.get_json(), 'Name', "PhoneNumber", "Email", 'BirthDate') new_token = Utils.create_token( { "Name": name, "PhoneNumber": phone_number, "Email": email, "BirthDate": birthday }, life_time_hours=TOKEN_LIFETIME) return new_token
def forget_password(): phone_number, = Utils.check_json_vaild(request.get_json(), 'PhoneNumber') driver_data = Driver.check_phone_number_validation( phone_number=phone_number) code_number = random.randrange( (10**(FORGET_PASSWORD_CODE_LENGTH - 1) + 1), 10**FORGET_PASSWORD_CODE_LENGTH, 1) SMS.send_sms(driver_data['PhoneNumber'], FORGET_PASSWORD_SMS_MESSAGE + str(code_number)) restoration_code = dict() restoration_code['Password restoration code'] = code_number Driver.update_db({"PhoneNumber": phone_number}, restoration_code) token = Utils.create_token({"PhoneNumber": driver_data['PhoneNumber']}, life_time_minutes=CODE_NUMBER_DURATION) image = Driver.get_image({"PhoneNumber": driver_data['PhoneNumber']}) return token, image
def login(): phone_number, password, coordination = Utils.check_json_vaild( request.get_json(), 'PhoneNumber', "Password", 'Coordination') driver_data = Driver.check_phone_number_validation( phone_number=phone_number) Driver.check_password_validation(password, driver_data) if driver_data['Confirmed account'] is False: raise AccountNotActivated("the account not yet activated!") Driver.store_driver_shift(driver_data.copy(), coordination) wanted_keys = {'Name', 'PhoneNumber', 'Email', 'BirthDate'} token_data = { key: value for key, value in driver_data.items() if key in wanted_keys } token = Utils.create_token(token_data, life_time_hours=TOKEN_LIFETIME) image = Driver.get_image({"PhoneNumber": phone_number}) return token, image
def get_data(collection): token, = Utils.check_json_vaild(request.get_json(), "Token") Utils.decode_token(token=token) data = Database.find(collection=collection, query={}, options={'_id': 0, 'created_at': 0, 'Password restoration code': 0, 'Password': 0}) data = [sorted(i.items(), key=lambda k: PRIORITIES[list(k)[0]]) for i in data] if collection == 'Drivers' or collection == 'Current shifts' or collection == 'Managers': for i in data: for detail in i: if detail[0] == "PhoneNumber": print(detail[1]) image = Database.find_image(collection=DB_COLLECION_IMAGES, filter={"PhoneNumber": detail[1]}) print(image) i.append(('Image', image.read().decode())) print(data) # image = Driver.get_image({"PhoneNumber": phone_number}) return data
def new_ride(): token, phone_number, number_of_passengers = Utils.check_json_vaild( request.get_json(), "Token", "PhoneNumber", "Num of passengers") Utils.phone_number_Isvalid(phone_number) Passenger.check_num_of_passengers(number_of_passengers) passenger_details = Passenger.find_passenger( query={'phone_number': phone_number}) passenger_details['Number of passengers'] = number_of_passengers passenger_details['_id'] = uuid.uuid4().hex decoded_token = Utils.decode_token(token=token) passenger_details['Wًith driver'] = decoded_token['Name'] passenger_details['Riding place'] = \ Driver.get_coordination({'Email': decoded_token['Email']}, {'Current location': 1, '_id': 0})[ 'Current location'] # important thing i did here, its telling the mongo database to give me just the one field without other fields in document(which satisfy the requirements) passenger_details['Riding time'] = ( datetime.datetime.now() + datetime.timedelta(hours=3)).strftime("%Y/%m/%d, %H:%M") passenger_details['created_at'] = datetime.datetime.utcnow() Passenger.store_new_ride(passenger_details) return passenger_details['name']
def use_moovit_feature(): phone_number, token = Utils.check_json_vaild(request.get_json(), "PhoneNumber", "Token") Utils.phone_number_Isvalid(phone_number=phone_number) query = {} decoded_token = Utils.decode_token(token=token) query['Wًith driver'] = decoded_token['Name'] query['Riding place'] = \ Driver.get_coordination({'Email': decoded_token['Email']}, {'Current location': 1, '_id': 0})[ 'Current location'] # important thing i did here, its telling the mongo database to give me just the one field without other fields in document(which satisfy the requirements) query.update({ "PhoneNumber": phone_number, "_id": uuid.uuid4().hex, "Riding time": (datetime.datetime.now() + datetime.timedelta(hours=3)).strftime("%Y/%m/%d, %H:%M") }) Moovit.save_to_db(query=query) SMS.send_sms(phone_number=phone_number, text=INVITAION_MESSAGE)
def registration(mode="registration_mode"): name, phone_number, email, password, birthday, image = Utils.check_json_vaild( request.get_json(), 'Name', "PhoneNumber", 'Email', 'Password', 'BirthDate', 'Image') try: Driver.check_phone_number_validation(phone_number=phone_number) except DriverError: try: Driver.check_email_validation(email=email) except DriverNotExistError: Utils.password_isvalid(password, PASSWORD_MIN_LENGTH, PASSWORD_PATTERN) hashed_password = Utils.hash_password(password) confirmed_account = False if mode != 'edit_mode' else True query = { "Name": name, "PhoneNumber": phone_number, "Email": email, 'BirthDate': birthday, "Password": hashed_password, "_id": uuid.uuid4().hex, "Confirmed account": confirmed_account } Database.save_to_db(collection=DB_COLLECTION_DRIVER, query=query) Driver.save_image({ "Name": name, "PhoneNumber": phone_number }, image) if mode != 'edit_mode': SMS.send_sms( phone_number, ACTIVATING_ACCOUNT_SMS_MESSAGE + "\nhttps://shabus-21aa4.firebaseapp.com/confirmation/" + str(query['_id'])) else: raise DriverExistError("the driver email already exist!") else: raise DriverExistError("the driver phone number already exist!")
def check_email_validation(email): Utils.email_Isvalid(email) driver_data = Driver.find_driver(query={'Email': email}) return driver_data
def confirmation_of_driver_account(): id, = Utils.check_json_vaild(request.get_json(), "id") data = Driver.find_driver({"_id": id}, {"Name": 1}) Driver.update_db({'_id': id}, {"Confirmed account": True}) return data['Name']
def check_password_validation(password, manager_data): Utils.password_isvalid(password, PASSWORD_MIN_LENGTH, PASSWORD_PATTERN) if not Utils.passwords_matching(password, manager_data['Password']): raise InCorrectPasswordError("wrong password associated with user phoneNumber")
def check_phone_number_validation(phone_number): Utils.phone_number_Isvalid(phone_number=phone_number) manager_data = Manager.find_manager(query={'PhoneNumber': phone_number}) return manager_data
def check_phone_number_validation(phone_number): Utils.phone_number_Isvalid(phone_number=phone_number) driver_data = Driver.find_driver(query={'PhoneNumber': phone_number}) return driver_data