Merge pull request #802 from abdnh/ensure-non-empty-names

Ensure non-empty names in the backend
This commit is contained in:
Damien Elmes 2020-10-23 07:14:06 +10:00 committed by GitHub
commit c86711e2d6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 27 additions and 10 deletions

View File

@ -581,7 +581,7 @@ class CardLayout(QDialog):
def onRename(self):
template = self.current_template()
name = getOnlyText(_("New name:"), default=template["name"]).strip('"')
name = getOnlyText(_("New name:"), default=template["name"]).replace('"', "")
if not name.strip():
return

View File

@ -80,7 +80,7 @@ class FieldDialog(QDialog):
self.loadField(idx)
def _uniqueName(self, prompt, ignoreOrd=None, old=""):
txt = getOnlyText(prompt, default=old).strip('"')
txt = getOnlyText(prompt, default=old).replace('"', "")
if not txt:
return
for f in self.model["flds"]:

View File

@ -85,7 +85,7 @@ class Models(QDialog):
def onRename(self) -> None:
nt = self.current_notetype()
txt = getText(_("New name:"), default=nt["name"])
name = txt[0].strip('"')
name = txt[0].replace('"', "")
if txt[1] and name:
nt["name"] = name
self.saveAndRefresh(nt)
@ -120,7 +120,7 @@ class Models(QDialog):
def onAdd(self) -> None:
m = AddModel(self.mw, self).get()
if m:
txt = getText(_("Name:"), default=m["name"])[0].strip('"')
txt = getText(_("Name:"), default=m["name"])[0].replace('"', "")
if txt:
m["name"] = txt
self.saveAndRefresh(m)

View File

@ -227,9 +227,12 @@ impl NoteType {
if self.name.contains(bad_chars) {
self.name = self.name.replace(bad_chars, "");
}
if self.name.is_empty() {
return Err(AnkiError::invalid_input("Empty note type name"));
}
self.normalize_names();
self.fix_field_names();
self.fix_template_names();
self.fix_field_names()?;
self.fix_template_names()?;
self.ensure_names_unique();
self.reposition_sort_idx();
@ -323,12 +326,26 @@ impl NoteType {
DeckID(self.config.target_deck_id)
}
fn fix_field_names(&mut self) {
self.fields.iter_mut().for_each(NoteField::fix_name);
fn fix_field_names(&mut self) -> Result<()> {
for mut f in &mut self.fields {
NoteField::fix_name(&mut f);
if f.name.is_empty() {
return Err(AnkiError::invalid_input("Empty field name"));
}
}
Ok(())
}
fn fix_template_names(&mut self) {
self.templates.iter_mut().for_each(CardTemplate::fix_name);
fn fix_template_names(&mut self) -> Result<()> {
for mut t in &mut self.templates {
CardTemplate::fix_name(&mut t);
if t.name.is_empty() {
return Err(AnkiError::invalid_input("Empty template name"));
}
}
Ok(())
}
/// Find the field index of the provided field name.