def formfield_for_foreignkey(self, db_field, request, **kwargs): if db_field.name == "geo_record": levels = get_default_levels() kwargs["queryset"] = GeoRecord.objects.filter( level__in=levels).order_by('name') return super(PrecalculatedValueAdmin, self).formfield_for_foreignkey(db_field, request, **kwargs)
def formfield_for_manytomany(self, db_field, request, **kwargs): if db_field.name == "levels": levs = get_default_levels() kwargs["queryset"] = GeoLevel.objects.filter( name__in=levs, year='') | GeoLevel.objects.exclude( name__in=levs).filter(year='') return super(DenominatorPartInline, self).formfield_for_manytomany(db_field, request, **kwargs)
def _geo_nav_context(context): "Create a context object to render geo nav widgets" from profiles.utils import get_default_levels levels = get_default_levels() geo_record = None indicator = None data_domain = None if 'geo_record' in context: geo_record = context['geo_record'] if 'indicator' in context: indicator = context['indicator'] if 'data_domain' in context: data_domain = context['data_domain'] if geo_record == None: if not hasattr(settings, 'DEFAULT_GEO_RECORD_ID'): raise Exception( 'No geo_record was selected, and DEFAULT_GEO_RECORD_ID was not configured.' ) try: geo_record = GeoRecord.objects.get( pk=settings.DEFAULT_GEO_RECORD_ID) except GeoRecord.DoesNotExist: geo_record = GeoRecord.objects.filter(level=GeoLevel.objects.get( slug=levels[0].lower()))[0] # construct a list of geo levels to display in nav, with a list of records # and optionally a selected record #geo_levels = GeoLevel.objects.filter(slug__in=[lev.lower() for lev in levels]) nav = {} parent_level = geo_record.level.parent while parent_level: nav[parent_level.sort_key] = { 'name': parent_level.name, 'pk': parent_level.pk, 'slug': parent_level.slug } parent_level = parent_level.parent # at this point any level that is not in nav already, is a sub level sub_levels = GeoLevel.objects.exclude(name__in=nav.keys()).filter( name__in=levels) for lev in sub_levels: nav[lev.sort_key] = {'name': lev.name, 'pk': lev.pk, 'slug': lev.slug} sorted(nav, key=lambda key: nav[key]) # now legs get all the geographoes for k, v in nav.iteritems(): lev = nav[k] lev['geos'] = GeoRecord.objects.filter(level__pk=lev['pk']).only( 'slug', 'pk', 'name') try: default_geo = GeoRecord.objects.get(pk=settings.DEFAULT_GEO_RECORD_ID) except GeoRecord.DoesNotExist: default_geo = GeoRecord.objects.filter(level=GeoLevel.objects.get( slug=levels[0].lower()))[0] try: del nav[1] # we never show the topmost geography except Exception as e: pass return { #'levels': levels, # a list of the geo levels, with selections, to display 'levels': nav, 'data_domain': data_domain, # currently selected domain 'indicator': indicator, # currently selected indicator 'geo_record': geo_record, # currently selected record 'default_georecord': default_geo }
def formfield_for_manytomany(self, db_field, request, **kwargs): if db_field.name == "levels": levs= get_default_levels() kwargs["queryset"] = GeoLevel.objects.filter(name__in=levs, year='')|GeoLevel.objects.exclude(name__in=levs).filter(year='') return super(DenominatorPartInline, self).formfield_for_manytomany(db_field, request, **kwargs)
def formfield_for_foreignkey(self, db_field, request, **kwargs): if db_field.name == "geo_record": levels = get_default_levels() kwargs["queryset"] = GeoRecord.objects.filter(level__in=levels).order_by('name') return super(PrecalculatedValueAdmin, self).formfield_for_foreignkey(db_field,request,**kwargs)
def __init__(self): levels = get_default_levels() self.valid_levels = GeoLevel.objects.filter(name__in=levels) super(IndicatorResource, self).__init__()
def _geo_nav_context(context): "Create a context object to render geo nav widgets" from profiles.utils import get_default_levels levels = get_default_levels() geo_record = None indicator = None data_domain = None if 'geo_record' in context: geo_record = context['geo_record'] if 'indicator' in context: indicator = context['indicator'] if 'data_domain' in context: data_domain = context['data_domain'] if geo_record == None: if not hasattr(settings, 'DEFAULT_GEO_RECORD_ID'): raise Exception('No geo_record was selected, and DEFAULT_GEO_RECORD_ID was not configured.') try: geo_record = GeoRecord.objects.get(pk=settings.DEFAULT_GEO_RECORD_ID) except GeoRecord.DoesNotExist: geo_record = GeoRecord.objects.filter(level=GeoLevel.objects.get(slug=levels[0].lower()))[0] # construct a list of geo levels to display in nav, with a list of records # and optionally a selected record #geo_levels = GeoLevel.objects.filter(slug__in=[lev.lower() for lev in levels]) nav = {} parent_level = geo_record.level.parent while parent_level: nav[parent_level.sort_key] = {'name':parent_level.name, 'pk':parent_level.pk, 'slug':parent_level.slug} parent_level = parent_level.parent # at this point any level that is not in nav already, is a sub level sub_levels = GeoLevel.objects.exclude(name__in=nav.keys()).filter(name__in=levels) for lev in sub_levels: nav[lev.sort_key] = {'name':lev.name, 'pk':lev.pk, 'slug':lev.slug} sorted(nav, key=lambda key: nav[key]) # now legs get all the geographoes for k, v in nav.iteritems(): lev = nav[k] lev['geos'] = GeoRecord.objects.filter(level__pk=lev['pk']).only('slug', 'pk', 'name') try: default_geo = GeoRecord.objects.get(pk=settings.DEFAULT_GEO_RECORD_ID) except GeoRecord.DoesNotExist: default_geo = GeoRecord.objects.filter(level=GeoLevel.objects.get(slug=levels[0].lower()))[0] try: del nav[1] # we never show the topmost geography except Exception as e: pass return { #'levels': levels, # a list of the geo levels, with selections, to display 'levels':nav, 'data_domain': data_domain, # currently selected domain 'indicator': indicator, # currently selected indicator 'geo_record': geo_record, # currently selected record 'default_georecord':default_geo }