00001 ## 00002 # 00003 # request is the Django Request, 00004 # cls_or_object is the Django Model class or a model object or list of model objects 00005 # func is a callable that takes a row and returns a list of items in the proper order for the grid. 00006 def grid_handler(request,cls_or_object,func=None): 00007 import math 00008 import simplejson 00009 00010 from vyperlogix.misc import _utils 00011 00012 from vyperlogix.django import django_utils 00013 from vyperlogix.misc import ObjectTypeName 00014 00015 page = django_utils.get_from_post_or_get(request,'page',default=-1) 00016 rows = django_utils.get_from_post_or_get(request,'rows',default=-1) 00017 sidx = django_utils.get_from_post_or_get(request,'sidx',default='') 00018 sord = django_utils.get_from_post_or_get(request,'sord',default='') 00019 00020 try: 00021 if sidx == '' : sidx = 1 00022 00023 symbol_ModelBase = 'django.db.models.base.ModelBase' 00024 symbol_QuerySet = 'django.db.models.query.QuerySet' 00025 class_ = ObjectTypeName.typeClassName(cls_or_object) 00026 if (class_ in [symbol_ModelBase,symbol_QuerySet]): 00027 try: 00028 items = cls_or_object.objects.order_by(sidx) if (class_ == symbol_ModelBase) else cls_or_object.order_by(sidx) 00029 if sord == 'desc': 00030 items = misc.reverse(items) 00031 except Exception, details: 00032 info_string = _utils.formattedException(details=details) 00033 print >>sys.stderr, info_string 00034 elif (isinstance(cls_or_object,list)): 00035 try: 00036 items = cls_or_object 00037 except Exception, details: 00038 info_string = _utils.formattedException(details=details) 00039 print >>sys.stderr, info_string 00040 else: 00041 try: 00042 items = [cls_or_object] 00043 except Exception, details: 00044 info_string = _utils.formattedException(details=details) 00045 print >>sys.stderr, info_string 00046 00047 count = len(items) 00048 total_pages = int(math.ceil(count/rows)) if (count > 0) else 1 00049 total_pages = total_pages if (total_pages > 0) else 1 00050 00051 if (page > total_pages): 00052 page = total_pages 00053 start = (rows*page)-rows 00054 00055 start = 0 if (start < 0) else start 00056 00057 cells = [ func(r) if (callable(func)) else r for r in items[start:rows] ] 00058 results = [ {'cell' : c } for c in cells ] 00059 except Exception, details: 00060 results = [] 00061 count = _real_count(results) 00062 info_string = _utils.formattedException(details=details) 00063 print >>sys.stderr, info_string 00064 00065 ret = { 'page' : page, 00066 'total' : total_pages, 00067 'records' : count, 00068 'rows': results 00069 } 00070 return simplejson.dumps(ret) 00071 00072 00073
© Copyright 2008-2009 Vyper Logix Corp., All Right Reserved; If you reference this document or any part of this document you must use the citation verbatim (including the link) "© Copyright 2008-2009 Vyper Logix Corp., All Right Reserved."
Notice: This source code contained in this document is NOT open source and is NOT being distributed as open source.
122,241 lines of code and growing...