Chris Perkins avatar Chris Perkins committed 25bd12f

added sprox tutorial code.

Comments (0)

Files changed (12)

moviedemo/config/app_cfg.py

 
 #Set the default renderer
 base_config.default_renderer = 'genshi'
-base_config.renderers.append('genshi') 
+base_config.renderers.append('genshi')
+base_config.renderers.append('json')
 # if you want raw speed and have installed chameleon.genshi
 # you should try to use this renderer instead.
 # warning: for the moment chameleon does not handle i18n translations

moviedemo/controllers/root.py

 
 from moviedemo.controllers.crc import MovieCrudRestController, DeclarativeMovieController
 from moviedemo.controllers.rest import MovieRestController
-
+from moviedemo.controllers.sproxdemo import SproxMovieController, SproxDirectorController
 
 import inspect
 from sqlalchemy.orm import class_mapper
     #swap these lines out to use the different controllers in this demo
     #movies = MovieCrudRestController(DBSession, models)
     #movies = DeclarativeMovieController(DBSession, models)
-    movies = MovieRestController(DBSession)
+    #movies = MovieRestController(DBSession)
+    movies = SproxMovieController()
+    directors = SproxDirectorController()
     
     admin = Catwalk(model, DBSession)
     error = ErrorController()

moviedemo/controllers/sproxdemo.py

+"""Rest Controller Info"""
+from tg import tmpl_context, request
+from tg.decorators import expose, validate
+
+from tg.controllers import RestController, redirect
+from moviedemo.model import DBSession, Movie, Genre, Director
+
+from sprox.dojo.tablebase import DojoTableBase as TableBase
+from sprox.dojo.fillerbase import DojoTableFiller as TableFiller
+
+class MovieTableFiller(TableFiller):
+    __model__ = Movie
+    __add_fields__ = {'related_movies':None}
+    def directors(self, obj):
+        directors = ', '.join(['<a href="/directors/'+str(d.director_id)+'">'+d.name+'</a>' 
+                               for d in obj.directors])
+        return directors.join(('<div>', '</div>'))
+
+    def related_movies(self, obj):
+        return ', '.join([movie.title for movie in obj.genre.movies if movie!=obj])
+
+movie_filler = MovieTableFiller(DBSession)
+
+class MovieTable(TableBase):
+    __model__ = Movie
+    __omit_fields__ = ['movie_id', 'genre_id', '__actions__']
+    __xml_fields__ = ['directors']
+    __add_fields__ = {'related_movies':None}
+    __column_widths__ = {'description':"30em",
+                         'release_date':"7em",
+                         'genre':"4em",
+                         }
+    __url__ = "/movies.json"
+    
+movie_table = MovieTable(DBSession)
+
+#uncomment this if you want to play with the "normal" table code
+#from sprox.tablebase import TableBase
+#from sprox.fillerbase import TableFiller
+#class MovieTableFiller(TableFiller):
+#    __model__ = Movie
+#    __add_fields__ = {'related_movies':None}
+#    def directors(self, obj):
+#        directors = ', '.join(['<a href="/directors/'+str(d.director_id)+'">'+d.name+'</a>' 
+#                               for d in obj.directors])
+#        return directors.join(('<div>', '</div>'))
+
+#    def related_movies(self, obj):
+#        return ', '.join([movie.title for movie in obj.genre.movies if movie!=obj])
+
+#movie_filler = MovieTableFiller(DBSession)
+
+#class MovieTable(TableBase):
+#    __model__ = Movie
+#    __omit_fields__ = ['movie_id', 'genre_id', '__actions__']
+#    __xml_fields__ = ['directors']
+#    __add_fields__ = {'related_movies':None}
+
+#movie_table = MovieTable(DBSession);
+
+class SproxMovieController(RestController):
+
+    @expose('moviedemo.templates.sproxdemo.movies.get_all')
+    @expose('json')
+    def get_all(self, *args, **kw):
+        if request.response_type == 'application/json':
+            return movie_filler.get_value(**kw)
+
+        value = []
+        tmpl_context.widget = movie_table
+        return dict(value=value)
+ 
+# uncomment this to work sprox w/out dojo
+#    @expose('moviedemo.templates.sproxdemo.movies.get_all')
+#    def get_all(self):
+#        tmpl_context.widget = movie_table
+#        value = movie_filler.get_value()
+#        return dict(value=value)
+
+class DirectorMovieTableFiller(TableFiller):
+    __model__ = Movie
+    __add_fields__ = {'related_movies':None}
+    
+    def directors(self, obj, **kw):
+        director = kw['director']
+        directors = ', '.join(['<a href="/directors/'+str(d.director_id)+'">'+d.name+'</a>' 
+                               for d in obj.directors if d!=director])
+        return directors.join(('<div>', '</div>'))
+
+    def related_movies(self, obj, **kw):
+        return ', '.join([movie.title for movie in obj.genre.movies if movie!=obj])
+
+    def _do_get_provider_count_and_objs(self, director=None, **kw):
+        movies = DBSession.query(Movie).filter(Movie.directors.contains(director)).all()
+        return len(movies), movies
+
+director_movie_filler = DirectorMovieTableFiller(DBSession)
+
+class DirectorMovieTable(TableBase):
+    __model__ = Movie
+    __omit_fields__ = ['movie_id', 'genre_id', '__actions__']
+    __xml_fields__ = ['other directors']
+    __add_fields__ = {'related_movies':None}
+    __headers__ = {'directors': 'other directors'}
+    
+director_movie_table = DirectorMovieTable(DBSession);
+
+class SproxDirectorController(RestController):
+    
+    @expose('moviedemo.templates.sproxdemo.directors.get_one')
+    def get_one(self, director_id):
+        tmpl_context.widget = director_movie_table
+        director = DBSession.query(Director).get(director_id)
+        value = director_movie_filler.get_value(director=director)
+        return dict(director=director, value=value)
Add a comment to this file

