Beispiel #1
0
    def is_reservation_allowed(cls, user, reservation):
        """
        Check if reservation is allowed with restrictions of given user
        :param user: user to whom reservation belongs
        :param reservation: reservation to be checked
        :return: True if reservation is allowed, False otherwise
        """
        try:
            resource = Resource.get(reservation.resource_id)
        except NoResultFound:
            return False

        user_restrictions = user.get_restrictions(include_group=True)
        # get global restrictions or applied to selected resource
        restrictions = [
            r for r in user_restrictions
            if r.is_global or resource in r.resources
        ]

        # time interval required to create restriction
        start_date = reservation.start
        end_date = reservation.end

        while True:
            start_date_changed = False
            for restriction in restrictions:
                if restriction.starts_at <= start_date and \
                        (restriction.ends_at is None or start_date < restriction.ends_at):
                    schedules = restriction.schedules
                    if not schedules:
                        if restriction.ends_at is None:
                            # If restriction lasts indefinitely, reservation is allowed
                            return True
                        else:
                            start_date = restriction.ends_at
                            start_date_changed = True
                    else:
                        date = cls.__get_latest_date_allowed_by_schedules(
                            start_date, end_date, schedules)
                        if date > start_date:
                            start_date_changed = True
                            start_date = date
                    if start_date >= end_date:
                        return True
            if not start_date_changed:
                break
        return False
Beispiel #2
0
def get_selected(user_id: Optional[UserId], group_id: Optional[GroupId], resource_id: Optional[ResourceId],
                 schedule_id: Optional[ScheduleId], include_user_groups: Optional[bool] = False) \
        -> Tuple[Union[List[Any], Content], HttpStatusCode]:
    try:
        # If a specific group is selected then groups are not included in the restriction information in response
        # The same applies to users and resources
        include_groups = group_id is None
        include_users = user_id is None
        include_resources = schedule_id is None

        restrictions = []  # type: List[Restriction]
        if user_id is not None:
            user = User.get(user_id)
            restrictions.extend(
                user.get_restrictions(include_group=include_user_groups))
        if group_id is not None:
            group = Group.get(group_id)
            restrictions.extend(group.get_restrictions())
        if resource_id is not None:
            resource = Resource.get(resource_id)
            restrictions.extend(resource.get_restrictions())
        if schedule_id is not None:
            schedule = RestrictionSchedule.get(schedule_id)
            restrictions.extend(schedule.restrictions)

        # Take unique restrictions
        result = set(restrictions)
    except NoResultFound as e:
        log.warning(e)
        content, status = {
            'msg': GENERAL['bad_request']
        }, HTTPStatus.BAD_REQUEST.value
    except Exception as e:
        log.critical(e)
        content, status = {
            'msg': GENERAL['internal_error']
        }, HTTPStatus.INTERNAL_SERVER_ERROR.value
    else:
        content = [
            restriction.as_dict(
                include_groups=include_groups,
                include_users=include_users,  # type: ignore
                include_resources=include_resources) for restriction in result
        ]
        status = HTTPStatus.OK.value
    finally:
        return content, status
Beispiel #3
0
def apply_to_resource(
        restriction_id: RestrictionId,
        resource_uuid: ResourceId) -> Tuple[Content, HttpStatusCode]:
    restriction = None
    try:
        restriction = Restriction.get(restriction_id)
        resource = Resource.get(resource_uuid)
        restriction.apply_to_resource(resource)
        for user in restriction.get_all_affected_users():
            ReservationVerifier.update_user_reservations_statuses(
                user, have_users_permissions_increased=True)
    except NoResultFound:
        if restriction is None:
            content, status = {
                'msg': RESTRICTION['not_found']
            }, HTTPStatus.NOT_FOUND.value
        else:
            content, status = {
                'msg': RESOURCE['not_found']
            }, HTTPStatus.NOT_FOUND.value
    except InvalidRequestException:
        content, status = {
            'msg': RESTRICTION['resources']['apply']['failure']['duplicate']
        }, HTTPStatus.CONFLICT.value
    except AssertionError as e:
        content, status = {'msg': RESTRICTION['resources']['apply']['failure']['assertions'].format(reason=e)}, \
            HTTPStatus.UNPROCESSABLE_ENTITY.value
    except Exception as e:
        log.critical(e)
        content, status = {
            'msg': GENERAL['internal_error']
        }, HTTPStatus.INTERNAL_SERVER_ERROR.value
    else:
        content, status = {
            'msg':
            RESTRICTION['resources']['apply']['success'],
            'restriction':
            restriction.as_dict(include_groups=True,
                                include_users=True,
                                include_resources=True)
        }, HTTPStatus.OK.value
    finally:
        return content, status
Beispiel #4
0
def get_by_id(uuid: ResourceUUID) -> Tuple[Content, HttpStatusCode]:
    get_infrastructure()  # Save new GPUs in database
    try:
        resource = Resource.get(uuid)
    except NoResultFound as e:
        log.warning(e)
        content, status = {
            'msg': RESOURCE['not_found']
        }, HTTPStatus.NOT_FOUND.value
    except Exception as e:
        log.critical(e)
        content, status = {
            'msg': GENERAL['internal_error']
        }, HTTPStatus.INTERNAL_SERVER_ERROR.value
    else:
        content, status = {
            'msg': RESOURCE['get']['success'],
            'resource': resource.as_dict()
        }, HTTPStatus.OK.value
    finally:
        return content, status
Beispiel #5
0
def test_resource_creation(tables):
    new_resource = Resource(id="34943e60-0acd-4c31-b96e-02f88cc156f3")
    new_resource.save()

    assert Resource.get(new_resource.id) is not None