summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRalph Bean <ralph.bean@gmail.com>2011-04-14 10:21:21 -0400
committerRalph Bean <ralph.bean@gmail.com>2011-04-14 10:21:21 -0400
commite9d4d07a53822fc75d608fc1eef0c52fbbfe58e9 (patch)
tree172ff177bd61a55c6f56dc1bf8ffa383a71e10b3
parent092c5a967673bdd0aacb3eabdc49525f645c331c (diff)
parentf62a1c9b336ae632a8767ea6270cad5c1f61a187 (diff)
downloadleafymiracle-e9d4d07a53822fc75d608fc1eef0c52fbbfe58e9.zip
leafymiracle-e9d4d07a53822fc75d608fc1eef0c52fbbfe58e9.tar.gz
leafymiracle-e9d4d07a53822fc75d608fc1eef0c52fbbfe58e9.tar.xz
Merge branch 'searchbar'
-rw-r--r--leafymiracle/__init__.py7
-rw-r--r--leafymiracle/resources.py51
-rw-r--r--leafymiracle/views.py52
3 files changed, 56 insertions, 54 deletions
diff --git a/leafymiracle/__init__.py b/leafymiracle/__init__.py
index 1d9203b..6be768e 100644
--- a/leafymiracle/__init__.py
+++ b/leafymiracle/__init__.py
@@ -31,12 +31,11 @@ def main(global_config, **settings):
config.add_view('leafymiracle.views.view_model',
context='leafymiracle.models.Root',
renderer="templates/model.pt")
+ config.add_view('leafymiracle.views.view_search',
+ context='leafymiracle.resources.SearchHandler',
+ xhr=True)
config.add_static_view(name='static', path='static')
- # Create the searchbar handler
- config.add_route('search', '/search',
- view='leafymiracle.views.search', xhr=True)
-
# Create the data view for our tw2.jit.SQLARadialGraph
jit_view = lambda context, request: LeafyGraph.request(request)
config.add_route('data', '/data', view=jit_view, xhr=True)
diff --git a/leafymiracle/resources.py b/leafymiracle/resources.py
index 8185f9f..443da1b 100644
--- a/leafymiracle/resources.py
+++ b/leafymiracle/resources.py
@@ -16,12 +16,63 @@
from sqlalchemy.orm.exc import NoResultFound
from models import DBSession, Root, initialize_sql
+import models
+import sqlalchemy
+
+class SearchHandler(object):
+ def search(self, term, cats):
+ # Don't do any search if the term is too short
+ if len(term) < 2:
+ return {'data':[]}
+
+ attrs = {
+ 'Category' : {
+ 'search_on' : ['name', 'description'],
+ },
+ 'Group' : {
+ 'search_on' : ['name', 'description'],
+ },
+ 'Package' : {
+ 'search_on' : ['name'],
+ }
+ }
+ srch = '%%%s%%' % term
+ cats = cats.split(',')
+ results = []
+
+ for cat in cats:
+ if not cat in attrs.keys():
+ raise ValueError, "'%s' is a disallowed category." % cat
+ cls = getattr(models, cat)
+ entries = cls.query.filter(sqlalchemy.or_(
+ *[getattr(cls, srch_attr).like(srch)
+ for srch_attr in attrs[cat]['search_on']]
+ ))
+ results += [[unicode(e), e.id, cat] for e in entries]
+
+ data = {
+ 'data' : [
+ {
+ 'label' : label,
+ 'value' : value,
+ 'category' : category }
+ for label, value, category in results
+ ]
+ }
+ return data
+
class MyApp(object):
__name__ = None
__parent__ = None
def __getitem__(self, key):
session = DBSession()
+ if key == 'search':
+ handler = SearchHandler()
+ handler.__name__ = key
+ handler.__parent__ = self
+ return handler
+
try:
id = int(key)
except (ValueError, TypeError):
diff --git a/leafymiracle/views.py b/leafymiracle/views.py
index 65ee626..d38b654 100644
--- a/leafymiracle/views.py
+++ b/leafymiracle/views.py
@@ -19,8 +19,6 @@ from widgets import LeafyGraph
from widgets import LeafyDialog
from widgets import LeafySearchbar
-import leafymiracle.models
-import sqlalchemy
import simplejson
import webob
@@ -37,58 +35,12 @@ def view_model(context, request):
}
-def search(context, request):
- """ NOTE :: this is *not* the `pyramid` way of doing things.
-
- DB stuff should not happen in the 'view' but should instead happen..
- elsewhere? Quick hack to make this work before submission.
-
- --Ralph
- """
-
+def view_search(context, request):
term = request.params['term']
cats = request.params.get('cats', 'Category,Group,Package')
- # Don't do any search if the term is too short
- if len(term) < 2:
- resp = webob.Response(request=request, content_type="application/json")
- resp.body = simplejson.dumps({'data':[]})
- return resp
-
- attrs = {
- 'Category' : {
- 'search_on' : ['name', 'description'],
- },
- 'Group' : {
- 'search_on' : ['name', 'description'],
- },
- 'Package' : {
- 'search_on' : ['name'],
- }
- }
- srch = '%%%s%%' % term
- cats = cats.split(',')
- results = []
-
- for cat in cats:
- if not cat in attrs.keys():
- raise ValueError, "'%s' is a disallowed category." % cat
- cls = getattr(leafymiracle.models, cat)
- entries = cls.query.filter(sqlalchemy.or_(
- *[getattr(cls, srch_attr).like(srch)
- for srch_attr in attrs[cat]['search_on']]
- ))
- results += [[unicode(e), e.id, cat] for e in entries]
+ data = context.search(term, cats)
- data = {
- 'data' : [
- {
- 'label' : label,
- 'value' : value,
- 'category' : category }
- for label, value, category in results
- ]
- }
resp = webob.Response(request=request, content_type="application/json")
resp.body = simplejson.dumps(data)
return resp