use add_card()

This commit is contained in:
Damien Elmes 2020-03-27 15:11:07 +10:00
parent 6b9378fb41
commit 84b84ae31c
5 changed files with 29 additions and 29 deletions

View file

@ -49,6 +49,7 @@ message BackendInput {
Empty close_collection = 37; Empty close_collection = 37;
int64 get_card = 38; int64 get_card = 38;
Card update_card = 39; Card update_card = 39;
Card add_card = 40;
} }
} }
@ -81,6 +82,7 @@ message BackendOutput {
Empty close_collection = 37; Empty close_collection = 37;
GetCardOut get_card = 38; GetCardOut get_card = 38;
Empty update_card = 39; Empty update_card = 39;
int64 add_card = 40;
BackendError error = 2047; BackendError error = 2047;
} }

View file

@ -14,7 +14,7 @@ from anki.models import NoteType, Template
from anki.notes import Note from anki.notes import Note
from anki.rsbackend import BackendCard from anki.rsbackend import BackendCard
from anki.sound import AVTag from anki.sound import AVTag
from anki.utils import intTime, joinFields, timestampID from anki.utils import joinFields
# Cards # Cards
########################################################################## ##########################################################################
@ -38,32 +38,23 @@ class Card:
self.col = col.weakref() self.col = col.weakref()
self.timerStarted = None self.timerStarted = None
self._render_output: Optional[anki.template.TemplateRenderOutput] = None self._render_output: Optional[anki.template.TemplateRenderOutput] = None
self._note = None
if id: if id:
# existing card
self.id = id self.id = id
self.load() self.load()
else: else:
# to flush, set nid, ord, and due # new card with defaults
self.id = timestampID(col.db, "cards") self._load_from_backend_card(BackendCard())
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 = ""
def load(self) -> None: def load(self) -> None:
self._render_output = None
self._note = None
c = self.col.backend.get_card(self.id) c = self.col.backend.get_card(self.id)
assert c 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.nid = c.nid
self.did = c.did self.did = c.did
self.ord = c.ord self.ord = c.ord
@ -112,7 +103,10 @@ class Card:
flags=self.flags, flags=self.flags,
data=self.data, 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: def question(self, reload: bool = False, browser: bool = False) -> str:
return self.css() + self.render_output(reload, browser).question_text return self.css() + self.render_output(reload, browser).question_text

View file

@ -487,6 +487,9 @@ class RustBackend:
def update_card(self, card: BackendCard) -> None: def update_card(self, card: BackendCard) -> None:
self._run_command(pb.BackendInput(update_card=card)) 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( def translate_string_in(
key: TR, **kwargs: Union[str, int, float] key: TR, **kwargs: Union[str, int, float]

View file

@ -6,7 +6,7 @@ use crate::backend_proto::backend_input::Value;
use crate::backend_proto::{BuiltinSortKind, Empty, RenderedTemplateReplacement, SyncMediaIn}; use crate::backend_proto::{BuiltinSortKind, Empty, RenderedTemplateReplacement, SyncMediaIn};
use crate::card::{Card, CardID}; use crate::card::{Card, CardID};
use crate::card::{CardQueue, CardType}; use crate::card::{CardQueue, CardType};
use crate::collection::{open_collection, Collection, CollectionOp}; use crate::collection::{open_collection, Collection};
use crate::config::SortKind; use crate::config::SortKind;
use crate::decks::DeckID; use crate::decks::DeckID;
use crate::err::{AnkiError, NetworkErrorKind, Result, SyncErrorKind}; use crate::err::{AnkiError, NetworkErrorKind, Result, SyncErrorKind};
@ -260,6 +260,7 @@ impl Backend {
self.update_card(card)?; self.update_card(card)?;
OValue::UpdateCard(pb::Empty {}) 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<()> { fn update_card(&self, pbcard: pb::Card) -> Result<()> {
let mut card = pbcard_to_native(pbcard)?; let mut card = pbcard_to_native(pbcard)?;
self.with_col(|col| { self.with_col(|col| col.transact(None, |ctx| ctx.update_card(&mut card)))
col.transact(Some(CollectionOp::UpdateCard), |ctx| { }
ctx.update_card(&mut card)
}) fn add_card(&self, pbcard: pb::Card) -> Result<i64> {
}) let mut card = pbcard_to_native(pbcard)?;
self.with_col(|col| col.transact(None, |ctx| ctx.add_card(&mut card)))?;
Ok(card.id.0)
} }
} }

View file

@ -51,9 +51,7 @@ pub struct Collection {
state: CollectionState, state: CollectionState,
} }
pub(crate) enum CollectionOp { pub(crate) enum CollectionOp {}
UpdateCard,
}
pub(crate) struct RequestContext<'a> { pub(crate) struct RequestContext<'a> {
pub storage: StorageContext<'a>, pub storage: StorageContext<'a>,