Commits

Atsushi Odagiri committed a2bba11

show model's data grid.

  • Participants
  • Parent commits 691be2a

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 jinja2 import Environment, PackageLoader
+from sqlalchemy.orm import sessionmaker, scoped_session
+from formalchemy import Grid
+
 default_template_env = Environment(loader=PackageLoader('alchemyadmin', 'templates'))
 
+
 class Application(object):
     def __init__(self, settings, *models, **modelsdict):
         self.template_env = default_template_env
         self.models = modelsdict.copy()
+        self.DBSession = scoped_session(sessionmaker())
         for m in models:
             self.models[m.__name__.lower()] = m
 
+        urls = Map([
+                Rule('/', endpoint='index'),
+                ])
+        self.grids = {}
+        for m in self.models:
+            urls.add(Rule('/' + m, endpoint='model_index', 
+                          defaults={'model':m}))
+            self.grids[m] = grid = Grid(self.models[m])
+            grid.configure(readonly=True)
+        self.urls = urls
 
     def __call__(self, environ, start_response):
+        try:
+            adapter = self.urls.bind_to_environ(environ)
+            endpoint, args = adapter.match()
+            request = Request(environ)
+            res = getattr(self, endpoint)(request, **args)
+            
+            return res(environ, start_response)
+        except HTTPException, e:
+            return e(environ, start_response)
+
+    def index(self, request):
         res = Response(self.render('index.html', models=self.models),
                        content_type='text/html;charset=utf-8')
-        return res(environ, start_response)
+        return res
+
+    def model_index(self, request, model):
+        rows = self.DBSession.query(self.models[model]).all()
+        grid = self.grids[model].bind(instances=rows)
+        res = Response(self.render('model/index.html', 
+                                   model=model,
+                                   grid=grid,
+                                   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/demo/__init__.py

 from sqlalchemy import *
 from sqlalchemy.orm import *
 from sqlalchemy.ext.declarative import declarative_base
+from datetime import datetime
 
 Base = declarative_base()
 metadata = Base.metadata
     conf.update(app_conf)
     engine = engine_from_config(conf)
     metadata.bind = engine
+    metadata.drop_all()
+    metadata.create_all()
+    Session = sessionmaker()
+    session = Session()
+    u = User()
+    u.user_name = 'johndoe'
+    u.display_name = u'John Doe'
+    u.password = 'secret'
+    u.birthday = datetime.now()
+    u.description = """ This is demo user."""
+    session.add(u)
+    session.commit()
+
     app = Application(conf, User)
     return app
 

src/alchemyadmin/templates/model/index.html

+{{model}}
+<table>
+{{grid.render()}}
+</table>