def test_get_content_items_for_tag_title(self, root): from kotti.resources import Tag, TagsToContents, Content from kotti.views.util import content_with_tags root["folder_1"] = Content() root["folder_1"].tags = ["first tag", "second tag"] root["folder_1"]["content_1"] = Content() root["folder_1"]["content_1"].tags = ["third tag"] root["folder_1"]["content_2"] = Content() root["folder_1"]["content_2"].tags = ["first tag", "third tag"] result = ( Content.query.join(TagsToContents) .join(Tag) .filter(Tag.title == "first tag") .all() ) assert [res.name for res in result] == ["folder_1", "content_2"] result = ( Content.query.join(TagsToContents) .join(Tag) .filter(Tag.title == "second tag") .all() ) assert [res.name for res in result] == ["folder_1"] result = ( Content.query.join(TagsToContents) .join(Tag) .filter(Tag.title == "third tag") .all() ) assert sorted(res.name for res in result) == sorted(["content_1", "content_2"]) # The same tests again, using content_with_tags(): # # About expected sort order: # # In the first set of tests below, where we search by single # tags, the query in the content_with_tags() function returns # results in hierarchical order, from root. # # content_with_tags() is written to take a list of tags, but in the # default Kotti, presently, after some consideration about specialized # add-ons for searching, we do not support multiple tags searching, in # part to avoid establishing a specification. # result = content_with_tags(["first tag"]) assert sorted([res.name for res in result]) == sorted(["folder_1", "content_2"]) result = content_with_tags(["second tag"]) assert sorted([res.name for res in result]) == sorted(["folder_1"]) result = content_with_tags(["third tag"]) assert sorted([res.name for res in result]) == sorted( ["content_1", "content_2"] )
def search_content(search_term, request=None, permission='pview'): searchstring = u'%{0}%'.format(search_term) # generic_filter can be applied to all Node (and subclassed) objects generic_filter = or_(Content.name.like(searchstring), Content.title.like(searchstring), Content.description.like(searchstring)) results = DBSession.query(Content).filter(generic_filter).\ order_by(Content.title.asc()).all() # specific result contain objects matching additional criteria # but must not match the generic criteria (because these objects # are already in the generic_results) document_results = DBSession.query(Document).filter( and_(Document.body.like(searchstring), not_(generic_filter))) for results_set in [content_with_tags([searchstring]), document_results.all()]: [results.append(c) for c in results_set if c not in results] result_dicts = [] for result in results: if has_permission(permission, result, request): result_dicts.append(dict( name=result.name, title=result.title, description=result.description, path=request.resource_path(result))) return result_dicts
def test_get_content_items_for_tag_title(self, db_session, events): from kotti import DBSession from kotti.resources import get_root from kotti.resources import Tag, TagsToContents, Content from kotti.views.util import content_with_tags ses = DBSession root = get_root() root[u'folder_1'] = Content() root[u'folder_1'].tags = [u'first tag', u'second tag'] root[u'folder_1'][u'content_1'] = Content() root[u'folder_1'][u'content_1'].tags = [u'third tag'] root[u'folder_1'][u'content_2'] = Content() root[u'folder_1'][u'content_2'].tags = [u'first tag', u'third tag'] result = ses.query(Content).join(TagsToContents).join(Tag).filter( Tag.title == u'first tag').all() assert [res.name for res in result] == [u'folder_1', u'content_2'] result = ses.query(Content).join(TagsToContents).join(Tag).filter( Tag.title == u'second tag').all() assert [res.name for res in result] == [u'folder_1'] result = ses.query(Content).join(TagsToContents).join(Tag).filter( Tag.title == u'third tag').all() assert [res.name for res in result] == [u'content_1', u'content_2'] # The same tests again, using content_with_tags(): # # About expected sort order: # # In the first set of tests below, where we search by single # tags, the query in the content_with_tags() function returns # results in hierarchical order, from root. # # content_with_tags() is written to take a list of tags, but in the # default Kotti, presently, after some consideration about specialized # add-ons for searching, we do not support multiple tags searching, in # part to avoid establishing a specification. # result = content_with_tags([u'first tag']) assert [res.name for res in result] == [u'folder_1', u'content_2'] result = content_with_tags([u'second tag']) assert [res.name for res in result] == [u'folder_1'] result = content_with_tags([u'third tag']) assert [res.name for res in result] == [u'content_1', u'content_2']
def test_get_content_items_for_tag_title(self, root): from kotti.resources import Tag, TagsToContents, Content from kotti.views.util import content_with_tags root["folder_1"] = Content() root["folder_1"].tags = ["first tag", "second tag"] root["folder_1"]["content_1"] = Content() root["folder_1"]["content_1"].tags = ["third tag"] root["folder_1"]["content_2"] = Content() root["folder_1"]["content_2"].tags = ["first tag", "third tag"] result = (Content.query.join(TagsToContents).join(Tag).filter( Tag.title == "first tag").all()) assert [res.name for res in result] == ["folder_1", "content_2"] result = (Content.query.join(TagsToContents).join(Tag).filter( Tag.title == "second tag").all()) assert [res.name for res in result] == ["folder_1"] result = (Content.query.join(TagsToContents).join(Tag).filter( Tag.title == "third tag").all()) assert sorted(res.name for res in result) == sorted(["content_1", "content_2"]) # The same tests again, using content_with_tags(): # # About expected sort order: # # In the first set of tests below, where we search by single # tags, the query in the content_with_tags() function returns # results in hierarchical order, from root. # # content_with_tags() is written to take a list of tags, but in the # default Kotti, presently, after some consideration about specialized # add-ons for searching, we do not support multiple tags searching, in # part to avoid establishing a specification. # result = content_with_tags(["first tag"]) assert sorted([res.name for res in result]) == sorted(["folder_1", "content_2"]) result = content_with_tags(["second tag"]) assert sorted([res.name for res in result]) == sorted(["folder_1"]) result = content_with_tags(["third tag"]) assert sorted([res.name for res in result ]) == sorted(["content_1", "content_2"])
def search_content_for_tags(tags, request=None, permission='pview'): result_dicts = [] for result in content_with_tags(tags): if has_permission(permission, result, request): result_dicts.append(dict( name=result.name, title=result.title, description=result.description, path=request.resource_path(result))) return result_dicts