def restore(self, user): group_dashboard_mappings = DomainMapping.objects.get_group_dashboard_mappings( self.default_group.id) group_dashboard_clone_mappings = DomainMapping.objects. \ get_group_dashboard_clone_mappings(group_dashboard_mappings) user_dashboards_for_group = Dashboard.objects.filter(pk__in=list( group_dashboard_clone_mappings.values_list("src_id", flat=True)), user=user) for user_dashboard in user_dashboards_for_group: Dashboard.restore(user_dashboard) group_dashboard_ids = group_dashboard_mappings.values_list("dest_id", flat=True) user_cloned_dashboards_ids_for_group = DomainMapping.objects.filter( src_id__in=list( user_dashboards_for_group.values_list("id", flat=True)), src_type=MappingType.dashboard, relationship_type=RelationshipType.cloneOf, dest_type=MappingType.dashboard).values_list("dest_id", flat=True) # Create missing dashboards # TODO: extract this into a method for reuse. Same logic exists in people/models.py # TODO: modify this to use .exclude(). This was originally refactored, because of issues # using .difference() on querysets with mysql. missing_dashboard_ids = [] for group_dashboard_id in group_dashboard_ids: if group_dashboard_id not in user_cloned_dashboards_ids_for_group: missing_dashboard_ids.append(group_dashboard_id) Dashboard.create_missing_dashboards_for_user(user, missing_dashboard_ids)
def sync_dashboards(self): from stacks.models import StackGroups, Stack from domain_mappings.models import DomainMapping import uuid group_ids = list(self.groups.values_list("id", flat=True)) # Default groups that this user is in, represents user's direct assignment to stacks default_group_ids = [] for stacks in self.get_directly_assigned_stacks(): default_group_ids.append(stacks.default_group.id) # Get default groups from groups assigned to stacks stack_ids_assigned_through_group = [] for stack in self.get_group_assigned_stacks(): stack_ids_assigned_through_group.append(stack.id) default_group_ids_from_stack_groups_assignment = Stack.objects.filter(pk__in=stack_ids_assigned_through_group) \ .values_list("default_group_id", flat=True) # List of all default groups from stacks assigned to user and remove any duplicates default_group_ids_for_stacks = list( set(default_group_ids + list(default_group_ids_from_stack_groups_assignment))) group_dashboard_ids = DomainMapping.objects.filter( src_id__in=default_group_ids_for_stacks, src_type=MappingType.group, relationship_type=RelationshipType.owns, dest_type=MappingType.dashboard).values_list("dest_id", flat=True) user_dashboards = Dashboard.objects.filter(user=self) user_cloned_dashboards_ids = DomainMapping.objects.filter( src_id__in=list(user_dashboards.values_list("id", flat=True)), src_type=MappingType.dashboard, relationship_type=RelationshipType.cloneOf, dest_type=MappingType.dashboard).values_list("dest_id", flat=True) # Create missing dashboards # TODO: extract this into a method for reuse. Same logic exists in stacks/models.py # TODO: modify this to use .exclude(). This was originally refactored, because of issues # using .difference() on querysets with mysql. missing_dashboard_ids = [] for group_dashboard_id in group_dashboard_ids: if group_dashboard_id not in user_cloned_dashboards_ids: missing_dashboard_ids.append(group_dashboard_id) Dashboard.create_missing_dashboards_for_user(self, missing_dashboard_ids)