Source code for akvo.rest.serializers.typeahead

# -*- 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.store.default_codelists import SECTOR_CATEGORY
from akvo.rsr.models import Organisation, Project, ProjectUpdate, Sector
from akvo.utils import codelist_choices


[docs]class TypeaheadOrganisationSerializer(serializers.ModelSerializer):
[docs] class Meta: model = Organisation fields = ('id', 'name', 'long_name')
[docs]class TypeaheadProjectSerializer(serializers.ModelSerializer):
[docs] class Meta: model = Project fields = ('id', 'title', 'subtitle')
[docs]class TypeaheadProjectUpdateSerializer(serializers.ModelSerializer):
[docs] class Meta: model = ProjectUpdate fields = ('id', 'project', 'title')
[docs]class TypeaheadSectorSerializer(serializers.ModelSerializer): id = serializers.SerializerMethodField() name = serializers.SerializerMethodField() # Lookup attribute for sector names. Not a serialized attribute. sectors = dict(codelist_choices(SECTOR_CATEGORY, show_code=False))
[docs] def get_id(self, obj): return obj['sector_code']
[docs] def get_name(self, obj): return self.sectors[obj['sector_code']]
[docs] class Meta: model = Sector fields = ('id', 'name')