def listInstance(request): if request.GET.keys(): cols = request.GET.keys() else: cols = default_cols request.session['cols'] = cols # 获取所有的列名 mongoTool = MongoTool() allColumns = mongoTool.getAllColumnNames() return render_to_response('assets/list.html', RequestContext(request, locals()))
def fetchAll(request): if request.session.has_key('cols'): cols = request.session['cols'] else: cols = default_cols # 获取当前页码 page = 1 if request.GET.get("page"): try: page = int(request.GET.get("page")) except (TypeError, ValueError): page = 1 # 获取每页显示的记录条数 perPage = 10 if request.GET.get("perPage"): try: perPage = int(request.GET.get("perPage")) except (TypeError, ValueError): perPage = 10 offset = 0 if request.GET.get("offset"): try: offset = int(request.GET.get("offset")) except (TypeError, ValueError): offset = 0 # 获取排序条件 # fetchAll/?sorts[name]=1&page=1&perPage=10&offset=0 sort = [] for k in request.GET.keys(): if k.startswith("sorts["): if request.GET.get(k) == "1": sort.append((k[6:-1], pymongo.ASCENDING)) else: sort.append((k[6:-1], pymongo.DESCENDING)) # 获取查询字符串 queries = None for k in request.GET.keys(): if k.startswith("queries["): keyword = request.GET.get(k) # 拼mongo 查询条件语句,自动检索所有的字段 queries = {} tmp = [] for col in cols: tmp.append({col: {'$regex': keyword, '$options': 'i'}}) queries['$or'] = tmp mongoTool = MongoTool() data = mongoTool.getInstances(page=page, perPage=perPage, queries=queries, cols=cols, sorts=sort, offset=offset) return HttpResponse(content=json.dumps(data, default=json_serial))
def findAssets(request): if request.method == "POST": filters = {} if request.POST.get("filters"): filters = eval(request.POST.get("filters")) projections = None if request.POST.get("projections"): projections = eval(request.POST.get("projections")) mongoTool = MongoTool() data = mongoTool.findAssets(filters=filters, projections=projections) return HttpResponse(content=json.dumps(data, sort_keys=True, indent=4, separators=(',', ': '))) elif request.method == "GET": filters = {} if request.GET.get("filters"): filters = eval(request.GET.get("filters").encode("utf-8")) projections = None if request.GET.get("projections"): projections = eval(request.GET.get("projections").encode("utf-8")) mongoTool = MongoTool() data = mongoTool.findAssets(filters=filters, projections=projections) return HttpResponse(content=json.dumps(data, sort_keys=True, indent=4, separators=(',', ': ')))