def add_initial_election_types(apps, schema_editor): ElectionType = apps.get_model("elections", "ElectionType") ElectionSubType = apps.get_model("elections", "ElectionSubType") for type_name, info in ELECTION_TYPES.items(): election_type, _ = ElectionType.objects.update_or_create( election_type=type_name, defaults={'name': info['name']}) for subtype in info['subtypes']: ElectionSubType.objects.update_or_create( election_type=election_type, election_subtype=subtype['election_subtype'], defaults={'name': subtype['name']})
def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) # ELECTION_TYPES is optimised for fast # lookups and not duplicating information. # We need to transform ELECTION_TYPES into a data structure # which is more optimised for generating HTML in a template: election_types_table = [] for et_key, et_record in OrderedDict(sorted( ELECTION_TYPES.items())).items(): # for the moment leave 'ref' and 'eu' types out of the docs # because our spec for these is incomplete if et_key == 'eu' or et_key == 'ref': continue et_record['slug'] = et_key et_record['subtype'] = None if et_record['subtypes']: # if we've got subtypes, duplicate the # election type data for each subtype for s_record in et_record['subtypes']: table_rec = et_record.copy() table_rec['subtype'] = s_record # subtype data takes precedence if it exists if 'can_have_orgs' in s_record: table_rec['can_have_orgs'] = s_record['can_have_orgs'] if 'can_have_divs' in s_record: table_rec['can_have_divs'] = s_record['can_have_divs'] election_types_table.append(table_rec) else: # otherwise just shove it in the list election_types_table.append(et_record) context['election_types'] = election_types_table return context
def remove_initial_election_types(apps, schema_editor): ElectionType = apps.get_model("elections", "ElectionType") ElectionType.objects.filter(election_type__in=ELECTION_TYPES.keys()).delete()