From c2b1ab5eb06935e93aea6af09a224a99f4b971f0 Mon Sep 17 00:00:00 2001 From: Abdo Date: Wed, 20 Sep 2023 09:09:54 +0300 Subject: [PATCH] Skip template checks in Fields screen (#2670) --- proto/anki/notetypes.proto | 8 +++++++- pylib/anki/models.py | 8 ++++++-- qt/aqt/fields.py | 6 +++--- qt/aqt/operations/notetype.py | 5 ++++- rslib/src/notetype/service.rs | 5 +++-- 5 files changed, 23 insertions(+), 9 deletions(-) diff --git a/proto/anki/notetypes.proto b/proto/anki/notetypes.proto index c58fcea83b5..f143956ce2a 100644 --- a/proto/anki/notetypes.proto +++ b/proto/anki/notetypes.proto @@ -14,7 +14,8 @@ service NotetypesService { rpc AddNotetype(Notetype) returns (collection.OpChangesWithId); rpc UpdateNotetype(Notetype) returns (collection.OpChanges); rpc AddNotetypeLegacy(generic.Json) returns (collection.OpChangesWithId); - rpc UpdateNotetypeLegacy(generic.Json) returns (collection.OpChanges); + rpc UpdateNotetypeLegacy(UpdateNotetypeLegacyRequest) + returns (collection.OpChanges); rpc AddOrUpdateNotetype(AddOrUpdateNotetypeRequest) returns (NotetypeId); rpc GetStockNotetypeLegacy(StockNotetype) returns (generic.Json); rpc GetNotetype(NotetypeId) returns (Notetype); @@ -134,6 +135,11 @@ message AddOrUpdateNotetypeRequest { bool skip_checks = 3; } +message UpdateNotetypeLegacyRequest { + bytes json = 1; + bool skip_checks = 2; +} + message StockNotetype { enum Kind { KIND_BASIC = 0; diff --git a/pylib/anki/models.py b/pylib/anki/models.py index fe9c9b5f27f..1277ed8afdb 100644 --- a/pylib/anki/models.py +++ b/pylib/anki/models.py @@ -217,11 +217,15 @@ def ensure_name_unique(self, notetype: NotetypeDict) -> None: if existing_id is not None and existing_id != notetype["id"]: notetype["name"] += f"-{checksum(str(time.time()))[:5]}" - def update_dict(self, notetype: NotetypeDict) -> OpChanges: + def update_dict( + self, notetype: NotetypeDict, skip_checks: bool = False + ) -> OpChanges: "Update a NotetypeDict. Caller will need to re-load notetype if new fields/cards added." self._remove_from_cache(notetype["id"]) self.ensure_name_unique(notetype) - return self.col._backend.update_notetype_legacy(to_json_bytes(notetype)) + return self.col._backend.update_notetype_legacy( + json=to_json_bytes(notetype), skip_checks=skip_checks + ) def _mutate_after_write(self, notetype: NotetypeDict) -> None: # existing code expects the note type to be mutated to reflect diff --git a/qt/aqt/fields.py b/qt/aqt/fields.py index 9c1ddfc7060..991b3d80cb6 100644 --- a/qt/aqt/fields.py +++ b/qt/aqt/fields.py @@ -289,9 +289,9 @@ def on_done(changes: OpChanges) -> None: tooltip(tr.card_templates_changes_saved(), parent=self.parentWidget()) QDialog.accept(self) - update_notetype_legacy(parent=self.mw, notetype=self.model).success( - on_done - ).run_in_background() + update_notetype_legacy( + parent=self.mw, notetype=self.model, skip_checks=True + ).success(on_done).run_in_background() def onHelp(self) -> None: openHelp(HelpPage.CUSTOMIZING_FIELDS) diff --git a/qt/aqt/operations/notetype.py b/qt/aqt/operations/notetype.py index 909ec0a9815..911da692e4e 100644 --- a/qt/aqt/operations/notetype.py +++ b/qt/aqt/operations/notetype.py @@ -22,8 +22,11 @@ def update_notetype_legacy( *, parent: QWidget, notetype: NotetypeDict, + skip_checks: bool = False, ) -> CollectionOp[OpChanges]: - return CollectionOp(parent, lambda col: col.models.update_dict(notetype)) + return CollectionOp( + parent, lambda col: col.models.update_dict(notetype, skip_checks) + ) def remove_notetype( diff --git a/rslib/src/notetype/service.rs b/rslib/src/notetype/service.rs index 98ab93b750b..f1950f0f53e 100644 --- a/rslib/src/notetype/service.rs +++ b/rslib/src/notetype/service.rs @@ -53,11 +53,12 @@ impl crate::services::NotetypesService for Collection { fn update_notetype_legacy( &mut self, - input: generic::Json, + input: anki_proto::notetypes::UpdateNotetypeLegacyRequest, ) -> error::Result { let legacy: NotetypeSchema11 = serde_json::from_slice(&input.json)?; let mut notetype: Notetype = legacy.into(); - self.update_notetype(&mut notetype, false).map(Into::into) + self.update_notetype(&mut notetype, input.skip_checks) + .map(Into::into) } fn add_or_update_notetype(