use aux var when retrieving last deck for notetype
The adding case was already covered by defaults_for_adding(), but we the code was using the old variable when adding new cards to an existing note.
This commit is contained in:
parent
b160c2d85c
commit
610f0b5254
@ -77,7 +77,8 @@ def test_gendeck():
|
|||||||
assert note.cards()[0].did == 1
|
assert note.cards()[0].did == 1
|
||||||
# set the model to a new default col
|
# set the model to a new default col
|
||||||
newId = col.decks.id("new")
|
newId = col.decks.id("new")
|
||||||
cloze["did"] = newId
|
# fixme: dedicated API for this
|
||||||
|
col.set_config(f"_nt_{cloze['id']}_lastDeck", newId)
|
||||||
col.models.save(cloze, updateReqs=False)
|
col.models.save(cloze, updateReqs=False)
|
||||||
# a newly generated card should share the first card's col
|
# a newly generated card should share the first card's col
|
||||||
note["Text"] += "{{c2::two}}"
|
note["Text"] += "{{c2::two}}"
|
||||||
|
@ -466,7 +466,8 @@ message Notetype {
|
|||||||
Kind kind = 1;
|
Kind kind = 1;
|
||||||
uint32 sort_field_idx = 2;
|
uint32 sort_field_idx = 2;
|
||||||
string css = 3;
|
string css = 3;
|
||||||
int64 target_deck_id = 4; // moved into config var
|
/// This is now stored separately; retrieve with DefaultsForAdding()
|
||||||
|
int64 target_deck_id_unused = 4;
|
||||||
string latex_pre = 5;
|
string latex_pre = 5;
|
||||||
string latex_post = 6;
|
string latex_post = 6;
|
||||||
bool latex_svg = 7;
|
bool latex_svg = 7;
|
||||||
|
@ -256,7 +256,9 @@ impl Collection {
|
|||||||
note.notetype_id = nt.id;
|
note.notetype_id = nt.id;
|
||||||
|
|
||||||
// write note, updating tags and generating missing cards
|
// write note, updating tags and generating missing cards
|
||||||
let ctx = genctx.get_or_insert_with(|| CardGenContext::new(&nt, usn));
|
let ctx = genctx.get_or_insert_with(|| {
|
||||||
|
CardGenContext::new(&nt, self.get_last_deck_added_to_for_notetype(nt.id), usn)
|
||||||
|
});
|
||||||
self.update_note_inner_generating_cards(&ctx, &mut note, &original, false, norm)?;
|
self.update_note_inner_generating_cards(&ctx, &mut note, &original, false, norm)?;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -74,7 +74,8 @@ impl Collection {
|
|||||||
let nt = col
|
let nt = col
|
||||||
.get_notetype(note.notetype_id)?
|
.get_notetype(note.notetype_id)?
|
||||||
.ok_or_else(|| AnkiError::invalid_input("missing note type"))?;
|
.ok_or_else(|| AnkiError::invalid_input("missing note type"))?;
|
||||||
let ctx = CardGenContext::new(&nt, col.usn()?);
|
let last_deck = col.get_last_deck_added_to_for_notetype(note.notetype_id);
|
||||||
|
let ctx = CardGenContext::new(&nt, last_deck, col.usn()?);
|
||||||
let norm = col.get_bool(BoolKey::NormalizeNoteText);
|
let norm = col.get_bool(BoolKey::NormalizeNoteText);
|
||||||
col.add_note_inner(&ctx, note, did, norm)
|
col.add_note_inner(&ctx, note, did, norm)
|
||||||
})
|
})
|
||||||
@ -383,7 +384,8 @@ impl Collection {
|
|||||||
let nt = self
|
let nt = self
|
||||||
.get_notetype(note.notetype_id)?
|
.get_notetype(note.notetype_id)?
|
||||||
.ok_or_else(|| AnkiError::invalid_input("missing note type"))?;
|
.ok_or_else(|| AnkiError::invalid_input("missing note type"))?;
|
||||||
let ctx = CardGenContext::new(&nt, self.usn()?);
|
let last_deck = self.get_last_deck_added_to_for_notetype(note.notetype_id);
|
||||||
|
let ctx = CardGenContext::new(&nt, last_deck, self.usn()?);
|
||||||
let norm = self.get_bool(BoolKey::NormalizeNoteText);
|
let norm = self.get_bool(BoolKey::NormalizeNoteText);
|
||||||
self.update_note_inner_generating_cards(&ctx, note, &existing_note, true, norm)?;
|
self.update_note_inner_generating_cards(&ctx, note, &existing_note, true, norm)?;
|
||||||
Ok(())
|
Ok(())
|
||||||
@ -484,7 +486,13 @@ impl Collection {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if out.generate_cards {
|
if out.generate_cards {
|
||||||
let ctx = genctx.get_or_insert_with(|| CardGenContext::new(&nt, usn));
|
let ctx = genctx.get_or_insert_with(|| {
|
||||||
|
CardGenContext::new(
|
||||||
|
&nt,
|
||||||
|
self.get_last_deck_added_to_for_notetype(nt.id),
|
||||||
|
usn,
|
||||||
|
)
|
||||||
|
});
|
||||||
self.update_note_inner_generating_cards(
|
self.update_note_inner_generating_cards(
|
||||||
&ctx,
|
&ctx,
|
||||||
&mut note,
|
&mut note,
|
||||||
|
@ -8,16 +8,8 @@ use rand::{rngs::StdRng, Rng, SeedableRng};
|
|||||||
|
|
||||||
use super::Notetype;
|
use super::Notetype;
|
||||||
use crate::{
|
use crate::{
|
||||||
card::{Card, CardId},
|
cloze::add_cloze_numbers_in_string, notetype::NotetypeKind, prelude::*,
|
||||||
cloze::add_cloze_numbers_in_string,
|
|
||||||
collection::Collection,
|
|
||||||
deckconfig::{DeckConfig, DeckConfigId},
|
|
||||||
decks::DeckId,
|
|
||||||
error::{AnkiError, Result},
|
|
||||||
notes::{Note, NoteId},
|
|
||||||
notetype::NotetypeKind,
|
|
||||||
template::ParsedTemplate,
|
template::ParsedTemplate,
|
||||||
types::Usn,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/// Info about an existing card required when generating new cards
|
/// Info about an existing card required when generating new cards
|
||||||
@ -49,6 +41,8 @@ pub(crate) struct SingleCardGenContext {
|
|||||||
pub(crate) struct CardGenContext<'a> {
|
pub(crate) struct CardGenContext<'a> {
|
||||||
pub usn: Usn,
|
pub usn: Usn,
|
||||||
pub notetype: &'a Notetype,
|
pub notetype: &'a Notetype,
|
||||||
|
/// The last deck that was added to with this note type
|
||||||
|
pub last_deck: Option<DeckId>,
|
||||||
cards: Vec<SingleCardGenContext>,
|
cards: Vec<SingleCardGenContext>,
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -60,9 +54,10 @@ pub(crate) struct CardGenCache {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl CardGenContext<'_> {
|
impl CardGenContext<'_> {
|
||||||
pub(crate) fn new(nt: &Notetype, usn: Usn) -> CardGenContext<'_> {
|
pub(crate) fn new(nt: &Notetype, last_deck: Option<DeckId>, usn: Usn) -> CardGenContext<'_> {
|
||||||
CardGenContext {
|
CardGenContext {
|
||||||
usn,
|
usn,
|
||||||
|
last_deck,
|
||||||
notetype: &nt,
|
notetype: &nt,
|
||||||
cards: nt
|
cards: nt
|
||||||
.templates
|
.templates
|
||||||
@ -233,13 +228,7 @@ impl Collection {
|
|||||||
note: &Note,
|
note: &Note,
|
||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
let existing = self.storage.existing_cards_for_note(note.id)?;
|
let existing = self.storage.existing_cards_for_note(note.id)?;
|
||||||
self.generate_cards_for_note(
|
self.generate_cards_for_note(ctx, note, &existing, ctx.last_deck, &mut Default::default())
|
||||||
ctx,
|
|
||||||
note,
|
|
||||||
&existing,
|
|
||||||
Some(ctx.notetype.target_deck_id()),
|
|
||||||
&mut Default::default(),
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn generate_cards_for_note(
|
fn generate_cards_for_note(
|
||||||
|
@ -17,7 +17,8 @@ pub struct EmptyCardsForNote {
|
|||||||
|
|
||||||
impl Collection {
|
impl Collection {
|
||||||
fn empty_cards_for_notetype(&self, nt: &Notetype) -> Result<Vec<EmptyCardsForNote>> {
|
fn empty_cards_for_notetype(&self, nt: &Notetype) -> Result<Vec<EmptyCardsForNote>> {
|
||||||
let ctx = CardGenContext::new(nt, self.usn()?);
|
let last_deck = self.get_last_deck_added_to_for_notetype(nt.id);
|
||||||
|
let ctx = CardGenContext::new(nt, last_deck, self.usn()?);
|
||||||
let existing_cards = self.storage.existing_cards_for_notetype(nt.id)?;
|
let existing_cards = self.storage.existing_cards_for_notetype(nt.id)?;
|
||||||
let by_note = group_generated_cards_by_note(existing_cards);
|
let by_note = group_generated_cards_by_note(existing_cards);
|
||||||
let mut out = Vec::with_capacity(by_note.len());
|
let mut out = Vec::with_capacity(by_note.len());
|
||||||
|
@ -35,16 +35,11 @@ pub use crate::backend_proto::{
|
|||||||
Notetype as NotetypeProto,
|
Notetype as NotetypeProto,
|
||||||
};
|
};
|
||||||
use crate::{
|
use crate::{
|
||||||
collection::Collection,
|
|
||||||
decks::DeckId,
|
|
||||||
define_newtype,
|
define_newtype,
|
||||||
error::{AnkiError, Result, TemplateSaveError},
|
error::TemplateSaveError,
|
||||||
notes::Note,
|
|
||||||
prelude::*,
|
prelude::*,
|
||||||
template::{FieldRequirements, ParsedTemplate},
|
template::{FieldRequirements, ParsedTemplate},
|
||||||
text::ensure_string_in_nfc,
|
text::ensure_string_in_nfc,
|
||||||
timestamp::TimestampSecs,
|
|
||||||
types::Usn,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
define_newtype!(NotetypeId, i64);
|
define_newtype!(NotetypeId, i64);
|
||||||
@ -99,10 +94,6 @@ impl Notetype {
|
|||||||
|
|
||||||
template.ok_or(AnkiError::NotFound)
|
template.ok_or(AnkiError::NotFound)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn target_deck_id(&self) -> DeckId {
|
|
||||||
DeckId(self.config.target_deck_id)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Collection {
|
impl Collection {
|
||||||
@ -320,14 +311,6 @@ impl Notetype {
|
|||||||
self.templates.push(CardTemplate::new(name, qfmt, afmt));
|
self.templates.push(CardTemplate::new(name, qfmt, afmt));
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn prepare_for_adding(&mut self) -> Result<()> {
|
|
||||||
// defaults to 0
|
|
||||||
if self.config.target_deck_id == 0 {
|
|
||||||
self.config.target_deck_id = 1;
|
|
||||||
}
|
|
||||||
self.prepare_for_update(None)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) fn prepare_for_update(&mut self, existing: Option<&Notetype>) -> Result<()> {
|
pub(crate) fn prepare_for_update(&mut self, existing: Option<&Notetype>) -> Result<()> {
|
||||||
if self.fields.is_empty() {
|
if self.fields.is_empty() {
|
||||||
return Err(AnkiError::invalid_input("1 field required"));
|
return Err(AnkiError::invalid_input("1 field required"));
|
||||||
@ -489,7 +472,7 @@ impl Collection {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn add_notetype_inner(&mut self, nt: &mut Notetype, usn: Usn) -> Result<()> {
|
pub(crate) fn add_notetype_inner(&mut self, nt: &mut Notetype, usn: Usn) -> Result<()> {
|
||||||
nt.prepare_for_adding()?;
|
nt.prepare_for_update(None)?;
|
||||||
self.ensure_notetype_name_unique(nt, usn)?;
|
self.ensure_notetype_name_unique(nt, usn)?;
|
||||||
self.storage.add_new_notetype(nt)
|
self.storage.add_new_notetype(nt)
|
||||||
}
|
}
|
||||||
|
@ -98,7 +98,7 @@ impl From<NotetypeSchema11> for Notetype {
|
|||||||
kind: nt.kind as i32,
|
kind: nt.kind as i32,
|
||||||
sort_field_idx: nt.sortf as u32,
|
sort_field_idx: nt.sortf as u32,
|
||||||
css: nt.css,
|
css: nt.css,
|
||||||
target_deck_id: nt.did.unwrap_or(DeckId(0)).0,
|
target_deck_id_unused: nt.did.unwrap_or(DeckId(0)).0,
|
||||||
latex_pre: nt.latex_pre,
|
latex_pre: nt.latex_pre,
|
||||||
latex_post: nt.latex_post,
|
latex_post: nt.latex_post,
|
||||||
latex_svg: nt.latexsvg,
|
latex_svg: nt.latexsvg,
|
||||||
@ -148,10 +148,10 @@ impl From<Notetype> for NotetypeSchema11 {
|
|||||||
mtime: p.mtime_secs,
|
mtime: p.mtime_secs,
|
||||||
usn: p.usn,
|
usn: p.usn,
|
||||||
sortf: c.sort_field_idx as u16,
|
sortf: c.sort_field_idx as u16,
|
||||||
did: if c.target_deck_id == 0 {
|
did: if c.target_deck_id_unused == 0 {
|
||||||
None
|
None
|
||||||
} else {
|
} else {
|
||||||
Some(DeckId(c.target_deck_id))
|
Some(DeckId(c.target_deck_id_unused))
|
||||||
},
|
},
|
||||||
tmpls: p.templates.into_iter().map(Into::into).collect(),
|
tmpls: p.templates.into_iter().map(Into::into).collect(),
|
||||||
flds: p.fields.into_iter().map(Into::into).collect(),
|
flds: p.fields.into_iter().map(Into::into).collect(),
|
||||||
|
@ -127,7 +127,8 @@ impl Collection {
|
|||||||
.move_cards_for_repositioned_templates(nt.id, &changes.moved)?;
|
.move_cards_for_repositioned_templates(nt.id, &changes.moved)?;
|
||||||
}
|
}
|
||||||
|
|
||||||
let ctx = CardGenContext::new(nt, self.usn()?);
|
let last_deck = self.get_last_deck_added_to_for_notetype(nt.id);
|
||||||
|
let ctx = CardGenContext::new(nt, last_deck, self.usn()?);
|
||||||
self.generate_cards_for_notetype(&ctx)?;
|
self.generate_cards_for_notetype(&ctx)?;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
|
@ -15,6 +15,7 @@ use crate::{
|
|||||||
impl SqliteStorage {
|
impl SqliteStorage {
|
||||||
pub(crate) fn add_stock_notetypes(&self, tr: &I18n) -> Result<()> {
|
pub(crate) fn add_stock_notetypes(&self, tr: &I18n) -> Result<()> {
|
||||||
for (idx, mut nt) in all_stock_notetypes(tr).into_iter().enumerate() {
|
for (idx, mut nt) in all_stock_notetypes(tr).into_iter().enumerate() {
|
||||||
|
nt.prepare_for_update(None)?;
|
||||||
self.add_new_notetype(&mut nt)?;
|
self.add_new_notetype(&mut nt)?;
|
||||||
if idx == Kind::Basic as usize {
|
if idx == Kind::Basic as usize {
|
||||||
self.set_config_entry(&ConfigEntry::boxed(
|
self.set_config_entry(&ConfigEntry::boxed(
|
||||||
@ -63,7 +64,6 @@ pub(crate) fn basic(tr: &I18n) -> Notetype {
|
|||||||
fieldref(back),
|
fieldref(back),
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
nt.prepare_for_adding().unwrap();
|
|
||||||
nt
|
nt
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -79,7 +79,6 @@ pub(crate) fn basic_typing(tr: &I18n) -> Notetype {
|
|||||||
fieldref(front),
|
fieldref(front),
|
||||||
back
|
back
|
||||||
);
|
);
|
||||||
nt.prepare_for_adding().unwrap();
|
|
||||||
nt
|
nt
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -97,7 +96,6 @@ pub(crate) fn basic_forward_reverse(tr: &I18n) -> Notetype {
|
|||||||
fieldref(front),
|
fieldref(front),
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
nt.prepare_for_adding().unwrap();
|
|
||||||
nt
|
nt
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -108,7 +106,6 @@ pub(crate) fn basic_optional_reverse(tr: &I18n) -> Notetype {
|
|||||||
nt.add_field(addrev.as_ref());
|
nt.add_field(addrev.as_ref());
|
||||||
let tmpl = &mut nt.templates[1].config;
|
let tmpl = &mut nt.templates[1].config;
|
||||||
tmpl.q_format = format!("{{{{#{}}}}}{}{{{{/{}}}}}", addrev, tmpl.q_format, addrev);
|
tmpl.q_format = format!("{{{{#{}}}}}{}{{{{/{}}}}}", addrev, tmpl.q_format, addrev);
|
||||||
nt.prepare_for_adding().unwrap();
|
|
||||||
nt
|
nt
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -134,6 +131,5 @@ pub(crate) fn cloze(tr: &I18n) -> Notetype {
|
|||||||
color: lightblue;
|
color: lightblue;
|
||||||
}
|
}
|
||||||
";
|
";
|
||||||
nt.prepare_for_adding().unwrap();
|
|
||||||
nt
|
nt
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user