Atsushi Odagiri avatar Atsushi Odagiri committed 83a3c14

show edit form.

Comments (0)

Files changed (2)

src/alchemyadmin/__init__.py

 from werkzeug.routing import Map, Rule
 from werkzeug.exceptions import HTTPException
 from jinja2 import Environment, PackageLoader
-from sqlalchemy.orm import sessionmaker, scoped_session
-from formalchemy import Grid
+from sqlalchemy.orm import sessionmaker, scoped_session, class_mapper
+from formalchemy import Grid, FieldSet
 
 default_template_env = Environment(loader=PackageLoader('alchemyadmin', 'templates'))
 
                 Rule('/', endpoint='index'),
                 ])
         self.grids = {}
+        self.fieldsets = {}
         for m in self.models:
             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', 
+                          defaults={'model':m}))
+
             self.grids[m] = grid = Grid(self.models[m])
             grid.configure(readonly=True)
+            self.fieldsets[m] = fieldset = FieldSet(self.models[m])
         self.urls = urls
 
+    def pk_url_map(self, model):
+        mapper = class_mapper(model)
+        return '/'.join(['<' + p.name + '>' for p in mapper.primary_key])
+
+
     def __call__(self, environ, start_response):
         try:
             adapter = self.urls.bind_to_environ(environ)
                        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])
+
+        for p in mapper.primary_key:
+            q = q.filter(p==keys[p.name])
+        row = q.one()
+        form = self.fieldsets[model].bind(row)
+        res = Response(self.render('model/edit.html', 
+                                   model=model,
+                                   row=row,
+                                   form=form,
+                                   models=self.models),
+                       content_type='text/html;charset=utf-8')
+        return res
+
     def render(self, template_name, **kw):
         template = self.template_env.get_template(template_name)
         return template.render(**kw)

src/alchemyadmin/templates/model/edit.html

+{{model}}
+<form action="" method="post">
+{{form.render()}}
+<button type="submit">Save</button>
+</form>
+
+
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.