Skip to content

Commit

Permalink
Merge pull request #1133 from akvo/1118_typeahead
Browse files Browse the repository at this point in the history
[#1118] typeahead
  • Loading branch information
KasperBrandt committed Feb 19, 2015
2 parents f72b25e + afe6563 commit 67dcdb6
Show file tree
Hide file tree
Showing 18 changed files with 430 additions and 213 deletions.
38 changes: 25 additions & 13 deletions akvo/rest/serializers/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
# -*- 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 >.
"""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 BenchmarkSerializer
from .benchmark_name import BenchmarknameSerializer
Expand Down Expand Up @@ -32,7 +35,8 @@
from .project_contact import ProjectContactSerializer
from .project_document import ProjectDocumentSerializer
from .project_location import ProjectLocationSerializer
from .project_update import ProjectUpdateSerializer, ProjectUpdateExtraSerializer
from .project_update import (ProjectUpdateSerializer,
ProjectUpdateExtraSerializer)
from .project_update_location import ProjectUpdateLocationSerializer
from .publishing_status import PublishingStatusSerializer
from .recipient_country import RecipientCountrySerializer
Expand All @@ -41,14 +45,17 @@
from .result import ResultSerializer
from .sector import SectorSerializer
from .transaction import TransactionSerializer
from .typeahead import (TypeaheadCountrySerializer,
TypeaheadOrganisationSerializer,
TypeaheadProjectSerializer,
TypeaheadProjectUpdateSerializer)
from .user import UserSerializer, UserDetailsSerializer, UserPasswordSerializer
from .project import ProjectSerializer, ProjectExtraSerializer

__all__ = [
'BenchmarkSerializer',
'BenchmarknameSerializer',
'BudgetItemSerializer',
'BudgetItemLabelSerializer',
'BudgetItemSerializer',
'CategorySerializer',
'CountrySerializer',
'EmploymentSerializer',
Expand All @@ -61,33 +68,38 @@
'KeywordSerializer',
'LegacyDataSerializer',
'LinkSerializer',
'OrganisationSerializer',
'OrganisationLocationSerializer',
'OrganisationSerializer',
'PartnerSiteSerializer',
'PartnerTypeSerializer',
'PartnershipSerializer',
'PlannedDisbursementSerializer',
'PolicyMarkerSerializer',
'ProjectSerializer',
'ProjectExtraSerializer',
'ProjectCommentSerializer',
'ProjectConditionSerializer',
'ProjectContactSerializer',
'ProjectDocumentSerializer',
'ProjectExtraSerializer',
'ProjectExtraSerializer',
'ProjectLocationSerializer',
'ProjectUpdateSerializer',
'ProjectSerializer',
'ProjectSerializer',
'ProjectUpdateExtraSerializer',
'ProjectUpdateLocationSerializer',
'ProjectUpdateSerializer',
'PublishingStatusSerializer',
'RecipientCountrySerializer',
'RecipientRegionSerializer',
'RelatedProjectSerializer',
'ResultSerializer',
'SectorSerializer',
'TransactionSerializer',
'UserSerializer',
'TypeaheadCountrySerializer',
'TypeaheadOrganisationSerializer',
'TypeaheadProjectSerializer',
'TypeaheadProjectUpdateSerializer',
# 'TypeaheadSectorSerializer',
'UserDetailsSerializer',
'UserPasswordSerializer',
'ProjectSerializer',
'ProjectExtraSerializer',
'UserSerializer',
]
72 changes: 72 additions & 0 deletions akvo/rest/serializers/typeahead.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# -*- 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 rest_framework import serializers
# from akvo.codelists.models import SectorCategory
from akvo.rsr.models import Country, Organisation, Project, ProjectUpdate


class TypeaheadCountrySerializer(serializers.ModelSerializer):

class Meta:
model = Country
fields = (
'id',
'name',
'iso_code',
'continent_code',
)


class TypeaheadOrganisationSerializer(serializers.ModelSerializer):

class Meta:
model = Organisation
fields = (
'id',
'name',
'long_name',
)


class TypeaheadProjectSerializer(serializers.ModelSerializer):

class Meta:
model = Project
fields = (
'id',
'project_plan_summary',
'subtitle',
'title',
)


class TypeaheadProjectUpdateSerializer(serializers.ModelSerializer):

class Meta:
model = ProjectUpdate
fields = (
'id',
'project',
'title'
)

# class TypeaheadSectorSerializer(serializers.ModelSerializer):

# class Meta:
# model = Project
# depth = 1
# fields = (
# 'id',
# 'sectors',
# )
# # fields = (
# # 'id',
# # # 'categories',
# # 'name',
# # )
64 changes: 51 additions & 13 deletions akvo/rest/urls.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
# -*- 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 >.
"""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 >.
"""

import views

from django.conf.urls import patterns, url, include
from rest_framework import routers
import views

router = routers.DefaultRouter()

Expand All @@ -22,7 +24,8 @@
router.register(r'goal', views.GoalViewSet)
router.register(r'indicator', views.IndicatorViewSet)
router.register(r'indicator_period', views.IndicatorPeriodViewSet)
router.register(r'internal_organisation_id', views.InternalOrganisationIDViewSet)
router.register(r'internal_organisation_id',
views.InternalOrganisationIDViewSet)
router.register(r'invoice', views.InvoiceViewSet)
router.register(r'keyword', views.KeywordViewSet)
router.register(r'legacy_data', views.LegacyDataViewSet)
Expand All @@ -35,14 +38,17 @@
router.register(r'planned_disbursement', views.PlannedDisbursementViewSet)
router.register(r'policy_marker', views.PolicyMarkerViewSet)
router.register(r'project', views.ProjectViewSet)
router.register(r'project_extra', views.ProjectExtraViewSet, base_name='project_extra')
router.register(r'project_extra', views.ProjectExtraViewSet,
base_name='project_extra')
router.register(r'project_comment', views.ProjectCommentViewSet)
router.register(r'project_condition', views.ProjectConditionViewSet)
router.register(r'project_contact', views.ProjectContactViewSet)
router.register(r'project_document', views.ProjectDocumentViewSet)
router.register(r'project_location', views.ProjectLocationViewSet)
router.register(r'project_update_extra', views.ProjectUpdateExtraViewSet, base_name='project_update_extra')
router.register(r'project_update', views.ProjectUpdateViewSet, base_name='project_update')
router.register(r'project_update_extra', views.ProjectUpdateExtraViewSet,
base_name='project_update_extra')
router.register(r'project_update', views.ProjectUpdateViewSet,
base_name='project_update')
router.register(r'project_update_location', views.ProjectUpdateLocationViewSet)
router.register(r'publishing_status', views.PublishingStatusViewSet)
router.register(r'recipient_country', views.RecipientCountryViewSet)
Expand All @@ -55,12 +61,44 @@

# Wire up our API using automatic URL routing.
# Additionally, we include URLs for non-viewsets (functional views).

urlpatterns = patterns(
'',
url(r'^', include(router.urls)),
url(r'^employment/(?P<pk>[0-9]+)/approve/$', views.approve_employment, name='approve_employment'),
url(r'^employment/(?P<pk>[0-9]+)/set_group/(?P<group_id>[0-9]+)/$', views.set_group, name='set_group'),
url(r'^user/(?P<pk>[0-9]+)/change_password/$', views.change_password, name='user_change_password'),
url(r'^user/(?P<pk>[0-9]+)/update_details/$', views.update_details, name='user_update_details'),
url(r'^user/(?P<pk>[0-9]+)/request_organisation/$', views.request_organisation, name='user_request_organisation'),
url(r'^employment/(?P<pk>[0-9]+)/approve/$',
views.approve_employment,
name='approve_employment'),
url(r'^employment/(?P<pk>[0-9]+)/set_group/(?P<group_id>[0-9]+)/$',
views.set_group,
name='set_group'),
url(r'^user/(?P<pk>[0-9]+)/change_password/$',
views.change_password,
name='user_change_password'),
url(r'^user/(?P<pk>[0-9]+)/update_details/$',
views.update_details,
name='user_update_details'),
url(r'^user/(?P<pk>[0-9]+)/request_organisation/$',
views.request_organisation,
name='user_request_organisation'),
)

# Typeahead
urlpatterns += patterns(
'',
url(r'typeaheads/countries$',
views.typeahead_country,
name='country_typeahead'),
url(r'typeaheads/organisations$',
views.typeahead_organisation,
name='organisation_typeahead'),
url(r'typeaheads/projects$',
views.typeahead_project,
name='project_typeahead'),
url(r'typeaheads/project_updates$',
views.typeahead_projectupdate,
name='projectupdate_typeahead'),

# url(r'typeaheads/sectors$',
# views.typeahead_sector,
# name='sector_typeahead'),
)
20 changes: 16 additions & 4 deletions akvo/rest/views/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
# -*- 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 >.
"""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 BenchmarkViewSet
Expand Down Expand Up @@ -42,7 +44,12 @@
from .result import ResultViewSet
from .sector import SectorViewSet
from .transaction import TransactionViewSet
from .user import UserViewSet, change_password, update_details, request_organisation
from .typeahead import (typeahead_country,
typeahead_organisation,
typeahead_project,
typeahead_projectupdate)
from .user import (UserViewSet, change_password, update_details,
request_organisation)

__all__ = [
'BenchmarkViewSet',
Expand Down Expand Up @@ -87,6 +94,11 @@
'ResultViewSet',
'SectorViewSet',
'TransactionViewSet',
'typeahead_country,',
'typeahead_project',
'typeahead_projectupdate',
'typeahead_organisation',
# 'typeahead_sector',
'UserViewSet',
'change_password',
'update_details',
Expand Down
80 changes: 80 additions & 0 deletions akvo/rest/views/typeahead.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
# -*- 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 rest_framework.decorators import api_view
from rest_framework.response import Response

from akvo.rest.serializers import (TypeaheadCountrySerializer,
TypeaheadOrganisationSerializer,
TypeaheadProjectSerializer,
TypeaheadProjectUpdateSerializer)
# from akvo.codelists.models import SectorCategory
from akvo.rsr.models import Country, Organisation, Project, ProjectUpdate


def rejig(queryset, serializer):
"""Rearrange & add queryset count to the response data."""
return {
'count': queryset.count(),
'results': serializer.data
}


@api_view(['GET'])
def typeahead_country(request):
countries = Country.objects.all()
return Response(
rejig(countries, TypeaheadCountrySerializer(countries, many=True))
)


@api_view(['GET'])
def typeahead_organisation(request):
organisations = Organisation.objects.all()
return Response(
rejig(organisations, TypeaheadOrganisationSerializer(organisations,
many=True))
)


@api_view(['GET'])
def typeahead_project(request):
projects = Project.objects.published()
return Response(
rejig(projects, TypeaheadProjectSerializer(projects, many=True))
)


@api_view(['GET'])
def typeahead_projectupdate(request):
updates = ProjectUpdate.objects.all()
return Response(
rejig(updates, TypeaheadProjectUpdateSerializer(updates, many=True))
)


# @api_view(['GET'])
# def typeahead_sector(request):
# """
# """

# sectors = Sector.objects.all()

# return Response(
# rejig(sectors, TypeaheadSectorSerializer(sectors, many=True))
# )


# @api_view(['GET'])
# def typeahead_sector(request):
# """
# """
# sectors = SectorCategory.objects.all()
# return Response(
# rejig(sectors, TypeaheadSectorSerializer(sectors, many=True))
# )
Loading

0 comments on commit 67dcdb6

Please sign in to comment.