def process_area_access_record_with_parents(user: User): show_not_qualified_areas = get_customization( 'dashboard_display_not_qualified_areas') records = AreaAccessRecord.objects.filter(end=None, staff_charge=None) if not user.is_staff and show_not_qualified_areas != 'enabled': records = records.filter(area__in=user.accessible_areas()) records = records.prefetch_related('customer', 'project', 'area') no_occupants = not records.exists() area_items = None area_model_tree = get_area_model_tree() if not no_occupants: areas_and_parents = area_model_tree.get_ancestor_areas( area_model_tree.get_areas([record.area.id for record in records]), include_self=True) # Sort to have area without children before others areas_and_parents.sort(key=lambda x: f'{x.tree_category}zz' if x.is_leaf else f'{x.tree_category}/aa') area_summary = create_area_summary(area_model_tree=area_model_tree, add_resources=False, add_outages=False, add_occupants=True) area_summary_dict = {area['id']: area for area in area_summary} for area_item in areas_and_parents: area_item.item = area_summary_dict[area_item.id] area_items = area_tree_helper(areas_and_parents, records) return area_items, no_occupants
def load_areas_for_use_in_template(user: User): """ This method returns accessible areas for the user and a queryset ready to be used in template view. The template view needs to use the {% recursetree %} tag from mptt """ accessible_areas = user.accessible_areas() areas = list(set([ancestor for area in accessible_areas for ancestor in area.get_ancestors(include_self=True)])) areas.sort(key=lambda x: x.tree_category()) areas = Area.objects.filter(id__in=[area.id for area in areas]) return accessible_areas, areas
def check_user_reply_error(buddy_request: BuddyRequest, user: User) -> Optional[str]: error_message = None try: check_policy_to_enter_any_area(user) except InactiveUserError: error_message = "You cannot reply to this request because your account has been deactivated" except NoActiveProjectsForUserError: error_message = "You cannot reply to this request because you don't have any active projects" except PhysicalAccessExpiredUserError: error_message = "You cannot reply to this request because your facility access has expired" except NoPhysicalAccessUserError: error_message = "You cannot reply to this request because you do not have access to any areas" else: if buddy_request.area not in user.accessible_areas(): error_message = ( f"You cannot reply to this request because you do not have access to the {buddy_request.area.name}" ) return error_message