| 1 | = Trac SQLAlchemy Bridge = |
| 2 | |
| 3 | == Description == |
| 4 | |
| 5 | Bridge for a plugin developer to use SQLAlchemy with trac |
| 6 | |
| 7 | == Bugs/Feature Requests == |
| 8 | |
| 9 | Existing bugs and feature requests for TracSqlAlchemyBridgeIntegration are |
| 10 | [report:9?COMPONENT=TracSqlAlchemyBridgeIntegration here]. |
| 11 | |
| 12 | If you have any issues, create a |
| 13 | [http://trac-hacks.org/newticket?component=TracSqlAlchemyBridgeIntegration&owner=s0undt3ch new ticket]. |
| 14 | |
| 15 | == Download == |
| 16 | |
| 17 | Download the zipped source from [download:tracsqlalchemybridgeintegration here]. |
| 18 | |
| 19 | == Source == |
| 20 | |
| 21 | You can check out TracSqlAlchemyBridgeIntegration from [http://trac-hacks.org/svn/tracsqlalchemybridgeintegration here] using Subversion, or [source:tracsqlalchemybridgeintegration browse the source] with Trac. |
| 22 | |
| 23 | == Example == |
| 24 | |
| 25 | You define your tables as SQLAlchemy tells you to on it's docs. |
| 26 | |
| 27 | Making trac upgrade it's tables to include yours(from the plugin I was developing from which this package was born): |
| 28 | {{{ |
| 29 | #!python |
| 30 | from trac.core import * |
| 31 | from trac.env import IEnvironmentSetupParticipant |
| 32 | from tsab import engine |
| 33 | |
| 34 | from tl10nm import model |
| 35 | |
| 36 | class TracL10nManagerSetup(Component): |
| 37 | implements(IEnvironmentSetupParticipant) |
| 38 | |
| 39 | # IEnvironmentSetupParticipant Methods |
| 40 | def environment_created(self): |
| 41 | self.found_db_version = 0 |
| 42 | self.upgrade_environment(self.env.get_db_cnx()) |
| 43 | |
| 44 | def environment_needs_upgrade(self, db): |
| 45 | cursor = db.cursor() |
| 46 | cursor.execute("SELECT value FROM system WHERE name=%s", |
| 47 | (model.name,)) |
| 48 | value = cursor.fetchone() |
| 49 | if not value: |
| 50 | self.found_db_version = 0 |
| 51 | return True |
| 52 | else: |
| 53 | self.found_db_version = int(value[0]) |
| 54 | self.log.debug("%s: Found db version %s, current is %s", |
| 55 | __package__, self.found_db_version, model.version) |
| 56 | return self.found_db_version < model.version |
| 57 | |
| 58 | def upgrade_environment(self, db): |
| 59 | # Currently we only create the tables, so far there's no migration done |
| 60 | model.metadata.create_all(bind=engine(self.env)) |
| 61 | cursor = db.cursor() |
| 62 | if not self.found_db_version: |
| 63 | cursor.execute("INSERT INTO system (name, value) VALUES (%s, %s)", |
| 64 | (model.name, model.version)) |
| 65 | else: |
| 66 | cursor.execute("UPDATE system SET value=%s WHERE name=%s", |
| 67 | (model.version, model.name)) |
| 68 | }}} |
| 69 | |
| 70 | Then to use it in your code((rom the plugin I was developing from which this package was born): |
| 71 | {{{ |
| 72 | #!python |
| 73 | |
| 74 | #--------8<------- code cut for readability --------8<------- |
| 75 | from tsab import session |
| 76 | #--------8<------- code cut for readability --------8<------- |
| 77 | class L10nModule(Component): |
| 78 | #--------8<------- code cut for readability --------8<------- |
| 79 | def _list_messages(self, req, catalog_id, locale_name, page): |
| 80 | Session = session(self.env) |
| 81 | locale = Session.query(Locale).filter_by(locale=locale_name, |
| 82 | catalog_id=catalog_id).first() |
| 83 | |
| 84 | data = {'locale': locale, 'catalog_id': catalog_id} |
| 85 | |
| 86 | paginator = Paginator(list(locale.catalog.messages), page-1, 5) |
| 87 | data['messages'] = paginator |
| 88 | shown_pages = paginator.get_shown_pages(25) |
| 89 | pagedata = [] |
| 90 | for show_page in shown_pages: |
| 91 | page_href = req.href.translations(catalog_id, locale_name, |
| 92 | show_page) |
| 93 | pagedata.append([page_href, None, str(show_page), |
| 94 | 'page %s' % show_page]) |
| 95 | fields = ['href', 'class', 'string', 'title'] |
| 96 | paginator.shown_pages = [dict(zip(fields, p)) for p in pagedata] |
| 97 | paginator.current_page = {'href': None, 'class': 'current', |
| 98 | 'string': str(paginator.page + 1), |
| 99 | 'title':None} |
| 100 | if paginator.has_next_page: |
| 101 | add_link(req, 'next', req.href.translations(locale_id, page+1), |
| 102 | _('Next Page')) |
| 103 | if paginator.has_previous_page: |
| 104 | add_link(req, 'prev', req.href.translations(locale_id, page-1), |
| 105 | _('Previous Page')) |
| 106 | return 'l10n_messages.html', data, None |
| 107 | #--------8<------- code cut for readability --------8<------- |
| 108 | }}} |
| 109 | |
| 110 | |
| 111 | |
| 112 | == Recent Changes == |
| 113 | |
| 114 | [[ChangeLog(tracsqlalchemybridgeintegration, 3)]] |
| 115 | |
| 116 | == Author/Contributors == |
| 117 | |
| 118 | '''Author:''' [wiki:s0undt3ch] [[BR]] |
| 119 | '''Contributors:''' |