Trac SQLAlchemy Bridge
Description
Bridge for a plugin developer to use SQLAlchemy with trac
Bugs/Feature Requests
Existing bugs and feature requests for TracSqlAlchemyBridgeIntegration are here.
If you have any issues, create a new ticket.
Download
Download the zipped source from here.
Source
You can check out TracSqlAlchemyBridgeIntegration from here using Subversion, or browse the source with Trac.
Example
You define your tables as SQLAlchemy tells you to on it's docs.
Making trac upgrade it's 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
[4086] by s0undt3ch on 07/30/08 07: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.
[4008] by s0undt3ch on 07/12/08 18:10:39
First working version of package
[4007] by s0undt3ch on 07/12/08 17:16:07
New hack TracSqlAlchemyBridgeIntegration, created by s0undt3ch