Commits

Chris Perkins committed 0fa6279

delete and edit are documented.

Comments (0)

Files changed (5)

devdata.db

Binary file removed.

moviedemo/controllers/rest.py

         movie = Movie(title=title, description=description, release_date=release_date, directors=directors, genre_id=genre_id)
         DBSession.add(movie)
         redirect('./')
+
+    @expose('moviedemo.templates.rest.movies.edit')
+    def edit(self, movie_id, *args, **kw):
+        genres = DBSession.query(Genre).all()
+        directors = DBSession.query(Director).all()
+        movie = DBSession.query(Movie).get(movie_id)
+        
+        values = dict(title=movie.title, 
+                      description=movie.description, 
+                      genre_id=movie.genre_id,
+                      directors = [str(director.director_id) for director in movie.directors],
+                      release_date = datetime.strftime(movie.release_date, "%m/%d/%y"),
+                      )
+                      
+        if 'directors' in kw and not isinstance(kw['directors'], list):
+            kw['directors'] = [kw['directors']]
+        values.update(kw)
+
+        return dict(values=values, genres=genres, directors=directors)
+
+    @validate({'title':NotEmpty, 
+               'description':NotEmpty, 
+               'genre_id':Int(not_empty=True), 
+               'release_date':DateConverter(not_empty=True)}, error_handler=edit)
+    @expose()
+    def put(self, movie_id, title, description, directors, genre_id, release_date, **kw):
+        movie = DBSession.query(Movie).get(movie_id)
+        genre_id = int(genre_id)
+        if not isinstance(directors, list):
+            directors = [directors]
+        directors = [DBSession.query(Director).get(director) for director in directors]
+            
+        movie.genre_id = genre_id
+        movie.title=title
+        movie.description = description
+        movie.directors = directors
+        movie.release_date = release_date
+
+        DBSession.flush()
+        redirect('../')
+
+    @expose('moviedemo.templates.rest.movies.get_delete')
+    def get_delete(self, movie_id):
+        movie = DBSession.query(Movie).get(movie_id)
+        return dict(movie=movie)
+
+    @expose()
+    def post_delete(self, movie_id, **kw):
+        DBSession.delete(DBSession.query(Movie).get(movie_id))
+        redirect('../')

moviedemo/templates/rest/movies/edit.html

 </head>
 <body class="tundra">
   <div style="height:0px;"> &nbsp; </div>
-  <div style="float:left; padding-left:0px;">
-        <ul id="menu_items" py:if="hasattr(tmpl_context, 'menu_items')">
-            <li py:for="lower, item in sorted(tmpl_context.menu_items.iteritems())"><a href="../../${lower}s/">$item.__name__</a></li>
-       </ul>
-  </div>
-  <div style="float:left;">
-    <h2 style="margin-top:1px;">Edit $model</h2>
-     ${XML(tmpl_context.widget(value=value, action='./'))}
-  </div>
-  <div style="height:0px; clear:both;"> &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/rest/movies/get_all.html

       <a href='new/'>New Movie</a>
         <table>
           <tr>
+          <th/>
           <th>Title</th>
           <th>Description</th>
           <th>Genre</th>
           <th>Release Date</th>
           </tr>
           <tr py:for="i, movie in enumerate(movies)" class="${i%2 and 'even' or 'odd'}">
+            <td><a href="${movie.movie_id}/edit">edit</a>|<a href="${movie.movie_id}/delete">delete</a></td>
             <td>${movie.title}</td>
             <td>${movie.description}</td>
             <td>${movie.genre.name}</td>

moviedemo/templates/rest/movies/get_delete.html

 <xi:include href="master.html" />
 <head>
 </head>
-<body class="tundra">
-A page to help confirm deletes.
+<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>