def block_unblock_a_user(self, requestor_users_email: str,
                          user_to_be_blocked_unblocked_email: str,
                          action) -> list:
     try:
         if requestor_users_email == user_to_be_blocked_unblocked_email:
             return [{
                 'error': ErrorEnums.INVALID_INPUT_SUPPLIED_ERROR.value
             }, 400]
         self.target_user = UserDatabaseService.get_active_inactive_single_user_by_email(
             user_to_be_blocked_unblocked_email)
         if not self.target_user:
             return [{'error': ErrorEnums.NO_USER_FOUND_ERROR.value}, 404]
         if not self.target_user.is_active:
             return [{'error': ErrorEnums.INACTIVE_USER_ERROR.value}, 404]
         if action not in [
                 str(UserEnums.BLOCK.value),
                 str(UserEnums.UNBLOCK.value)
         ]:
             return [{'error': ErrorEnums.UNKNOWN_REQUEST_ERROR.value}, 400]
         self._performer = UserDatabaseService.get_active_user_by_email(
             requestor_users_email)
         self.user_dao = UserDatabaseService()
         self.user_dao.block_unblock_users(user_dto(self._performer),
                                           user_dto(self.target_user),
                                           action)
         return [{'response': 'SUCCESS.'}, 200]
     except Exception as e:
         return [{
             'error':
             ErrorEnums.EXCEPTION_ERROR.value.format(
                 e, "block/unblock user request")
         }, 400]
 def follow_unfollow_a_user(self, requesting_user_email: str,
                            user_to_be_followed_unfollowed_email: str,
                            action) -> list:
     try:
         if requesting_user_email == user_to_be_followed_unfollowed_email:
             return [{
                 'error': ErrorEnums.INVALID_INPUT_SUPPLIED_ERROR.value
             }, 400]
         self.target_user = UserDatabaseService.get_active_inactive_single_user_by_email(
             user_to_be_followed_unfollowed_email)
         if not self.target_user:
             return [{'error': ErrorEnums.NO_USER_FOUND_ERROR.value}, 404]
         if not self.target_user.is_active:
             return [{'error': ErrorEnums.INACTIVE_USER_ERROR.value}, 404]
         if action not in [
                 UserEnums.FOLLOW.value, UserEnums.UNFOLLOW.value
         ]:
             return [{'error': ErrorEnums.UNKNOWN_REQUEST_ERROR.value}, 400]
         self._performer = UserDatabaseService.get_active_user_by_email(
             requesting_user_email)
         self.user_dao = UserDatabaseService()
         response = self.user_dao.follow_unfollow_user(
             user_dto(self._performer), user_dto(self.target_user), action)
         if isinstance(response, dict):
             return [response, 500]
         return [{'response': 'SUCCESS.'}, 200]
     except Exception as e:
         return [{'error': 'Exception - {} - occurred.'.format(e)}, 400]
Exemple #3
0
def admin_access(performer, user_email: str, permission_type: str) -> list:
    user = UserDatabaseService.get_active_inactive_single_user_by_email(user_email)
    if not user.is_active:
        return [{'error': 'Cannot grant access as the user is inactive. Please activate the user profile first.'}, 400]
    access_type = AdminPermissionEnums.__dict__.get(permission_type.upper())
    UserDatabaseService.admin_access(performer, user_email, access_type.value)
    verify_user_access = UserDatabaseService.get_active_user_by_email(user_email).is_admin
    if verify_user_access is True:
        return [{'response': 'User granted admin privileges.'}, 200]
    elif verify_user_access is False:
        return [{'response': 'User revoked from admin privileges.'}, 200]
    return [{'error': 'There was some error.'}, 500]
Exemple #4
0
def create_update_user(user_identity, user_request_details: dict, user_identity_provided: bool):
    phone_length_is_valid = is_phone_vaild(user_request_details.setdefault('phone_number', None))

    if not phone_length_is_valid:
        return ErrorEnums.INVALID_PHONE_LENGTH_ERROR.value

    user_request_details.__setitem__(
        'date_of_birth', TimeUtils.convert_time(
            user_request_details.get('date_of_birth')
        )
    ) if user_request_details.get('date_of_birth') else None

    if not user_identity_provided:
        if not validate_email_format(user_request_details.get('email')):
            return ErrorEnums.INVALID_EMAIL_FORMAT_ERROR.value
        if not validate_password_format(user_request_details.get('password')):
            return ErrorEnums.INVALID_PASSWORD_ERROR.value
        user_request_details.__setitem__('password', UserSecurity.encrypt_pass(user_request_details.get('password')))
        created_user = UserDatabaseService.create_user(user_request_details)
        if isinstance(created_user, str):
            return created_user
        return dataStateAccess.UserDTO.user_dto(created_user)

    if user_request_details.get('email'):
        if not validate_email_format(user_request_details.get('email')):
            return ErrorEnums.INVALID_EMAIL_FORMAT_ERROR.value

        if user_request_details.get('email') and user_identity.get('email') != user_request_details.get('email'):
            verify_existence_for_user_email = UserDatabaseService.get_active_user_by_email(user_request_details.get('email'))

            if verify_existence_for_user_email:
                return ErrorEnums.EMAIL_ALREADY_EXISTS_ERROR.value

    if user_request_details.get('username'):
        if user_request_details.get('username') and user_identity.get('username') != user_request_details.get('username'):
            verify_username_existence = UserDatabaseService.get_active_user_by_username(user_request_details.get('username'))
            verify_alt_username_existence = UserDatabaseService().get_user_by_alt_username(user_request_details.get('username'))

            if verify_username_existence or verify_alt_username_existence:
                return ErrorEnums.USER_NAME_ALREADY_EXISTS.value

    updated_user = UserDatabaseService.update_user_generic_data(user_identity, user_request_details)
    if isinstance(updated_user, str):
        return updated_user
    return dataStateAccess.UserDTO.user_dto(updated_user)
Exemple #5
0
def get_active_user_by_email(email):
    user = UserDatabaseService.get_active_user_by_email(email)
    return dataStateAccess.UserDTO.user_dto(user)