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
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 'root_service_nodes' in ret: if obj.root_service_nodes is None or obj.root_service_nodes == '': ret['root_service_nodes'] = None else: ret['root_service_nodes'] = [ int(x) for x in obj.root_service_nodes.split(',') ] include_fields = self.context.get('include', []) for field in ['department', 'root_department']: if field in include_fields: dep_json = DepartmentSerializer(getattr(obj, field), context=self.context).data ret[field] = dep_json # Not using actual serializer instances below is a performance optimization. if 'service_nodes' in include_fields: service_nodes_json = [] for s in obj.service_nodes.all(): # Optimization: # Store root nodes by tree_id in a dict because otherwise # this would generate multiple db queries for every single unit tree_id = s._mpttfield('tree_id') # Forget your privacy! root_node = self._root_node_cache.get(tree_id) if root_node is None: root_node = s.get_root() self._root_node_cache[tree_id] = root_node name = {} for lang in LANGUAGES: name[lang] = getattr(s, 'name_{0}'.format(lang)) data = { 'id': s.id, 'name': name, 'root': root_node.id, 'service_reference': s.service_reference } # if s.identical_to: # data['identical_to'] = getattr(s.identical_to, 'id', None) if s.level is not None: data['level'] = s.level service_nodes_json.append(data) ret['service_nodes'] = service_nodes_json if 'services' in include_fields: ret['services'] = (ServiceDetailsSerializer(obj.service_details, many=True).data) 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 'request' not 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']) try: shortcomings = obj.accessibility_shortcomings except UnitAccessibilityShortcomings.DoesNotExist: shortcomings = UnitAccessibilityShortcomings() if 'accessibility_shortcoming_count' in getattr( self, 'keep_fields', ['accessibility_shortcoming_count']): ret['accessibility_shortcoming_count'] = shortcomings.accessibility_shortcoming_count if qparams.get('accessibility_description', '').lower() in ('true', '1'): ret['accessibility_description'] = shortcomings.accessibility_description return ret
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) 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
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 'root_service_nodes' in ret: if obj.root_service_nodes is None or obj.root_service_nodes == '': ret['root_service_nodes'] = None else: ret['root_service_nodes'] = [int(x) for x in obj.root_service_nodes.split(',')] include_fields = self.context.get('include', []) for field in ['department', 'root_department']: if field in include_fields: dep_json = DepartmentSerializer(getattr(obj, field), context=self.context).data ret[field] = dep_json # Not using actual serializer instances below is a performance optimization. if 'service_nodes' in include_fields: service_nodes_json = [] for s in obj.service_nodes.all(): # Optimization: # Store root nodes by tree_id in a dict because otherwise # this would generate multiple db queries for every single unit tree_id = s._mpttfield('tree_id') # Forget your privacy! root_node = self._root_node_cache.get(tree_id) if root_node is None: root_node = s.get_root() self._root_node_cache[tree_id] = root_node name = {} for lang in LANGUAGES: name[lang] = getattr(s, 'name_{0}'.format(lang)) data = { 'id': s.id, 'name': name, 'root': root_node.id, 'service_reference': s.service_reference } # if s.identical_to: # data['identical_to'] = getattr(s.identical_to, 'id', None) if s.level is not None: data['level'] = s.level service_nodes_json.append(data) ret['service_nodes'] = service_nodes_json if 'services' in include_fields: ret['services'] = ( ServiceDetailsSerializer(obj.service_details, many=True).data) 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 'request' not 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']) try: shortcomings = obj.accessibility_shortcomings except UnitAccessibilityShortcomings.DoesNotExist: shortcomings = UnitAccessibilityShortcomings() if 'accessibility_shortcoming_count' in getattr(self, 'keep_fields', ['accessibility_shortcoming_count']): ret['accessibility_shortcoming_count'] = shortcomings.accessibility_shortcoming_count if qparams.get('accessibility_description', '').lower() in ('true', '1'): ret['accessibility_description'] = shortcomings.accessibility_description return ret