def get_header_columns(self): from MCWidgets.Utils import html_dict_anchor_args cols = [] order = self._get_order_data() arg_key, arg_reverse = self.get_names_order_data() for col in self.get_arg('columns'): if order and col.has_key('key'): args = order['extra_args'].copy() args[arg_key] = col['key'] if order['key'] == col['key']: args[arg_reverse] = 'yn'[order['reverse'] == 'y'] html = '<img border="0" alt="" src="/staticmcw/ord_%s.png" />' % ( ['up', 'down'][order['reverse'] == 'y']) else: html = '' args[arg_reverse] = 'n' cols.append('<a href="%s?%s">%s%s</a>' % (order['url'], html_dict_anchor_args(args), html, html_escape(col['name']))) elif not col.has_key( 'widget') or col['widget']['type'] is not HiddenData: cols.append(html_escape(col['name'])) else: # es un HiddenData, pasamos de todo pass return cols
def render_table(self): import md5 key = self.get_arg('key') cols = self.get_arg('columns') source = dict([(s[key], s) for s in self.get_arg('source')]) table_id = self._get_table_name() s = '<table class="%s" id="%s">\n<tr>' % (self.get_arg( 'table_class', ''), table_id) for c in cols: s += '<th>%s</th>' % html_escape(c['name']) s += '<th> <!-- remove button --> </th> </tr>\n' for item in self.get_value(): row_id = table_id + '_row_' + md5.md5(str(item)).hexdigest() s += '<tr id="%s">' % row_id try: item = source[item] rowkey = item[key] for c in cols: s += '<td>%s</td>' % html_escape(str(item[c['key']])) except KeyError, (e, ): rowkey = str(item) s += "<td>%s <!-- the item, I did not convert it: Key %s --></td>" % ( item, html_escape(str(e))) s += ('<td><input type="hidden" name="%s" value="%s" /> ' + '<input type="button" value="-" ' + ' onclick="mcw_mv_delitem("%s")" />' + '</td></tr>\n') % (self.get_html_name(), html_escape(str(rowkey)), row_id)
def render_table(self): import md5 key = self.get_arg('key') cols = self.get_arg('columns') source = dict([(s[key], s) for s in self.get_arg('source')]) table_id = self._get_table_name() s = '<table class="%s" id="%s">\n<tr>' % (self.get_arg('table_class', ''), table_id) for c in cols: s += '<th>%s</th>' % html_escape(c['name']) s += '<th> <!-- remove button --> </th> </tr>\n' for item in self.get_value(): row_id = table_id + '_row_' + md5.md5(str(item)).hexdigest() s += '<tr id="%s">' % row_id try: item = source[item] rowkey = item[key] for c in cols: s += '<td>%s</td>' % html_escape(str(item[c['key']])) except KeyError, (e,): rowkey = str(item) s += "<td>%s <!-- the item, I did not convert it: Key %s --></td>" % (item, html_escape(str(e))) s += ('<td><input type="hidden" name="%s" value="%s" /> ' + '<input type="button" value="-" ' + ' onclick="mcw_mv_delitem("%s")" />' + '</td></tr>\n') % ( self.get_html_name(), html_escape(str(rowkey)), row_id)
def get_header_columns(self): from MCWidgets.Utils import html_dict_anchor_args cols = [] order = self._get_order_data() arg_key, arg_reverse = self.get_names_order_data() for col in self.get_arg('columns'): if order and col.has_key('key'): args = order['extra_args'].copy() args[arg_key] = col['key'] if order['key'] == col['key']: args[arg_reverse] = 'yn'[order['reverse'] == 'y'] html = '<img border="0" alt="" src="/staticmcw/ord_%s.png" />' % ( ['up', 'down'][order['reverse'] == 'y']) else: html = '' args[arg_reverse] = 'n' cols.append('<a href="%s?%s">%s%s</a>' % ( order['url'], html_dict_anchor_args(args), html, html_escape(col['name']))) elif not col.has_key('widget') or col['widget']['type'] is not HiddenData: cols.append(html_escape(col['name'])) else: # es un HiddenData, pasamos de todo pass return cols
def render_input(self): s = '<select id="%s">\n' % self._get_input_name() key = self.get_arg('key') tpl = self.get_arg('option_template') for item in self.get_arg('source'): s += ' <option value="%s">%s</option>\n' % (html_escape( str(item[key])), html_escape(tpl % item)) return s + '</select>\n'
def buildFormFields(fields): r = '' for name, values in fields.items(): if not isinstance(values, list): values = [values] n = html_escape(name) for v in values: r += '<input type="hidden" name="%s" value="%s" />' % (n, html_escape(v)) return r
def render_input(self): s = '<select id="%s">\n' % self._get_input_name() key = self.get_arg('key') tpl = self.get_arg('option_template') for item in self.get_arg('source'): s += ' <option value="%s">%s</option>\n' % ( html_escape(str(item[key])), html_escape(tpl % item)) return s + '</select>\n'
def buildFormFields(fields): r = '' for name, values in fields.items(): if not isinstance(values, list): values = [values] n = html_escape(name) for v in values: r += '<input type="hidden" name="%s" value="%s" />' % ( n, html_escape(v)) return r
def html_attribute(name, value): '''html_attribute(name, value) -> str Devuelve una cadena representando el atributo HTML de los valores indicados. Escapa caracteres que puedan ser conflictivos. Si value es None, devolverá sólo el nombre del atributo.''' if value is None: return html_escape(name) return '%s="%s"' % (html_escape(name), html_escape(value))
def _tpl_make(tpl): return (tpl.replace( '%DELROW%', html_escape(self.calljs_delrow(numrows))).replace( '%N', str(numrows)).replace('%R', tr_id).replace('%I', base_id))
def html_dict_anchor_args(args): '''html_dict_anchor_args(args) -> str Crea una cadena para usar en los argumentos del anchor, a partir del diccionario pasado. ''' new = [] for key,value in args.items(): if isinstance(value, list): for v in value: new.append('%s=%s' % (key, uri_escape(v))) else: new.append('%s=%s' % (key, uri_escape(value))) return html_escape('&'.join(new))
def render(self): options = [] selected = self.get_value() ho = self.get_arg('headoption', None) if ho is not None and not selected: options.append(' <option value="null" style="font-style: italic" disabled selected>%s</option>' % ho) for value, text in self.get_arg('options', []): s = '' if value in selected: s = ' selected' options.append(' <option value="%s"%s>%s</option>' % (html_escape(value), s, html_escape(text))) from MCWidgets.Utils import html_dict_attrs as hda result = '<select %s>%s</select>' % ( hda(self.get_calc_html_attrs()), '\n'.join(options)) if self.get_arg('tabindex', None) == 1: result += '<script>document.getElementById("%s").focus()</script>' % self.get_html_id() return result
def html_dict_anchor_args(args): '''html_dict_anchor_args(args) -> str Crea una cadena para usar en los argumentos del anchor, a partir del diccionario pasado. ''' new = [] for key, value in args.items(): if isinstance(value, list): for v in value: new.append('%s=%s' % (key, uri_escape(v))) else: new.append('%s=%s' % (key, uri_escape(value))) return html_escape('&'.join(new))
def render_button_add(self, label = 'Add'): return '<input type="button" value="%s" onclick="%s" />' % ( label, html_escape(self.calljs_addrow()))
def buildSelectFromList(options): r = '<select>' for opt in options: r += '<option>%s</option>' % html_escape(opt) return r + '</select>'
def render_button(self): return '<input type="button" value="%s" onclick="%s" />' % ( self.get_arg('label_button', 'Add'), html_escape(self.calljs_additem()))
def calljs_value_to_add(self): return 'document.getElementById("%s").value' % html_escape(self._get_input_name())
def render(self): from cStringIO import StringIO out = StringIO() base_id = self.get_html_id() numrows = 0 # Ver si queremos ejecutar una expesión cuando cambian las filas if self.get_arg('on_rows_change', None): out.write('<script>var %s_js_rows_change = %s;</script>' % ( self.get_html_id(), json.write(self.get_arg('on_rows_change')))) # Obtener las columnas. Si es un listado ordenable, añadimos la columa # con las flechas para subir y bajar filas columns = self.get_arg('columns') if self.get_arg('orderable'): columns = [{ 'name': '', 'template': '<span style="white-space: nowrap">' '<input type="image" src="/staticmcw/up.png" onclick="try{mcw_lmc_moverow(this, -1);}catch(e){}; return false;" />' '<input type="image" src="/staticmcw/down.png" onclick="try{mcw_lmc_moverow(this, 1);}catch(e){}; return false;" />' '</span>' }] + columns from MCWidgets.Utils import html_dict_attrs, html_dict_anchor_args out.write('<table %s>\n' % html_dict_attrs(self.get_calc_html_attrs())) rows_per_page = self.get_arg('rows_per_page', 0) formvalues = self.get_form()._get_raw_form_values() if rows_per_page > 0: order = self._get_order_data() args = order['extra_args'] page = int(formvalues.get(self._make_var_page(), 0)) args[self._make_var_page()] = str(page) # Coopiar la ordenación args = args.copy() arg_key, arg_reverse = self.get_names_order_data() args[arg_key] = order['key'] args[arg_reverse] = order['reverse'] out.write('<tr><td align="right" colspan="%d">' % len(columns)) num_pags = len(self.get_arg('data')) / rows_per_page if page > 0: args[self._make_var_page()] = str(page - 1) out.write('<a href="%s?%s">Anterior</a> ' % (order['url'], html_dict_anchor_args(args))) if page < (num_pags-1): args[self._make_var_page()] = str(page + 1) out.write('<a href="%s?%s">Siguiente</a>' % (order['url'], html_dict_anchor_args(args))) args[self._make_var_page()] = 'xxNUMxPAGExMCWxLMCxx' out.write(' <select onchange="location.href="%s?" + %s.replace("xxNUMxPAGExMCWxLMCxx", this.value-1)">' % ( order['url'], json.write(html_dict_anchor_args(args)))) for n in range(num_pags): out.write('<option') if n == page: out.write(' selected') out.write('>%s</option>' % (n+1)) out.write('</select></td></tr>') # Cabeceras out.write('<tr class="' + self.get_arg('header_style', '') + '">') if self.get_arg('orderable'): out.write('<th> </th>') for col in self.get_header_columns(): out.write('<th>' + col + '</th>') out.write('</tr>\n') # Filas data = self.get_arg('data') out.write('<input type="hidden" name="%(c)s" id=%(c)s value="%(n)d" />' % {'c': self.get_counter_name(), 'n': len(data)+1}) # Vemos si tenemos que reordenar las filas order = self._get_order_data() if order: data = data[:] key = order['key'] # TODO Si la columna tiene un translate, usar éste para ordenar # en lugar de directamente el valor data.sort(lambda a,b: cmp(a[key], b[key])) if order['reverse'] == 'y': data.reverse() # Miramos si queremos paginar if rows_per_page > 0: page = int(formvalues.get(self._make_var_page(), 0)) data = data[rows_per_page*page:rows_per_page*(page+1)] out.write('<input type="hidden" name="%s" value="%s" />' % ( self._make_var_page(), page)) for row in data: numrows += 1 tr_id = "%s_row_%d" % (base_id, numrows) out.write('<tr id="%s">' % tr_id) # Usaremos esta función para hacer las sustituciones necesarias # para las plantillas def _tpl_make(tpl): return (tpl. replace('%DELROW%', html_escape(self.calljs_delrow(numrows))). replace('%N', str(numrows)). replace('%R', tr_id). replace('%I', base_id)) tds = [] hid_cols = "" for col in columns: # TODO: Sacar a un método aparte el algoritmo para sacar el valor # de cada fila, # Comprobar si la columna se genera por una clausura o directamente # desde el valor de la lista if col.has_key('call'): val = col['call'](row) elif col.has_key('call_template'): val = _tpl_make(col['call_template'](row)) elif not col.has_key('key'): val = _tpl_make(col['template']) else: val = row[col['key']] if type(val) is mxDateTime.DateTimeType: val = val.strftime('%d/%m/%Y') elif type(val) is mxDateTime.DateTimeDeltaType: val = val.strftime('%H:%M') elif val is None: val = '-' else: val = str(val) if col.has_key('translate'): val = col['translate'].get(val, val) if col.get('html_escape', True): val = html_escape(val) if col.get('as_template', False): val = _tpl_make(val) if col.has_key('widget') and col['widget']['type'] is HiddenData: hid_cols += val else: tds.append(val) out.write('<td>'+'</td><td>'.join(tds[:-1])+'</td>') out.write('<td>'+tds[-1]+hid_cols+'</td>') out.write('</tr>\n') # Procesamos las columnas para poner los hiddendata al final en lo que le pasamos al calljs_addrow hid_cols = '' tds = [] for col in columns: val = col.get('template', '-empty-') if col.has_key('widget') and col['widget']['type'] is HiddenData: hid_cols += val else: tds.append(val) if hid_cols: tds[-1] += hid_cols # Template columns out.write(Utils.buildScript('var %s = %s ;' % ( self.varjs_columns(), json.write(tds)))) return out.getvalue() + '</table>\n'
def render_input(self): return'<input onkeypress="return mcw_mv_keydown(this, event, function(){%s})" id="%s" value="" />' % ( (html_escape(self.calljs_additem()), self._get_input_name()))
def calljs_value_to_add(self): return 'document.getElementById("%s").value' % html_escape( self._get_input_name())
def render_button_add(self, label='Add'): return '<input type="button" value="%s" onclick="%s" />' % ( label, html_escape(self.calljs_addrow()))
def _tpl_make(tpl): return (tpl. replace('%DELROW%', html_escape(self.calljs_delrow(numrows))). replace('%N', str(numrows)). replace('%R', tr_id). replace('%I', base_id))
def render_input(self): return '<input onkeypress="return mcw_mv_keydown(this, event, function(){%s})" id="%s" value="" />' % ( (html_escape(self.calljs_additem()), self._get_input_name()))
def render_filter(self): return '<input size="40" onkeypress="mcw_lmc_filter_keypress(this, %s)" />' % \ html_escape(json.write(self.get_html_id()))
def render(self): from cStringIO import StringIO out = StringIO() base_id = self.get_html_id() numrows = 0 # Ver si queremos ejecutar una expesión cuando cambian las filas if self.get_arg('on_rows_change', None): out.write('<script>var %s_js_rows_change = %s;</script>' % (self.get_html_id(), json.write(self.get_arg('on_rows_change')))) # Obtener las columnas. Si es un listado ordenable, añadimos la columa # con las flechas para subir y bajar filas columns = self.get_arg('columns') if self.get_arg('orderable'): columns = [{ 'name': '', 'template': '<span style="white-space: nowrap">' '<input type="image" src="/staticmcw/up.png" onclick="try{mcw_lmc_moverow(this, -1);}catch(e){}; return false;" />' '<input type="image" src="/staticmcw/down.png" onclick="try{mcw_lmc_moverow(this, 1);}catch(e){}; return false;" />' '</span>' }] + columns from MCWidgets.Utils import html_dict_attrs, html_dict_anchor_args out.write('<table %s>\n' % html_dict_attrs(self.get_calc_html_attrs())) rows_per_page = self.get_arg('rows_per_page', 0) formvalues = self.get_form()._get_raw_form_values() if rows_per_page > 0: order = self._get_order_data() args = order['extra_args'] page = int(formvalues.get(self._make_var_page(), 0)) args[self._make_var_page()] = str(page) # Coopiar la ordenación args = args.copy() arg_key, arg_reverse = self.get_names_order_data() args[arg_key] = order['key'] args[arg_reverse] = order['reverse'] out.write('<tr><td align="right" colspan="%d">' % len(columns)) num_pags = len(self.get_arg('data')) / rows_per_page if page > 0: args[self._make_var_page()] = str(page - 1) out.write('<a href="%s?%s">Anterior</a> ' % (order['url'], html_dict_anchor_args(args))) if page < (num_pags - 1): args[self._make_var_page()] = str(page + 1) out.write('<a href="%s?%s">Siguiente</a>' % (order['url'], html_dict_anchor_args(args))) args[self._make_var_page()] = 'xxNUMxPAGExMCWxLMCxx' out.write( ' <select onchange="location.href="%s?" + %s.replace("xxNUMxPAGExMCWxLMCxx", this.value-1)">' % (order['url'], json.write(html_dict_anchor_args(args)))) for n in range(num_pags): out.write('<option') if n == page: out.write(' selected') out.write('>%s</option>' % (n + 1)) out.write('</select></td></tr>') # Cabeceras out.write('<tr class="' + self.get_arg('header_style', '') + '">') if self.get_arg('orderable'): out.write('<th> </th>') for col in self.get_header_columns(): out.write('<th>' + col + '</th>') out.write('</tr>\n') # Filas data = self.get_arg('data') out.write( '<input type="hidden" name="%(c)s" id=%(c)s value="%(n)d" />' % { 'c': self.get_counter_name(), 'n': len(data) + 1 }) # Vemos si tenemos que reordenar las filas order = self._get_order_data() if order: data = data[:] key = order['key'] # TODO Si la columna tiene un translate, usar éste para ordenar # en lugar de directamente el valor data.sort(lambda a, b: cmp(a[key], b[key])) if order['reverse'] == 'y': data.reverse() # Miramos si queremos paginar if rows_per_page > 0: page = int(formvalues.get(self._make_var_page(), 0)) data = data[rows_per_page * page:rows_per_page * (page + 1)] out.write('<input type="hidden" name="%s" value="%s" />' % (self._make_var_page(), page)) for row in data: numrows += 1 tr_id = "%s_row_%d" % (base_id, numrows) out.write('<tr id="%s">' % tr_id) # Usaremos esta función para hacer las sustituciones necesarias # para las plantillas def _tpl_make(tpl): return (tpl.replace( '%DELROW%', html_escape(self.calljs_delrow(numrows))).replace( '%N', str(numrows)).replace('%R', tr_id).replace('%I', base_id)) tds = [] hid_cols = "" for col in columns: # TODO: Sacar a un método aparte el algoritmo para sacar el valor # de cada fila, # Comprobar si la columna se genera por una clausura o directamente # desde el valor de la lista if col.has_key('call'): val = col['call'](row) elif col.has_key('call_template'): val = _tpl_make(col['call_template'](row)) elif not col.has_key('key'): val = _tpl_make(col['template']) else: val = row[col['key']] if type(val) is mxDateTime.DateTimeType: val = val.strftime('%d/%m/%Y') elif type(val) is mxDateTime.DateTimeDeltaType: val = val.strftime('%H:%M') elif val is None: val = '-' else: val = str(val) if col.has_key('translate'): val = col['translate'].get(val, val) if col.get('html_escape', True): val = html_escape(val) if col.get('as_template', False): val = _tpl_make(val) if col.has_key( 'widget') and col['widget']['type'] is HiddenData: hid_cols += val else: tds.append(val) out.write('<td>' + '</td><td>'.join(tds[:-1]) + '</td>') out.write('<td>' + tds[-1] + hid_cols + '</td>') out.write('</tr>\n') # Procesamos las columnas para poner los hiddendata al final en lo que le pasamos al calljs_addrow hid_cols = '' tds = [] for col in columns: val = col.get('template', '-empty-') if col.has_key('widget') and col['widget']['type'] is HiddenData: hid_cols += val else: tds.append(val) if hid_cols: tds[-1] += hid_cols # Template columns out.write( Utils.buildScript('var %s = %s ;' % (self.varjs_columns(), json.write(tds)))) return out.getvalue() + '</table>\n'
def render(self): from MCWidgets.Utils import html_dict_attrs return '<textarea %s />%s</textarea>' % ( html_dict_attrs(self.get_calc_html_attrs()), html_escape(self.get_value()))