Package madrona :: Package features :: Module managers
[hide private]

Source Code for Module madrona.features.managers

 1  from django.contrib.gis.db import models 
 2  from django.contrib.auth.models import User, Group, Permission 
 3  from django.conf import settings 
 4  from madrona.features import registered_models 
 5   
6 -class ShareableGeoManager(models.GeoManager):
7 - def shared_with_user(self, user, filter_groups=None, exclude_models=None):
8 """ 9 Returns a queryset containing any objects that have been 10 shared with a group the user belongs to. 11 12 Assumes that the model has been setup according to the instructions 13 for implementing a shared model. 14 """ 15 app_name = self.model._meta.app_label 16 model_name = self.model.__name__.lower() 17 perm = Permission.objects.get(codename='can_share_features') 18 19 if user.is_anonymous() or not user.is_authenticated(): 20 # public users get special treatment - 21 # ONLY get to see anything shared with a public group 22 groups = Group.objects.filter(name__in=settings.SHARING_TO_PUBLIC_GROUPS) 23 else: 24 if user.is_staff: 25 # Staff users get their groups,plus 'shared_to_staff_groups', plus public groups 26 groups = Group.objects.filter( 27 models.Q( 28 pk__in=[x.pk for x in user.groups.all()] 29 ) | 30 models.Q( 31 name__in=settings.SHARING_TO_PUBLIC_GROUPS 32 ) | 33 models.Q( 34 name__in=settings.SHARING_TO_STAFF_GROUPS 35 ) 36 ).distinct() 37 else: 38 # Non-staff authenticated users get their groups plus public groups, MINUS shared_to_staff groups 39 groups = Group.objects.filter( 40 models.Q( 41 pk__in=[x.pk for x in user.groups.all()] 42 ) | 43 models.Q( 44 name__in=settings.SHARING_TO_PUBLIC_GROUPS 45 ) 46 ).distinct().exclude(name__in=settings.SHARING_TO_STAFF_GROUPS) 47 48 if filter_groups and len(filter_groups) > 0: 49 groups = groups.filter(pk__in=[x.pk for x in filter_groups]) 50 else: 51 filter_groups = None 52 53 # Check for a Container 54 potential_parents = self.model.get_options().get_potential_parents() 55 if potential_parents: 56 contained_ids = [] 57 for collection_model in potential_parents: 58 # Avoid infinite recursion 59 if exclude_models and collection_model in exclude_models: 60 continue 61 62 exclude_models = None 63 if collection_model == self.model: 64 # On the next recurisive go-round, avoid infinite recursion 65 exclude_models = [self.model] 66 67 # Get container objects shared with user 68 if filter_groups: 69 shared_containers = collection_model.objects.shared_with_user(user,filter_groups=filter_groups,exclude_models=exclude_models) 70 else: 71 shared_containers = collection_model.objects.shared_with_user(user,exclude_models=exclude_models) 72 73 # Create list of contained object ids 74 for sc in shared_containers: 75 #contained = sc.__getattribute__(shared_content_type.container_set_property) 76 contained = sc.feature_set(recurse=True,feature_classes=[self.model]) 77 contained_ids.extend([x.id for x in contained]) 78 79 return self.filter( 80 models.Q( 81 sharing_groups__permissions=perm, 82 sharing_groups__in=groups 83 ) | 84 models.Q( 85 pk__in=contained_ids 86 ) 87 ).distinct() 88 else: 89 # No containers, just a straight 'is it shared' query 90 return self.filter( 91 models.Q( 92 sharing_groups__permissions=perm, 93 sharing_groups__in=groups 94 ) 95 ).distinct()
96