def check_policy_to_enter_this_area(area: Area, user: User): # If explicitly set on the Physical Access Level, staff may be exempt from being granted explicit access if user.is_staff and any([access_level.accessible() for access_level in PhysicalAccessLevel.objects.filter(allow_staff_access=True, area=area)]): pass else: # Check if the user normally has access to this area door at the current time (or access to any parent) if not any([access_level.accessible() for access_level in user.accessible_access_levels_for_area(area)]): first_access_exception = next(iter([access_level.ongoing_exception() for access_level in user.accessible_access_levels_for_area(area)]), None) raise NoAccessiblePhysicalAccessUserError(user=user, area=area, access_exception=first_access_exception) if not user.is_staff and not user.is_service_personnel: for a in area.get_ancestors(ascending=True, include_self=True): unavailable_resources = a.required_resources.filter(available=False) if unavailable_resources: raise UnavailableResourcesUserError(user=user, area=a, resources=unavailable_resources) # Non staff users may not enter an area during a scheduled outage. if area.scheduled_outage_in_progress(): raise ScheduledOutageInProgressError(user=user, area=area) # If we reached maximum capacity, fail (only for non staff users) for a in area.get_ancestors(ascending=True, include_self=True): if a.maximum_capacity and 0 < a.maximum_capacity <= a.occupancy_count(): raise MaximumCapacityReachedError(user=user, area=a) if area.requires_reservation and not area.get_current_reservation_for_user(user): raise ReservationRequiredUserError(user=user, area=area)
def check_policy_to_enter_this_area(area:Area, user:User): # If explicitly set on the Physical Access Level, staff may be exempt from being granted explicit access if user.is_staff and any([access_level.accessible() for access_level in PhysicalAccessLevel.objects.filter(allow_staff_access=True, area=area)]): pass else: # Check if the user normally has access to this area door at the current time if not any([access_level.accessible() for access_level in user.physical_access_levels.filter(area=area)]): raise NoAccessiblePhysicalAccessUserError(user=user, area=area) if not user.is_staff: unavailable_resources = area.required_resources.filter(available=False) if unavailable_resources: raise UnavailableResourcesUserError(user=user, area=area, resources=unavailable_resources) # If we reached maximum capacity (non-staff user), fail area_occupancy = AreaAccessRecord.objects.filter(area=area, end=None, staff_charge=None).count() if 0 < area.maximum_capacity <= area_occupancy: raise MaximumCapacityReachedError(user=user, area=area)
def check_policy_to_enter_this_area(area: Area, user: User): # If explicitly set on the Physical Access Level, staff may be exempt from being granted explicit access if user.is_staff and any([ access_level.accessible() for access_level in PhysicalAccessLevel.objects.filter( allow_staff_access=True, area=area) ]): pass else: # Check if the user normally has access to this area door at the current time if not any([ access_level.accessible() for access_level in user.physical_access_levels.filter( area=area) ]): raise NoAccessiblePhysicalAccessUserError(user=user, area=area) if not user.is_staff: unavailable_resources = area.required_resources.filter(available=False) if unavailable_resources: raise UnavailableResourcesUserError( user=user, area=area, resources=unavailable_resources)