def theme_select_autocomplete(self, form_id, element): assets.activate_jquery(self.req) self.req.content.report('header', tags.script(type="text/javascript", src=self.req.get_path("/assets/jquery/jquery.autocomplete.js"))['']) self.req.content.report('header', tags.script(type="text/javascript", src=self.req.get_path("/assets/editable-autocomplete.js"))['']) self.req.content.report('header', tags.style(type="text/css")[ """@import '%s';""" % self.req.get_path('/assets/jquery/jquery.autocomplete.css')]) self.req.content.report('header', tags.script(type="text/javascript")[ """ function formatItem(item, index, totalItems){ return item[0].replace('<', '<').replace('>', '>') } """ ]) ac_id = '%s-%s-autocomplete' % (form_id, element.name) ac_cb_id = '%s-%s-ac-callback' % (form_id, element.name) options = element.attr('options', []) optlist = repr([[v, k] for k, v in options.items()]) prefs = dict( autoFill = 1, selectFirst = 1, matchSubset = 0, selectOnly = 1, formatItem = 'formatItem', ) prefs = ','.join(['%s:%s' % (k, v) for k, v in prefs.items()]) ac_javascript = tags.script(type='text/javascript')[ '$("#%s").autocompleteArray(%s, {onItemSelect:select_item("%s"), %s});' % (ac_id, optlist, ac_cb_id, prefs) ] value = element.attr('value') if(value): label = options.get(value, None) if(label is None): label = value output = tags.input(type="text", name=element.get_element_name() + '[ac]', id=ac_id, value=label) output += tags.input(type="hidden", name=element.get_element_name() + '[value]', id=ac_cb_id, value=value) else: output = tags.input(type="text", name=element.get_element_name() + '[ac]', id=ac_id) output += tags.input(type="hidden", name=element.get_element_name() + '[value]', id=ac_cb_id) output += ac_javascript return output
def theme_date(self, form_id, element): import time attribs = element.attr('attributes', {}) months, days = date.get_date_arrays() value = element.attr('value', None) if(value is None): year = datetime.datetime.now().year month, day = (months[0], 1) else: if(isinstance(value, (int, float))): value = datetime.datetime.utcfromtimestamp(value) month, day, year = date.strftime(value, '%B:%d:%Y').split(':') arrays = (months, days) values = (month, int(day)) names = ('month', 'day') monthday = self._generate_datetime_select(element, attribs, arrays, values, names) yearfield = '%s[year]' % element.get_element_name() return str(monthday) + str(tags.input(type='text', size=5, value=year, name=yearfield))
def theme_datetime(self, form_id, element): import time attribs = element.attr('attributes', {}) months, days = date.get_date_arrays() hours, minutes = date.get_time_arrays() #value = date.convert_to_timestamp(element.attr('value', None)) value = element.attr('value', None) if(value is None): year = datetime.datetime.now().year month, day, hour, minute = (months[0], 1, '00', '00') else: month, day, year, hour, minute = date.strftime(value, '%B:%d:%Y:%H:%M').split(':') arrays = (months, days, hours, minutes) values = (month, int(day), hour, minute) names = ('month', 'day', 'hour', 'minute') dateselect = self._generate_datetime_select(element, attribs, arrays[:2], values[:2], names[:2]) year_attribs = dict( name = '%s[year]' % element.get_element_name(), size = 5, value = year, ) yearfield = tags.input(type='text', **year_attribs) timeselect = self._generate_datetime_select(element, attribs, arrays[2:], values[2:], names[2:]) return ''.join([str(x) for x in (dateselect, yearfield, timeselect)])
def theme_radio(self, form_id, element): attribs = element.attr('attributes', {}) attribs['name'] = element.get_element_name() attribs['value'] = element.attr('value', 1) if(element.attr('selected', False)): attribs['checked'] = None return tags.input(type='radio', **attribs)
def _create_radio(value, default_value): attribs = {} if(str(value) == str(default_value)): attribs['checked'] = None attribs['value'] = value attribs['type'] = 'radio' attribs['name'] = element.get_element_name() return tags.input(**attribs)
def theme_textfield(self, form_id, element): value = element.attr('value', element.attr('default_value', '')) if(value is None): value = '' attribs = element.attr('attributes', {}) attribs['name'] = element.get_element_name() attribs['size'] = element.attr('size', 30) attribs['value'] = value return tags.input(type='text', **attribs)
def theme_checkbox(self, form_id, element): attribs = element.attr('attributes', {}) attribs['name'] = element.get_element_name() attribs['value'] = element.attr('value', 1) if(element.attr('checked', False)): attribs['checked'] = None if(element.attr('disabled', False)): attribs['disabled'] = None return tags.label()[[ tags.input(type='checkbox', **attribs), element.attr('text', '') ]]
def theme_hidden(self, form_id, element): # note that we still use formelement, even though it seems # unintuitive. this allows us to make label-esque fields # with a hidden form element value = element.attr('value', element.attr('default_value', '')) if(value is None): value = '' attribs = element.attr('attributes', {}) attribs['name'] = element.get_element_name() attribs['value'] = value return tags.input(type='hidden', **attribs)
def get_element(self, req, style, storable): """ @see: L{modu.editable.define.definition.get_element()} """ mlabel = self.get('flabel', '') if(mlabel.find('.') == -1): mlabel = 'm.%s' % mlabel mlabel = self.get('flabel_sql', mlabel) where = self.get('fwhere', '') if(callable(where)): where = where(storable) elif(isinstance(where, dict)): where = sql.build_where(where) limit = 'LIMIT %d' % self.get('limit_choices', 20) ntom_query = """SELECT m.%s AS value, %s AS label FROM %s m INNER JOIN %s n2m ON m.%s = n2m.%s AND n2m.%s = %%s %s ORDER BY label %s""" % (self['fvalue'], q(mlabel), q(self['ftable']), q(self['ntof']), self.get('fvalue', 'id'), self['ntof_f_id'], self['ntof_n_id'], where, limit) store = storable.get_store() results = store.pool.runQuery(sql.interp(ntom_query, storable.get_id())) if(style == 'listing' or self.get('read_only', False)): label_value = ', '.join([result['label'] for result in results]) return form.FormNode(self.name)(type='label', value=label_value) options = dict([(str(result['value']), result['label']) for result in results]) form_name = '%s-form' % storable.get_table() ac_id = '%s-%s-autocomplete' % (form_name, self.name) select_id = '%s-foreign-select' % self.name ac_url = req.get_path(req.prepath, 'autocomplete', storable.get_table(), self.name) + '?time=' + str(time.time()) hidden_options = '' for value in options: hidden_options += tags.input(type='hidden', name='%s[%s]' % (form_name, self.name), value=value) select_frm = form.FormNode('%s-select-view' % self.name) select_frm(type='select', options=options, size=self.get('size', 5), multiple=None, suffix=hidden_options + '<br/>', attributes={'id':select_id}) prefs = 'autoFill:1, selectFirst:1, matchSubset:0, selectOnly:1, extraParams:{t:%d}, minChars:%d' % (int(time.time()), self.get('min_chars', 3)) # ac_js = '$(document).ready(function(){$("#%s").autocomplete("%s", {onItemSelect:add_foreign_item("%s", "%s"), %s});});' % (ac_id, ac_url, form_name, self.name, prefs) ac_js = """ $(document).ready(function(){ $("#%s").autocomplete("%s", {%s}); $("#%s").result(add_foreign_item("%s", "%s")); }); """ % (ac_id, ac_url, prefs, ac_id, form_name, self.name) ac_controls = tags.script(type='text/javascript')[ac_js] ac_field = form.FormNode('%s-autocomplete' % self.name) ac_field(type='textfield', weight=10, attributes={'id':ac_id}, suffix=ac_controls) req.content.report('header', tags.style(type="text/css")[ """@import '%s';""" % req.get_path('/assets/jquery/jquery.autocomplete.css')]) assets.activate_jquery(req) req.content.report('header', tags.script(type="text/javascript", src=req.get_path("/assets/jquery/jquery.autocomplete.js"))['']) req.content.report('header', tags.script(type="text/javascript", src=req.get_path("/assets/editable-autocomplete.js"))['']) frm = form.FormNode('%s-ac-fieldset' % self.name)(type='fieldset', style='brief') frm[select_frm.name] = select_frm frm[ac_field.name] = ac_field return frm
def test_checkbox(self): tag = tags.input(type='checkbox', value=1, checked=None) expected = '<input type="checkbox" value="1" checked />' self.assertEqual(str(tag), expected, "Got '%s' instead of '%s'" % (str(tag), expected))
def test_input_quoted(self): tag = tags.input(_class='inputfield', type='password', value="This value has \"quotes\"", size=30) expected = '<input class="inputfield" size="30" type="password" value="This value has "quotes"" />' self.assertEqual(str(tag), expected, "Got '%s' instead of '%s'" % (str(tag), expected))
def test_input(self): tag = tags.input(_class='inputfield', type='password', size=30) expected = '<input class="inputfield" size="30" type="password" />' self.assertEqual(str(tag), expected, "Got '%s' instead of '%s'" % (str(tag), expected))
def theme_submit(self, form_id, element): attribs = element.attr('attributes', {}) attribs['name'] = element.get_element_name() attribs['value'] = element.attr('value', 'Submit') return tags.input(type='submit', **attribs)
def theme_password(self, form_id, element): attribs = element.attr('attributes', {}) attribs['name'] = element.get_element_name() attribs['size'] = element.attr('size', 30) attribs['value'] = element.attr('value', element.attr('default_value', '')) return tags.input(type='password', **attribs)
def theme_file(self, form_id, element): attribs = element.attr('attributes', {}) attribs['name'] = element.get_element_name() return tags.input(type='file', **attribs)