(task)
Move cards between decks
Move cards between decks
Move cards between decks
def moveCardsAction(task):
    """Move cards between decks"""
    orig = task.get("orig", None)
    dest = task.get("dest", None)
    count = task.get("count", None)
    order = task.get("order", "due")
    invert = task.get("invert", False)

    try:
        assert orig and dest and count
        assert order in VALID_ORDERS
    except AssertionError:
        return "invalid"

    orig_did = mw.col.decks.id(orig, create=False)
    if not orig_did:
        return "invalid"

    dest_did = mw.col.decks.id(dest, create=False)

    if dest_did and mw.col.decks.isDyn(dest_did):
        # cards can't be moved into filtered deck
        return "dynamic"


    cids = mw.col.decks.cids(orig_did)
    if not cids:
        return "nocids"
    
    if order in ("random", "added"):
        cmd = "select id from cards where did = ? and type=0 order by id"
    elif order == "due":
        cmd = "select id from cards where did = ? and type=0 order by due"

    scids = mw.col.db.list(cmd, orig_did)

    if order == "random":
        scids = random.sample(scids, count)
    elif invert:
        scids = scids[-count:]
    else:
        scids = scids[:count]

    if not scids:
        return "nocids"

    if not dest_did:
        # create destination deck now
        dest_did = mw.col.decks.id(dest)

    # remove any cards from filtered deck first
    mw.col.sched.remFromDyn(scids)
    # then move into new deck
    mw.col.decks.setDeck(scids, dest_did)
def moveCardsAction ( task ) : orig = task . get ( "orig" , None ) dest = task . get ( "dest" , None ) count = task . get ( "count" , None ) order = task . get ( "order" , "due" ) invert = task . get ( "invert" , False ) try : assert orig and dest and count assert order in VALID_ORDERS except AssertionError : return "invalid" orig_did = mw . col . decks . id ( orig , create = False ) if not orig_did : return "invalid" dest_did = mw . col . decks . id ( dest , create = False ) if dest_did and mw . col . decks . isDyn ( dest_did ) : # cards can't be moved into filtered deck return "dynamic" cids = mw . col . decks . cids ( orig_did ) if not cids : return "nocids" if order in ( "random" , "added" ) : cmd = "select id from cards where did = ? and type=0 order by id" elif order == "due" : cmd = "select id from cards where did = ? and type=0 order by due" scids = mw . col . db . list ( cmd , orig_did ) if order == "random" : scids = random . sample ( scids , count ) elif invert : scids = scids [ - count : ] else : scids = scids [ : count ] if not scids : return "nocids" if not dest_did : # create destination deck now dest_did = mw . col . decks . id ( dest ) # remove any cards from filtered deck first mw . col . sched . remFromDyn ( scids ) # then move into new deck mw . col . decks . setDeck ( scids , dest_did )