From d6116a53778299235300194084b33b6a664c0b4f Mon Sep 17 00:00:00 2001 From: Damien Elmes Date: Sun, 10 Apr 2011 21:35:12 +0900 Subject: [PATCH] model and group searching --- anki/find.py | 26 ++++++++++++++++++++++++++ tests/test_find.py | 8 ++++++++ 2 files changed, 34 insertions(+) diff --git a/anki/find.py b/anki/find.py index 19af8c3c1..661e16af0 100644 --- a/anki/find.py +++ b/anki/find.py @@ -11,6 +11,8 @@ SEARCH_PHRASE = 2 SEARCH_FID = 3 SEARCH_TEMPLATE = 4 SEARCH_FIELD = 5 +SEARCH_MODEL = 6 +SEARCH_GROUP = 7 # Find ########################################################################## @@ -94,6 +96,10 @@ order by %s""" % (lim, sort) self._findTemplate(token, isNeg) elif type == SEARCH_FIELD: self._findField(token, isNeg) + elif type == SEARCH_MODEL: + self._findModel(token, isNeg, c) + elif type == SEARCH_GROUP: + self._findGroup(token, isNeg, c) else: self._findText(token, isNeg, c) @@ -136,6 +142,20 @@ order by %s""" % (lim, sort) def _findFids(self, val): self.lims['fact'].append("id in (%s)" % val) + def _findModel(self, val, isNeg, c): + extra = "not" if isNeg else "" + self.lims['fact'].append( + "mid %s in (select id from models where name like :_mod_%d)" % ( + extra, c)) + self.lims['args']['_mod_%d'%c] = val + + def _findGroup(self, val, isNeg, c): + extra = "not" if isNeg else "" + self.lims['card'].append( + "gid %s in (select id from groups where name like :_grp_%d)" % ( + extra, c)) + self.lims['args']['_grp_%d'%c] = val + def _findTemplate(self, val, isNeg): lims = [] comp = "!=" if isNeg else "=" @@ -266,6 +286,12 @@ where mid in %s and flds like ? escape '\\'""" % ( elif token['value'].startswith("is:"): token['value'] = token['value'][3:].lower() type = SEARCH_TYPE + elif token['value'].startswith("model:"): + token['value'] = token['value'][6:].lower() + type = SEARCH_MODEL + elif token['value'].startswith("group:"): + token['value'] = token['value'][6:].lower() + type = SEARCH_GROUP elif token['value'].startswith("fid:") and len(token['value']) > 4: dec = token['value'][4:] try: diff --git a/tests/test_find.py b/tests/test_find.py index d25f01c5e..9545573ac 100644 --- a/tests/test_find.py +++ b/tests/test_find.py @@ -84,3 +84,11 @@ def test_findCards(): assert deck.findCards("", sort="factFld")[0] == catCard.id assert deck.findCards("", sort="factFld")[-1] == c.id assert deck.findCards("", sort="cardMod")[-1] == c.id + # model + assert len(deck.findCards("model:basic")) == 5 + assert len(deck.findCards("-model:basic")) == 0 + assert len(deck.findCards("-model:foo")) == 5 + # group + assert len(deck.findCards("group:default")) == 5 + assert len(deck.findCards("-group:default")) == 0 + assert len(deck.findCards("-group:foo")) == 5