Please register to create new tickets

It is possible to embed rum applications into TurboGears. Some helpers are provided in the TgRum package.

A sample app can be cloned from its mercurial repository or you can also download an archived version. Note that you must first install the helpers from TgRum.

Here a few snippets (package name of application is owpdb) owpdb/lib/rumapp.py

from rum import RumApp
from owpdb.model import DBSession
import owpdb.model as model

from pylons import request, config
from tg.configuration import asbool
from rumalchemy.util import get_mapper
from tgrum import Flash

rum_app = RumApp({
    # be sure to turn this off on production!
    'debug': asbool(config['debug']),
    # because Rum will now share some middleware with tg
    'full_stack': False,
    'rum.repositoryfactory': {
        'use': 'sqlalchemy',
        # pick up all models from the model package. We can also pass an
        # explicit list as the 'models' parameter
        'scan_modules': ['owpdb.model.objects'],
        # We use TG's configured DBSession
        'session_factory': DBSession,
        
    },
    'flash_class': Flash,
    'rum.viewfactory': {
        'use': 'toscawidgets',
    },
    'rum.translator': {
        'use': 'default',
        'locales': ['de'],
    },
    
})

Alternatively, one can use the helpers provided in TgRum.

The following just illustrates, the possibility to make adjustments:

owpdb/model/rumified.py

from rum import fields, app
from objects import *
from tw.rum.viewfactory import WidgetFactory
from tw.rum.widgets import CollectionLinks
from tw.rum.autocompletion import AutoCompletion
from rum.validators import RelatedFetcher

person_relations_field=fields.List(name="person_relations",
    other=PhotoPersonRelation,
    remote_name="photo",
    required=False,
    label="Persons on Photo")

occasion_search_field=AutoCompletion(name="occasion", completion_url="/rum/occasions/complete.json")
person_search_field=AutoCompletion("person",completion_url="/admin/complete_persons", validator=RelatedFetcher(Person))
author_search_field=AutoCompletion("author",completion_url="/admin/complete_persons?nullable=1", validator=RelatedFetcher(Person))
fields.FieldFactory.fields(Photo, [
    'author',
    'occasion',
    'place',
    'year',
    'type',
    'published',
    'copyright',
    'source',
    fields.HTMLText(name="label",label="Annotation"),
    fields.Unicode(name="remark", label="Internal Remark"),
    fields.JPEGImage(name="photo_small"),
    fields.JPEGImage(name="photo_normal"),
    fields.JPEGImage(name="photo_original"),
    person_relations_field,
    'persons',
    'persons_with_position',
    
])




@WidgetFactory.get.when("isinstance(attr, fields.Relation) and action in self.input_actions and attr.other in [Occasion, Person]", prio=0)
def __autocompletion_widget_when(self, resource, parent, remote_name, attr, action,
    args):
    return AutoCompletion

fields.FieldFactory.fields(Person, [
    'lastname',
    'firstname',
    fields.Unicode(name='remark',required=False, default=u""),
    'mfo_informix_id',
    'photos',
    'photos_recorded_by'
])







fields.FieldFactory.fields(PersonOnPhoto, [
    'lastname',
    'firstname',
    fields.Unicode(name='remark_for_photo', required=False, default=u""),
    fields.Unicode(name='remark',required=False, default=u""),
    'mfo_informix_id',
    fields.Relation(name="photo", other=Photo, remote_name="persons_with_position"),
    'photo_id'
])


fields.FieldFactory.fields(PhotoPersonRelation,
    [
    'person',
    'photo',
    fields.Unicode(name='remark', required=False, default=u'')
    ])

fields.FieldFactory.fields(HTMLPiece,
    [
    'title',
    fields.HTMLText('text'),
    'edited',
    'mapping',
    ])

for (res, attr) in [
    (Photo,"persons"),
    (Occasion,"photos"),
    (Person,"photos"), 
    (Person, "photos_recorded_by"),
    (Photo, 'persons_with_position'), 
    (Photo,"person_relations")]:
    for action in WidgetFactory.input_actions:
        WidgetFactory.view(res,None,attr=attr,action=action)

for (res, attr) in [(PersonOnPhoto,'photo')]:
    for action in WidgetFactory.edit_actions:
        WidgetFactory.view(res,None,attr=attr,action=action)

for action in WidgetFactory.input_actions:
    WidgetFactory.view(PhotoPersonRelation, person_search_field, attr='person', action=action)
    WidgetFactory.view(Photo,author_search_field,attr='author',action=action)





for action in ['inline_index', 'inline_show', 'inline_preview']:#, 'index','show', 'preview']:

    WidgetFactory.view(Photo, 
        CollectionLinks(field=person_relations_field, show_items=True),
        #CollectionLinks(field=persons_with_position),
        #person_search_field,
        attr="person_relations", action=action, prio=37)
    WidgetFactory.view(Photo,None,attr='persons',action=action)

Generate a link into rum in the helpers functions.

owpdb/lib/helpers

from __future__ import with_statement
from webhelpers import date, feedgenerator, html, number, misc, text 
from tg import url


rum_mount_point="/rum"


def photo_person_edit_url(photo_id, person_id):
    # rum app is imported here to make sure the model classes have been
    # mapped
    from owpdb.lib.rumapp import rum_app
    from owpdb.lib.rumapp import get_id
    from owpdb.model import PersonOnPhoto
    obj=PersonOnPhoto.query.filter_by(photo_id=photo_id, person_id=person_id).one()
    
    with rum_app.mounted_at(rum_mount_point):
        return rum_app.url_for(obj=obj,action="edit")

It is crucial to make sure that the model classes are mapped by SQLAlchemy (in yourapp.model.init_model() or by using a declsarative base) before initializing RumApp? or else RumAlchemy won't be able to find any models. You don't need to worry about this if using the helpers in TgRum.