def get_geos(self, geo_ids): """ Return a tuple (data_geos, info_geos) of geo objects, where data_geos or geos we should get data for, and info_geos are geos that we only need to return geo info/metadata for. """ data_geos = [] info_geos = [] for geo_id in geo_ids: # either country-KE or level|country-KE, which indicates # we must break country-KE into +levels+ if '-' not in geo_id: raise LocationNotFound('Invalid geo id: %s' % geo_id) level, code = geo_id.split('-', 1) if '|' in level: # break geo down further split_level, level = level.split('|', 1) geo = geo_data.get_geography(code, level) info_geos.append(geo) try: data_geos.extend(geo.split_into(split_level)) except ValueError: raise LocationNotFound('Invalid geo level: %s' % split_level) else: # normal geo data_geos.append(geo_data.get_geography(code, level)) return data_geos, info_geos
def get(self, request, geo_id, *args, **kwargs): try: level, code = geo_id.split('-', 1) geo = geo_data.get_geography(code, level) except (ValueError, LocationNotFound): raise Http404 parents = [g.as_dict() for g in geo.ancestors()] return render_json_to_response(parents)
def get_context_data(self, geo_id1, geo_id2): page_context = { 'geo_id1': geo_id1, 'geo_id2': geo_id2, } try: level, code = geo_id1.split('-', 1) page_context['geo1'] = geo_data.get_geography(code, level) level, code = geo_id2.split('-', 1) page_context['geo2'] = geo_data.get_geography(code, level) topic = self.request.GET.get('topic', None) if topic: page_context['get_params'] = '?topic=' + topic except (ValueError, LocationNotFound): raise Http404 return page_context
def dispatch(self, *args, **kwargs): self.geo_id = self.kwargs.get('geography_id', None) try: self.geo_level, self.geo_code = self.geo_id.split('-', 1) self.geo = geo_data.get_geography(self.geo_code, self.geo_level) except (ValueError, LocationNotFound): raise Http404 # check slug if self.adjust_slugs and (kwargs.get('slug') or self.geo.slug): if kwargs['slug'] != self.geo.slug: kwargs['slug'] = self.geo.slug url = '/profiles/%s-%s-%s' % (self.geo_level, self.geo_code, self.geo.slug) topic = self.request.GET.get('topic', None) if topic: url += '?topic=' + topic return redirect(url, permanent=True) # Skip the parent class's logic completely and go back to basics return TemplateView.dispatch(self, *args, **kwargs)