Commits

Doug Latornell committed 8dff3ee

Refactor admin items list view into a single view function.

  • Participants
  • Parent commits c59f5da

Comments (0)

Files changed (7)

     pyramid_debugtoolbar
     pyramid_tm
 
+debugtoolbar.hosts = 192.168.211.10
+
 mako.directories = randopony:templates
 
 persona.secret = This is some secret string

randopony/__init__.py

     config.add_route('home', '/')
     # admin routes
     config.add_route('admin.home', '/admin')
-    config.add_route('admin.brevets', '/admin/brevets/')
-    config.add_route('admin.club_events', '/admin/club-events/')
-    config.add_route('admin.populaires', '/admin/populaires/')
-    config.add_route('admin.wranglers', '/admin/wranglers/')
-    config.add_route('admin.wrangler.edit', '/admin/wranglers/{item}/edit')
+    config.add_route('admin.list', '/admin/{list}/')
+    config.add_route('admin.wranglers.edit', '/admin/wranglers/{item}/edit')

randopony/static/css/admin.css

 /* Styles for RandoPony admin interface pages */
 
+.container .admin-list {
+    width: 320px;
+    margin-left: 0;
+}
+
 ul.admin-list {
     padding-left: 30px;
 }

randopony/templates/admin/home.mako

 <%inherit file="page.mako"/>
 
 <ul class="unstyled admin-list">
-  <li><a href="${request.route_url('admin.brevets')}">Brevets</a></li>
-  <li><a href="${request.route_url('admin.club_events')}">Club Events</a></li>
-  <li><a href="${request.route_url('admin.populaires')}">Populaires</a></li>
-  <li><a href="${request.route_url('admin.wranglers')}">Pony Wranglers</a></li>
+  <li>
+    <a href="${request.route_url('admin.list', list='brevets')}">
+      Brevets
+    </a>
+  </li>
+  <li>
+    <a href="${request.route_url('admin.list', list='club_events')}">
+      Club Events
+    </a>
+  </li>
+  <li>
+    <a href="${request.route_url('admin.list', list='populaires')}">
+      Populaires
+    </a>
+  </li>
+  <li>
+    <a href="${request.route_url('admin.list', list='wranglers')}">
+      Pony Wranglers (aka Administrators)
+    </a>
+  </li>
 </ul>

randopony/templates/admin/list.mako

+<%inherit file="page.mako"/>
+
+<div class="container admin-list">
+  <ul class="unstyled">
+    %for item in items:
+    <li>
+        <a href="${request.route_url(
+                    'admin.{}.{}'.format(list, action), item=str(item))}">
+          ${item.persona_email}
+        </a>
+        <button class="btn btn-danger pull-right">Delete</button>
+    </li>
+    %endfor
+  </ul>
+</div>

randopony/templates/admin/wranglers.mako

-<%inherit file="page.mako"/>
-
-<ul class="unstyled admin-list">
-  %for wrangler in wranglers:
-  <li>
-    <a href="${request.route_url('admin.wrangler.edit', item=str(wrangler))}">
-      ${wrangler.persona_email}
-    </a>
-  </li>
-  %endfor
-</ul>

randopony/views/admin.py

 
 
 @forbidden_view_config()
-def admin_login(request):
+def login(request):
     body = render('admin/login.mako', {'logout_btn': False}, request=request)
     return Response(body, status='403 Forbidden')
 
 
 @view_config(route_name='admin.home', renderer='admin/home.mako',
-             permission='admin')
-def admin_home(request):
+     permission='admin')
+def home(request):
     return {'logout_btn': True}
 
 
-@view_config(route_name='admin.brevets', renderer='string',
-             permission='admin')
-def admin_brevets(request):
-    return 'brevets stub'
-
-
-@view_config(route_name='admin.club_events', renderer='string',
-             permission='admin')
-def admin_club_events(request):
-    return 'club events stub'
-
-
-@view_config(route_name='admin.populaires', renderer='string',
-             permission='admin')
-def admin_populaires(request):
-    return 'populaires stub'
-
-
-@view_config(route_name='admin.wranglers',
-    renderer='admin/wranglers.mako',
+@view_config(route_name='admin.list', renderer='admin/list.mako',
     permission='admin')
-def admin_wranglers(request):
+def items_list(request):
+    lists = {
+        'wranglers': {
+            'model': Administrator,
+            'order_by': Administrator.persona_email,
+            'action': 'edit',
+        },
+    }
     tmpl_vars = {'logout_btn': True}
-    wranglers = DBSession.query(Administrator).\
-        order_by(Administrator.persona_email)
-    tmpl_vars.update({'wranglers': wranglers})
+    list_name = request.matchdict['list']
+    params = lists[list_name]
+    items = DBSession.query(params['model']).\
+        order_by(params['order_by'])
+    tmpl_vars.update({
+        'items': items, 'action': params['action'], 'list': list_name})
     return tmpl_vars
 
 
-@view_config(route_name='admin.wrangler.edit',
+@view_config(route_name='admin.wranglers.edit',
     renderer='string',
     permission='admin')
-def admin_wrangler_edit(request):
+def wrangler_edit(request):
     admin = request.matchdict['item']
     return 'wrangler edit stub for {}'.format(admin)