涡轮齿轮2应用的标记支持
tgext.tagging的Python项目详细描述
关于标记
tagging是一个TurboGears2库,它允许快速添加标记 任何管理标记、标记、标记云和小部件的项目 列出、删除和向实体添加标记。
安装
tgext.tagging既可以从pypi安装,也可以从bitbucket安装:
pip install tgext.tagging
应该只对大多数用户有效
启用标记管理
对于涡轮齿轮2.2+,建议使用tgext.pluggables:
from tgext.pluggable import plug plug(base_config, 'tgext.tagging')
对于以前的版本,请编辑model/__init__.py,并添加以下行:
import tgext.tagging Tag, Tagging = tgext.tagging.setup_model()
管理标记的大多数实用程序都由标记类公开 他们曝光:
- Tag.lookup(tag_name) -> Returns the Tag instance for the given tag name
- Tag.lookup_list(comma_separated_tags) -> Returns the tags instances for each entry in the tags list.
- Tagging.items_for_tags(Model, comma_separated_tags) -> Returns the list of items having the given tags
- Tagging.tag_cloud_for_object(item) -> Returns the list of tags for the given object
- Tagging.tag_cloud_for_set(Model, items=None) -> Returns the tag cloud for the given set of items. if a list of items is passed it will retrieve tags for the given list, otherwise for all the items of the given Model.
- Tagging.tag_cloud_for_user(user, Model=None) -> Returns all the tags set by the given user. if any Model is passed it will retrieve tags only for that model.
- Tagging.add_tags(item, comma_separated_tags) -> Add the given tags to the item
- Tagging.del_tags(item, comma_separated_tags) -> Removes the given tags from the item
- Tagging.set_tags(item, comma_separated_tags) -> Replaces all the tags of the item with the new list
标记控制器
tagging提供一个控制器来管理标记。 使用以下代码在项目中启用它:
from tgext.tagging.controllers import TaggingController class RootController(BaseController): tagging = TaggingController(model=Group, session=DBSession, allow_edit=None)
您可以为每个可用的模型启用多个taggingcontroller 在你的申请中。model参数指示将为哪个模型对象标记 托管的,session是用于执行查询和allow\u edit的sqlalchemy会话。 是repoze.what谓词,用于检查是否显示编辑函数。
控制器提供tags,add,remove,search操作:
- /tags/id -> Partial view that can be loaded with jQuery.load that displays a taglist with form to add/remove tags for the given object.
- /add/id?tags=tag1,tag2,tag3 -> Permits to add one or more tags to the item
- /remove/id?tags=tag1,tag2 -> Permits to remove one or more tags from the item
- /search?tags=tag1,tag -> Searches for items having the given tags, will use Model.tagging_display method to display the results if available. Otherwise str(Model) is performed.
标记小部件
tagging提供了一些小部件来管理标记。 ^提供了{str 1}$tgext.tagging.widgets.taglist和tgext.tagging.widget.tagcloud。 两者在构造时都采用一个tagging_url参数,该参数指向标记的url 控件,该控件可用于管理标记。默认情况下,它指向/标记。 taglist小部件还接受一个editmode参数,该参数允许指定控件是否 添加和删除标记必须显示或不显示。
在呈现tag list时,接受一个对象作为参数,并将显示给定对象的标记列表, 而tag cloud接受tagg.tag戋ucloud for戋object、tagg.tag戋ucloud for戋set或 tagging.tag_cloud_用于_用户,并将显示加权标记云。
与sqlite一起使用
标记在很大程度上依赖于SAVEPOINT来正确处理 标记唯一性。默认的sqlite行为不兼容 使用保存点并需要一些其他更改 使用mysql或postgres时必需。
通常要使sqlite和zope.sqlalchemy正确地执行以下代码 在使用您的型号之前需要:
from sqlalchemy import event @event.listens_for(engine, "connect") def do_connect(dbapi_connection, connection_record): # Disable SQLITE automatic transactions dbapi_connection.isolation_level = None @event.listens_for(engine, "begin") def do_begin(conn): # Manually emit SQLITE transaction begin conn.execute("BEGIN") # Tell zope.sqlalchemy that SQLITE now supports SAVEPOINT from zope.sqlalchemy import datamanager datamanager.NO_SAVEPOINT_SUPPORT = set()
忘记正确放置此代码通常会导致如下错误:
(OperationalError) no such savepoint: sa_savepoint_3 u'ROLLBACK TO SAVEPOINT sa_savepoint_3' ()
或:
TypeError: ('Savepoints unsupported', <zope.sqlalchemy.datamanager.SessionDataManager object at 0x7fe9feae45d0>)
见SQLite Serializable 更多细节。