Esempio n. 1
0
def for_allocations(resources):
    """Returns a function which takes an allocation and returns true if the
    allocation can be exposed.

    resources can be a list of uuids or a list of resource objects

    """

    # get a dictionary with uuids as keys and resources as values
    def get_object(obj):
        if is_uuid(obj):
            return UUID(obj), get_resource_by_uuid(obj)
        else:
            return UUID(obj.uuid()), obj

    resource_objects = dict([get_object(o) for o in resources])

    # get timeframes for each uuid
    timeframes = {}
    for uuid, resource in resource_objects.items():

        # Don't load the timeframes of the resources for which the user has
        # special access to. This way they won't get checked later in
        # 'is_exposed'
        if checkPermission(
                'seantis.reservation.ViewHiddenAllocations', resource):
            timeframes[uuid] = []
        elif resource is None:
            timeframes[uuid] = [None]
        else:
            timeframes[uuid] = timeframes_by_context(resource)

    # returning closure
    def is_exposed(allocation):

        # use the mirror_of as resource-keys of mirrors do not really exist
        # as plone objects
        frames = timeframes[allocation.mirror_of]

        if frames is None or len(frames) == 0:
            return True

        if frames == [None]:
            return False

        # the start date is relevant
        day = allocation.start.date()
        for frame in frames:
            if frame.start <= day and day <= frame.end:
                return frame.visible()

        return False

    return is_exposed
Esempio n. 2
0
 def timeframes(self):
     return timeframes_by_context(self)