Skip to content

Commit

Permalink
Merge pull request #1844 from akvo/#1843_refactor_content_owned_orgs
Browse files Browse the repository at this point in the history
[#1843] Refactor the Organisation.content_owned_organisations methods
  • Loading branch information
KasperBrandt committed Oct 6, 2015
2 parents a4e53d0 + cec61be commit 2fbafdc
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 24 deletions.
28 changes: 9 additions & 19 deletions akvo/rsr/models/organisation.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

from django.conf import settings
from django.db import models
from django.db.models import Sum
from django.db.models import Sum, Q
from django.db.models.query import QuerySet as DjangoQuerySet
from django.utils.translation import ugettext_lazy as _

Expand Down Expand Up @@ -249,13 +249,14 @@ def content_owned_organisations(self):
Returns a list of Organisations of which these organisations are the content owner.
Includes self, is recursive.
"""
org_set = set()

for org in self:
for co_org in org.content_owned_organisations():
org_set.add(co_org)

return list(org_set)
kids = Organisation.objects.filter(content_owner__in=self).exclude(organisation=self)
if kids:
return Organisation.objects.filter(
Q(pk__in=self.values_list('pk', flat=True)) | Q(pk__in=kids.content_owned_organisations().values_list('pk', flat=True))
)
else:
return self

def __unicode__(self):
return self.name
Expand Down Expand Up @@ -312,18 +313,7 @@ def content_owned_organisations(self):
Returns a list of Organisations of which this organisation is the content owner.
Includes self and is recursive.
"""
org_set = set()
org_set.add(self)

self_content_owned_list = list(Organisation.objects.filter(content_owner=self))

while self_content_owned_list:
org = self_content_owned_list.pop()
org_set.add(org)
for co_org in org.content_owned_organisations():
org_set.add(co_org)

return list(org_set)
return Organisation.objects.filter(content_owner=self).content_owned_organisations()

def countries_where_active(self):
"""Returns a Country queryset of countries where this organisation has
Expand Down
9 changes: 4 additions & 5 deletions akvo/rsr/views/my_rsr.py
Original file line number Diff line number Diff line change
Expand Up @@ -377,11 +377,10 @@ def user_management(request):
employments = Employment.objects.select_related().\
prefetch_related('country', 'group').order_by('-id')
else:
organisations_list = user.employers.approved().organisations().content_owned_organisations()
for count, org in enumerate(organisations_list):
if not user.has_perm('rsr.user_management', org):
organisations_list.pop(count)
organisations = Organisation.objects.filter(pk__in=[org.pk for org in organisations_list])
connected_orgs = user.employers.approved().organisations().content_owned_organisations()
connected_orgs_list = [
org.pk for org in connected_orgs if user.has_perm('rsr.user_management', org)]
organisations = Organisation.objects.filter(pk__in=connected_orgs_list)
employments = organisations.employments().exclude(user=user).select_related().\
prefetch_related('country', 'group').order_by('-id')

Expand Down

0 comments on commit 2fbafdc

Please sign in to comment.