Source code for akvo.rsr.models.model_querysets.organisation

# -*- coding: utf-8 -*-

# Akvo Reporting is covered by the GNU Affero General Public License.
# See more details in the license.txt file located at the root folder of the Akvo RSR module.
# For additional details on the GNU license please see < http://www.gnu.org/licenses/agpl.html >.


from django.apps import apps
from django.contrib.auth import get_user_model
from django.db import models


[docs]class OrganisationManager(models.Manager):
[docs] def get_queryset(self): return super(OrganisationManager, self).get_queryset().extra( select={ 'lower_name': 'lower(rsr_organisation.name)' } ).order_by('lower_name')
[docs]class OrganisationQuerySet(models.QuerySet):
[docs] def has_location(self): return self.filter(primary_location__isnull=False)
[docs] def partners(self, role): "return the organisations in the queryset that are partners of type role" return self.filter(partnerships__iati_organisation_role__exact=role).distinct()
[docs] def allpartners(self): return self.distinct()
[docs] def fieldpartners(self): Partnership = apps.get_model('rsr.partnership') return self.partners(Partnership.IATI_IMPLEMENTING_PARTNER)
[docs] def fundingpartners(self): Partnership = apps.get_model('rsr.partnership') return self.partners(Partnership.IATI_FUNDING_PARTNER)
[docs] def reportingpartners(self): Partnership = apps.get_model('rsr.partnership') return self.partners(Partnership.IATI_REPORTING_ORGANISATION)
[docs] def sponsorpartners(self): Partnership = apps.get_model('rsr.partnership') return self.partners(Partnership.AKVO_SPONSOR_PARTNER)
[docs] def supportpartners(self): Partnership = apps.get_model('rsr.partnership') return self.partners(Partnership.IATI_ACCOUNTABLE_PARTNER)
[docs] def extendingpartners(self): Partnership = apps.get_model('rsr.partnership') return self.partners(Partnership.IATI_EXTENDING_PARTNER)
[docs] def supportpartners_with_projects(self): """return the organisations in the queryset that are support partners with published projects, not counting archived projects""" Partnership = apps.get_model('rsr.partnership') Project = apps.get_model('rsr.project') PublishingStatus = apps.get_model('rsr.publishingstatus') return self.filter( partnerships__iati_organisation_role=Partnership.IATI_ACCOUNTABLE_PARTNER, partnerships__project__publishingstatus__status=PublishingStatus.STATUS_PUBLISHED, partnerships__project__iati_status__in=Project.NOT_SUSPENDED ).distinct()
[docs] def ngos(self): from ..organisation import ORG_TYPE_NGO return self.filter(organisation_type__exact=ORG_TYPE_NGO)
[docs] def governmental(self): from ..organisation import ORG_TYPE_GOV return self.filter(organisation_type__exact=ORG_TYPE_GOV)
[docs] def commercial(self): from ..organisation import ORG_TYPE_COM return self.filter(organisation_type__exact=ORG_TYPE_COM)
[docs] def knowledge(self): from ..organisation import ORG_TYPE_KNO return self.filter(organisation_type__exact=ORG_TYPE_KNO)
[docs] def all_projects(self): "returns a queryset with all projects that has self as any kind of partner" Project = apps.get_model('rsr.project') return Project.objects.of_partners(self).distinct()
[docs] def users(self): return get_user_model().objects.filter(employers__organisation__in=self).distinct()
[docs] def all_updates(self): ProjectUpdate = apps.get_model('rsr.projectupdate') return ProjectUpdate.objects.filter(user__organisations__in=self).distinct()
[docs] def employments(self): Employment = apps.get_model('rsr.employment') return Employment.objects.filter(organisation__in=self).distinct()
[docs] def content_owned_organisations(self, exclude_orgs=None): """Returns a list of Organisations of which these organisations are the content owner. Includes self, is recursive. The exclude_orgs parameter is used to avoid recursive calls that can happen in case there are organisations that set each other as content owned organisations. """ Organisation = apps.get_model('rsr.organisation') result = set() for org in self: result = result | set( org.content_owned_organisations(exclude_orgs=exclude_orgs).values_list('pk', flat=True) ) return Organisation.objects.filter(pk__in=result).distinct()
OrgManager = OrganisationManager.from_queryset(OrganisationQuerySet)