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 get_fieldsets(self, request, obj: Area = None): """ Remove some fieldsets if this area is a parent """ if obj and not obj.is_leaf_node(): return [i for i in self.fieldsets if i[0] not in ["Area access", "Reservation", "Policy"]] return super().get_fieldsets(request, obj)
def get_area_model_tree(): from NEMO.models import Area only_fields = [ "name", "category", "maximum_capacity", "reservation_warning", "count_staff_in_occupancy" ] return ModelTreeHelper(Area(), "parent_area", "area_children_set", only_fields)