mirror of
https://github.com/ankitects/anki.git
synced 2025-09-19 06:22:22 -04:00
expose read-only access to new deck objects
This commit is contained in:
parent
8e16c94b96
commit
c60b88cd2f
7 changed files with 51 additions and 21 deletions
|
@ -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
|
||||||
|
|
||||||
|
|
|
@ -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)
|
||||||
|
|
||||||
|
|
|
@ -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,13 +270,14 @@ 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
|
||||||
|
|
|
@ -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,22 +1148,26 @@ 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)
|
||||||
return
|
|
||||||
|
|
||||||
rename_deck(
|
def after_fetch(deck: Deck) -> None:
|
||||||
mw=self.mw,
|
if new_name == deck.name:
|
||||||
deck_id=DeckId(item.id),
|
return
|
||||||
new_name=new_name,
|
|
||||||
after_rename=lambda: self.refresh(
|
rename_deck(
|
||||||
lambda other: other.item_type == SidebarItemType.DECK
|
mw=self.mw,
|
||||||
and other.id == item.id
|
deck_id=deck_id,
|
||||||
),
|
new_name=new_name,
|
||||||
)
|
after_rename=lambda: self.refresh(
|
||||||
|
lambda other: other.item_type == SidebarItemType.DECK
|
||||||
|
and other.id == item.id
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
|
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())
|
||||||
|
|
|
@ -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);
|
||||||
|
|
|
@ -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
|
||||||
|
|
|
@ -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 {
|
||||||
|
|
Loading…
Reference in a new issue