Skip to content

Commit

Permalink
[#224] Refactor resources.py into its own module
Browse files Browse the repository at this point in the history
Split resources.py into separate files under api/resources.
Some resources are grouped in one file based on sharing of the
underlying model or function of the resource.
  • Loading branch information
zzgvh committed Jul 18, 2013
1 parent b04e5ee commit 9de1374
Show file tree
Hide file tree
Showing 21 changed files with 1,382 additions and 1,091 deletions.
1,091 changes: 0 additions & 1,091 deletions akvo/api/resources.py

This file was deleted.

56 changes: 56 additions & 0 deletions akvo/api/resources/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# -*- coding: utf-8 -*-

# Akvo RSR 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 .benchmark import BenchmarknameResource, BenchmarkResource
from .budget_item import BudgetItemLabelResource, BudgetItemResource, IATIBudgetItemResource
from .category import CategoryResource
from .country import CountryResource
from .focus_area import FocusAreaResource
from .goal import GoalResource, IATIGoalResource
from .invoice import InvoiceResource
from .link import LinkResource
from .location import IATIProjectLocationResource, OrganisationLocationResource, ProjectLocationResource
from .map import OrganisationMapResource, ProjectMapResource
from .organisation import OrganisationResource
from .partnership import PartnershipResource, IATIPartnershipResource
from .project import ProjectResource, IATIProjectResource
from .project_comment import ProjectCommentResource
from .project_update import ProjectUpdateResource
from .right_now_in_akvo import RightNowInAkvoResource
from .user import UserResource
from .user_profile import UserProfileResource

__all__ = [
'BenchmarkResource',
'BenchmarknameResource',
'BudgetItemResource',
'BudgetItemLabelResource',
'CategoryResource',
'CountryResource',
'FocusAreaResource',
'GoalResource',
'IATIBudgetItemResource',
'IATIGoalResource',
'IATIPartnershipResource',
'IATIProjectLocationResource',
'IATIProjectResource',
'InvoiceResource',
'LinkResource',
'OrganisationResource',
'OrganisationLocationResource',
'OrganisationMapResource',
'PartnershipResource',
'ProjectResource',
'ProjectCommentResource',
'ProjectLocationResource',
'ProjectMapResource',
'ProjectUpdateResource',
'RightNowInAkvoResource',
'UserResource',
'UserProfileResource',
]

42 changes: 42 additions & 0 deletions akvo/api/resources/benchmark.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# -*- coding: utf-8 -*-

# Akvo RSR 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 tastypie.constants import ALL, ALL_WITH_RELATIONS

from akvo.api.fields import ConditionalFullToOneField

from akvo.rsr.models import Benchmark, Benchmarkname

from .resources import ConditionalFullResource


class BenchmarkResource(ConditionalFullResource):
project = ConditionalFullToOneField('akvo.api.resources.ProjectResource', 'project')
category = ConditionalFullToOneField('akvo.api.resources.CategoryResource', 'category')
name = ConditionalFullToOneField('akvo.api.resources.BenchmarknameResource', 'name', full=True)

class Meta:
allowed_methods = ['get']
queryset = Benchmark.objects.all()
resource_name = 'benchmark'
filtering = dict(
# foreign keys
category = ALL_WITH_RELATIONS,
name = ALL_WITH_RELATIONS,
project = ALL_WITH_RELATIONS,
)


class BenchmarknameResource(ConditionalFullResource):
class Meta:
allowed_methods = ['get']
queryset = Benchmarkname.objects.all()
resource_name = 'benchmarkname'
filtering = dict(
# other fields
name = ALL,
)
67 changes: 67 additions & 0 deletions akvo/api/resources/budget_item.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# -*- coding: utf-8 -*-

# Akvo RSR 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.core.urlresolvers import reverse

from tastypie import fields

from tastypie.authorization import Authorization
from tastypie.constants import ALL, ALL_WITH_RELATIONS
from tastypie.resources import ModelResource

from akvo.api.authentication import ConditionalApiKeyAuthentication
from akvo.api.fields import ConditionalFullToOneField

from akvo.rsr.models import BudgetItem, BudgetItemLabel

from .resources import ConditionalFullResource


class IATIBudgetItemResource(ModelResource):
project = fields.ToOneField('akvo.api.resources.IATIProjectResource', 'project', full=True,)
label = fields.ToOneField('akvo.api.resources.BudgetItemLabelResource', 'label',)

class Meta:
allowed_methods = ['post']
resource_name = 'iati_budget_item'
authorization = Authorization()
authentication = ConditionalApiKeyAuthentication(methods_requiring_key=['POST'])
queryset = BudgetItem.objects.all()

def hydrate_label(self, bundle):
bundle.data['label'] = reverse(
'api_dispatch_detail', kwargs={
'resource_name':'budget_item_label', 'api_name': 'v1', 'pk': bundle.data['label']
}
)
return bundle


class BudgetItemResource(ConditionalFullResource):
label = ConditionalFullToOneField('akvo.api.resources.BudgetItemLabelResource', 'label', full=True)
project = ConditionalFullToOneField('akvo.api.resources.ProjectResource', 'project')

class Meta:
allowed_methods = ['get']
queryset = BudgetItem.objects.all()
resource_name = 'budget_item'
filtering = dict(
# foreign keys
label = ALL_WITH_RELATIONS,
project = ALL_WITH_RELATIONS,
)


class BudgetItemLabelResource(ConditionalFullResource):
class Meta:
allowed_methods = ['get']
queryset = BudgetItemLabel.objects.all()
resource_name = 'budget_item_label'
filtering = dict(
# other fields
label = ALL,
)
25 changes: 25 additions & 0 deletions akvo/api/resources/category.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# -*- coding: utf-8 -*-

# Akvo RSR 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 tastypie.constants import ALL, ALL_WITH_RELATIONS

from akvo.rsr.models import Category

from .resources import ConditionalFullResource


class CategoryResource(ConditionalFullResource):
class Meta:
allowed_methods = ['get']
queryset = Category.objects.all()
resource_name = 'category'
filtering = dict(
# other fields
name = ALL,
# foreign keys
focus_area = ALL_WITH_RELATIONS,
)
24 changes: 24 additions & 0 deletions akvo/api/resources/country.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# -*- coding: utf-8 -*-

# Akvo RSR 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 tastypie.constants import ALL

from akvo.rsr.models import Country

from .resources import ConditionalFullResource


class CountryResource(ConditionalFullResource):
class Meta:
allowed_methods = ['get']
queryset = Country.objects.all()
resource_name = 'country'
filtering = dict(
# other fields
iso_code = ALL,
continent_code = ALL,
)
27 changes: 27 additions & 0 deletions akvo/api/resources/focus_area.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# -*- coding: utf-8 -*-

# Akvo RSR 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 tastypie.constants import ALL

from akvo.api.fields import ConditionalFullToManyField

from akvo.rsr.models import FocusArea

from .resources import ConditionalFullResource


class FocusAreaResource(ConditionalFullResource):
categories = ConditionalFullToManyField('akvo.api.resources.CategoryResource', 'categories')

class Meta:
allowed_methods = ['get']
queryset = FocusArea.objects.all()
resource_name = 'focus_area'
filtering = dict(
# other fields
slug = ALL,
)
42 changes: 42 additions & 0 deletions akvo/api/resources/goal.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# -*- coding: utf-8 -*-

# Akvo RSR 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 tastypie import fields

from tastypie.authorization import Authorization
from tastypie.constants import ALL_WITH_RELATIONS
from tastypie.resources import ModelResource

from akvo.api.authentication import ConditionalApiKeyAuthentication
from akvo.api.fields import ConditionalFullToOneField

from akvo.rsr.models import Goal

from .resources import ConditionalFullResource


class IATIGoalResource(ModelResource):
project = fields.ToOneField('akvo.api.resources.IATIProjectResource', 'project', full=True,)

class Meta:
allowed_methods = ['post']
resource_name = 'iati_goal'
authorization = Authorization()
authentication = ConditionalApiKeyAuthentication(methods_requiring_key=['POST'])
queryset = Goal.objects.all()


class GoalResource(ConditionalFullResource):
project = ConditionalFullToOneField('akvo.api.resources.ProjectResource', 'project')

class Meta:
allowed_methods = ['get']
queryset = Goal.objects.all()
resource_name = 'goal'
filtering = dict(
# foreign keys
project = ALL_WITH_RELATIONS,
)
39 changes: 39 additions & 0 deletions akvo/api/resources/invoice.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# -*- coding: utf-8 -*-

# Akvo RSR 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 tastypie.constants import ALL_WITH_RELATIONS

from akvo.api.fields import ConditionalFullToOneField

from akvo.rsr.models import Invoice
from akvo.rsr.utils import PAYPAL_INVOICE_STATUS_COMPLETE

from .resources import ConditionalFullResource


class InvoiceResource(ConditionalFullResource):
project = ConditionalFullToOneField('akvo.api.resources.ProjectResource', 'project')

class Meta:
allowed_methods = ['get']
queryset = Invoice.objects.filter(status__exact=PAYPAL_INVOICE_STATUS_COMPLETE)
resource_name = 'invoice'
fields = ['amount', 'amount_received', 'is_anonymous',]
filtering = dict(
# foreign keys
project = ALL_WITH_RELATIONS,
user = ALL_WITH_RELATIONS,
)

def dehydrate(self, bundle):
""" Add name and email for non-anonymous donators
"""
bundle = super(InvoiceResource, self).dehydrate(bundle)
if not bundle.obj.is_anonymous:
bundle.data['email'] = bundle.obj.email
bundle.data['name'] = bundle.obj.name
return bundle
27 changes: 27 additions & 0 deletions akvo/api/resources/link.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# -*- coding: utf-8 -*-

# Akvo RSR 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 tastypie.constants import ALL_WITH_RELATIONS

from akvo.api.fields import ConditionalFullToOneField

from akvo.rsr.models import Link

from .resources import ConditionalFullResource


class LinkResource(ConditionalFullResource):
project = ConditionalFullToOneField('akvo.api.resources.ProjectResource', 'project')

class Meta:
allowed_methods = ['get']
queryset = Link.objects.all()
resource_name = 'link'
filtering = dict(
# foreign keys
project = ALL_WITH_RELATIONS,
)
Loading

0 comments on commit 9de1374

Please sign in to comment.