moviedemo/templates/sproxdemo/__init__.py

Empty file added.

Add a comment to this file

moviedemo/templates/sproxdemo/directors/__init__.py

Empty file added.

moviedemo/templates/sproxdemo/directors/get_one.html

+<html xmlns="http://www.w3.org/1999/xhtml"
+      xmlns:py="http://genshi.edgewall.org/"
+      xmlns:xi="http://www.w3.org/2001/XInclude">
+  <xi:include href="master.html" />
+<head/>
+<body>
+  <div style="height:0px;"> &nbsp; </div>
+  <div>
+    <div style="float:left width: 80%">
+      <h1>${director.name}</h1>
+      <h2 style="margin-top:1px;">Movies Directed</h2>
+      ${tmpl_context.widget(value=value)}
+    </div>
+  </div>
+</body>
+</html>
Add a comment to this file

moviedemo/templates/sproxdemo/movies/__init__.py

Empty file added.

moviedemo/templates/sproxdemo/movies/edit.html

+<html xmlns="http://www.w3.org/1999/xhtml"
+      xmlns:py="http://genshi.edgewall.org/"
+      xmlns:xi="http://www.w3.org/2001/XInclude">
+
+<xi:include href="master.html" />
+<head>
+<style>
+#menu_items {
+  padding:0px 12px 0px 2px;
+  list-style-type:None
+  }
+</style>
+</head>
+<body class="tundra">
+  <div style="height:0px;"> &nbsp; </div>
+  <h2> Edit Movie</h2>
+  <form method="POST" action="./">
+    <table>
+    <tr><td>Title </td><td><input type="textfield" name="title" value="${values['title']}"/>${tmpl_context.form_errors.get('title')}</td></tr>
+    <tr><td>Description</td><td> <textarea name="description">${values.get('description')}</textarea>${tmpl_context.form_errors.get('description')}</td></tr>
+    <tr><td>Genre</td>
+        <td>
+          <select name="genre_id">
+              <py:for each="genre in genres">
+                <py:if test="values.get('genre_id') == unicode(genre.genre_id)">
+                  <option value="$genre.genre_id" selected="selected">$genre.name</option>
+                </py:if>
+                <py:if test="values.get('genre_id')!= unicode(genre.genre_id)">
+                  <option value="$genre.genre_id">$genre.name</option>
+                </py:if>
+              </py:for>
+          </select>
+        </td></tr>
+    <tr><td>Directors</td>
+        <td>
+          <select multiple="true" name="directors">
+              <py:for each="director in directors">
+                $director.name
+                <py:if test="unicode(director.director_id) in values['directors']">
+                  <option value="$director.director_id" selected="selected">$director.name</option>
+                </py:if>
+                <py:if test="unicode(director.director_id) not in values['directors']">
+                  <option value="$director.director_id">$director.name</option>
+                </py:if>
+              </py:for>
+          </select>
+        </td></tr>
+    <tr><td>Release Date</td><td><input type="textfield" name="release_date" value="${values.get('release_date')}"/>${tmpl_context.form_errors.get('release_date')}</td></tr>
+    </table><br/>
+    <input type="hidden" name="_method" value="PUT"/>
+    <input type="submit" value="update"/>
+  </form>
+
+</body>
+</html>

moviedemo/templates/sproxdemo/movies/get_all.html

