Atsushi Odagiri avatar Atsushi Odagiri committed fb29b79

update edited data.

Comments (0)

Files changed (3)

src/alchemyadmin/__init__.py

 from werkzeug import Request, Response
 from werkzeug.routing import Map, Rule
 from werkzeug.exceptions import HTTPException
+from werkzeug import Local, LocalManager
 from jinja2 import Environment, PackageLoader
 from sqlalchemy.orm import sessionmaker, scoped_session, class_mapper
 from formalchemy import Grid, FieldSet
 
 default_template_env = Environment(loader=PackageLoader('alchemyadmin', 'templates'))
 
+local = Local()
+local_manager = LocalManager([local])
 
 class Application(object):
     def __init__(self, settings, *models, **modelsdict):
             urls.add(Rule('/' + m, endpoint='model_index', 
                           defaults={'model':m}))
             
-            urls.add(Rule('/' + m + '/' + self.pk_url_map(self.models[m]) + ';edit', endpoint='model_edit', 
+            urls.add(Rule('/' + m + '/' + self.pk_url_map(self.models[m]) + ';edit', 
+                          endpoint='model_edit', 
+                          methods=['GET'],
+                          defaults={'model':m}))
+            urls.add(Rule('/' + m + '/' + self.pk_url_map(self.models[m]),
+                          endpoint='model_update',
+                          methods=['POST'], 
                           defaults={'model':m}))
 
             self.grids[m] = grid = Grid(self.models[m])
 
     def __call__(self, environ, start_response):
         try:
-            adapter = self.urls.bind_to_environ(environ)
+            local.adapter = adapter = self.urls.bind_to_environ(environ)
             endpoint, args = adapter.match()
-            request = Request(environ)
+            local.request = request = Request(environ)
             res = getattr(self, endpoint)(request, **args)
             
             return res(environ, start_response)
         except HTTPException, e:
             return e(environ, start_response)
+        finally:
+            local_manager.cleanup()
 
     def index(self, request):
         res = Response(self.render('index.html', models=self.models),
                        content_type='text/html;charset=utf-8')
         return res
 
+    def model_update(self, request, model, **keys):
+        q = self.DBSession.query(self.models[model])
+        mapper = class_mapper(self.models[model])
+
+        for p in mapper.primary_key:
+            q = q.filter(p==keys[p.name])
+        row = q.one()
+        fs = self.fieldsets[model].bind(row, data=request.form)
+        fs.sync()
+        self.DBSession.commit()
+
+        fs.configure(readonly=True)
+        res = Response(self.render('model/show.html', 
+                                   model=model,
+                                   row=row,
+                                   form=fs,
+                                   models=self.models),
+                       content_type='text/html;charset=utf-8')
+        return res
+
     def model_edit(self, request, model, **keys):
         q = self.DBSession.query(self.models[model])
         mapper = class_mapper(self.models[model])
             q = q.filter(p==keys[p.name])
         row = q.one()
         form = self.fieldsets[model].bind(row)
+        post_url = self.url_for('model_update',
+                                values=self.instance_pk_values(row),
+                                method='POST')
         res = Response(self.render('model/edit.html', 
                                    model=model,
                                    row=row,
                                    form=form,
+                                   post_url=post_url,
                                    models=self.models),
                        content_type='text/html;charset=utf-8')
         return res
 
+    def instance_pk_values(self, instance):
+        mapper = class_mapper(instance.__class__)
+        values = {}
+        for pk in mapper.primary_key:
+            values[pk.name] = getattr(instance, pk.name)
+        return values
+
+
+    def url_for(self, endpoint, values, method="GET"):
+        return local.adapter.build(endpoint, method=method,
+                                   values=values)
+
     def render(self, template_name, **kw):
+        values = kw.copy()
+        values.update(request=local.request,
+                      url_for=self.url_for)
         template = self.template_env.get_template(template_name)
-        return template.render(**kw)
+        return template.render(**values)
 

src/alchemyadmin/templates/model/edit.html

 {{model}}
-<form action="" method="post">
+<form action="{{post_url}}" method="post">
 {{form.render()}}
 <button type="submit">Save</button>
 </form>

src/alchemyadmin/templates/model/show.html

+{{model}}
+<table>
+{{form.render()}}
+</table>
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.