def clean(self): ensure_unique(self._meta.model, self, 'center_id') if self.is_copy and (self.center_type != self.Types.COPY): raise ValidationError(_('Copy centre type must be "copy".')) if self.id: # For existing centers, copy status can't be changed. Once a copy center, always a # copy center, and ditto for non-copy centers. Ne'er the twain shall meet. # Note that we have to search through all centers (including deleted ones) to allow # undeleting. # In addition, copy centers are not editable. center = RegistrationCenter.objects.unfiltered().get(pk=self.id) if center.is_copy: raise ValidationError(_('Copy centres are read-only.')) # The check above ensures the user can't change the type *from* copy. We also need # to check that the type is not being changed *to* copy. if self.center_type == RegistrationCenter.Types.COPY: raise ValidationError(_('A centre may not be changed to a copy centre.')) else: # New centers can become copy centers if self.is_copy: if self.copy_of.copy_of: raise ValidationError(_("A copy centre cannot copy another copy centre.")) # Ensure that the original doesn't already have the max # of copies. if len(self.copy_of.copied_by.all()) == settings.N_MAX_COPY_CENTERS: msg = _('Copy centre already has the maximum number of copies ({n_max}).') raise ValidationError(msg.format(n_max=settings.N_MAX_COPY_CENTERS)) else: if self.center_type == self.Types.COPY: msg = _('Centre type "copy" requires copy centre information.') raise ValidationError(msg) if ((self.center_lat is None and self.center_lon is not None) or (self.center_lon is None and self.center_lat is not None)): raise ValidationError(_("Either set both latitude and longitude or neither.")) if self.center_type == self.Types.SPLIT: if self.subconstituency.id != SPLIT_CENTER_SUBCONSTITUENCY_ID: msg = _("Split centers must be associated with subconstituency {}.") msg = msg.format(SPLIT_CENTER_SUBCONSTITUENCY_ID) raise ValidationError(msg) else: if self.subconstituency.id == SPLIT_CENTER_SUBCONSTITUENCY_ID: msg = _("Only split centers may be associated with subconstituency {}.") msg = msg.format(SPLIT_CENTER_SUBCONSTITUENCY_ID) raise ValidationError(msg)
def clean(self): ensure_unique(self._meta.model, self, 'phone_number')
def clean(self): ensure_unique(self._meta.model, self, 'citizen_id')
def clean(self): """Give nicer error than a database integrity error on an attempt to create a duplicate confirmed registration""" if not self.archive_time: ensure_unique(self._meta.model, self, 'citizen', archive_time=None)