diff --git a/paramecio/citoplasma/lists.py b/paramecio/citoplasma/lists.py index 5be1115..6c6ab4b 100644 --- a/paramecio/citoplasma/lists.py +++ b/paramecio/citoplasma/lists.py @@ -8,6 +8,7 @@ from paramecio.citoplasma.httputils import GetPostFiles from bottle import request import sys import re +from paramecio.citoplasma.pages import Pages class SimpleList: @@ -232,9 +233,81 @@ class SimpleList: class AjaxList(SimpleList): - def __init__(self, model, url, ajax_url, t): + # Fields example: [[I18n.lang('cuchulu', 'hostname', 'Hostname'), True], ['IP', True], [I18n.lang('cuchulu', 'options', 'Options'), False]] + + # arr_order_fields=['server.hostname', 'server.ip'] + + # 'select count(servercloud.id) as num_elements from servercloud where servercloud.user_id=%s' + # params count_query [s['cu_id']] + + # str_query no order, no limit -> select server.hostname, server.ip, servercloud.id from server, servercloud where server.id=servercloud.server_id and servercloud.user_id=%s + # str_query_params -> [s['cu_id'], begin_page, limit] + + def __init__(self, db, fields, arr_order_fields, count_query, str_query): - pass + self.fields=fields + self.arr_order_fields=arr_order_fields + self.limit=20 + self.count_query=count_query[0] + self.count_query_params=count_query[1] + self.str_query=str_query[0] + self.str_query_params=str_query[1] + self.initial_num_pages=20 + self.db=db + + def show(self): + + p=GetPostFiles() + + p.obtain_query() + + begin_page=int(p.query.get('position', 0)) + order_field=p.query.get('order_field', '') + order=p.query.get('order', 0) + limit=self.limit + + arr_order=['ASC', 'DESC'] + + order_sql='' + order_params=[] + + if order_field!='': + try: + order_field=int(order_field) + order=int(order) + + if order_field>=0 and order_field=0 and order<2: + order_sql+=' %s' % arr_order[order] + + #order_params=[self.arr_order_fields[order_field]] + + except: + order_field=0 + order=0 + + + rows=[] + + with self.db.query(self.count_query, self.count_query_params) as cursor: + total_elements=cursor.fetchone()['num_elements'] + + str_query=self.str_query+' '+order_sql+' limit %s, %s' + + params=self.str_query_params + params.append(begin_page) + params.append(limit) + + with self.db.query(str_query, params) as cursor: + for row in cursor: + rows.append(row) + + pages=Pages() + + html_pages=pages.show( begin_page, total_elements, limit, '#' ,initial_num_pages=self.initial_num_pages, variable='begin_page', label='', func_jscript='') + + return {'fields': self.fields, 'rows': rows, 'html_pages': I18n.lang('cuchulu', 'pages', 'Pages')+': '+html_pages} diff --git a/paramecio/citoplasma/pages.py b/paramecio/citoplasma/pages.py index 8f7fc76..d0f34d8 100644 --- a/paramecio/citoplasma/pages.py +++ b/paramecio/citoplasma/pages.py @@ -54,7 +54,7 @@ class Pages: middle_link=add_get_parameters(link, **{variable: str(x)} ) num_page=ceil(x/num_elements)+1; - arr_pages[x]=""+str(num_page)+" " + arr_pages[x]=""+str(num_page)+" " arr_pages[begin_page]=''+str(num_page)+' '; pages += arr_pages[x]