def render_cell(self, w, rownum): msg = self.data[rownum][3] lines = msg.splitlines() if len(lines) <= self.snip_over: w(u'<pre class="rawtext">%s</pre>' % msg) else: # The make_uid argument has no specific meaning here. div_snip_id = make_uid(u'log_snip_') div_full_id = make_uid(u'log_full_') divs_id = (div_snip_id, div_full_id) snip = u'\n'.join( (lines[0], lines[1], u' ...', u' %i more lines [double click to expand]' % (len(lines) - 4), u' ...', lines[-2], lines[-1])) divs = ((div_snip_id, snip, u'expand', "class='collapsed'"), (div_full_id, msg, u'collapse', "class='hidden'")) for div_id, content, button, h_class in divs: text = self._cw._(button) js = u"toggleVisibility('%s'); toggleVisibility('%s');" % divs_id w(u'<div id="%s" %s>' % (div_id, h_class)) w(u'<pre class="raw_test" ondblclick="javascript: %s" ' u'title="%s" style="display: block;">' % (js, text)) w(content) w(u'</pre>') w(u'</div>')
def _set_pageid(self): """initialize self.pageid if req.form provides a specific pageid, use it, otherwise build a new one. """ pid = self.form.get('pageid') if pid is None: pid = make_uid(id(self)) self.html_headers.define_var('pageid', pid, override=False) self.pageid = pid
def call(self, cssclass=""): self._cw.add_css(('fullcalendar.css', 'cubicweb.calendar.css')) self._cw.add_js(('jquery.ui.js', 'fullcalendar.min.js', 'jquery.qtip.min.js', 'fullcalendar.locale.js')) self.calendar_id = 'cal' + make_uid('uid') self.add_onload() # write calendar div to load jquery fullcalendar object if cssclass: self.w(u'<div class="%s" id="%s"></div>' % (cssclass, self.calendar_id)) else: self.w(u'<div id="%s"></div>' % self.calendar_id)
def test_2(self): d = set() while len(d) < 10000: uid = make_uid('xyz') if uid in d: self.fail(len(d)) if re.match('\d', uid): self.fail('make_uid must not return something begining with ' 'some numeric character, got %s' % uid) d.add(uid)
def _execmany_thread(sql_connect, statements, dump_output_dir=None, support_copy_from=True, encoding='utf-8'): """ Execute sql statement. If 'INSERT INTO', try to use 'COPY FROM' command, or fallback to execute_many. """ if support_copy_from: execmany_func = _execmany_thread_copy_from else: execmany_func = _execmany_thread_not_copy_from cnx = sql_connect() cu = cnx.cursor() try: for statement, data in statements: table = None columns = None try: if not statement.startswith('INSERT INTO'): cu.executemany(statement, data) continue table = statement.split()[2] if isinstance(data[0], (tuple, list)): columns = None else: columns = list(data[0]) execmany_func(cu, statement, data, table, columns, encoding) except Exception: print('unable to copy data into table %s' % table) # Error in import statement, save data in dump_output_dir if dump_output_dir is not None: pdata = { 'data': data, 'statement': statement, 'time': asctime(), 'columns': columns } filename = make_uid() try: with open( osp.join(dump_output_dir, '%s.pickle' % filename), 'wb') as fobj: pickle.dump(pdata, fobj) except IOError: print('ERROR while pickling in', dump_output_dir, filename + '.pickle') cnx.rollback() raise finally: cnx.commit() cu.close()
def _init_params(self, subvid, treeid, initial_load, morekwargs): form = self._cw.form if subvid is None: subvid = form.pop('treesubvid', self.subvid) # consume it if treeid is None: treeid = form.pop('treeid', None) if treeid is None: treeid = 'throw_away' + make_uid('uid') if 'morekwargs' in self._cw.form: ajaxargs = json.loads(form.pop('morekwargs')) # got unicode & python keywords must be strings morekwargs.update(dict((str(k), v) for k, v in ajaxargs.items())) return subvid, treeid
def __init__(self, repo, user): self.user = user # XXX deprecate and store only a login. self.repo = repo self.sessionid = make_uid(unormalize(user.login)) self.data = {}
def render_tabs(self, tabs, default, entity=None): # delegate to the default tab if there is more than one entity # in the result set (tabs are pretty useless there) if entity and len(self.cw_rset) > 1: entity.view(default, w=self.w) return self._cw.add_css('jquery.ui.css') self._cw.add_js( ('jquery.ui.js', 'cubicweb.ajax.js', 'jquery.cookie.js')) # prune tabs : not all are to be shown tabs, active_tab = self.prune_tabs(tabs, default) # build the html structure w = self.w uid = entity and entity.eid or utils.make_uid('tab') w(u'<div id="entity-tabs-%s">' % uid) w(u'<ul>') active_tab_idx = None for i, (tabid, domid, tabkwargs) in enumerate(tabs): w(u'<li>') w(u'<a href="#%s">' % domid) w(tabkwargs.pop('label', self._cw._(tabid))) w(u'</a>') w(u'</li>') if domid == active_tab: active_tab_idx = i w(u'</ul>') for tabid, domid, tabkwargs in tabs: w(u'<div id="%s">' % domid) if self.lazy: tabkwargs.setdefault('tabid', domid) tabkwargs.setdefault('vid', tabid) self.lazyview(**tabkwargs) else: self._cw.view(tabid, w=self.w, **tabkwargs) w(u'</div>') w(u'</div>') # call the setTab() JS function *after* each tab is generated # because the callback binding needs to be done before # XXX make work history: true if self.lazy: self._cw.add_onload( u""" jQuery('#entity-tabs-%(uid)s').tabs( { active: %(tabindex)s, activate: function(event, ui) { setTab(ui.newPanel.attr('id'), '%(cookiename)s'); } }); setTab('%(domid)s', '%(cookiename)s'); """ % { 'tabindex': active_tab_idx, 'domid': active_tab, 'uid': uid, 'cookiename': self.cookie_name }) else: self._cw.add_onload( u"jQuery('#entity-tabs-%(uid)s').tabs({active: %(tabindex)s});" % { 'tabindex': active_tab_idx, 'uid': uid })
def redirect_message_id(self): return make_uid()
def domid(self): return make_uid('logTable')
def test_1(self): self.assertNotEqual(make_uid('xyz'), make_uid('abcd')) self.assertNotEqual(make_uid('xyz'), make_uid('xyz'))
def domid(self): return self._cw.form.get('divid') or domid( '%s-%s' % (self.__regid__, make_uid()))
def call(self, title=None, subvid=None, displayfilter=None, headers=None, displaycols=None, displayactions=None, actions=(), divid=None, cellvids=None, cellattrs=None, mainindex=None, paginate=False, page_size=None): """Produces a table displaying a composite query :param title: title added before table :param subvid: cell view :param displayfilter: filter that selects rows to display :param headers: columns' titles :param displaycols: indexes of columns to display (first column is 0) :param displayactions: if True, display action menu """ req = self._cw divid = divid or req.form.get('divid') or 'rs%s' % make_uid( id(self.cw_rset)) self._setup_tablesorter(divid) # compute label first since the filter form may remove some necessary # information from the rql syntax tree if mainindex is None: mainindex = self.main_var_index() computed_labels = self.columns_labels(mainindex) if not subvid and 'subvid' in req.form: subvid = req.form.pop('subvid') actions = list(actions) if mainindex is None: displayfilter, displayactions = False, False else: if displayfilter is None and req.form.get('displayfilter'): displayfilter = True if displayactions is None and req.form.get('displayactions'): displayactions = True displaycols = self.displaycols(displaycols, headers) if self.initial_load: self.w(u'<div class="section">') if not title and 'title' in req.form: title = req.form['title'] if title: self.w(u'<h2 class="tableTitle">%s</h2>\n' % title) if displayfilter: actions += self.form_filter(divid, displaycols, displayfilter, displayactions, paginate) elif displayfilter: actions += self.show_hide_actions(divid, True) self.w(u'<div id="%s">' % divid) if displayactions: actionsbycat = self._cw.vreg['actions'].possible_actions( req, self.cw_rset) for action in actionsbycat.get('mainactions', ()): for action in action.actual_actions(): actions.append((action.url(), req._(action.title), action.html_class(), None)) # render actions menu if actions: self.render_actions(divid, actions) # render table if paginate: self.divid = divid # XXX iirk (see usage in page_navigation_url) self.paginate(page_size=page_size, show_all_option=False) table = self.table_widget_class(self) for column in self.get_columns(computed_labels, displaycols, headers, subvid, cellvids, cellattrs, mainindex): table.append_column(column) table.render(self.w) self.w(u'</div>\n') if self.initial_load: self.w(u'</div>\n')