Example #1
0
  def processDeckTransactions_FORK(self, data):
    orig_did = self.getDID(data["on"])
    # orig_deck = mw.col.decks.get(orig_did)
    new_did = mw.col.decks.id(data["data"]["name"])
    new_deck = mw.col.decks.get(new_did)

    for cid in mw.col.decks.cids(orig_did):
        card = mw.col.getCard(cid)
        note = card.note(reload=True)
        model = note.model()
        createNewModel = False # create new model?

        if createNewModel:
            new_model = mw.col.models.copy(model) # models.copy saves
        new_note = copy.deepcopy(note)
        new_note.col = note.col
        new_note.id = timestampID(mw.col.db, "notes")
        new_note.guid = guid64()
        if createNewModel:
            new_note._model = new_model
            new_note.mid = new_model['id']
        new_note.flush()
        new_card = copy.deepcopy(card)
        new_card.col = card.col
        new_card.id = timestampID(mw.col.db, "cards")
        new_card.crt = intTime()
        new_card.did = new_did
        new_card.nid = new_note.id
        new_card.flush()
    mw.col.decks.save(new_deck)
    mw.col.decks.flush()
Example #2
0
 def copyToIncrementalDeck(self, noteId):
     "Copies all cards belonging to the note id to the incremental deck."
     cardIds = mw.col.db.list("select id from cards where nid=?", noteId)
     for cardId in cardIds:
         # Create a copy, and copy it to the current deck id.
         card = mw.col.getCard(cardId)
         card.did = mw.col.decks.selected()
         card.id = timestampID(mw.col.db, "cards")
         card.flush()
Example #3
0
 def __init__(self, col, model=None, id=None):
     assert not (model and id)
     self.col = col
     if id:
         self.id = id
         self.load()
     else:
         self.id = timestampID(col.db, "notes")
         self.guid = guid64()
         self._model = model
         self.mid = model['id']
         self.tags = []
         self.fields = [""] * len(self._model['flds'])
         self.flags = 0
         self.data = ""
         self._fmap = self.col.models.fieldMap(self._model)
         self.scm = self.col.scm
Example #4
0
 def __init__(self, col, id=None):
     self.col = col
     self.timerStarted = None
     self._qa = None
     self._note = None
     if id:
         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 = 0
         self.queue = 0
         self.ivl = 0
         self.factor = 0
         self.reps = 0
         self.lapses = 0
         self.left = 0
         self.edue = 0
         self.flags = 0
         self.data = ""
Example #5
0
    def importNotes(self, notes):
        "Convert each card into a note, apply attributes and add to col."
        assert self.mappingOk()
        # note whether tags are mapped
        self._tagsMapped = False
        for f in self.mapping:
            if f == "_tags":
                self._tagsMapped = True
        # gather checks for duplicate comparison
        csums = {}
        for csum, id in self.col.db.execute(
            "select csum, id from notes where mid = ?", self.model['id']):
            if csum in csums:
                csums[csum].append(id)
            else:
                csums[csum] = [id]
        firsts = {}
        fld0idx = self.mapping.index(self.model['flds'][0]['name'])
        self._fmap = self.col.models.fieldMap(self.model)
        self._nextID = timestampID(self.col.db, "notes")
        # loop through the notes
        updates = []
        new = []
        self._ids = []
        self._cards = []
        self._emptyNotes = False
        for n in notes:
            if not self.allowHTML:
                for c in range(len(n.fields)):
                    n.fields[c] = cgi.escape(n.fields[c])
            fld0 = n.fields[fld0idx]
            csum = fieldChecksum(fld0)
            # first field must exist
            if not fld0:
                self.log.append(_("Empty first field: %s") %
                                " ".join(n.fields))
                continue
            # earlier in import?
            if fld0 in firsts and self.importMode != 2:
                # duplicates in source file; log and ignore
                self.log.append(_("Appeared twice in file: %s") %
                                fld0)
                continue
            firsts[fld0] = True
            # already exists?
            found = False
            if csum in csums:
                # csum is not a guarantee; have to check
                for id in csums[csum]:
                    flds = self.col.db.scalar(
                        "select flds from notes where id = ?", id)
                    sflds = splitFields(flds)
                    if fld0 == sflds[0]:
                        # duplicate
                        found = True
                        if self.importMode == 0:
                            data = self.updateData(n, id, sflds)
                            if data:
                                updates.append(data)
                                found = True
                            break
                        elif self.importMode == 2:
                            # allow duplicates in this case
                            found = False
            # newly add
            if not found:
                data = self.newData(n)
                if data:
                    new.append(data)
                    # note that we've seen this note once already
                    firsts[fld0] = True
        self.addNew(new)
        self.addUpdates(updates)
        self.col.updateFieldCache(self._ids)
        # generate cards
        if self.col.genCards(self._ids):
            self.log.insert(0, _(
                "Empty cards found. Please run Tools>Empty Cards."))
        # apply scheduling updates
        self.updateCards()
        # make sure to update sflds, etc
        part1 = ngettext("%d note added", "%d notes added", len(new)) % len(new)
        part2 = ngettext("%d note updated", "%d notes updated", self.updateCount) % self.updateCount
        self.log.append("%s, %s." % (part1, part2))
        if self._emptyNotes:
            self.log.append(_("""\
One or more notes were not imported, because they didn't generate any cards. \
This can happen when you have empty fields or when you have not mapped the \
content in the text file to the correct fields."""))
        self.total = len(self._ids)
