Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fixes #1758 #1766] Fix for DRF API filters #1767

Merged
merged 27 commits into from
Aug 26, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
e8cb0f0
[#1633] Remove PartnerType model and its uses
zzgvh Aug 17, 2015
9e1622d
[#1633] Refactor listing of partner types
zzgvh Aug 17, 2015
b28e2c4
[#1633] Use Partnership.iati_organisation_role instead of
zzgvh Aug 18, 2015
9ded58a
[#1633] Move offending import of Partnership model
zzgvh Aug 19, 2015
0c31cc8
[#1633] Migration creating Partnership.iati_organisation_role
zzgvh Aug 19, 2015
8b9cab3
Merge branch 'develop' into #1633_partner_types_and_iati_roles
zzgvh Aug 19, 2015
9ec6430
[#1633] Fix migration history after merging in develop
zzgvh Aug 19, 2015
af5859c
[#1633] Minor fixes
zzgvh Aug 19, 2015
4a388d3
[#1633] Migration removing a number of keywords
zzgvh Aug 19, 2015
b0ba0a2
[#1633] Fixes to migration 0024
zzgvh Aug 20, 2015
a68f480
[#1633] Migration removing unused keywords
zzgvh Aug 20, 2015
0f28902
[#1633] Add keywords for sponsor partners
zzgvh Aug 20, 2015
e1a4269
[#1633] Merge branch '#1759-editor-feedback' into #1633_partner_types…
KasperBrandt Aug 24, 2015
ce3cd11
[#1633] Updated migrations
KasperBrandt Aug 24, 2015
f42d8fa
[#1633] Fix migration 0022 dependency
KasperBrandt Aug 24, 2015
df49f4a
[#1633] Added try, except clause in case keywords do not exist
KasperBrandt Aug 24, 2015
9fc714c
[#1633] Update project editor with IATI partner roles
KasperBrandt Aug 24, 2015
e219f73
[#1633] Migrate Partnership.partner_type to Partnership.iati_organisa…
zzgvh Aug 24, 2015
105a185
[#1633] Added partner_type_2 to DRF and Tastypie
KasperBrandt Aug 25, 2015
e117bad
[#1633] Remove partner_type from Partnership model
KasperBrandt Aug 25, 2015
6de0cd7
[#1633] Updated partnership.iati_organisation_role in IATI export
KasperBrandt Aug 25, 2015
e50dc96
[#1633] Add filtering on legacy PartnershipResource.partner_type field
zzgvh Aug 25, 2015
37adae7
[Fixes #1766] Fix multiple filters on one DRF resource
KasperBrandt Aug 25, 2015
a8ba9fd
[Fixes #1758] Allow organisation filter for project updates resource
KasperBrandt Aug 25, 2015
810352b
[#1633] Renamed partner_type_2 to partner_type in APIs
KasperBrandt Aug 26, 2015
1ced055
[#1766] Merge branch '#1633_partner_types_and_iati_roles' into #1766-…
KasperBrandt Aug 26, 2015
e7105d2
[#1766] Limit target_location in location update extra serializer
KasperBrandt Aug 26, 2015
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 43 additions & 9 deletions akvo/api/resources/partnership.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
FIELD_NAME = 'name'
FIELD_LONG_NAME = 'long_name'
FIELD_NEW_ORGANISATION_TYPE = 'new_organisation_type'
FIELD_PARTNER_TYPE = 'partner_type'
FIELD_PARTNER_TYPE = 'iati_organisation_role'
ORG_FIELDS = [FIELD_IATI_ORG_ID, FIELD_NAME, FIELD_LONG_NAME, FIELD_NEW_ORGANISATION_TYPE]
# InternalOrganisationID
FIELD_INTERNAL_ORG_ID = 'internal_org_id'
Expand Down Expand Up @@ -173,7 +173,7 @@ def hydrate(self, bundle):
bundle.data[FIELD_ORGANISATION] = organisation
if (
organisation.iati_org_id != bundle.data[FIELD_REPORTING_ORG] or
bundle.data[FIELD_PARTNER_TYPE] != Partnership.SUPPORT_PARTNER
bundle.data[FIELD_PARTNER_TYPE] != Partnership.IATI_ACCOUNTABLE_PARTNER
):
bundle.data[FIELD_IATI_ACTIVITY_ID] = None
bundle.data[FIELD_INTERNAL_ID] = None
Expand All @@ -197,15 +197,48 @@ def hydrate_organisation(self, bundle):
class PartnershipResource(ConditionalFullResource):
organisation = ConditionalFullToOneField('akvo.api.resources.OrganisationResource', 'organisation')
project = ConditionalFullToOneField('akvo.api.resources.ProjectResource', 'project')
partner_type = 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
"""
super(PartnershipResource, self).__init__(api_name=None)
self.fields['partner_type'].help_text = "Uses the following key-value pair list: {%s}" % ', '.join(
['"%s": "%s"' % (k, v) for k, v in Partnership.PARTNER_TYPES]
self.fields[
'iati_organisation_role'].help_text = "Uses the following key-value pair list: {%s}" % ', '.join(
['"%s": "%s"' % (k, v) for k, v in Partnership.IATI_ROLES]
)

def apply_filters(self, request, applicable_filters):
""" Custom filtering for the "fake" field partner_type allowing the filtering of the
resource using the deprecated Akvo partner type values

Uses Partnership.PARTNER_TYPES_TO_ROLES_MAP map the filter to the
Partnership.iati_organisation_role field
"""

partner_type_filter = {}
if 'iati_role_to_partner_type' in [k.split('__')[0] for k in applicable_filters.keys()]:
for k in applicable_filters.keys():
if k.startswith('iati_role_to_partner_type'):
operator = k.split('__')[1]
if operator == 'in':
values = [
Partnership.PARTNER_TYPES_TO_ROLES_MAP[v] for v in applicable_filters[k]
]
partner_type_filter['iati_organisation_role__in'] = values
else:
partner_type_filter[
"iati_organisation_role__{}".format(operator)
] = Partnership.PARTNER_TYPES_TO_ROLES_MAP[applicable_filters[k]]
applicable_filters.pop(k)

default_filtering = super(PartnershipResource, self).apply_filters(request, applicable_filters)

if partner_type_filter:
return default_filtering.filter(**partner_type_filter)
else:
return default_filtering

class Meta:
max_limit = 10
allowed_methods = ['get']
Expand All @@ -214,10 +247,11 @@ class Meta:
filtering = dict(organisation=ALL_WITH_RELATIONS)
filtering = dict(
# other fields
iati_activity_id = ALL,
internal_id = ALL,
partner_type = ALL,
iati_activity_id = ALL,
internal_id = ALL,
partner_type = ALL,
iati_organisation_role = ALL,
# foreign keys
organisation = ALL_WITH_RELATIONS,
project = ALL_WITH_RELATIONS,
organisation = ALL_WITH_RELATIONS,
project = ALL_WITH_RELATIONS,
)
2 changes: 1 addition & 1 deletion akvo/api/resources/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ def alter_deserialized_detail_data(self, request, data):
reporting_org=temp_org['reporting_org'],
name='Incorrect business unit', #this should never be used, if it is the lookup of existing BUs is borked
long_name='Incorrect business unit',
partner_type='sponsor',
iati_organisation_role=100, # old partner type sponsor partner
new_organisation_type='21',
organisation=None,
)]
Expand Down
8 changes: 4 additions & 4 deletions akvo/api/xml/iati-xslt.xsl
Original file line number Diff line number Diff line change
Expand Up @@ -524,16 +524,16 @@
</xsl:otherwise>
</xsl:choose>
<xsl:if test="@role='Accountable'">
<partner_type>support</partner_type>
<iati_organisation_role>2</iati_organisation_role>
</xsl:if>
<xsl:if test="@role='Extending'">
<partner_type>support</partner_type>
<iati_organisation_role>3</iati_organisation_role>
</xsl:if>
<xsl:if test="@role='Funding'">
<partner_type>funding</partner_type>
<iati_organisation_role>1</iati_organisation_role>
</xsl:if>
<xsl:if test="@role='Implementing'">
<partner_type>field</partner_type>
<iati_organisation_role>4</iati_organisation_role>
</xsl:if>
</object>
</xsl:template>
Expand Down
4 changes: 1 addition & 3 deletions akvo/cordaid_org_importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from django.core.files.temp import NamedTemporaryFile

from akvo.codelists.store.codelists_v201 import ORGANISATION_TYPE as IATI_LIST_ORGANISATION_TYPE
from akvo.rsr.models import InternalOrganisationID, Organisation, PartnerType
from akvo.rsr.models import InternalOrganisationID, Organisation
from akvo.utils import model_and_instance_based_filename


Expand Down Expand Up @@ -95,8 +95,6 @@ def import_orgs(xml_file):
identifier=identifier
)
internal_org_id.save()
for partner_type in PartnerType.objects.all():
referenced_org.partner_types.add(partner_type)
except Exception, e:
action = "failed"
internal_org_id.delete()
Expand Down
9 changes: 5 additions & 4 deletions akvo/iati/checks/v201.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,15 +109,16 @@ def partners(self):
if partnership.organisation:
org = partnership.organisation
org_name = org.long_name or org.name
if partnership.partner_type and (org.iati_org_id or org_name):
if (partnership.iati_organisation_role and
partnership.iati_organisation_role < 100 and (org.iati_org_id or org_name)):
valid_partner = True
if not partnership.partner_type:
if not partnership.iati_organisation_role:
checks.append((u'error', u'missing role for partner %s' % org_name))
if not org.iati_org_id:
checks.append((u'warning', u'partner %s has no IATI identifier' % org_name))
if not org_name:
checks.append((u'warning', u'%s partner has no organisation '
u'name' % partnership.partner_type))
checks.append((u'warning', u'%s has no organisation '
u'name' % partnership.iati_organisation_role))
else:
checks.append((u'error', u'partnership has no organisation'))

Expand Down
8 changes: 5 additions & 3 deletions akvo/iati/elements/participating_org.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ def participating_org(project):
"""
partnership_elements = []

from akvo.rsr.models import Partnership

for partnership in project.partnerships.all():
org = partnership.organisation
element = etree.Element("participating-org")
Expand All @@ -32,9 +34,9 @@ def participating_org(project):

if org.new_organisation_type:
element.attrib['type'] = str(org.new_organisation_type)

if partnership.partner_type in TYPE_TO_CODE.keys():
element.attrib['role'] = TYPE_TO_CODE[partnership.partner_type]
# don't include old akvo sponsor partner value when checking
if partnership.iati_organisation_role in Partnership.IATI_ROLE_LIST[:-1]:
element.attrib['role'] = str(partnership.iati_organisation_role)

narrative_element = etree.SubElement(element, "narrative")

Expand Down
5 changes: 0 additions & 5 deletions akvo/rest/rsr_api_upload/json/organisation_1.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,6 @@
"content_owner": null,
"allow_edit": true,
"primary_location": null,
"partner_types": [
"field",
"funding",
"sponsor"
],
"internal_org_ids": []
},
"organisation_location": {
Expand Down
4 changes: 2 additions & 2 deletions akvo/rest/rsr_api_upload/json/project_1.json
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@
},
"partnership": {
"organisation": "Akvo",
"partner_type": "field",
"iati_organisation_role": "4",
"funding_amount": null,
"partner_type_extra": null,
"iati_activity_id": null,
Expand All @@ -107,7 +107,7 @@
},
"partnership": {
"organisation": "Akvo",
"partner_type": "funding",
"iati_organisation_role": "1",
"funding_amount": 4711,
"partner_type_extra": null,
"iati_activity_id": null,
Expand Down
2 changes: 0 additions & 2 deletions akvo/rest/serializers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
from .organisation_location import (OrganisationLocationSerializer,
MapOrganisationLocationSerializer)
from .partner_site import PartnerSiteSerializer
from .partner_type import PartnerTypeSerializer
from .partnership import PartnershipSerializer
from .planned_disbursement import PlannedDisbursementSerializer
from .policy_marker import PolicyMarkerSerializer
Expand Down Expand Up @@ -82,7 +81,6 @@
'OrganisationLocationSerializer',
'PartnershipSerializer',
'PartnerSiteSerializer',
'PartnerTypeSerializer',
'PlannedDisbursementSerializer',
'PolicyMarkerSerializer',
'ProjectCommentSerializer',
Expand Down
16 changes: 0 additions & 16 deletions akvo/rest/serializers/partner_type.py

This file was deleted.

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 = serializers.Field(source='iati_role_to_partner_type')

class Meta:
model = Partnership
3 changes: 3 additions & 0 deletions akvo/rest/serializers/project_update_location.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ class Meta:

class ProjectUpdateLocationExtraSerializer(ProjectUpdateLocationSerializer):

# Limit update data to its PK, this is needed because of Meta.depth = 2
location_target = serializers.Field(source='location_target.pk')

class Meta(ProjectUpdateLocationSerializer.Meta):
depth = 2

Expand Down
1 change: 0 additions & 1 deletion akvo/rest/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
router.register(r'organisation_location', views.OrganisationLocationViewSet)
router.register(r'organisation_map_location', views.MapOrganisationLocationViewSet)
router.register(r'partner_site', views.PartnerSiteViewSet)
router.register(r'partner_type', views.PartnerTypeViewSet)
router.register(r'partnership', views.PartnershipViewSet)
router.register(r'planned_disbursement', views.PlannedDisbursementViewSet)
router.register(r'policy_marker', views.PolicyMarkerViewSet)
Expand Down
2 changes: 0 additions & 2 deletions akvo/rest/views/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
from .organisation import OrganisationViewSet
from .organisation_location import OrganisationLocationViewSet, MapOrganisationLocationViewSet
from .partner_site import PartnerSiteViewSet
from .partner_type import PartnerTypeViewSet
from .partnership import PartnershipViewSet
from .planned_disbursement import PlannedDisbursementViewSet
from .policy_marker import PolicyMarkerViewSet
Expand Down Expand Up @@ -98,7 +97,6 @@
'OrganisationCustomFieldViewSet',
'PartnershipViewSet',
'PartnerSiteViewSet',
'PartnerTypeViewSet',
'PlannedDisbursementViewSet',
'PolicyMarkerViewSet',
'ProjectCommentViewSet',
Expand Down
16 changes: 0 additions & 16 deletions akvo/rest/views/internal_organisation_id.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,3 @@ class InternalOrganisationIDViewSet(BaseRSRViewSet):
serializer_class = InternalOrganisationIDSerializer
queryset = InternalOrganisationID.objects.all()
filter_fields = ('recording_org', 'referenced_org', 'identifier', )

def get_queryset(self):
"""
filter on recording org and/or identifier
"""
queryset = self.queryset
recording_org = self.request.QUERY_PARAMS.get('recording_org', None)
identifier = self.request.QUERY_PARAMS.get('identifier', None)
filter_params = {}
if recording_org is not None:
filter_params.update(recording_org__id=recording_org)
if identifier is not None:
filter_params.update(identifier=identifier)
if filter:
queryset = queryset.filter(**filter_params)
return queryset
21 changes: 0 additions & 21 deletions akvo/rest/views/organisation.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,24 +71,3 @@ class OrganisationViewSet(BaseRSRViewSet):
serializer_class = OrganisationSerializer
parser_classes = (AkvoOrganisationParser, JSONParser,)
filter_fields = ('name', 'long_name', 'iati_org_id', 'content_owner')

def get_queryset(self):
""" Enable filtering of Organisations on iati_org_id or name
"""
queryset = super(OrganisationViewSet, self).get_queryset()
pk = self.request.QUERY_PARAMS.get('id', None)
if pk is not None:
try:
queryset = queryset.filter(pk=pk)
except ValueError:
pass
iati_org_id = self.request.QUERY_PARAMS.get('iati_org_id', None)
if iati_org_id is not None:
queryset = queryset.filter(iati_org_id=iati_org_id)
name = self.request.QUERY_PARAMS.get('name', None)
if name is not None:
queryset = queryset.filter(name=name)
long_name = self.request.QUERY_PARAMS.get('long_name', None)
if long_name is not None:
queryset = queryset.filter(long_name=long_name)
return queryset
18 changes: 0 additions & 18 deletions akvo/rest/views/partner_type.py

This file was deleted.

12 changes: 11 additions & 1 deletion akvo/rest/views/partnership.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,14 @@ class PartnershipViewSet(BaseRSRViewSet):
"""
queryset = Partnership.objects.all()
serializer_class = PartnershipSerializer
filter_fields = ('project', 'organisation', 'partner_type', )
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', None)
if partner_type and partner_type in Partnership.PARTNER_TYPES_TO_ROLES_MAP.keys():
queryset = queryset.filter(
iati_organisation_role=Partnership.PARTNER_TYPES_TO_ROLES_MAP[partner_type]
)
return queryset
3 changes: 2 additions & 1 deletion akvo/rest/views/project_editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@

PARTNER_FIELDS = (
('organisation', 'value-partner-', 'related-object'),
('partner_type', 'partner-type-', 'text'),
('iati_organisation_role', 'partner-role-', 'integer'),
('funding_amount', 'funding-amount-', 'decimal'),
)

Expand Down Expand Up @@ -487,6 +487,7 @@ def log_addition(obj, user):
change_message=change_message
)


@api_view(['POST'])
@permission_classes((IsAuthenticated, ))
def project_editor_delete_document(request, project_pk=None, document_pk=None):
Expand Down
Loading