expose read-only access to new notetype objects

This commit is contained in:
Damien Elmes 2021-04-04 20:45:37 +10:00
parent c60b88cd2f
commit 1a4c4373d2
4 changed files with 16 additions and 1 deletions

View File

@ -43,7 +43,7 @@ from anki.decks import Deck, DeckId, DeckManager
from anki.errors import AbortSchemaModification, DBError
from anki.lang import FormatTimeSpan
from anki.media import MediaManager, media_paths_from_col_path
from anki.models import ModelManager, NotetypeDict, NotetypeId
from anki.models import ModelManager, NotetypeDict, NotetypeId, Notetype
from anki.notes import Note, NoteId
from anki.scheduler.v1 import Scheduler as V1Scheduler
from anki.scheduler.v2 import Scheduler as V2Scheduler
@ -335,6 +335,10 @@ class Collection:
"Get a new-style deck object. Currently read-only."
return self._backend.get_deck(id)
def get_notetype(self, id: NotetypeId) -> Notetype:
"""Get a new-style notetype object. This is not cached; avoid calling frequently."""
return self._backend.get_notetype(id)
getCard = get_card
getNote = get_note

View File

@ -27,6 +27,7 @@ from anki.utils import (
)
# public exports
Notetype = _pb.Notetype
NotetypeNameId = _pb.NotetypeNameId
NotetypeNameIdUseCount = _pb.NotetypeNameIdUseCount

View File

@ -198,6 +198,7 @@ service ConfigService {
service NotetypesService {
rpc AddOrUpdateNotetype(AddOrUpdateNotetypeIn) returns (NotetypeId);
rpc GetStockNotetypeLegacy(StockNotetype) returns (Json);
rpc GetNotetype(NotetypeId) returns (Notetype);
rpc GetNotetypeLegacy(NotetypeId) returns (Json);
rpc GetNotetypeNames(Empty) returns (NotetypeNames);
rpc GetNotetypeNamesAndCounts(Empty) returns (NotetypeUseCounts);

View File

@ -34,6 +34,15 @@ impl NotetypesService for Backend {
.map(Into::into)
}
fn get_notetype(&self, input: pb::NotetypeId) -> Result<pb::Notetype> {
self.with_col(|col| {
col.storage
.get_notetype(input.into())?
.ok_or(AnkiError::NotFound)
.map(Into::into)
})
}
fn get_notetype_legacy(&self, input: pb::NotetypeId) -> Result<pb::Json> {
self.with_col(|col| {
let schema11: NotetypeSchema11 = col