mirror of
https://github.com/ankitects/anki.git
synced 2025-09-20 06:52:21 -04:00
Use backend for getting deck children (#1465)
* Add backend routine for child deck names and ids * Use backend for getting deck children Instead of flawed case-sensitive Python logic.
This commit is contained in:
parent
a5ed0b0bc7
commit
16ad0137f7
4 changed files with 56 additions and 22 deletions
|
@ -21,6 +21,7 @@ service DecksService {
|
||||||
rpc SetDeckCollapsed(SetDeckCollapsedRequest) returns (collection.OpChanges);
|
rpc SetDeckCollapsed(SetDeckCollapsedRequest) returns (collection.OpChanges);
|
||||||
rpc GetDeckLegacy(DeckId) returns (generic.Json);
|
rpc GetDeckLegacy(DeckId) returns (generic.Json);
|
||||||
rpc GetDeckNames(GetDeckNamesRequest) returns (DeckNames);
|
rpc GetDeckNames(GetDeckNamesRequest) returns (DeckNames);
|
||||||
|
rpc GetDeckAndChildNames(DeckId) returns (DeckNames);
|
||||||
rpc NewDeckLegacy(generic.Bool) returns (generic.Json);
|
rpc NewDeckLegacy(generic.Bool) returns (generic.Json);
|
||||||
rpc RemoveDecks(DeckIds) returns (collection.OpChangesWithCount);
|
rpc RemoveDecks(DeckIds) returns (collection.OpChangesWithCount);
|
||||||
rpc ReparentDecks(ReparentDecksRequest)
|
rpc ReparentDecks(ReparentDecksRequest)
|
||||||
|
|
|
@ -443,26 +443,31 @@ class DeckManager(DeprecatedNamesMixin):
|
||||||
def key(cls, deck: DeckDict) -> list[str]:
|
def key(cls, deck: DeckDict) -> list[str]:
|
||||||
return cls.path(deck["name"])
|
return cls.path(deck["name"])
|
||||||
|
|
||||||
def children(self, did: DeckId) -> list[tuple[str, DeckId]]:
|
def deck_and_child_name_ids(self, deck_id: DeckId) -> Iterable[tuple[str, DeckId]]:
|
||||||
"All children of did, as (name, id)."
|
"""The deck of did and all its children, as (name, id)."""
|
||||||
name = self.get(did)["name"]
|
|
||||||
actv = []
|
|
||||||
for entry in self.all_names_and_ids():
|
|
||||||
if entry.name.startswith(f"{name}::"):
|
|
||||||
actv.append((entry.name, DeckId(entry.id)))
|
|
||||||
return actv
|
|
||||||
|
|
||||||
def child_ids(self, parent_name: str) -> Iterable[DeckId]:
|
|
||||||
prefix = f"{parent_name}::"
|
|
||||||
return (
|
return (
|
||||||
DeckId(d.id) for d in self.all_names_and_ids() if d.name.startswith(prefix)
|
(entry.name, DeckId(entry.id))
|
||||||
|
for entry in self.col._backend.get_deck_and_child_names(deck_id)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
def children(self, did: DeckId) -> list[tuple[str, DeckId]]:
|
||||||
|
"All children of did, as (name, id)."
|
||||||
|
return [
|
||||||
|
name_id
|
||||||
|
for name_id in self.deck_and_child_name_ids(did)
|
||||||
|
if name_id[1] != did
|
||||||
|
]
|
||||||
|
|
||||||
|
def child_ids(self, parent_name: str) -> Iterable[DeckId]:
|
||||||
|
if not (parent_id := self.id_for_name(parent_name)):
|
||||||
|
return []
|
||||||
|
return (name_id[1] for name_id in self.children(parent_id))
|
||||||
|
|
||||||
def deck_and_child_ids(self, deck_id: DeckId) -> list[DeckId]:
|
def deck_and_child_ids(self, deck_id: DeckId) -> list[DeckId]:
|
||||||
parent_name = self.name(deck_id)
|
return [
|
||||||
out = [deck_id]
|
DeckId(entry.id)
|
||||||
out.extend(self.child_ids(parent_name))
|
for entry in self.col._backend.get_deck_and_child_names(deck_id)
|
||||||
return out
|
]
|
||||||
|
|
||||||
def parents(
|
def parents(
|
||||||
self, did: DeckId, name_map: dict[str, DeckDict] | None = None
|
self, did: DeckId, name_map: dict[str, DeckDict] | None = None
|
||||||
|
|
|
@ -128,12 +128,14 @@ impl DecksService for Backend {
|
||||||
} else {
|
} else {
|
||||||
col.get_all_normal_deck_names()?
|
col.get_all_normal_deck_names()?
|
||||||
};
|
};
|
||||||
Ok(pb::DeckNames {
|
Ok(names.into())
|
||||||
entries: names
|
})
|
||||||
.into_iter()
|
}
|
||||||
.map(|(id, name)| pb::DeckNameId { id: id.0, name })
|
|
||||||
.collect(),
|
fn get_deck_and_child_names(&self, input: pb::DeckId) -> Result<pb::DeckNames> {
|
||||||
})
|
self.with_col(|col| {
|
||||||
|
col.get_deck_and_child_names(input.did.into())
|
||||||
|
.map(Into::into)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -293,6 +295,23 @@ impl From<pb::deck::Kind> for DeckKind {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl From<(DeckId, String)> for pb::DeckNameId {
|
||||||
|
fn from(id_name: (DeckId, String)) -> Self {
|
||||||
|
pb::DeckNameId {
|
||||||
|
id: id_name.0 .0,
|
||||||
|
name: id_name.1,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<Vec<(DeckId, String)>> for pb::DeckNames {
|
||||||
|
fn from(id_names: Vec<(DeckId, String)>) -> Self {
|
||||||
|
pb::DeckNames {
|
||||||
|
entries: id_names.into_iter().map(Into::into).collect(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// fn new_deck(&self, input: pb::Bool) -> Result<pb::Deck> {
|
// fn new_deck(&self, input: pb::Bool) -> Result<pb::Deck> {
|
||||||
// let deck = if input.val {
|
// let deck = if input.val {
|
||||||
// Deck::new_filtered()
|
// Deck::new_filtered()
|
||||||
|
|
|
@ -161,6 +161,15 @@ impl Collection {
|
||||||
self.storage.get_all_deck_names()
|
self.storage.get_all_deck_names()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn get_deck_and_child_names(&self, did: DeckId) -> Result<Vec<(DeckId, String)>> {
|
||||||
|
Ok(self
|
||||||
|
.storage
|
||||||
|
.deck_with_children(did)?
|
||||||
|
.iter()
|
||||||
|
.map(|deck| (deck.id, deck.name.human_name()))
|
||||||
|
.collect())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn invalid_char_for_deck_component(c: char) -> bool {
|
fn invalid_char_for_deck_component(c: char) -> bool {
|
||||||
|
|
Loading…
Reference in a new issue