Commits

Atsushi Odagiri committed 3e66d5e

add link for edit url to data grid.

  • Participants
  • Parent commits fb29b79

Comments (0)

Files changed (2)

src/alchemyadmin/__init__.py

 from werkzeug import Local, LocalManager
 from jinja2 import Environment, PackageLoader
 from sqlalchemy.orm import sessionmaker, scoped_session, class_mapper
-from formalchemy import Grid, FieldSet
+from formalchemy import Grid, FieldSet, Field
 
 default_template_env = Environment(loader=PackageLoader('alchemyadmin', 'templates'))
 
 local = Local()
 local_manager = LocalManager([local])
 
+def url_for(endpoint, values, method="GET"):
+    return local.adapter.build(endpoint, method=method,
+                               values=values)
+
+
+def make_edit_url(instance):
+    values = {}
+    for pk in class_mapper(instance.__class__).primary_key:
+        values[pk.name] = getattr(instance, pk.name)
+
+    return url_for('model_edit',
+                   method='GET',
+                   values=values)
+
 class Application(object):
     def __init__(self, settings, *models, **modelsdict):
         self.template_env = default_template_env
         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', 
+            edit_url = '/' + m + '/' + self.pk_url_map(self.models[m]) + ';edit'
+            urls.add(Rule(edit_url, 
                           endpoint='model_edit', 
                           methods=['GET'],
                           defaults={'model':m}))
                           defaults={'model':m}))
 
             self.grids[m] = grid = Grid(self.models[m])
-            grid.configure(readonly=True)
+            grid.configure(readonly=True, pk=True)
+            grid.append(Field('edit', 
+                              value=lambda item: '<a href="%s">Edit</a>' % make_edit_url(item)))
             self.fieldsets[m] = fieldset = FieldSet(self.models[m])
         self.urls = urls
 
 
 
     def url_for(self, endpoint, values, method="GET"):
-        return local.adapter.build(endpoint, method=method,
-                                   values=values)
+        return url_for(endpoint, values, method)
+
 
     def render(self, template_name, **kw):
         values = kw.copy()

src/alchemyadmin/templates/index.html

 <ul>
   {%for m in models%}
-  <li>{{m}}</li>
+  <li><a href="{{url_for('model_index', values={'model':m})}}">{{m}}</a></li>
   {%endfor %}
 </ul>