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
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
21
22 groups = Group.objects.filter(name__in=settings.SHARING_TO_PUBLIC_GROUPS)
23 else:
24 if user.is_staff:
25
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
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
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
59 if exclude_models and collection_model in exclude_models:
60 continue
61
62 exclude_models = None
63 if collection_model == self.model:
64
65 exclude_models = [self.model]
66
67
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
74 for sc in shared_containers:
75
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
90 return self.filter(
91 models.Q(
92 sharing_groups__permissions=perm,
93 sharing_groups__in=groups
94 )
95 ).distinct()
96