Example #6
0
    def importNotes(self, notes):
        "Convert each card into a note, apply attributes and add to col."
        assert self.mappingOk()
        # note whether tags are mapped
        self._tagsMapped = False
        for f in self.mapping:
            if f == "_tags":
                self._tagsMapped = True
        # gather checks for duplicate comparison
        csums = {}
        for csum, id in self.col.db.execute(
                "select csum, id from notes where mid = ?", self.model['id']):
            if csum in csums:
                csums[csum].append(id)
            else:
                csums[csum] = [id]
        firsts = {}
        fld0idx = self.mapping.index(self.model['flds'][0]['name'])
        self._fmap = self.col.models.fieldMap(self.model)
        self._nextID = timestampID(self.col.db, "notes")
        # loop through the notes
        updates = []
        updateLog = []
        updateLogTxt = _("First field matched: %s")
        dupeLogTxt = _("Added duplicate with first field: %s")
        new = []
        self._ids = []
        self._cards = []
        self._emptyNotes = False
        dupeCount = 0
        dupes = []
        for n in notes:
            for c in range(len(n.fields)):
                if not self.allowHTML:
                    n.fields[c] = cgi.escape(n.fields[c])
                n.fields[c] = n.fields[c].strip().replace("\n", "<br>")
            fld0 = n.fields[fld0idx]
            csum = fieldChecksum(fld0)
            # first field must exist
            if not fld0:
                self.log.append(_("Empty first field: %s") %
                                " ".join(n.fields))
                continue
            # earlier in import?
            if fld0 in firsts and self.importMode != 2:
                # duplicates in source file; log and ignore
                self.log.append(_("Appeared twice in file: %s") %
                                fld0)
                continue
            firsts[fld0] = True
            # already exists?
            found = False
            if csum in csums:
                # csum is not a guarantee; have to check
                for id in csums[csum]:
                    flds = self.col.db.scalar(
                        "select flds from notes where id = ?", id)
                    sflds = splitFields(flds)
                    if fld0 == sflds[0]:
                        # duplicate
                        found = True
                        if self.importMode == 0:
                            data = self.updateData(n, id, sflds)
                            if data:
                                updates.append(data)
                                updateLog.append(updateLogTxt % fld0)
                                dupeCount += 1
                                found = True
                        elif self.importMode == 1:
                            dupeCount += 1
                        elif self.importMode == 2:
                            # allow duplicates in this case
                            if fld0 not in dupes:
                                # only show message once, no matter how many
                                # duplicates are in the collection already
                                updateLog.append(dupeLogTxt % fld0)
                                dupes.append(fld0)
                            found = False
            # newly add
            if not found:
                data = self.newData(n)
                if data:
                    new.append(data)
                    # note that we've seen this note once already
                    firsts[fld0] = True
        self.addNew(new)
        self.addUpdates(updates)
        # make sure to update sflds, etc
        self.col.updateFieldCache(self._ids)
        # generate cards
        if self.col.genCards(self._ids):
            self.log.insert(0, _(
                "Empty cards found. Please run Tools>Empty Cards."))
        # apply scheduling updates
        self.updateCards()
        # we randomize or order here, to ensure that siblings
        # have the same due#
        did = self.col.decks.selected()
        conf = self.col.decks.confForDid(did)
        # in order due?
        if conf['new']['order'] == NEW_CARDS_RANDOM:
            self.col.sched.randomizeCards(did)
        else:
            self.col.sched.orderCards(did)

        part1 = ngettext("%d note added", "%d notes added",
                         len(new)) % len(new)
        part2 = ngettext("%d note updated", "%d notes updated",
                         self.updateCount) % self.updateCount
        if self.importMode == 0:
            unchanged = dupeCount - self.updateCount
        elif self.importMode == 1:
            unchanged = dupeCount
        else:
            unchanged = 0
        part3 = ngettext("%d note unchanged", "%d notes unchanged",
                         unchanged) % unchanged
        self.log.append("%s, %s, %s." % (part1, part2, part3))
        self.log.extend(updateLog)
        if self._emptyNotes:
            self.log.append(_("""\
One or more notes were not imported, because they didn't generate any cards. \
This can happen when you have empty fields or when you have not mapped the \
content in the text file to the correct fields."""))
        self.total = len(self._ids)
