Skip to content

Commit

Permalink
[#113] Add new resources for the API driven maps
Browse files Browse the repository at this point in the history
Add 'map_for_project' and 'map_for_organisation' resources that produce
a lean dataset for use with the API driven maps. The resources don't
include any foreign key data except the locations and also excludes a
number of long text fields.
  • Loading branch information
zzgvh committed Dec 11, 2012
1 parent c8a4d1f commit 153ac11
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 3 deletions.
84 changes: 84 additions & 0 deletions akvo/api/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,12 @@
'LinkResource',
'OrganisationResource',
'OrganisationLocationResource',
'OrganisationMapResource',
'PartnershipResource',
'ProjectResource',
'ProjectCommentResource',
'ProjectLocationResource',
'ProjectMapResource',
'ProjectUpdateResource',
'UserResource',
'UserProfileResource',
Expand Down Expand Up @@ -317,6 +319,41 @@ class Meta:
)


class OrganisationMapResource(ConditionalFullResource):
"""
a limited resource for delivering data to be used when creating maps
"""
locations = ConditionalFullToManyField('akvo.api.resources.OrganisationLocationResource', 'locations', null=True)
primary_location = fields.ToOneField('akvo.api.resources.OrganisationLocationResource', 'primary_location', full=True, null=True)

class Meta:
allowed_methods = ['get']
queryset = Organisation.objects.all()
resource_name = 'map_for_organisation'
include_absolute_url = True

filtering = dict(
# other fields
iati_org_id = ALL,
name = ALL,
organisation_type = ALL,
# foreign keys
locations = ALL_WITH_RELATIONS,
partnerships = ALL_WITH_RELATIONS,
)

def dehydrate(self, bundle):
""" add thumbnails inline info for Organisation.logo
"""
bundle = super(OrganisationMapResource, self).dehydrate(bundle)
del bundle.data['description']
bundle.data['logo'] = {
'original': bundle.data['logo'],
'thumbnails': get_extra_thumbnails(bundle.obj.logo),
}
return bundle


class PartnershipResource(ConditionalFullResource):
organisation = ConditionalFullToOneField('akvo.api.resources.OrganisationResource', 'organisation')
project = ConditionalFullToOneField('akvo.api.resources.ProjectResource', 'project')
Expand Down Expand Up @@ -430,6 +467,53 @@ class Meta:
)


class ProjectMapResource(ConditionalFullResource):
"""
a limited resource for delivering data to be used when creating maps
"""
locations = ConditionalFullToManyField('akvo.api.resources.ProjectLocationResource', 'locations')
primary_location = fields.ToOneField('akvo.api.resources.ProjectLocationResource', 'primary_location', full=True, null=True)

class Meta:
allowed_methods = ['get']
queryset = Project.objects.published()
resource_name = 'map_for_project'
include_absolute_url = True

filtering = dict(
# other fields
status = ALL,
title = ALL,
budget = ALL,
funds = ALL,
funds_needed = ALL,
# foreign keys
benchmarks = ALL_WITH_RELATIONS,
budget_items = ALL_WITH_RELATIONS,
categories = ALL_WITH_RELATIONS,
goals = ALL_WITH_RELATIONS,
invoices = ALL_WITH_RELATIONS,
links = ALL_WITH_RELATIONS,
locations = ALL_WITH_RELATIONS,
partnerships = ALL_WITH_RELATIONS,
project_comments = ALL_WITH_RELATIONS,
project_updates = ALL_WITH_RELATIONS,
)

def dehydrate(self, bundle):
""" add thumbnails inline info for Project.current_image
"""
bundle = super(ProjectMapResource, self).dehydrate(bundle)
ignored_fields = ('goals_overview', 'current_status', 'project_plan', 'sustainability', 'background', 'project_rating', 'notes',)
for field in ignored_fields:
del bundle.data[field]
bundle.data['current_image'] = {
'original': bundle.data['current_image'],
'thumbnails': get_extra_thumbnails(bundle.obj.current_image),
}
return bundle


