expose read-only access to new deck objects

This commit is contained in:
Damien Elmes 2021-04-04 00:15:25 +10:00
parent 8e16c94b96
commit c60b88cd2f
7 changed files with 51 additions and 21 deletions

View file

@ -39,7 +39,7 @@ from anki.cards import Card, CardId
from anki.config import Config, ConfigManager from anki.config import Config, ConfigManager
from anki.consts import * from anki.consts import *
from anki.dbproxy import DBProxy from anki.dbproxy import DBProxy
from anki.decks import DeckId, DeckManager from anki.decks import Deck, DeckId, DeckManager
from anki.errors import AbortSchemaModification, DBError from anki.errors import AbortSchemaModification, DBError
from anki.lang import FormatTimeSpan from anki.lang import FormatTimeSpan
from anki.media import MediaManager, media_paths_from_col_path from anki.media import MediaManager, media_paths_from_col_path
@ -331,6 +331,10 @@ class Collection:
note=note._to_backend_note(), skip_undo_entry=False note=note._to_backend_note(), skip_undo_entry=False
) )
def get_deck(self, id: DeckId) -> Deck:
"Get a new-style deck object. Currently read-only."
return self._backend.get_deck(id)
getCard = get_card getCard = get_card
getNote = get_note getNote = get_note

View file

@ -30,6 +30,9 @@ defaultDynamicDeck = 1
DeckDict = Dict[str, Any] DeckDict = Dict[str, Any]
DeckConfigDict = Dict[str, Any] DeckConfigDict = Dict[str, Any]
# currently only supports read-only access
Deck = _pb.Deck
DeckId = NewType("DeckId", int) DeckId = NewType("DeckId", int)
DeckConfId = NewType("DeckConfId", int) DeckConfId = NewType("DeckConfId", int)

View file

@ -9,7 +9,7 @@ from typing import Any
import aqt import aqt
from anki.collection import OpChanges from anki.collection import OpChanges
from anki.decks import DeckId, DeckTreeNode from anki.decks import Deck, DeckId, DeckTreeNode
from anki.utils import intTime from anki.utils import intTime
from aqt import AnkiQt, gui_hooks from aqt import AnkiQt, gui_hooks
from aqt.operations.deck import ( from aqt.operations.deck import (
@ -270,14 +270,15 @@ class DeckBrowser:
self.mw.onExport(did=did) self.mw.onExport(did=did)
def _rename(self, did: DeckId) -> None: def _rename(self, did: DeckId) -> None:
deck = self.mw.col.decks.get(did) def prompt(deck: Deck) -> None:
current_name = deck["name"] new_name = getOnlyText(tr.decks_new_deck_name(), default=deck.name)
new_name = getOnlyText(tr.decks_new_deck_name(), default=current_name) if not new_name or new_name == deck.name:
if not new_name or new_name == current_name:
return return
else:
rename_deck(mw=self.mw, deck_id=did, new_name=new_name) rename_deck(mw=self.mw, deck_id=did, new_name=new_name)
self.mw.query_op(lambda: self.mw.col.get_deck(did), success=prompt)
def _options(self, did: DeckId) -> None: def _options(self, did: DeckId) -> None:
# select the deck first, because the dyn deck conf assumes the deck # select the deck first, because the dyn deck conf assumes the deck
# we're editing is the current one # we're editing is the current one

View file

@ -8,7 +8,7 @@ from typing import Dict, Iterable, List, Optional, Tuple, cast
import aqt import aqt
from anki.collection import Config, OpChanges, SearchJoiner, SearchNode from anki.collection import Config, OpChanges, SearchJoiner, SearchNode
from anki.decks import DeckId, DeckTreeNode from anki.decks import Deck, DeckId, DeckTreeNode
from anki.models import NotetypeId from anki.models import NotetypeId
from anki.notes import Note from anki.notes import Note
from anki.tags import TagTreeNode from anki.tags import TagTreeNode
@ -1148,16 +1148,18 @@ class SidebarTreeView(QTreeView):
########################### ###########################
def rename_deck(self, item: SidebarItem, new_name: str) -> None: def rename_deck(self, item: SidebarItem, new_name: str) -> None:
deck = self.mw.col.decks.get(DeckId(item.id))
if not new_name: if not new_name:
return return
new_name = item.name_prefix + new_name new_name = item.name_prefix + new_name
if new_name == deck["name"]: deck_id = DeckId(item.id)
def after_fetch(deck: Deck) -> None:
if new_name == deck.name:
return return
rename_deck( rename_deck(
mw=self.mw, mw=self.mw,
deck_id=DeckId(item.id), deck_id=deck_id,
new_name=new_name, new_name=new_name,
after_rename=lambda: self.refresh( after_rename=lambda: self.refresh(
lambda other: other.item_type == SidebarItemType.DECK lambda other: other.item_type == SidebarItemType.DECK
@ -1165,6 +1167,8 @@ class SidebarTreeView(QTreeView):
), ),
) )
self.mw.query_op(lambda: self.mw.col.get_deck(deck_id), success=after_fetch)
def delete_decks(self, _item: SidebarItem) -> None: def delete_decks(self, _item: SidebarItem) -> None:
remove_decks(mw=self.mw, parent=self.browser, deck_ids=self._selected_decks()) remove_decks(mw=self.mw, parent=self.browser, deck_ids=self._selected_decks())

View file

@ -142,6 +142,7 @@ service DecksService {
rpc DeckTreeLegacy(Empty) returns (Json); rpc DeckTreeLegacy(Empty) returns (Json);
rpc GetAllDecksLegacy(Empty) returns (Json); rpc GetAllDecksLegacy(Empty) returns (Json);
rpc GetDeckIdByName(String) returns (DeckId); rpc GetDeckIdByName(String) returns (DeckId);
rpc GetDeck(DeckId) returns (Deck);
rpc GetDeckLegacy(DeckId) returns (Json); rpc GetDeckLegacy(DeckId) returns (Json);
rpc GetDeckNames(GetDeckNamesIn) returns (DeckNames); rpc GetDeckNames(GetDeckNamesIn) returns (DeckNames);
rpc NewDeckLegacy(Bool) returns (Json); rpc NewDeckLegacy(Bool) returns (Json);

View file

@ -78,6 +78,17 @@ impl DecksService for Backend {
}) })
} }
fn get_deck(&self, input: pb::DeckId) -> Result<pb::Deck> {
self.with_col(|col| {
Ok(col
.storage
.get_deck(input.into())?
.ok_or(AnkiError::NotFound)?
.with_human_name()
.into())
})
}
fn get_deck_legacy(&self, input: pb::DeckId) -> Result<pb::Json> { fn get_deck_legacy(&self, input: pb::DeckId) -> Result<pb::Json> {
self.with_col(|col| { self.with_col(|col| {
let deck: DeckSchema11 = col let deck: DeckSchema11 = col

View file

@ -153,6 +153,12 @@ impl Deck {
String::new() String::new()
} }
} }
// Mutate name to human representation for sharing.
pub fn with_human_name(mut self) -> Self {
self.name = self.human_name();
self
}
} }
fn invalid_char_for_deck_component(c: char) -> bool { fn invalid_char_for_deck_component(c: char) -> bool {