diff --git a/proto/backend.proto b/proto/backend.proto index ad16f6bed..69a0480d9 100644 --- a/proto/backend.proto +++ b/proto/backend.proto @@ -49,6 +49,7 @@ message BackendInput { Empty close_collection = 37; int64 get_card = 38; Card update_card = 39; + Card add_card = 40; } } @@ -81,6 +82,7 @@ message BackendOutput { Empty close_collection = 37; GetCardOut get_card = 38; Empty update_card = 39; + int64 add_card = 40; BackendError error = 2047; } diff --git a/pylib/anki/cards.py b/pylib/anki/cards.py index d3887410c..4e0fb5610 100644 --- a/pylib/anki/cards.py +++ b/pylib/anki/cards.py @@ -14,7 +14,7 @@ from anki.models import NoteType, Template from anki.notes import Note from anki.rsbackend import BackendCard from anki.sound import AVTag -from anki.utils import intTime, joinFields, timestampID +from anki.utils import joinFields # Cards ########################################################################## @@ -38,32 +38,23 @@ class Card: self.col = col.weakref() self.timerStarted = None self._render_output: Optional[anki.template.TemplateRenderOutput] = None - self._note = None if id: + # existing card self.id = id self.load() else: - # to flush, set nid, ord, and due - self.id = timestampID(col.db, "cards") - self.did = 1 - self.crt = intTime() - self.type = CARD_TYPE_NEW - self.queue = QUEUE_TYPE_NEW - self.ivl = 0 - self.factor = 0 - self.reps = 0 - self.lapses = 0 - self.left = 0 - self.odue = 0 - self.odid = 0 - self.flags = 0 - self.data = "" + # new card with defaults + self._load_from_backend_card(BackendCard()) def load(self) -> None: - self._render_output = None - self._note = None c = self.col.backend.get_card(self.id) assert c + self._load_from_backend_card(c) + + def _load_from_backend_card(self, c: BackendCard) -> None: + self._render_output = None + self._note = None + self.id = c.id self.nid = c.nid self.did = c.did self.ord = c.ord @@ -112,7 +103,10 @@ class Card: flags=self.flags, data=self.data, ) - self.col.backend.update_card(card) + if self.id != 0: + self.col.backend.update_card(card) + else: + self.col.backend.add_card(card) def question(self, reload: bool = False, browser: bool = False) -> str: return self.css() + self.render_output(reload, browser).question_text diff --git a/pylib/anki/rsbackend.py b/pylib/anki/rsbackend.py index 8a98b9be4..eab65cebe 100644 --- a/pylib/anki/rsbackend.py +++ b/pylib/anki/rsbackend.py @@ -487,6 +487,9 @@ class RustBackend: def update_card(self, card: BackendCard) -> None: self._run_command(pb.BackendInput(update_card=card)) + def add_card(self, card: BackendCard) -> None: + card.id = self._run_command(pb.BackendInput(add_card=card)).add_card + def translate_string_in( key: TR, **kwargs: Union[str, int, float] diff --git a/rslib/src/backend/mod.rs b/rslib/src/backend/mod.rs index 0ba8efe53..27c2bc95f 100644 --- a/rslib/src/backend/mod.rs +++ b/rslib/src/backend/mod.rs @@ -6,7 +6,7 @@ use crate::backend_proto::backend_input::Value; use crate::backend_proto::{BuiltinSortKind, Empty, RenderedTemplateReplacement, SyncMediaIn}; use crate::card::{Card, CardID}; use crate::card::{CardQueue, CardType}; -use crate::collection::{open_collection, Collection, CollectionOp}; +use crate::collection::{open_collection, Collection}; use crate::config::SortKind; use crate::decks::DeckID; use crate::err::{AnkiError, NetworkErrorKind, Result, SyncErrorKind}; @@ -260,6 +260,7 @@ impl Backend { self.update_card(card)?; OValue::UpdateCard(pb::Empty {}) } + Value::AddCard(card) => OValue::AddCard(self.add_card(card)?), }) } @@ -638,11 +639,13 @@ impl Backend { fn update_card(&self, pbcard: pb::Card) -> Result<()> { let mut card = pbcard_to_native(pbcard)?; - self.with_col(|col| { - col.transact(Some(CollectionOp::UpdateCard), |ctx| { - ctx.update_card(&mut card) - }) - }) + self.with_col(|col| col.transact(None, |ctx| ctx.update_card(&mut card))) + } + + fn add_card(&self, pbcard: pb::Card) -> Result { + let mut card = pbcard_to_native(pbcard)?; + self.with_col(|col| col.transact(None, |ctx| ctx.add_card(&mut card)))?; + Ok(card.id.0) } } diff --git a/rslib/src/collection.rs b/rslib/src/collection.rs index 627a779be..5db81874e 100644 --- a/rslib/src/collection.rs +++ b/rslib/src/collection.rs @@ -51,9 +51,7 @@ pub struct Collection { state: CollectionState, } -pub(crate) enum CollectionOp { - UpdateCard, -} +pub(crate) enum CollectionOp {} pub(crate) struct RequestContext<'a> { pub storage: StorageContext<'a>,