-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[#224] Refactor resources.py into its own module
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
Showing
21 changed files
with
1,382 additions
and
1,091 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
) |
Oops, something went wrong.