Skip to content

Commit

Permalink
fixes in request helpers class
Browse files Browse the repository at this point in the history
  • Loading branch information
mdennebaum committed Nov 19, 2011
1 parent 7956581 commit 0494c67
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 81 deletions.
49 changes: 27 additions & 22 deletions whirlwind/core/request.py
Expand Up @@ -13,6 +13,7 @@
from tornado import ioloop
from pymongo import *
from whirlwind.view.filters import Filters
from whirlwind.view.paginator import Paginator
from whirlwind.core import dotdict
from whirlwind.db.mongo import Mongo

Expand Down Expand Up @@ -303,29 +304,33 @@ def paged_list(handler,table_class,select=None):
sort = None
order_by = handler.get_argument('order_by',None)

if order_by:
order = handler.get_argument('order',None)
order = pymongo.DESCENDING if order.lower() == 'desc' else pymongo.ASCENDING
sort = {
order:order_by
}

if select:
if sort:
results = table_class.find(select).skip((page-1)*count).limit(count).sort(sort)
else:
results = table_class.find(select).skip((page-1)*count).limit(count)

total = table_class.find(select).count()
else:
if sort:
results = table_class.find().skip((page-1)*count).limit(count).sort(sort)
else:
results = table_class.find().skip((page-1)*count).limit(count)
try:
if order_by:
order = handler.get_argument('order',None)
order = pymongo.DESCENDING if order.lower() == 'desc' else pymongo.ASCENDING
sort = {
order:order_by
}

if select:
if sort:
results = table_class.find(select).skip((page-1)*count).limit(count).sort(sort)
else:
results = table_class.find(select).skip((page-1)*count).limit(count)

total = table_class.find().count()

return Paginator(results,page,count,total)
total = table_class.find(select).count()
else:
if sort:
results = table_class.find().skip((page-1)*count).limit(count).sort(sort)
else:
results = table_class.find().skip((page-1)*count).limit(count)

total = table_class.find().count()

return Paginator(results,page,count,total)

except:
return Paginator([],page,count,0)

#delete checked list items
@staticmethod
Expand Down
73 changes: 14 additions & 59 deletions whirlwind/util/__init__.py
@@ -1,62 +1,17 @@
from tornado.options import options
import re

class MiddlewareManager():
class Util(object):

def __init__(self,request):
self.request = request
self.request_middleware = []
self.response_middleware = []
self.view_middleware = []
self.load()
@staticmethod
def normalize(username):
if not username :
return None
#allow legal email address
name = username.strip().lower()
name = re.sub(r'[^a-z0-9\\.\\@_\\-~#]+', '', name)
name = re.sub('\\s+', '_',name)

def run_request_hooks(self):
self.__run_hooks('request',self.request_middleware)

def run_response_hooks(self):
self.__run_hooks('response',self.response_middleware)

def run_view_hooks(self,view):
self.__run_hooks('view',self.view_middleware,view)

def __run_hooks(self,type,middleware_classes,view=None):
for middleware_class in middleware_classes:
try:
if(type == 'request'):
middleware_class.request_hook()
if(type == 'response'):
middleware_class.response_hook()
if(type == 'view'):
middleware_class.view_hook(view)
except AttributeError:
pass

def load(self):
if hasattr(options,'middleware_classes') and len(options.middleware_classes) > 0:
for mclass in options.middleware_classes:
modname, clsname = self.split_name(mclass)

try:
mod = __import__(modname, globals(), locals(), [clsname], -1)
except ImportError, ex:
print "module __import__ failed", ex

try:
cls = getattr(mod, clsname)
inst = cls(self.request)
except AttributeError, ex:
print "cant instantiate cls", ex

if hasattr(inst,'view_hook'):
self.view_middleware.append(inst)
if hasattr(inst,'request_hook'):
self.request_middleware.append(inst)
if hasattr(inst,'response_hook'):
self.response_middleware.append(inst)

def split_name(self,path):
try:
pos = path.rindex('.')
except ValueError:
raise Exception('%s is invalid' % path)

return path[:pos], path[pos+1:]
#don't allow $ and . because they screw up the db.
name = name.replace(".", "")
name = name.replace("$", "")
return name

0 comments on commit 0494c67

Please sign in to comment.