Beispiel #1
0
 def to_representation(self, obj):
     ret = super(JSONAPISerializer, self).to_representation(obj)
     include_fields = self.context.get('include', [])
     if 'municipality' in include_fields and obj.municipality:
         muni_json = munigeo_api.MunicipalitySerializer(
             obj.municipality, context=self.context).data
         ret['municipality'] = muni_json
     return ret
Beispiel #2
0
    def to_representation(self, obj):
        ret = super(UnitSerializer, self).to_representation(obj)
        if hasattr(obj, 'distance') and obj.distance:
            ret['distance'] = obj.distance.m

        if 'keywords' in ret:
            kw_dict = {}
            for kw in obj.keywords.all():
                if not kw.language in kw_dict:
                    kw_dict[kw.language] = []
                kw_dict[kw.language].append(kw.name)
            ret['keywords'] = kw_dict

        if 'root_services' in ret:
            if obj.root_services == None or obj.root_services == '':
                ret['root_services'] = None
            else:
                ret['root_services'] = [int(x) for x in obj.root_services.split(',')]

        include_fields = self.context.get('include', [])
        if 'department' in include_fields:
            dep_json = DepartmentSerializer(obj.department, context=self.context).data
            ret['department'] = dep_json
        if 'municipality' in include_fields and obj.municipality:
            muni_json = munigeo_api.MunicipalitySerializer(obj.municipality, context=self.context).data
            ret['municipality'] = muni_json
        # Not using actual serializer instances below is a performance optimization.
        if 'services' in include_fields:
            services_json = []
            for s in obj.services.all():
                name = {}
                for lang in LANGUAGES:
                    name[lang] = getattr(s, 'name_{0}'.format(lang))
                data = {'id': s.id, 'name': name, 'root': s.get_root().id}
                if s.identical_to:
                    data['identical_to'] = getattr(s.identical_to, 'id', None)
                if s.level is not None:
                    data['level'] = s.level
                services_json.append(data)
            ret['services'] = services_json
        if 'accessibility_properties' in include_fields:
            acc_props = [{'variable': s.variable_id, 'value': s.value}
                         for s in obj.accessibility_properties.all()]
            ret['accessibility_properties'] = acc_props

        if 'connections' in include_fields:
            ret['connections'] = UnitConnectionSerializer(obj.connections, many=True).data

        if not 'request' in self.context:
            return ret
        qparams = self.context['request'].query_params
        if qparams.get('geometry', '').lower() in ('true', '1'):
            geom = obj.geometry # TODO: different geom types
            if geom and obj.geometry != obj.location:
                ret['geometry'] = munigeo_api.geom_to_json(geom, self.srs)
        elif 'geometry' in ret:
            del ret['geometry']

        if 'extensions' in ret:
            ret['extensions'] = self.handle_extension_translations(ret['extensions'])
        if 'data_source' in ret:
            del ret['data_source']
        return ret