simplify previewCards() and use existing card ids where possible

The type arg is no longer used, as neither type 0 nor 1 appears to
have been used in the codebase.

By using the existing card ids, it allows add-ons that gather
information about a card to work properly in the card template screen
without extra hacks.
This commit is contained in:
Damien Elmes 2020-01-16 09:17:32 +10:00
parent 391d849e51
commit cc9a36c11a

View file

@ -474,22 +474,23 @@ insert into cards values (?,?,?,?,?,?,0,0,?,0,0,0,0,0,0,0,0,"")""",
) )
return rem return rem
# type 0 - when previewing in add dialog, only non-empty # type is no longer used
# type 1 - when previewing edit, only existing def previewCards(
# type 2 - when previewing in models dialog, all templates self, note: Note, type: int = 0, did: Optional[int] = None
def previewCards(self, note: Note, type: int = 0, did: None = None) -> List: ) -> List:
if type == 0: existing_cards = {}
cms = self.findTemplates(note) for card in note.cards():
elif type == 1: existing_cards[card.ord] = card
cms = [c.template() for c in note.cards()]
else: all_cards = []
cms = note.model()["tmpls"] for idx, template in enumerate(note.model()["tmpls"]):
if not cms: if idx in existing_cards:
return [] all_cards.append(existing_cards[idx])
cards = [] else:
for template in cms: # card not currently in database, generate an ephemeral one
cards.append(self._newCard(note, template, 1, flush=False, did=did)) all_cards.append(self._newCard(note, template, 1, flush=False, did=did))
return cards
return all_cards
def _newCard( def _newCard(
self, self,
@ -497,12 +498,12 @@ insert into cards values (?,?,?,?,?,?,0,0,?,0,0,0,0,0,0,0,0,"")""",
template: Template, template: Template,
due: int, due: int,
flush: bool = True, flush: bool = True,
did: None = None, did: Optional[int] = None,
) -> Card: ) -> Card:
"Create a new card." "Create a new card."
card = Card(self) card = Card(self)
card.nid = note.id card.nid = note.id
card.ord = template["ord"] card.ord = template["ord"] # type: ignore
card.did = self.db.scalar( card.did = self.db.scalar(
"select did from cards where nid = ? and ord = ?", card.nid, card.ord "select did from cards where nid = ? and ord = ?", card.nid, card.ord
) )