Skip to content

Commit

Permalink
[#1633] Added partner_type_2 to DRF and Tastypie
Browse files Browse the repository at this point in the history
  • Loading branch information
KasperBrandt committed Aug 25, 2015
1 parent e219f73 commit 105a185
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 0 deletions.
1 change: 1 addition & 0 deletions akvo/api/resources/partnership.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ def hydrate_organisation(self, bundle):
class PartnershipResource(ConditionalFullResource):
organisation = ConditionalFullToOneField('akvo.api.resources.OrganisationResource', 'organisation')
project = ConditionalFullToOneField('akvo.api.resources.ProjectResource', 'project')
partner_type_2 = fields.CharField(attribute='iati_role_to_partner_type')

def __init__(self, api_name=None):
""" override to be able to create custom help_text on the partner_type field
Expand Down
3 changes: 3 additions & 0 deletions akvo/rest/serializers/partnership.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
# 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 rest_framework import serializers

from akvo.rsr.models import Partnership

Expand All @@ -12,5 +13,7 @@

class PartnershipSerializer(BaseRSRSerializer):

partner_type_2 = serializers.Field(source='iati_role_to_partner_type')

class Meta:
model = Partnership
10 changes: 10 additions & 0 deletions akvo/rest/views/partnership.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,13 @@ class PartnershipViewSet(BaseRSRViewSet):
queryset = Partnership.objects.all()
serializer_class = PartnershipSerializer
filter_fields = ('project', 'organisation', 'iati_organisation_role', )

def get_queryset(self):
"""Allow filtering on partner_type."""
queryset = self.queryset
partner_type = self.request.QUERY_PARAMS.get('partner_type_2', None)
if partner_type and partner_type in Partnership.PARTNER_TYPES_TO_ROLES_MAP.keys():
queryset = self.queryset.filter(
iati_organisation_role=Partnership.PARTNER_TYPES_TO_ROLES_MAP[partner_type]
)
return queryset
6 changes: 6 additions & 0 deletions akvo/rsr/models/partnership.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,12 @@ class Partnership(models.Model):
def iati_organisation_role_label(self):
return dict(self.IATI_ROLES)[self.iati_organisation_role]

def iati_role_to_partner_type(self):
if self.iati_organisation_role:
return self.ROLES_TO_PARTNER_TYPES_MAP[int(self.iati_organisation_role)]
else:
return None

class Meta:
app_label = 'rsr'
verbose_name = _(u'project partner')
Expand Down

0 comments on commit 105a185

Please sign in to comment.