Example #7
0
 def importNotes(self, notes):
     "Convert each card into a note, apply attributes and add to col."
     assert self.mappingOk()
     # gather checks for duplicate comparison
     csums = {}
     for csum, id in self.col.db.execute(
         "select csum, id from notes where mid = ?", self.model['id']):
         if csum in csums:
             csums[csum].append(id)
         else:
             csums[csum] = [id]
     firsts = {}
     fld0idx = self.mapping.index(self.model['flds'][0]['name'])
     self._fmap = self.col.models.fieldMap(self.model)
     self._nextID = timestampID(self.col.db, "notes")
     # loop through the notes
     updates = []
     new = []
     self._ids = []
     self._cards = []
     for n in notes:
         fld0 = n.fields[fld0idx]
         csum = fieldChecksum(fld0)
         # first field must exist
         if not fld0:
             self.log.append(_("Empty first field: %s") %
                             " ".join(n.fields))
             continue
         # earlier in import?
         if fld0 in firsts:
             # duplicates in source file; log and ignore
             self.log.append(_("Appeared twice in file: %s") %
                             fld0)
             continue
         firsts[fld0] = True
         # already exists?
         found = False
         if csum in csums:
             # csum is not a guarantee; have to check
             for id in csums[csum]:
                 flds = self.col.db.scalar(
                     "select flds from notes where id = ?", id)
                 sflds = splitFields(flds)
                 if fld0 == sflds[0]:
                     # duplicate
                     found = True
                     if self.update:
                         data = self.updateData(n, id, sflds)
                         if data:
                             updates.append(data)
                             found = True
                         break
         # newly add
         if not found:
             data = self.newData(n)
             if data:
                 new.append(data)
                 # note that we've seen this note once already
                 firsts[fld0] = True
     self.addNew(new)
     self.addUpdates(updates)
     self.col.updateFieldCache(self._ids)
     # generate cards
     if self.col.genCards(self._ids):
         self.log.insert(0, _(
             "Empty cards found. Please run Tools>Maintenance>Empty Cards."))
     # apply scheduling updates
     self.updateCards()
     # make sure to update sflds, etc
     self.log.append(_("%(a)d notes added, %(b)d notes updated.") %
                     dict(a=len(new), b=self.updateCount))
     self.total = len(self._ids)
    def importNotes(self, notes):
        "Convert each card into a note, apply attributes and add to col."
        print "importNotes()"
        print notes

        assert self.mappingOk()
        # note whether tags are mapped
        self._tagsMapped = False
        for f in self.mapping:
            if f == "_tags":
                self._tagsMapped = True
        # gather checks for duplicate comparison
        csums = {}
        for csum, id in self.col.db.execute(
            "select csum, id from notes where mid = ?", self.model['id']):
            if csum in csums:
                csums[csum].append(id)
            else:
                csums[csum] = [id]
        firsts = {}
        fld0idx = self.mapping.index(self.model['flds'][0]['name'])
        self._fmap = self.col.models.fieldMap(self.model)
        self._nextID = timestampID(self.col.db, "notes")
        # loop through the notes
        updates = []
        updateLog = []
        updateLogTxt = _("First field matched: %s")
        dupeLogTxt = _("Added duplicate with first field: %s")
        new = []
        self._ids = []
        self._cards = []
        self._emptyNotes = False
        for n in notes:
            if not self.allowHTML:
                for c in range(len(n.fields)):
                    n.fields[c] = cgi.escape(n.fields[c])
            fld0 = n.fields[fld0idx]
            csum = fieldChecksum(fld0)
            # first field must exist
            if not fld0:
                self.log.append(_("Empty first field: %s") %
                                " ".join(n.fields))
                continue
            # earlier in import?
            if fld0 in firsts and self.importMode != 2:
                # duplicates in source file; log and ignore
                self.log.append(_("Appeared twice in file: %s") %
                                fld0)
                continue
            firsts[fld0] = True
            # already exists?
            found = False
            if csum in csums:
                # csum is not a guarantee; have to check
                for id in csums[csum]:
                    flds = self.col.db.scalar(
                        "select flds from notes where id = ?", id)
                    sflds = splitFields(flds)
                    if fld0 == sflds[0]:
                        # duplicate
                        found = True
                        if self.importMode == 0:
                            data = self.updateData(n, id, sflds)
                            if data:
                                updates.append(data)
                                updateLog.append(updateLogTxt % fld0)
                                found = True
                            break
                        elif self.importMode == 2:
                            # allow duplicates in this case
                            updateLog.append(dupeLogTxt % fld0)
                            found = False
            # newly add
            if not found:
                data = self.newData(n)
                if data:
                    new.append(data)
                    # note that we've seen this note once already
                    firsts[fld0] = True

        did = self.col.decks.id(self.deck)
        self.col.decks.select(did)
        #SUPER IMPORTANT (setting the associated deck to the model)
        self.model['did'] = did

        print "Selected: ", self.col.decks.get(self.col.decks.selected());

        self.addNew(new)
        self.addUpdates(updates)
        # make sure to update sflds, etc
        self.col.updateFieldCache(self._ids)
        # generate cards
        if self.col.genCards(self._ids):
            self.log.insert(0, _(
                "Empty cards found. Please run Tools>Empty Cards."))
        # apply scheduling updates
        self.updateCards()
        # we randomize or order here, to ensure that siblings
        # have the same due#
        

        # m = self.col.models.byName("Basic")
        # deck = self.col.decks.get(did)
        # deck['mid'] = m['id']
        # self.col.decks.save(deck)

        # print "Deck:", self.col.decks.byName(self.deck)
        # print "DID:", did
        
        # save tags to model
        # m = self.note.model()
        # m['tags'] = self.note.tags
        # self.mw.col.models.save(m)

        conf = self.col.decks.confForDid(did)
        # print "Conf: ",conf
        # in order due?
        if conf['new']['order'] == NEW_CARDS_RANDOM:
            self.col.sched.randomizeCards(did)
        else:
            self.col.sched.orderCards(did)
        part1 = ngettext("%d note added", "%d notes added", len(new)) % len(new)
        part2 = ngettext("%d note updated", "%d notes updated", self.updateCount) % self.updateCount
        self.log.append("%s, %s." % (part1, part2))
        print part1, part2, "on deck: [", self.deck, "]"
        self.log.extend(updateLog)
        if self._emptyNotes:
            print "there were empty notes"
            self.log.append(_("""\
One or more notes were not imported, because they didn't generate any cards. \
This can happen when you have empty fields or when you have not mapped the \
content in the text file to the correct fields."""))
        self.total = len(self._ids)