def clean_new_name(self): new_name = self.cleaned_data["new_name"] sample_name_format, match = utils.sample_name_format( new_name, with_match_object=True) if not sample_name_format: raise ValidationError(_("The sample name has an invalid format.")) elif not new_name.startswith(self.parent_name): if sample_name_format not in self.possible_new_name_formats: error_message = _( "The new sample name must start with the parent sample's name." ) if self.possible_new_name_formats: further_error_message = ungettext( " Alternatively, it must be a valid “{sample_formats}” name.", " Alternatively, it must be a valid name of one of these types: " "{sample_formats}", len(self.possible_new_name_formats)) further_error_message = further_error_message.format( sample_formats=format_enumeration( utils.verbose_sample_name_format(name_format) for name_format in self.possible_new_name_formats)) error_message += further_error_message raise ValidationError(error_message) utils.check_sample_name(match, self.user) if utils.does_sample_exist(new_name): raise ValidationError(_("Name does already exist in database.")) return new_name
def clean_samples(self): sample_names = self.cleaned_data["samples"].split(",") valid_names = [] invalid_names = [] for name in sample_names: name = name.strip() if name: if utils.sample_name_format(name) == "old": if name in valid_names: raise ValidationError(_("The name {name} appears more than once.").format(name=name)) valid_names.append(name) else: invalid_names.append(name) if invalid_names: error_message = ungettext( "The name {invalid_names} is not valid.", "The names {invalid_names} are not valid.", len(invalid_names)).format(invalid_names=format_enumeration(invalid_names)) raise ValidationError(error_message) if not valid_names: raise ValidationError(self.fields["samples"].error_messages["required"]) existing_names = [name for name in valid_names if utils.does_sample_exist(name)] if existing_names: # This opens a small security hole because people can find out that # confidential samples are existing. However, such samples mostly # have non-oldstyle names anyway. error_message = ungettext( "The name {existing_names} is already existing.", "The names {existing_names} are already existing.", len(existing_names)).format(existing_names=format_enumeration(existing_names)) raise ValidationError(error_message) return valid_names
def clean_new_name(self): if "sample" in self.cleaned_data: new_name = self.cleaned_data["new_name"] if new_name != self.cleaned_data["sample"].name and utils.does_sample_exist(new_name): raise ValidationError(_("This sample name exists already.")) elif utils.sample_name_format(new_name) == "provisional": raise ValidationError(_("You must get rid of the provisional sample name.")) return new_name
def clean_name(self): new_name = self.prefix_ + self.cleaned_data["name"] name_format, match = utils.sample_name_format(new_name, with_match_object=True) if name_format not in self.possible_new_name_formats: error_message = ungettext("New name must be a valid “{sample_formats}” name.", "New name must be a valid name of one of these types: {sample_formats}", len(self.possible_new_name_formats)) error_message = error_message.format(sample_formats=format_enumeration( utils.verbose_sample_name_format(name_format) for name_format in self.possible_new_name_formats)) raise ValidationError(error_message) utils.check_sample_name(match, self.user) if utils.does_sample_exist(new_name): raise ValidationError(_("This sample name exists already.")) return new_name
def clean_new_name(self): new_name = self.cleaned_data["new_name"] sample_name_format, match = utils.sample_name_format(new_name, with_match_object=True) if not sample_name_format: raise ValidationError(_("The sample name has an invalid format.")) elif not new_name.startswith(self.parent_name): if sample_name_format not in self.possible_new_name_formats: error_message = _("The new sample name must start with the parent sample's name.") if self.possible_new_name_formats: further_error_message = ungettext(" Alternatively, it must be a valid “{sample_formats}” name.", " Alternatively, it must be a valid name of one of these types: " "{sample_formats}", len(self.possible_new_name_formats)) further_error_message = further_error_message.format(sample_formats=format_enumeration( utils.verbose_sample_name_format(name_format) for name_format in self.possible_new_name_formats)) error_message += further_error_message raise ValidationError(error_message) utils.check_sample_name(match, self.user) if utils.does_sample_exist(new_name): raise ValidationError(_("Name does already exist in database.")) return new_name
def clean_samples(self): sample_names = self.cleaned_data["samples"].split(",") valid_names = [] invalid_names = [] for name in sample_names: name = name.strip() if name: if utils.sample_name_format(name) == "old": if name in valid_names: raise ValidationError( _("The name {name} appears more than once."). format(name=name)) valid_names.append(name) else: invalid_names.append(name) if invalid_names: error_message = ungettext( "The name {invalid_names} is not valid.", "The names {invalid_names} are not valid.", len(invalid_names)).format( invalid_names=format_enumeration(invalid_names)) raise ValidationError(error_message) if not valid_names: raise ValidationError( self.fields["samples"].error_messages["required"]) existing_names = [ name for name in valid_names if utils.does_sample_exist(name) ] if existing_names: # This opens a small security hole because people can find out that # confidential samples are existing. However, such samples mostly # have non-oldstyle names anyway. error_message = ungettext( "The name {existing_names} is already existing.", "The names {existing_names} are already existing.", len(existing_names)).format( existing_names=format_enumeration(existing_names)) raise ValidationError(error_message) return valid_names
def is_referentially_valid(original_data_forms, new_name_form_lists, deposition): """Test whether all forms are consistent with each other and with the database. For example, no sample name must occur twice, and the sample names must not exist within the database already. :param original_data_forms: all old samples and pieces numbers :param new_name_form_lists: new names for all pieces :param deposition: the deposition after which the split takes place :type original_data_forms: list of `OriginalDataForm` :type new_name_form_lists: list of `NewNameForm` :type deposition: `samples.models.Deposition` :return: whether all forms are consistent with each other and the database :rtype: bool """ referentially_valid = True samples = list(deposition.samples.all()) more_than_one_piece = len(original_data_forms) > 1 new_names = set() original_samples = set() for original_data_form in original_data_forms: if original_data_form.is_valid(): original_sample = original_data_form.cleaned_data["sample"] if original_sample in original_samples: original_data_form.add_error("sample", _("Sample {sample} occurs multiple times.").format(sample=original_sample)) referentially_valid = False original_samples.add(original_sample) if original_sample not in samples: original_data_form.add_error("sample", _("Sample {sample} doesn't belong to this deposition.").format(sample=original_sample)) referentially_valid = False new_name = original_data_form.cleaned_data["new_name"] if new_name in new_names: original_data_form.add_error("new_name", _("This sample name has been used already on this page.")) referentially_valid = False new_names.add(new_name) if more_than_one_piece and new_name == deposition.number: original_data_form.add_error("new_name", _("Since there is more than one piece, the new name " "must not be exactly the deposition's name.")) referentially_valid = False if all(original_data_form.is_valid() for original_data_form in original_data_forms): assert len(original_samples) <= len(samples) if len(original_samples) < len(samples): original_data_form.add_error(None, _("At least one sample of the original deposition is missing.")) referentially_valid = False for new_name_forms, original_data_form in zip(new_name_form_lists, original_data_forms): if original_data_form.is_valid(): original_sample = original_data_form.cleaned_data["sample"] if deposition != original_sample.processes.exclude(content_type=ContentType.objects. get_for_model(models.Result)) \ .order_by("-timestamp")[0].actual_instance and original_data_form.cleaned_data["number_of_pieces"] > 1: original_data_form.add_error("sample", _("The sample can't be split, because the deposition is not the latest process.")) referentially_valid = False else: for new_name_form in new_name_forms: if new_name_form.is_valid(): new_name = new_name_form.cleaned_data["new_name"] if original_data_form.cleaned_data["number_of_pieces"] == 1: if new_name != original_data_form.cleaned_data["new_name"]: new_name_form.add_error("new_name", _("If you don't split, you can't rename the single piece.")) referentially_valid = False else: if new_name in new_names: new_name_form.add_error("new_name", _("This sample name has been used already on this page.")) referentially_valid = False new_names.add(new_name) if utils.sample_name_format(new_name) in utils.get_renamable_name_formats() and \ not new_name.startswith(original_data_form.cleaned_data["new_name"]): new_name_form.add_error("new_name", _("If you choose a deposition-style name, it must begin " "with the parent's new name.")) referentially_valid = False if utils.does_sample_exist(new_name): new_name_form.add_error("new_name", _("This sample name exists already.")) referentially_valid = False return referentially_valid