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:
RumovZ 2021-10-29 11:43:17 +02:00 committed by GitHub
parent a5ed0b0bc7
commit 16ad0137f7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 56 additions and 22 deletions

View file

@ -21,6 +21,7 @@ service DecksService {
rpc SetDeckCollapsed(SetDeckCollapsedRequest) returns (collection.OpChanges);
rpc GetDeckLegacy(DeckId) returns (generic.Json);
rpc GetDeckNames(GetDeckNamesRequest) returns (DeckNames);
rpc GetDeckAndChildNames(DeckId) returns (DeckNames);
rpc NewDeckLegacy(generic.Bool) returns (generic.Json);
rpc RemoveDecks(DeckIds) returns (collection.OpChangesWithCount);
rpc ReparentDecks(ReparentDecksRequest)

View file

@ -443,26 +443,31 @@ class DeckManager(DeprecatedNamesMixin):
def key(cls, deck: DeckDict) -> list[str]:
return cls.path(deck["name"])
def children(self, did: DeckId) -> list[tuple[str, DeckId]]:
"All children of did, 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}::"
def deck_and_child_name_ids(self, deck_id: DeckId) -> Iterable[tuple[str, DeckId]]:
"""The deck of did and all its children, as (name, id)."""
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]:
parent_name = self.name(deck_id)
out = [deck_id]
out.extend(self.child_ids(parent_name))
return out
return [
DeckId(entry.id)
for entry in self.col._backend.get_deck_and_child_names(deck_id)
]
def parents(
self, did: DeckId, name_map: dict[str, DeckDict] | None = None

View file

@ -128,12 +128,14 @@ impl DecksService for Backend {
} else {
col.get_all_normal_deck_names()?
};
Ok(pb::DeckNames {
entries: names
.into_iter()
.map(|(id, name)| pb::DeckNameId { id: id.0, name })
.collect(),
})
Ok(names.into())
})
}
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> {
// let deck = if input.val {
// Deck::new_filtered()

View file

@ -161,6 +161,15 @@ impl Collection {
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 {