我是Python,MongoDB(mongoengine(ODM))和金字塔框架的新手。我目前正在使用上述技术开发一个项目,我想使用jQuery datatables(也是新的)我在他们的网站上找到了如何使用datatables with MongoDB的链接,但它是在php中而且我的翻译技巧也不是那么好。
我的问题是:
是否可以使用上述技术使用数据表,如果是,那么如何?
我已经尝试将使用SQLAlchemy的其他人的代码转换为数据表,但我不知道如何更改该功能。
@view_config(
route_name='candidate.list.json',
renderer='json',
permission="admin"
)
def candidate_list_json(context, request):
def format_output(vals):
vals = list(vals)
vals[-1] = """<div class="btn-group"><a href='%s' class=btn>View</a><a href='%s' class=btn>Edit</a></div>""" % (
request.route_url('candidates', id_number=vals[-1], traverse=()),
request.route_url('candidates', id_number=vals[-1], traverse="edit")
)
vals[0] = "<a href='%s'>%s</a>" % (request.route_url('candidates', id_number=vals[0], traverse=()), vals[0])
return vals
if has_permission('admin', context, request):
basefilt = None # I changed Up to here
return handle_datatable(
request,
Agents.id,
[Agents.db_agent_id, Agents.db_name, Agents.id_number, Agents.mobile_number, OrgUnits.name, Agents.db_agent_id],
lambda term: or_(Agents.db_agent_name.like('%'+term+'%'), OrgUnits.name.like('%'+term+'%'), Agents.mobile_number.like('%'+term+'%'), Agents.id_number.like('%'+term+'%'), ),
join=[Agents.ou],
formatfunc=format_output,
base_filt=basefilt
)
handle_datatable
是一种方法:
def handle_datatable(request, idcol, cols, filtfunc, options=None, cache='short_term', formatfunc=None, displaylength=90, join=None, base_filt=None, outerjoin=None, groupby=None, no_paginate=False, ordercols=None, orderby=None, printquery=False, nocount=False):
s = sqlahelper.get_session()
if groupby is not None and type(groupby) != list and type(groupby) != tuple:
groupby = [groupby]
def attachfilt(q, filt, nogroup=False):
if filt:
q = q.filter(and_(*filt))
if join:
q = q.join(*join)
if outerjoin:
q = q.outerjoin(*outerjoin)
if options:
q = q.options(*options)
if groupby and not nogroup:
for g in groupby:
q = q.group_by(g)
return q
@cache_region('short_term')
def perform_count(filt, idcol, term):
if not nocount:
return attachfilt(s.query(idcol), filt, nogroup=False).count()
else:
return 0
#@cache_region('short_term', 'handle_search')
def perform_search(filt, cols, iStart, iLength, order_cols):
q = attachfilt(s.query(*cols), filt)
if order_cols:
q = q.order_by(*order_cols)
if printquery:
print q
if no_paginate:
rows = q.all()
else:
rows = q[iStart:iStart+iLength]
if callable(formatfunc):
data = [formatfunc(row[:]) for row in rows]
else:
data = [row[:] for row in rows]
return data
if not callable(filtfunc):
raise Exception("Filter Function is not callable")
if not cols:
raise Exception("Please provide columns to search")
if not no_paginate:
iStart = int(str(request.params.get("iDisplayStart", 0)))
iLength = int(str(request.params.get("iDisplayLength", displaylength)))
else:
iStart = 0
iLength = 0
if not ordercols:
ordercols = cols
if orderby:
order_cols = orderby
else:
order_cols = []
if request.params.get("iSortCol_0", None):
iSortingCols = int(str(request.params.get('iSortingCols', 0)))
for k in range(0, iSortingCols):
iSortCol = int(str(request.params.get('iSortCol_%s' % k, 0)))
sSortDir = str(request.params.get('sSortDir_%s' % k, 0))
if str(request.params.get('bSortable_%s' % iSortCol, 'false') == 'true'):
col = ordercols[iSortCol]
if sSortDir == "asc":
order_cols.append(col.asc())
else:
order_cols.append(col.desc())
search = request.params.get("sSearch", None)
filt = []
if search:
filt = filtfunc(search)
if filt is not None and type(filt) != list:
filt = [filt]
if type(cols) != list:
cols = [cols]
itotal = perform_count([base_filt], idcol, search)
if no_paginate:
iLength = itotal
if base_filt is not None:
filt.append(base_filt)
cnt = perform_count(filt, idcol, search)
data = perform_search(filt, cols, iStart, iLength, order_cols)
return dict(
sEcho=request.params.get("sEcho", 0),
iTotalRecords=itotal,
iTotalDisplayRecords=cnt,
aaData=data
)
正如我之前所说,我是这些技术的新手,但我愿意学习,如果你能指出我正确的方向。
更新
所以现在我得到了这段代码:
import pymongo
from pyramid_mongo import get_db
# translation for sorting between datatables api and mongodb
order_dict = {'asc': 1, 'desc': -1}
class DataTables_Handler(object):
def __init__(self, request, columns, index, collection):
self.columns = columns
self.index = index
self.db = get_db(request)
self.collection = collection
# values specified by the datatable for filtering, sorting, paging
self.request_values = request.params
# results from the db
self.result_data = None
# total in the table after filtering
self.cardinality_filtered = 0
# total in the table unfiltered
self.cadinality = 0
self.run_queries()
def output_result(self):
output = {}
output['sEcho'] = str(int(self.request_values['sEcho']))
output['iTotalRecords'] = str(self.cardinality)
output['iTotalDisplayRecords'] = str(self.cardinality_filtered)
aaData_rows = []
for row in self.result_data:
aaData_row = []
for i in range(len(self.columns)):
aaData_row.append(row[self.columns[i]].replace('"', '\\"'))
# add additional rows here that are not represented in the database
# aaData_row.append(('''<input id='%s' type='checkbox'></input>''' % (str(row[ self.index ]))).replace('\\', ''))
aaData_rows.append(aaData_row)
output['aaData'] = aaData_rows
return output
def run_queries(self):
# pages has 'start' and 'length' attributes
pages = self.paging()
# the term you entered into the datatable search
filters = self.filtering()
# the document field you chose to sort
sorting = self.sorting()
# get result from db
self.result_data = self.db.self.collection.find(spec=filters,
skip=pages.start,
limit=pages.length,
sort=sorting)
total_count = len(list(self.db.self.collection.find(spec=filters)))
self.result_data = list(self.result_data)
self.cardinality_filtered = total_count
self.cardinality = len(list(self.db.self.collection.find()))
def filtering(self):
# build your filter spec
filters = {}
if (self.request_values.has_key('sSearch')) and (self.request_values['sSearch'] != ""):
# the term put into search is logically concatenated with 'or' between all columns
or_filter_on_all_columns = []
for i in range(len(self.columns)):
column_filter = {}
column_filter[self.columns[i]] = {'$regex': self.request_values['sSearch'], '$options': 'i'}
or_filter_on_all_columns.append(column_filter)
filters['$or'] = or_filter_on_all_columns
return filters
def sorting(self):
order = []
# mongo translation for sorting order
if (self.request_values['iSortCol_0'] != "") and (self.request_values['iSortingCols'] > 0):
order = []
for i in range(int(self.request_values['iSortingCols'])):
order.append((self.columns[int(self.request_values['iSortCol_' + str(i)])], order_dict[self.request_values['sSortDir_' + str(i)]]))
return order
def paging(self):
pages = namedtuple('pages', ['start', 'length'])
if (self.request_values['iDisplayStart'] != "") and (self.request_values['iDisplayLength'] != -1):
pages.start = int(self.request_values['iDisplayStart'])
pages.length = int(self.request_values['iDisplayLength'])
return pages
在视图中使用此代码:
@view_config(
route_name='candidates.list.json',
renderer='json',
permission='admin'
)
def candidate_list_json(context, request):
columns = [ 'id_number', 'first_name', 'email', 'mobile_number']
index_column = "id_number"
collection = "candidates"
results = DataTables_Handler(request, columns, index_column, collection).output_result()
# return the results as a string for the datatable
return {"results": results}
,这在模板中:
<a id="btn-addteam" class="btn btn-success" href="{{'add_candidate'|route_url}}"><i class="icon-plus"></i> Add Candidate</a>
身份证号码
候选人名字
候选人的电子邮件
手机号码
健康 - &gt;
<script src="{{'kivu:static/datatables/jquery.dataTables.js'|static_url}}"></script>
<script src="{{'kivu:static/datatables/dataTables.scroller.js'|static_url}}"></script>
<script>
$(document).ready(function() {
url = "{{'candidates.list.json'|route_url}}";
var oTable = $('#candidate_search').dataTable( {
"bProcessing": true,
"bServerSide": true,
"sPaginationType": "full_numbers",
"bjQueryUI": true,
"sAjaxSource": url
});
} );</script>
但是它给了我一个js错误:
TypeError: aData is undefined
for ( var i=0, iLen=aData.length ; i<iLen ; i++ )
我的GET响应返回:
{"results": {"aaData": [], "iTotalRecords": "0", "sEcho": "1", "iTotalDisplayRecords": "0"}}
数据库中有数据。我错过了什么?我希望这个更新不是主题......
答案 0 :(得分:1)
当然可以使用您提到的技术使用jQuery Datatables。 Pyramid处理请求并管理数据库连接,将请求处理成查询并将任何查询结果或其他数据汇编到Response中,或返回给予渲染器的数据,这些渲染器支持用Chameleon或Mako编写的模板。您可以将jQuery DataTable添加到模板中 - 这是从鸟瞰图中看到的。
您至少有两种方法可以将数据传递到DataTables
在视图中汇总查询结果,使用变色龙模板(http://docs.pylonsproject.org/projects/pyramid/en/1.5-branch/quick_tutorial/templating.html)将数据返回到渲染器,使用TAL迭代数据并生成有效的DataTables HTML,如DataTables docs所示
如果您完成版本1,可以选择切换到AJAX。然后在Javascript中创建DataTables标记,并使用Pyramid JSON渲染器将JSON响应返回给您的AJAX客户端,最有可能使用jQuery。 (http://docs.pylonsproject.org/projects/pyramid/en/1.5-branch/quick_tutorial/json.html)
你的例子混淆了过多的顾虑因为复杂性而失败了。采取任何官方金字塔教程 - 你几乎找不到任何难以阅读和理解的片段。你很难阅读和理解 - 至少对我而言。除此之外,它无论如何都是不可测试的。金字塔首发应遵循所示的发展风格。金字塔真的让Unit&amp;功能测试很容易。
遵循本教程 - 值得花时间:http://docs.pylonsproject.org/projects/pyramid/en/1.5-branch/quick_tutorial/index.html
答案 1 :(得分:1)
所以我开始工作:我修改了这个script:
我在__init__.py
:
from collections import namedtuple
import pymongo
# translation for sorting between datatables api and mongodb
order_dict = {'asc': 1, 'desc': -1}
class DataTables_Handler(object):
def __init__(self, request, columns, index, collection):
self.columns = columns
self.index = index
#self.db = get_db(request)
self.collection = collection
# values specified by the datatable for filtering, sorting, paging
self.request_values = request.params
self.db = pymongo.MongoClient()
# results from the db
self.result_data = None
# total in the table after filtering
self.cardinality_filtered = 0
# total in the table unfiltered
self.cadinality = 0
self.run_queries()
def output_result(self):
output = {}
output['sEcho'] = str(int(self.request_values['sEcho']))
output['iTotalRecords'] = str(self.cardinality)
output['iTotalDisplayRecords'] = str(self.cardinality_filtered)
aaData_rows = []
#print "sEcho", output['sEcho']
for row in self.result_data:
print row
aaData_row = []
for i in range(len(self.columns)):
aaData_row.append(row[self.columns[i]])
#print aData_row
#add additional rows here that are not represented in the database
aaData_row.append(('''<input id='%s' type='checkbox'></input>''' % (str(row[ self.index ]))).replace('\\', ''))
aaData_rows.append(aaData_row)
output['aaData'] = aaData_rows
print "output: ", output
return output
def run_queries(self):
#call db
db = self.db.mydb
# pages has 'start' and 'length' attributes
pages = self.paging()
# the term you entered into the datatable search
filters = self.filtering()
# the document field you chose to sort
sorting = self.sorting()
# get result from db
self.result_data = db[self.collection].find(spec=filters,
skip=pages.start,
limit=pages.length,
sort=sorting)
total_count = len(list(db[self.collection].find(spec=filters)))
self.result_data = list(self.result_data)
print "result_data", self.result_data
self.cardinality_filtered = total_count
self.cardinality = len(list(db[self.collection].find()))
print "cardinality", self.cardinality
def filtering(self):
# build your filter spec
filters = {}
if ('sSearch' in self.request_values.keys()) and (self.request_values['sSearch'] != ""):
# the term put into search is logically concatenated with 'or' between all columns
or_filter_on_all_columns = []
for i in range(len(self.columns)):
column_filter = {}
column_filter[self.columns[i]] = {'$regex': self.request_values['sSearch'], '$options': 'i'}
or_filter_on_all_columns.append(column_filter)
filters['$or'] = or_filter_on_all_columns
return filters
def sorting(self):
order = []
# mongo translation for sorting order
if (self.request_values['iSortCol_0'] != "") and (self.request_values['iSortingCols'] > 0):
order = []
for i in range(int(self.request_values['iSortingCols'])):
order.append((self.columns[int(self.request_values['iSortCol_' + str(i)])], order_dict[self.request_values['sSortDir_' + str(i)]]))
return order
def paging(self):
pages = namedtuple('pages', ['start', 'length'])
if (self.request_values['iDisplayStart'] != "") and (self.request_values['iDisplayLength'] != -1):
pages.start = int(self.request_values['iDisplayStart'])
pages.length = int(self.request_values['iDisplayLength'])
return pages
在我的View.py
我有:
@view_config(
route_name='candidates.list.json',
renderer='json',
permission='admin'
)
def candidate_list_json(context, request):
columns = [ '_id', 'first_name', 'email', 'mobile_number']
index_column = "_id"
collection = "candidates"
results = DataTables_Handler(request, columns, index_column, collection).output_result()
print "Results: ", results
# return the results as a string for the datatable
return results
然后在我的模板中我有:
<script src="{{'kivu:static/datatables/jquery.dataTables.js'|static_url}}"></script>
<script src="{{'kivu:static/datatables/dataTables.scroller.js'|static_url}}"></script>
<script>
url = "{{'candidates.list.json'|route_url}}";
$(document).ready(function() {
var oTable = $('#candidate_search').dataTable( {
"sScrollY": "425px",
// "sPaginationType": "full_numbers",
"sAjaxSource": url,
"bServerSide": true,
"sDom": "<'row'<'col-xs-6 col-md-6 col-lg-6'><'col-xs-6 col-md-6 col-lg-6'f>r>t<'row'<'col-xs-6 col-md-6 col-lg-6'i><'col-xs-6 col-md-6 col-lg-6'>>S",
//"sDom": "frtiS",
//"sPaginationType": "bootstrap",
"bProcessing" : true,
// "aoColumnDefs": [{ "bSortable": false, "aTargets": [ 5 ] }],
"bDeferRender": true,
"bStateSave": true
} );
console.log(oTable);
$.extend( $.fn.dataTableExt.oStdClasses, {
"sSortAsc": "header headerSortDown",
"sSortDesc": "header headerSortUp",
"sSortable": "header"
} );
$.extend( $.fn.dataTableExt.oStdClasses, {
"sWrapper": "dataTables_wrapper form-inline"
} );
} );</script>
我希望这可以帮助某人,因为class DataTables_Handler
可以重复使用。