Contents
Trac SQLAlchemy Bridge
Description
This is a bridge for a plugin developer to use SQLAlchemy with Trac. SQLAlchemy is a Python SQL toolkit and Object Relational Mapper. SQLAlchemy itself is released under a MIT license.
See also: SqlAlchemyQueryMacro
Bugs/Feature Requests
Existing bugs and feature requests for TracSqlAlchemyBridgeIntegration are here.
If you have any issues, create a new ticket.
task |
0 / 1 |
---|
Download
Download the zipped source from here.
Download at PyPI from here.
Source
You can check out TracSqlAlchemyBridgeIntegration from here using Subversion, or browse the source with Trac.
Installation
General instructions on installing Trac plugins can be found on the TracPlugins page.
Example
You define your tables as SQLAlchemy tells you to on its docs.
Making Trac upgrade its tables to include yours (from the plugin I was developing from which this package was born):
from trac.core import * from trac.env import IEnvironmentSetupParticipant from tsab import engine from tl10nm import model class TracL10nManagerSetup(Component): implements(IEnvironmentSetupParticipant) # IEnvironmentSetupParticipant Methods def environment_created(self): self.found_db_version = 0 self.upgrade_environment(self.env.get_db_cnx()) def environment_needs_upgrade(self, db): cursor = db.cursor() cursor.execute("SELECT value FROM system WHERE name=%s";, (model.name,)) value = cursor.fetchone() if not value: self.found_db_version = 0 return True else: self.found_db_version = int(value[0]) self.log.debug("%s: Found db version %s, current is %s", __package__, self.found_db_version, model.version) return self.found_db_version < model.version def upgrade_environment(self, db): # Currently we only create the tables, so far there's no migration done model.metadata.create_all(bind=engine(self.env)) cursor = db.cursor() if not self.found_db_version: cursor.execute("INSERT INTO system (name, value) VALUES (%s, %s)", (model.name, model.version)) else: cursor.execute("UPDATE system SET value=%s WHERE name=%s", (model.version, model.name))
Then to use it in your code (from the plugin I was developing from which this package was born):
#--------8<------- code cut for readability --------8<------- from tsab import session #--------8<------- code cut for readability --------8<------- class L10nModule(Component): #--------8<------- code cut for readability --------8<------- def _list_messages(self, req, catalog_id, locale_name, page): Session = session(self.env) locale = Session.query(Locale).filter_by(locale=locale_name, catalog_id=catalog_id).first() data = {'locale': locale, 'catalog_id': catalog_id} paginator = Paginator(list(locale.catalog.messages), page-1, 5) data['messages'] = paginator shown_pages = paginator.get_shown_pages(25) pagedata = [] for show_page in shown_pages: page_href = req.href.translations(catalog_id, locale_name, show_page) pagedata.append([page_href, None, str(show_page), 'page %s' % show_page]) fields = ['href', 'class', 'string', 'title'] paginator.shown_pages = [dict(zip(fields, p)) for p in pagedata] paginator.current_page = {'href': None, 'class': 'current', 'string': str(paginator.page + 1), 'title': None} if paginator.has_next_page: add_link(req, 'next', req.href.translations(locale_id, page+1), _('Next Page')) if paginator.has_previous_page: add_link(req, 'prev', req.href.translations(locale_id, page-1), _('Previous Page')) return 'l10n_messages.html', data, None #--------8<------- code cut for readability --------8<-------
Recent Changes
- 5089 by s0undt3ch on 2009-01-05 08:06:52
-
Bump version number and more old<->new SQLAlchemy compatibility changes.
- 5088 by s0undt3ch on 2009-01-05 06:55:18
-
Support both old and new versions of SQLAlchemy.
- 4086 by s0undt3ch on 2008-07-30 12:20:32
-
Fix bug where the
_engines
dict would only get entries if trac was in debug mode.
Include correct logging setup for future feature of trac. See this ticket on t.e.o.
(more)
Author/Contributors
Author: mitsuhiko
Maintainer: Pedro Algarvio, aka, s0undt3ch
Contributors: