def clean(self, *args, **kwargs): cleaned_data = super(ElectionForm, self).clean(*args, **kwargs) if 'name' in cleaned_data: slug = slughifi(cleaned_data['name']) dfrom = cleaned_data['voting_starts_at'] dto = cleaned_data['voting_ends_at'] dextend = None if 'voting_extended_until' in cleaned_data: dextend = cleaned_data['voting_extended_until'] if dfrom >= dto and (self.election and not self.election.frozen_at): raise forms.ValidationError(_("Invalid voting dates")) if dextend and cleaned_data['voting_extended_until'] < dto: raise forms.ValidationError(_("Invalid voting extension date")) if not 'departments' in cleaned_data: cleaned_data['departments'] = '' return cleaned_data
def save(self, election, institution, params): is_new = not bool(election.pk) institution = self.institution data = self.cleaned_data data['slug'] = slughifi(data['name']) e = election if self.fixed_election_type: e.election_type = self.fixed_election_type if is_new or not election.frozen_at: e.name = data.get('name') e.use_voter_aliases = True e.workflow_type = 'mixnet' e.private_p = True e.institution = institution e.help_phone = data['help_phone'] e.help_email = data['help_email'] e.departments = [d.strip() for d in data['departments'].strip().split("\n")] if not self.fixed_election_type: prev_type = e.election_type e.election_type = data.get('election_type') if prev_type != e.election_type: e.update_answers() if e.candidates: new_cands = [] for cand in e.candidates: if cand['department'] in e.departments: new_cands.append(cand) #if len(new_cands) != len(e.candidates): #messages.warning(_("Election candidates changed due to election" #" institution department changes")) e.candidates = new_cands e.update_answers() if not e.uuid: e.uuid = str(uuid.uuid1()) if is_new or not election.frozen_at: e.cast_url = settings.SECURE_URL_HOST + \ reverse('helios.views.one_election_cast', args=[e.uuid]) e.short_name = data['slug'] count = 0 q = Q(short_name=e.short_name) if e.pk and self.election: q = ~Q(pk=e.pk) & Q(short_name=e.short_name) short_name = e.short_name while Election.objects.filter(q).count() > 0: count += 1 e.short_name = short_name + "-" + str(count) q = Q(short_name=e.short_name) if e.pk: q = Q(short_name=e.short_name) if self.election: q = q & ~Q(pk=self.election.pk) e.description = data['description'] e.voting_starts_at = data['voting_starts_at'] e.voting_ends_at = data['voting_ends_at'] if 'voting_extended_until' in data: e.voting_extended_until = data['voting_extended_until'] if is_new or not e.voting_ended_at: if data['remote_mix']: e.generate_mix_key() else: e.mix_key = '' if 'eligibles_count' in data: e.eligibles_count = data['eligibles_count'] if 'has_department_limit' in data: e.has_department_limit = data['has_department_limit'] e.save() if is_new: e.generate_helios_mixnet({"name":"zeus mixnet %d" % 1}) if not e.get_helios_trustee(): e.generate_trustee() if is_new or not election.frozen_at: trustees = [] if data['trustees']: trustees = [t.split(",") for t in data['trustees'].split("\n")] for t in trustees: name, email = t[0], t[1] trustee, created = Trustee.objects.get_or_create(election=e, email=email) trustee.name = name if created: trustee.uuid = str(uuid.uuid1()) trustee.save() if created: trustee.send_url_via_mail() return e