def register_user(self, body): """ signup function :return: """ if body.get('password') != body.get('confirm_password'): message = "Password are not the same" error_handler(code=400, message=message, ui_status=True) check_user = Users.objects(email=body['email']) if check_user.count() > 0: message = "Email belongs to another user" error_handler(code=400, message=message, ui_status=True) #body = custom_marshal(body, UserDTO.user, 'create') body['password'] = argon2.using(rounds=4).hash(body['password']) new_user = Users(_id=ObjectId(), email=body['email'], first_name=body['first_name'], last_name=body['last_name'], password=body['password']) _id = new_user.save() link = app.config['ACTIVATION_URL'].format( id=new_user['_id'], url_prefix=app.config['APP_URL_PREFIX']) # print(link) send_mail([body['email']], "LimeHome App Account Activation", link, 'activation_email.html', { 'link': link, 'name': body['first_name'] })
def fetch_booking(self, id): ''' Fetch a booking :param id: :return: ''' try: booking = Bookings.objects.get(_id=ObjectId(id)) return json.loads(booking.to_json()) except DoesNotExist as e: message = "The booking you are were looking for was not found" error_handler(code=404, message=message, ui_status=True)
def soft_delete_booking(self, id): """ Soft deleting a booking :param payload: :return: """ try: inactive_booking = Bookings.objects(_id=ObjectId(id)).update_one( set__is_deleted=True) except DoesNotExist as e: message = "The booking you are trying to delete was not found" error_handler(code=400, message=message, ui_status=True)
def fetch_booking_by_place(self, id): ''' Fetch a booking :param id: :return: ''' booking = Bookings.objects(place_id=id, is_deleted=False) #print(booking.to_json()) if booking.count() > 0: return json.loads(booking.to_json()) else: message = "This place have no bookings yet" error_handler(code=404, message=message, ui_status=True)
def fetch_booking_by_user(self, user_id): ''' Fetch a booking :param user_id: :return: ''' # count, registry = mongobase_obj.get(COLLECTIONS['BOOKINGS'], {"user": id, "meta.is_deleted": False}) booking = Bookings.objects(user=user_id, is_deleted=False) if booking.count() > 0: return json.loads(booking.to_json()) else: message = "This user haven't book anything yet" error_handler(code=404, message=message, ui_status=True)
def activate_user(self, id): """ Get the user activated :param id: :return: """ try: check_user = Users.objects.get(_id=ObjectId(id)) if check_user['is_active']: message = "Account Already Active" error_handler(code=400, message=message, ui_status=True) else: active = check_user.update(is_active=True) except DoesNotExist as e: message = "This Link is invalid" error_handler(code=400, message=message, ui_status=True)
def post(self): ''' Logout :return: ''' try: raw_jti = get_raw_jwt()['jti'] ttl = redis_db.ttl(raw_jti) redis_db.set(raw_jti, 'true', ttl) return { 'ui': True, 'status': 'success', "sms": "Successful logout, hope to see again soon" }, 202 except Exception as e: message = "Something weird happen, we will log you out anyway, just to make sure" error_handler(code=400, message=message, ui_status=True)
def update_booking(self, id, user_id, payload): ''' Update a booking :param id: :param payload: :return: ''' try: booking = Bookings.objects(_id=ObjectId(id)).update_one( set__title=payload['title'], set__arrival=payload['arrival'], set__departure=payload['departure'], set__guests=payload['guests'], set__room_type=payload['room_type']) # booking.save() except DoesNotExist as e: message = "The booking you are trying to modify belongs to a different user" error_handler(code=400, message=message, ui_status=True)
def login_user(self, body): ''' User login to retrieve jwt tokens :param body: :return: ''' email, password = body['email'], body['password'] try: user = Users.objects.get(email=email) # "meta.is_deleted" if argon2.verify(password, user['password']): if user['is_active'] is True: user_id = str(user['_id']) access_token = create_access_token(identity=user_id) refresh_token = create_refresh_token(identity=user_id) access_jti = get_jti(encoded_token=access_token) refresh_jti = get_jti(encoded_token=refresh_token) redis_db.set(access_jti, 'false', app.config['JWT_ACCESS_TOKEN_EXPIRES']) redis_db.set(refresh_jti, 'false', app.config['JWT_REFRESH_TOKEN_EXPIRES']) return { "status": 202, "access_token": access_token, "refresh_token": refresh_token }, 202 else: message = "You are missing one step on your activation process, Please check your email for instruction to activate your user" error_handler(code=401, message=message, ui_status=True) else: message = "Your Credentials don't match with our registries" error_handler(code=401, message=message, ui_status=True) except DoesNotExist as e: message = "Your Credentials don't match with our registries" error_handler(code=401, message=message, ui_status=True)