class ProjectUpdateResource(ConditionalFullResource):
project = ConditionalFullToOneField('akvo.api.resources.ProjectResource', 'project')
user = ConditionalFullToOneField('akvo.api.resources.UserResource', 'user')
Expand Down

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion akvo/mediaroot/akvo/js/src/akvo-maps.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,9 @@
getResourceUrl = function (map) {
var opts, url, limit;
opts = map.mapOpts;
// call /api/v1/map_for_project/ or /api/v1/map_for_organisation/ resources
//TODO: derive the host from the current page URL instead maybe?
url = opts.host + 'api/v1/' + opts.resource + '/';
url = opts.host + 'api/v1/map_for_' + opts.resource + '/';
//limit = 0 means all objects. If this becomes too heavy limit can be set to get the objects in multiple chunks
limit = 0;
// if object_id holds a value then that's the ID of the object we want to fetch
Expand Down
2 changes: 1 addition & 1 deletion akvo/scripts/asset_manager/map.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
BUNDLE_MAP = {'partner_sites_style': {'path': 'partner_sites/css/', 'hash': '3b28cc631adecc91703230abb41b0c8125065c27', 'type': 'css'}, 'widget_project_updates': {'path': 'widgets/project_updates/css/', 'hash': '57ddaa0abcaef475a634bc2d6d0d59dc924ba109', 'type': 'css'}, 'akvo_ps_machinery_js': {'path': 'partner_sites/js/', 'hash': '2fcbb22efb57981a99cd9746f3b98575b4fa2b3c', 'type': 'js'}, 'widget_project_small': {'path': 'widgets/project_small/css/', 'hash': '984d858f5eff775d79c942a17358e5817a4092e2', 'type': 'css'}, 'widget_cobranded_leader': {'path': 'widgets/cobranded_leader/css/', 'hash': '1ae4b5057b7a715f2e00c20588bdc20a119dff39', 'type': 'css'}, 'partner_sites_script': {'path': 'partner_sites/js/', 'hash': 'a039f7e10cb45144d27a621a0469d61851dd5d02', 'type': 'js'}, 'akvo_ie6_style': {'path': 'akvo/css/', 'hash': '2c103c8bc0834a3e4145e6ce86541613b898d552', 'type': 'css'}, 'widget_cobranded_banner': {'path': 'widgets/cobranded_banner/css/', 'hash': '31ddbde98bffa9e8705c4b9a945d94202e20131b', 'type': 'css'}, 'widget_cobranded_narrow': {'path': 'widgets/cobranded_narrow/css/', 'hash': '572e7203abead79cf733c5687d67a9b4f8cf0a0c', 'type': 'css'}, 'akvo_machinery_js': {'path': 'akvo/js/', 'hash': 'c88b72cf88f3f6cb1803e5907a35f3e19e8e5b3b', 'type': 'js'}, 'widget_project_list': {'path': 'widgets/project_list/css/', 'hash': '64aaf44be5a55cabef3a0713a826c365eb7c20f2', 'type': 'css'}, 'widget_project': {'path': 'widgets/project/css/', 'hash': '4611d301f23eb9e63ed276d0b51dea16def73b52', 'type': 'css'}, 'widget_project_contribute': {'path': 'widgets/project_contribute/css/', 'hash': '36d113a496eec7965e63b0d67728a0612f8673e4', 'type': 'css'}, 'akvo_script': {'path': 'akvo/js/', 'hash': '4382c3c6a13a12372c2f3f7a60b11486570d5c75', 'type': 'js'}, 'akvo_style': {'path': 'akvo/css/', 'hash': '0fed68a1b4c8f0e737f7d288a0eb672a47551f6e', 'type': 'css'}, 'widget_cobranded_short': {'path': 'widgets/cobranded_short/css/', 'hash': '9b3f65379cf13146713cca113e6e8d2f621ab4de', 'type': 'css'}, 'widget_project_narrow': {'path': 'widgets/project_narrow/css/', 'hash': '984d858f5eff775d79c942a17358e5817a4092e2', 'type': 'css'}}
BUNDLE_MAP = {'partner_sites_style': {'path': 'partner_sites/css/', 'hash': '3b28cc631adecc91703230abb41b0c8125065c27', 'type': 'css'}, 'widget_project_updates': {'path': 'widgets/project_updates/css/', 'hash': '57ddaa0abcaef475a634bc2d6d0d59dc924ba109', 'type': 'css'}, 'akvo_ps_machinery_js': {'path': 'partner_sites/js/', 'hash': '2fcbb22efb57981a99cd9746f3b98575b4fa2b3c', 'type': 'js'}, 'widget_project_small': {'path': 'widgets/project_small/css/', 'hash': '984d858f5eff775d79c942a17358e5817a4092e2', 'type': 'css'}, 'widget_cobranded_leader': {'path': 'widgets/cobranded_leader/css/', 'hash': '1ae4b5057b7a715f2e00c20588bdc20a119dff39', 'type': 'css'}, 'partner_sites_script': {'path': 'partner_sites/js/', 'hash': 'a039f7e10cb45144d27a621a0469d61851dd5d02', 'type': 'js'}, 'akvo_ie6_style': {'path': 'akvo/css/', 'hash': '2c103c8bc0834a3e4145e6ce86541613b898d552', 'type': 'css'}, 'widget_cobranded_banner': {'path': 'widgets/cobranded_banner/css/', 'hash': '31ddbde98bffa9e8705c4b9a945d94202e20131b', 'type': 'css'}, 'widget_cobranded_narrow': {'path': 'widgets/cobranded_narrow/css/', 'hash': '572e7203abead79cf733c5687d67a9b4f8cf0a0c', 'type': 'css'}, 'akvo_machinery_js': {'path': 'akvo/js/', 'hash': 'c88b72cf88f3f6cb1803e5907a35f3e19e8e5b3b', 'type': 'js'}, 'widget_project_list': {'path': 'widgets/project_list/css/', 'hash': '64aaf44be5a55cabef3a0713a826c365eb7c20f2', 'type': 'css'}, 'widget_project': {'path': 'widgets/project/css/', 'hash': '4611d301f23eb9e63ed276d0b51dea16def73b52', 'type': 'css'}, 'widget_project_contribute': {'path': 'widgets/project_contribute/css/', 'hash': '36d113a496eec7965e63b0d67728a0612f8673e4', 'type': 'css'}, 'akvo_script': {'path': 'akvo/js/', 'hash': '35a8490eec7ef52da368ee4ada57a71784bd45c2', 'type': 'js'}, 'akvo_style': {'path': 'akvo/css/', 'hash': '0fed68a1b4c8f0e737f7d288a0eb672a47551f6e', 'type': 'css'}, 'widget_cobranded_short': {'path': 'widgets/cobranded_short/css/', 'hash': '9b3f65379cf13146713cca113e6e8d2f621ab4de', 'type': 'css'}, 'widget_project_narrow': {'path': 'widgets/project_narrow/css/', 'hash': '984d858f5eff775d79c942a17358e5817a4092e2', 'type': 'css'}}

0 comments on commit 153ac11

Please sign in to comment.