Skip to content

Commit

Permalink
Skip template checks in Fields screen (#2670)
Browse files Browse the repository at this point in the history
  • Loading branch information
abdnh committed Sep 20, 2023
1 parent 9b8dfd2 commit c2b1ab5
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 9 deletions.
8 changes: 7 additions & 1 deletion proto/anki/notetypes.proto
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down
8 changes: 6 additions & 2 deletions pylib/anki/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions qt/aqt/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
5 changes: 4 additions & 1 deletion qt/aqt/operations/notetype.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
5 changes: 3 additions & 2 deletions rslib/src/notetype/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<anki_proto::collection::OpChanges> {
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(
Expand Down

0 comments on commit c2b1ab5

Please sign in to comment.