Commits

Ralph Bean committed 8cc9e34

Reading and writing.

Comments (0)

Files changed (5)

myapp/myapp/config/app_cfg.py

 """
 
 from tg.configuration import AppConfig
+from tw2.core.middleware import ControllersApp as TW2ControllersApp
 
 import myapp
 from myapp import model
 
 # Enable tw2
 base_config.use_toscawidgets2 = True
+
+# Setup tw2 controllers (nasty!)
+base_config.custom_tw2_config['controllers'] = TW2ControllersApp()
+base_config.custom_tw2_config['controller_prefix'] = '/tw2_controllers/'
+base_config.custom_tw2_config['serve_controllers'] = True
+

myapp/myapp/controllers/movie.py

 
 import tw2.core
 import tw2.forms
+import tw2.sqla
 
-class MovieForm(tw2.forms.FormPage):
+class MovieForm(tw2.sqla.DbFormPage):
+    entity = model.Movie
     title = 'Movie'
     resources = [tw2.core.CSSLink(link='/css/myapp.css')]
     class child(tw2.forms.TableForm):
+        action = '/tw2_controllers/movie_submit'
+        id = tw2.forms.HiddenField()
         title = tw2.forms.TextField(validator=tw2.core.Required)
         director = tw2.forms.TextField()
-        genres = tw2.forms.CheckBoxList(options=['Action', 'Comedy', 'Romance', 'Sci-fi'])
+        genres = tw2.sqla.DbCheckBoxList(entity=model.Genre)
         class cast(tw2.forms.GridLayout):
             extra_reps = 5
             character = tw2.forms.TextField()
     @expose('myapp.templates.widget')
     def movie(self, *args, **kw):
         w = MovieForm(redirect='/movie/').req()
+        w.fetch_data(request)
+        mw = tw2.core.core.request_local()['middleware']
+        mw.controllers.register(w, 'movie_submit')
         return dict(widget=w, page='movie')

myapp/myapp/model/__init__.py

 
 # There are two convenient ways for you to spare some typing.
 # You can have a query property on all your model classes by doing this:
-# DeclarativeBase.query = DBSession.query_property()
+DeclarativeBase.query = DBSession.query_property()
 # Or you can use a session-aware mapper as it was used in TurboGears 1:
 # DeclarativeBase = declarative_base(mapper=DBSession.mapper)
 
 
 # Import your model modules here.
 from myapp.model.auth import User, Group, Permission
+from myapp.model.movie import Movie, Genre, Cast

myapp/myapp/model/movie.py

+from sqlalchemy import Table, ForeignKey, Column
+from sqlalchemy.types import Unicode, Integer
+from sqlalchemy.orm import relation, backref
+
+from myapp.model import DeclarativeBase, metadata, DBSession
+
+__all__ = ['Movie', 'Genre', 'Cast']
+
+movie_genre_table = Table('movie_genre', metadata,
+    Column('movie_id', Integer, ForeignKey('movies.id',
+        onupdate="CASCADE", ondelete="CASCADE"), primary_key=True),
+    Column('genre_id', Integer, ForeignKey('genres.id',
+        onupdate="CASCADE", ondelete="CASCADE"), primary_key=True)
+)
+
+class Movie(DeclarativeBase):
+    __tablename__ = 'movies'
+    id = Column(Integer, primary_key=True)
+    title = Column(Unicode(255))
+    director = Column(Unicode(255))
+
+class Genre(DeclarativeBase):
+    __tablename__ = 'genres'
+    id = Column(Integer, primary_key=True)
+    name = Column(Unicode(255))
+    movies = relation('Movie', secondary=movie_genre_table, backref='genres')
+    def __unicode__(self):
+        return unicode(self.name)
+
+class Cast(DeclarativeBase):
+    __tablename__ = 'casts'
+    id = Column(Integer, primary_key=True)
+    movie_id = Column(Integer, ForeignKey(Movie.id))
+    movie = relation(Movie, backref=backref('cast'))
+    character = Column(Unicode(255))
+    actor = Column(Unicode(255))

myapp/myapp/websetup/bootstrap.py

 
 def bootstrap(command, conf, vars):
     """Place any commands to setup myapp here"""
+    for name in ['Action', 'Comedy', 'Romance', 'Sci-fi']:
+        model.DBSession.add(model.Genre(name=name))
 
     # <websetup.bootstrap.before.auth
     from sqlalchemy.exc import IntegrityError