Commits

okfn committed 1b2fcd7

[wdmmg/wui/coins][s]: create app global for mongo connection (hope this reduces db lockup issue) and improve search result listing.

Comments (0)

Files changed (4)

wdmmg/wdmmg/controllers/coins.py

 log = logging.getLogger(__name__)
 
 class CoinsController(BaseController):
-    def __before__(self, action, **params):
-        super(CoinsController, self).__before__(action, **params)
-        import pymongo
-        connection = pymongo.Connection("localhost", 27017)
-        # dbname = 'coins'
-        dbname = 'coins'
-        self.db = connection[dbname]
 
     def index(self):
         from pymongo import ASCENDING, DESCENDING
         page=int(request.params.get('page', 1))
         if c.q:
             # textq = '/.*%s.*/i' % c.q
-            dbq = self.db.coins.find(
+            dbq = self._db.coins.find(
                     {'search_field':{'$all': c.q.split()}}
                     )
             dbq.sort('value', DESCENDING)
         else:
-            dbq = self.db.coins.find()
+            dbq = self._db.coins.find().sort('value', DESCENDING)
         c.results = [ x for x in
                 dbq.limit(c.items_per_page).skip((page-1)*c.items_per_page)
         ]
         return render('coins/index.html')
 
     def view(self, id):
-        c.entry= self.db.coins.find_one({'srcid': id})
+        c.entry= self._db.coins.find_one({'srcid': id})
         if not c.entry:
             abort(404)
         return render('coins/entry.html')

wdmmg/wdmmg/lib/app_globals.py

-"""The application's Globals object"""
+from pymongo.connection import Connection
+from pymongo.errors import ConnectionFailure
+from pylons import config
 
 class Globals(object):
-
     """Globals acts as a container for objects available throughout the
     life of the application
 
     """
-
     def __init__(self):
         """One instance of Globals is created during application
         initialization and is available during requests via the
         'app_globals' variable
 
         """
+        # Populate basic app globals
+        try:
+            # conn = Connection(db_info['host'], db_info['port'],
+            #                  pool_size=int(config['blog.database.pool']))
+            conn = Connection("localhost", 27017)
+        except ConnectionFailure:
+            raise Exception('Unable to connect to MongoDB')
+        dbname = config.get('mongodb.dbname', 'coins')
+        self.db = conn[dbname]
+        # auth = self.db.authenticate(db_info['username'], db_info['password'])
+        # if not auth:
+        #    raise Exception('Authentication to MongoDB failed')
+

wdmmg/wdmmg/lib/base.py

 
 Provides the BaseController class for subclassing.
 """
+import pylons
 from pylons.controllers import WSGIController
 from pylons.templating import render_genshi as render
 from pylons import tmpl_context as c, request, config
         # the request is routed to. This routing information is
         # available in environ['pylons.routes_dict']
         try:
+            self._db = pylons.app_globals.db
             return WSGIController.__call__(self, environ, start_response)
         finally:
             meta.Session.remove()

wdmmg/wdmmg/templates/coins/index.html

 
     <p>${c.page.item_count} results.</p>
     ${c.page.pager(items_per_page=c.items_per_page)}
-    <table py:with="keylist = ['department_description', 'value', 'programme_object_group_code', 'programme_object_group_description', 'time', 'data_type']">
+    <table py:with="keylist = ['department_description', 'value', 'programme_object_group_description', 'account_description', 'budget_boundary', 'time', 'data_type']">
       <thead>
         <tr>
           <th>SRCID</th>
           <th py:for="key in keylist">
-            ${key.capitalize()}
+            ${key.replace('_',' ').capitalize()}
           </th>
         </tr>
       </thead>