+<html xmlns="http://www.w3.org/1999/xhtml"
+      xmlns:py="http://genshi.edgewall.org/"
+      xmlns:xi="http://www.w3.org/2001/XInclude">
+  <xi:include href="master.html" />
+<head/>
+<body>
+  <div style="height:0px;"> &nbsp; </div>
+  <div>
+    <div style="float:left width: 80%">
+      <h1 style="margin-top:1px;">Movie Listing</h1>
+      <a href='new/'>New Movie</a>
+      ${tmpl_context.widget(value=value)}
+    </div>
+  </div>
+</body>
+</html>

moviedemo/templates/sproxdemo/movies/get_delete.html

+<html xmlns="http://www.w3.org/1999/xhtml"
+      xmlns:py="http://genshi.edgewall.org/"
+      xmlns:xi="http://www.w3.org/2001/XInclude">
+
+<xi:include href="master.html" />
+<head>
+</head>
+<body>
+<div style="height:0px;">&nbsp;</div>
+<h2>Delete Confirmation</h2>
+Are you sure you want to delete this record?
+<table>
+  <tr>
+  <th>Title</th>
+  <th>Description</th>
+  <th>Genre</th>
+  <th>Directors</th>
+  <th>Release Date</th>
+  </tr>
+  <tr>
+    <td>${movie.title}</td>
+    <td>${movie.description}</td>
+    <td>${movie.genre.name}</td>
+    <td>
+    <py:for each="director in movie.directors">
+    ${director.name},
+    </py:for>
+    </td>
+    <td>${movie.release_date}</td>
+  </tr>
+</table>
+<form action='./' method="POST">
+  <input type="hidden" name='_method' value='DELETE'/>
+  <input type="submit" value="Yes, Delete"/>
+</form>
+
+</body>
+</html>

moviedemo/templates/sproxdemo/movies/get_one.html

+<html xmlns="http://www.w3.org/1999/xhtml"
+      xmlns:py="http://genshi.edgewall.org/"
+      xmlns:xi="http://www.w3.org/2001/XInclude">
+
+<xi:include href="master.html" />
+<head>
+  <meta content="text/html; charset=UTF-8" http-equiv="content-type" py:replace="''"/>
+  <title>Welcome to TurboGears 2.0, standing on the
+  shoulders of giants, since 2007</title>
+
+</head>
+<body class="tundra">
+  <div id="getting_started">
+&nbsp;
+<div>
+<h1>$model Listing</h1>
+</div>
+<div>
+<a href='new/'>New</a> $model
+</div>
+<br/>
+${XML(tmpl_context.widget())}
+</div>
+</body>
+</html>

moviedemo/templates/sproxdemo/movies/new.html

+<html xmlns="http://www.w3.org/1999/xhtml"
+      xmlns:py="http://genshi.edgewall.org/"
+      xmlns:xi="http://www.w3.org/2001/XInclude">
+
+<xi:include href="master.html" />
+<head/>
+<body>
+  <div style="height:0px;"> &nbsp; </div>
+  <h2> New Movie</h2>
+  <form method="POST" action="./">
+    <table>
+    <tr><td>Title </td><td><input type="textfield" name="title" value="${values.get('title', '')}"/>${tmpl_context.form_errors.get('title')}</td></tr>
+    <tr><td>Description</td><td> <textarea name="description">${values.get('description')}</textarea>${tmpl_context.form_errors.get('description')}</td></tr>
+    <tr><td>Genre</td>
+        <td>
+          <select name="genre_id">
+              <py:for each="genre in genres">
+                <py:if test="values.get('genre_id') == unicode(genre.genre_id)">
+                  <option value="$genre.genre_id" selected="selected">$genre.name</option>
+                </py:if>
+                <py:if test="values.get('genre_id')!= unicode(genre.genre_id)">
+                  <option value="$genre.genre_id">$genre.name</option>
+                </py:if>
+              </py:for>
+          </select>
+        </td></tr>
+    <tr><td>Directors</td>
+        <td>
+          <select multiple="true" name="directors">
+              <py:for each="director in directors">
+                $director.name
+                <py:if test="unicode(director.director_id) in values.get('directors', [])">
+                  <option value="$director.director_id" selected="selected">$director.name</option>
+                </py:if>
+                <py:if test="unicode(director.director_id) not in values.get('directors', [])">
+                  <option value="$director.director_id">$director.name</option>
+                </py:if>
+              </py:for>
+          </select>
+        </td></tr>
+    <tr><td>Release Date</td><td><input type="textfield" name="release_date" value="${values.get('release_date')}"/>${tmpl_context.form_errors.get('release_date')}</td></tr>
+    </table><br/>
+    <input type="submit" value="create"/>
+  </form>
+  
+</body>
+</html>
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.