def setThermo(self, thermoId, progress_1=None, message_1=None, maximum_1=None, indeterminate_1=None, stop=None, end=None, **kwargs): try: thermoBag = Bag(self.pageLocalDocument('thermo_%s' % thermoId)) except: thermoBag = Bag() max = maximum_1 or thermoBag['t1.maximum'] prog = progress_1 or thermoBag['t1.maximum'] if max and prog > max: end = True if end: thermoBag = Bag() else: params = dict(progress_1=progress_1, message_1=message_1, maximum_1=maximum_1, indeterminate_1=indeterminate_1) params.update(kwargs) for k, v in params.items(): if v is not None: key, thermo = k.split('_') thermoBag['t%s.%s' % (thermo, key)] = v if key == 'progress': if not kwargs.get('indeterminate_%s' % thermo): thermoBag.pop('indeterminate_%s' % thermo) thermoBag.toXml(self.pageLocalDocument('thermo_%s' % thermoId), autocreate=True) return thermoBag['stop']
def table_script_resource_tree_data(self, table=None, res_type=None): #pkg,tblname = table.split('.') tblobj = self.db.table(table) pkg = tblobj.pkg.name tblname = tblobj.name result = Bag() resources = self.site.resource_loader.resourcesAtPath( page=self, pkg=None, path='tables/_default/%s' % res_type) resources_pkg = self.site.resource_loader.resourcesAtPath( page=self, pkg=pkg, path='tables/%s/%s' % (tblname, res_type)) resources_custom = self.site.resource_loader.resourcesAtPath( page=self, pkg=self.package.name, path='tables/_packages/%s/%s/%s' % (pkg, tblname, res_type)) resources.update(resources_pkg) resources.update(resources_custom) forbiddenNodes = [] def cb(node, _pathlist=None): has_parameters = False if node.attr['file_ext'] == 'py': resmodule = gnrImport(node.attr['abs_path']) tags = getattr(resmodule, 'tags', '') if tags and not self.application.checkResourcePermission( tags, self.userTags): if node.label == '_doc': forbiddenNodes.append('.'.join(_pathlist)) return caption = getattr(resmodule, 'caption', node.label) description = getattr(resmodule, 'description', '') description = getattr(resmodule, 'needSelection', True) if node.label == '_doc': result.setAttr( '.'.join(_pathlist), dict(caption=caption, description=description, tags=tags, has_parameters=has_parameters)) else: mainclass = getattr(resmodule, 'Main', None) assert mainclass, 'Main class is mandatory in tablescript resource' has_parameters = hasattr(mainclass, 'parameters_pane') result.setItem('.'.join(_pathlist + [node.label]), None, caption=caption, description=description, resource=node.attr['rel_path'][:-3], has_parameters=has_parameters) pl = [] resources.walk(cb, _pathlist=pl) if '_common' in result: n = result.popNode('_common') if len(result): result.setItem('r_zz', None, caption='-') result.setItem(n.label, n.value, n.attr) for forbidden in forbiddenNodes: result.pop(forbidden) return result
def tableTreeBag(self, packages=None, omit=None, tabletype=None): """add??? :param packages: add???. Default value is ``None`` :param omit: add???. Default value is ``None`` :param tabletype: add???. Default value is ``None`` :returns: add??? """ result = Bag() for pkg, pkgobj in self.packages.items(): if (pkg in packages and omit) or (not pkg in packages and not omit): continue pkgattr = dict(pkgobj.attributes) pkgattr['caption'] = pkgobj.attributes.get('name_long', pkg) result.setItem(pkg, Bag(), **pkgattr) for tbl, tblobj in pkgobj.tables.items(): tblattr = dict(tblobj.attributes) if tabletype and tblattr.get('tabletype') != tabletype: continue tblattr['caption'] = tblobj.attributes.get('name_long', pkg) result[pkg].setItem(tbl, None, **tblattr) if len(result[pkg]) == 0: result.pop(pkg) return result
def writeTransaction(self, mode, action, maintable, data, request_id=None, request_ts=None, user_id=None, session_id=None, user_ip=None, queue_id=None, file_name=None): kwargs = {} trtable = self.db.table(self.transaction_tname) kwargs['id'] = trtable.newPkeyValue() kwargs['request'] = datetime.now() kwargs['mode'] = mode kwargs['action'] = action kwargs['maintable'] = maintable kwargs['request_id'] = request_id kwargs['request_ts'] = request_ts kwargs['user_id'] = user_id kwargs['session_id'] = session_id kwargs['user_ip'] = user_ip kwargs['queue_id'] = queue_id kwargs['file_name'] = file_name if not isinstance(data, Bag): data = Bag(data) for k in data.keys(): if k.startswith('@'): data.pop(k) kwargs['data'] = data.toXml() if not request_id: # import trtable.insert(kwargs) else: # sync triggers prevTransactions = trtable.query(columns="$id, $error_id", where="$request_id=:request_id AND $queue_id = :queue_id", request_id=request_id, queue_id=queue_id).fetch() if len(prevTransactions) == 0: # normal case: is a new transaction trtable.insert(kwargs) elif len(prevTransactions) == 1: # the transaction yet exists if prevTransactions[0]['error_id']: kwargs.pop('request') # leave the old request timestamp in order to not alter the execution order trtable.update(kwargs) gnrlogger.warning( "Replacing old wrong transaction %s with new from file %s" % (request_id, file_name)) else: gnrlogger.error("Skipping duplicated transaction %s from file %s" % (request_id, file_name)) else: gnrlogger.critical("More than one old transactions with id %s from file %s" % (request_id, file_name)) raise self.db.notify("gnr_transaction_new") self.db.commit()
def ht_moveHierarchical(self, table=None, pkey=None, into_pkey=None, parent_id=None, into_parent_id=None, modifiers=None): tblobj = self.db.table(table) if not modifiers: into_pkey = into_pkey or None tblobj.batchUpdate(dict(parent_id=into_pkey), where='$id=:pkey', pkey=pkey) self.db.commit() elif (modifiers == 'Shift' or modifiers == 'Shift,Meta') and ( into_parent_id == parent_id) and tblobj.column('_row_count') is not None: where = '$parent_id=:p_id' if parent_id else '$parent_id IS NULL' f = tblobj.query(where=where, p_id=parent_id, for_update=True, order_by='$_row_count', addPkeyColumn=False).fetch() b = Bag([(r['id'], dict(r)) for r in f]) pref = '>' if modifiers == 'Shift' else '<' b.setItem(pkey, b.pop(pkey), _position='%s%s' % (pref, into_pkey)) for k, r in enumerate(b.values()): counter = k + 1 if r['_row_count'] != counter: old_rec = dict(r) r['_row_count'] = counter tblobj.update(r, old_rec) self.db.commit()
def getInfoBag(self,pkg=None,tbl=None,user=None,user_group=None,_editMode=False): result = Bag() if tbl: pkg = tbl.split('.')[0] tblobj = self.db.table(tbl) cols_permission_base = Bag() allcols = tblobj.columns.keys() + tblobj.model.virtual_columns.keys() result['cols_permission'] = cols_permission_base if user and not user_group: user_group = self.db.table('adm.user').readColumns(where='$username=:u',u=user,columns='$group_code') f = self.query(where="""($pkgid IS NULL OR $pkgid=:pkg) AND ($tblid IS NULL OR $tblid=:tbl) AND ($user_group IS NULL OR $user_group=:user_group) AND ($username IS NULL OR $username=:user) """,pkg=pkg,tbl=tbl,user_group=user_group,user=user, order_by='$rank ASC',columns="""$data""").fetch() for r in f: data = Bag(r['data']) last_permissions = data.pop('cols_permission') if tbl: result['cols_permission'] = self._updateColsPermission(result['cols_permission'], last_permissions or Bag(), allcols,_editMode) for k,v in data.items(): if v is not None: result[k] = v return result
def rpc_table_script_resource_tree_data(self, table=None, res_type=None): #pkg,tblname = table.split('.') tblobj = self.db.table(table) pkg = tblobj.pkg.name tblname = tblobj.name result = Bag() resources = self.site.resource_loader.resourcesAtPath( pkg, 'tables/%s/%s' % (tblname, res_type), 'py') forbiddenNodes = [] def cb(node, _pathlist=None): has_parameters = False if node.attr['file_ext'] == 'py': resmodule = gnrImport(node.attr['abs_path']) tags = getattr(resmodule, 'tags', '') if tags and not self.application.checkResourcePermission( tags, self.userTags): if node.label == '_doc': forbiddenNodes.append('.'.join(_pathlist)) return caption = getattr(resmodule, 'caption', node.label) description = getattr(resmodule, 'description', '') if node.label == '_doc': result.setAttr( '.'.join(_pathlist), dict(caption=caption, description=description, tags=tags, has_parameters=has_parameters)) else: mainclass = getattr(resmodule, 'Main', None) assert mainclass, 'Main class is mandatory in tablescript resource' has_parameters = hasattr(mainclass, 'parameters_pane') result.setItem('.'.join(_pathlist + [node.label]), None, caption=caption, description=description, resource=node.attr['rel_path'][:-3], has_parameters=has_parameters) resources.walk(cb, _pathlist=[]) for forbidden in forbiddenNodes: result.pop(forbidden) return result
def table_script_resource_tree_data(self, table=None, res_type=None): #pkg,tblname = table.split('.') tblobj = self.db.table(table) pkg = tblobj.pkg.name tblname = tblobj.name result = Bag() resources = self.site.resource_loader.resourcesAtPath(page=self,pkg=None,path='tables/_default/%s' % res_type) resources_pkg = self.site.resource_loader.resourcesAtPath(page=self,pkg=pkg, path='tables/%s/%s' % (tblname, res_type)) resources_custom = self.site.resource_loader.resourcesAtPath(page=self,pkg=self.package.name, path='tables/_packages/%s/%s/%s' % (pkg,tblname, res_type)) resources.update(resources_pkg) resources.update(resources_custom) forbiddenNodes = [] def cb(node, _pathlist=None): has_parameters = False if node.attr['file_ext'] == 'py': resmodule = gnrImport(node.attr['abs_path']) tags = getattr(resmodule, 'tags', '') if tags and not self.application.checkResourcePermission(tags, self.userTags): if node.label == '_doc': forbiddenNodes.append('.'.join(_pathlist)) return caption = getattr(resmodule, 'caption', node.label) description = getattr(resmodule, 'description', '') description = getattr(resmodule, 'needSelection', True) if node.label == '_doc': result.setAttr('.'.join(_pathlist), dict(caption=caption, description=description, tags=tags, has_parameters=has_parameters)) else: mainclass = getattr(resmodule, 'Main', None) assert mainclass, 'Main class is mandatory in tablescript resource' has_parameters = hasattr(mainclass, 'parameters_pane') result.setItem('.'.join(_pathlist + [node.label]), None, caption=caption, description=description, resource=node.attr['rel_path'][:-3], has_parameters=has_parameters) pl=[] resources.walk(cb,_pathlist=pl) if '_common' in result: n = result.popNode('_common') if len(result): result.setItem('r_zz',None,caption='-') result.setItem(n.label,n.value,n.attr) for forbidden in forbiddenNodes: result.pop(forbidden) return result
def writeSync(self, sync_out, maintable, action, record_data, transaction_id=None, transaction_request=None, queue_id=None): syncdata = {} syncdata['transaction_id'] = transaction_id syncdata['request'] = transaction_request or datetime.datetime.now() syncdata['action'] = action syncdata['maintable'] = maintable if not isinstance(record_data, Bag): record_data = Bag(record_data) for k in record_data.keys(): if k.startswith('@'): record_data.pop(k) syncdata['data'] = record_data.toXml() for sync_client in sync_out: if sync_client != queue_id: syncdata['client'] = sync_client self.insert(syncdata)
def rpc_table_script_resource_tree_data(self, table=None, res_type=None): # pkg,tblname = table.split('.') tblobj = self.db.table(table) pkg = tblobj.pkg.name tblname = tblobj.name result = Bag() resources = self.site.resource_loader.resourcesAtPath(pkg, "tables/%s/%s" % (tblname, res_type), "py") forbiddenNodes = [] def cb(node, _pathlist=None): has_parameters = False if node.attr["file_ext"] == "py": resmodule = gnrImport(node.attr["abs_path"]) tags = getattr(resmodule, "tags", "") if tags and not self.application.checkResourcePermission(tags, self.userTags): if node.label == "_doc": forbiddenNodes.append(".".join(_pathlist)) return caption = getattr(resmodule, "caption", node.label) description = getattr(resmodule, "description", "") if node.label == "_doc": result.setAttr( ".".join(_pathlist), dict(caption=caption, description=description, tags=tags, has_parameters=has_parameters), ) else: mainclass = getattr(resmodule, "Main", None) assert mainclass, "Main class is mandatory in tablescript resource" has_parameters = hasattr(mainclass, "parameters_pane") result.setItem( ".".join(_pathlist + [node.label]), None, caption=caption, description=description, resource=node.attr["rel_path"][:-3], has_parameters=has_parameters, ) resources.walk(cb, _pathlist=[]) for forbidden in forbiddenNodes: result.pop(forbidden) return result
def getSelectionData(self, **kwargs): q = self.getQuery() result = Bag() if not q: return result f = q.fetch() self.setInClientData('main.sql', q.sqltext) for i, r in enumerate(f): r = Bag(r) k = r.pop('pkey', None) or 'r_%i' % i result[k] = r return result
def getSelectionData(self,**kwargs): q = self.getQuery() result = Bag() if not q: return result f = q.fetch() self.setInClientData('main.sql', q.sqltext) for i,r in enumerate(f): r = Bag(r) k = r.pop('pkey',None) or 'r_%i' %i result[k] = r return result
def tableTreeBag(self, packages=None, omit=None, tabletype=None): """TODO :param packages: TODO :param omit: TODO :param tabletype: TODO""" result = Bag() for pkg, pkgobj in self.packages.items(): if (pkg in packages and omit) or (not pkg in packages and not omit): continue pkgattr = dict(pkgobj.attributes) pkgattr['caption'] = pkgobj.attributes.get('name_long', pkg) result.setItem(pkg, Bag(), **pkgattr) for tbl, tblobj in pkgobj.tables.items(): tblattr = dict(tblobj.attributes) if tabletype and tblattr.get('tabletype') != tabletype: continue tblattr['caption'] = tblobj.attributes.get('name_long', pkg) result[pkg].setItem(tbl, None, **tblattr) if len(result[pkg]) == 0: result.pop(pkg) return result
def tableTreeBag(self, packages=None, omit=None, tabletype=None): """add??? :param packages: add???. Default value is ``None`` :param omit: add???. Default value is ``None`` :param tabletype: add???. Default value is ``None`` :returns: add??? """ result = Bag() for pkg, pkgobj in self.packages.items(): if (pkg in packages and omit) or (not pkg in packages and not omit): continue pkgattr = dict(pkgobj.attributes) pkgattr["caption"] = pkgobj.attributes.get("name_long", pkg) result.setItem(pkg, Bag(), **pkgattr) for tbl, tblobj in pkgobj.tables.items(): tblattr = dict(tblobj.attributes) if tabletype and tblattr.get("tabletype") != tabletype: continue tblattr["caption"] = tblobj.attributes.get("name_long", pkg) result[pkg].setItem(tbl, None, **tblattr) if len(result[pkg]) == 0: result.pop(pkg) return result
def cb(row): b = Bag(row['data']) result = dict() if row['version'] >0: result['changed_fields'] = '<br/>'.join(b.keys()) b.pop('__ins_ts') b.pop('__version') b.pop('__mod_ts') self._curr_audit_record.update(b) result['__value__'] = self._curr_audit_record.deepcopy() return result
def ht_moveHierarchical(self,table=None,pkey=None,into_pkey=None,parent_id=None,into_parent_id=None,modifiers=None): tblobj = self.db.table(table) if not modifiers: into_pkey = into_pkey or None tblobj.batchUpdate(dict(parent_id=into_pkey),where='$id=:pkey',pkey=pkey) self.db.commit() elif (modifiers == 'Shift' or modifiers == 'Shift,Meta') and (into_parent_id==parent_id) and tblobj.column('_row_count') is not None: where='$parent_id=:p_id' if parent_id else '$parent_id IS NULL' f = tblobj.query(where=where,p_id=parent_id,for_update=True,order_by='$_row_count',addPkeyColumn=False).fetch() b = Bag([(r['id'],dict(r)) for r in f]) pref = '>' if modifiers == 'Shift' else '<' b.setItem(pkey,b.pop(pkey),_position='%s%s' %(pref,into_pkey)) for k,r in enumerate(b.values()): counter = k+1 if r['_row_count'] != counter: old_rec = dict(r) r['_row_count'] = counter tblobj.update(r,old_rec) self.db.commit()
def getInfoBag(self, pkg=None, tbl=None, user=None, user_group=None, _editMode=False): result = Bag() if tbl: pkg = tbl.split('.')[0] tblobj = self.db.table(tbl) cols_permission_base = Bag() allcols = tblobj.columns.keys( ) + tblobj.model.virtual_columns.keys() result['cols_permission'] = cols_permission_base if user and not user_group: user_group = self.db.table('adm.user').readColumns( where='$username=:u', u=user, columns='$group_code') f = self.query(where="""($pkgid IS NULL OR $pkgid=:pkg) AND ($tblid IS NULL OR $tblid=:tbl) AND ($user_group IS NULL OR $user_group=:user_group) AND ($username IS NULL OR $username=:user) """, pkg=pkg, tbl=tbl, user_group=user_group, user=user, order_by='$rank ASC', columns="""$data""").fetch() for r in f: data = Bag(r['data']) last_permissions = data.pop('cols_permission') if tbl: result['cols_permission'] = self._updateColsPermission( result['cols_permission'], last_permissions or Bag(), allcols, _editMode) for k, v in data.items(): if v is not None: result[k] = v return result
def test_pop(self): b = Bag(BAG_DATA) b.pop('phone.office') assert b != self.mybag
class DbStoresHandler(object): """Handler for using multi-database""" def __init__(self, db): self.db = db if db.application: self.config_folder = os.path.join(db.application.instanceFolder, 'dbstores') else: self.config_folder = None self.dbstores = {} self.load_config() self.create_stores() def load_config(self): """TODO""" self.config = Bag() if self.config_folder and os.path.isdir(self.config_folder): self.config = Bag(self.config_folder)['#0'] or Bag() def save_config(self): """TODO""" config = self.config.digest('#a.file_name,#v.#0?#') try: if os.path.isdir(self.config_folder): config_files = os.listdir(self.config_folder) for config_file in config_files: filepath = os.path.join(self.config_folder, config_file) if os.path.isfile(filepath): os.remove(filepath) except OSError: pass for name, params in config: dbstore_config = Bag() dbstore_config.setItem('db', None, **params) dbstore_config.toXml(os.path.join(self.config_folder, '%s.xml' % name), autocreate=True) def create_stores(self, check=False): """TODO""" for name in self.config.digest('#a.file_name'): self.add_store(name, check=check) def add_store(self, storename, check=False,dbattr=None): """TODO :param storename: TODO :param check: TODO""" attr = dbattr or self.config.getAttr('%s_xml.db' % storename) self.dbstores[storename] = dict(database=attr.get('dbname', storename), host=attr.get('host', self.db.host), user=attr.get('user', self.db.user), password=attr.get('password', self.db.password), port=attr.get('port', self.db.port), remote_host=attr.get('remote_host'), remote_port=attr.get('remote_port')) if check: self.dbstore_align(storename) def drop_dbstore_config(self, storename): """TODO :param storename: TODO""" return self.config.pop('%s_xml' % storename) def drop_store(self,storename): config = self.drop_dbstore_config(storename) if not config: return try: self.db.dropDb(config['db?dbname']) except Exception: print Exception self.save_config() def add_dbstore_config(self, storename, dbname=None, host=None, user=None, password=None, port=None, save=True): """TODO :param storename: TODO :param dbname: the database name :param host: the database server host :param user: the username :param password: the username's password :param port: TODO :param save: TODO""" self.config.setItem('%s_xml' % storename, None, file_name=storename) self.config.setItem('%s_xml.db' % storename, None, dbname=dbname, host=host, user=user, password=password, port=port) if save: self.save_config() self.load_config() self.add_store(storename, check=True) def dbstore_check(self, storename, verbose=False): """checks if dbstore exists and if it needs to be aligned :param storename: TODO :param verbose: TODO""" with self.db.tempEnv(storename=storename): self.db.use_store(storename) changes = self.db.model.check() if changes and not verbose: return False elif changes and verbose: return changes else: #not changes return True def dbstore_align(self, storename, changes=None): """TODO :param storename: TODO :param changes: TODO. """ with self.db.tempEnv(storename=storename): changes = changes or self.db.model.check() if changes: self.db.model.applyModelChanges()
class Main(BaseResourceMail): batch_prefix = 'mail_tpl' batch_cancellable = True batch_delay = 0.5 batch_immediate = True batch_title = None templates = '' def do(self): self.sendmail_selection() def sendmail_selection(self, thermo_selection=None, thermo_record=None): thermo_s = dict(line_code='selection', message='get_record_caption', tblobj=self.tblobj) thermo_s.update(thermo_selection or {}) thermo_r = dict(line_code='record', message='get_record_caption') thermo_r.update(thermo_record or {}) if isinstance(thermo_s['message'], basestring) and hasattr( self, thermo_s['message']): thermo_s['message'] = getattr(self, thermo_s['message']) if isinstance(thermo_r['message'], basestring) and hasattr( self.htmlMaker, thermo_r['message']): thermo_r['message'] = getattr(self.htmlMaker, thermo_r['message']) if not 'templates' in self.batch_parameters: self.batch_parameters['templates'] = self.templates #CONTROLLARE records = self.get_records() pkeyfield = self.tblobj.pkey if not self.get_selection(): return for record in self.btc.thermo_wrapper(records, maximum=len( self.get_selection()), **thermo_s): self.sendmail_record(record=record, thermo=thermo_r, storagekey=record[pkeyfield]) def pre_process(self): extra_parameters = self.batch_parameters.pop('extra_parameters') self.maintable = extra_parameters['table'] pkg, table = self.maintable.split('.') template_address = extra_parameters[ 'template_address'] or extra_parameters['template_id'] data = self.get_template(template_address) self.compiledTemplate = Bag(data['compiled']) self.mail_pars = Bag(data['metadata.email_compiled']) self.mail_pars['attachments'] = self.mail_pars.pop( 'attachments') if 'attachments' in self.mail_pars else '' self.batch_parameters.setdefault( 'letterhead_id', data.getItem('metadata.default_letterhead')) self.batch_parameters.setdefault('as_pdf', False) self.batch_title = data['summary'] or 'Mail' self.tblobj = self.db.table(self.maintable) self.virtual_columns = self.compiledTemplate.getItem( 'main?virtual_columns') self.mail_preference['html'] = not self.batch_parameters['as_pdf'] self.htmlMaker = TableScriptToHtml(self.page, self.tblobj) def sendmail_record(self, record=None, thermo=None, storagekey=None, **kwargs): record.update(self.batch_parameters) as_pdf = self.batch_parameters['as_pdf'] to_address = templateReplace(self.mail_pars.getItem('to_address', ''), record) subject = templateReplace(self.mail_pars.getItem('subject', ''), record) cc_address = templateReplace(self.mail_pars.getItem('cc_address', ''), record) htmlContent = templateReplace( self.compiledTemplate, record, safeMode=True, noneIsBlank=False, locale=self.page.locale, formats=self.compiledTemplate.getItem('main?formats'), masks=self.compiledTemplate.getItem('main?masks'), localizer=self.page.localizer) result = self.htmlMaker(htmlContent=htmlContent, filename='%s.html' % record['id'], record=record, thermo=thermo, pdf=as_pdf, **self.batch_parameters) if result: attachments = templateReplace( self.mail_pars.getItem('attachments', ''), record) attachments = attachments.split(',') if attachments else [] body = None if as_pdf: attachments.append(result) body = '' else: body = result attachments = attachments or None self.send_one_email(to_address=to_address, cc_address=cc_address, subject=subject, body=body, attachments=attachments, _record_id=record[self.tblobj.pkey]) def table_script_parameters_pane(self, pane, extra_parameters=None, record_count=None, **kwargs): pkg, tbl = extra_parameters['table'].split('.') pane = pane.div(padding='10px', min_height='60px') template_address = extra_parameters[ 'template_address'] or 'adm.userobject.data:%s' % extra_parameters[ 'template_id'] data = self.loadTemplate(template_address, asSource=True)[0] #data,meta = self.db.table('adm.userobject').loadUserObject(pkey=extra_parameters['template_id']) pane.dataFormula('#table_script_runner.dialog_pars.title', 'dlgtitle', dlgtitle='!!%s (%i)' % (data['metadata.summary'] or 'Mail', record_count), _onBuilt=True) fb = pane.formbuilder(cols=1, fld_width='20em', border_spacing='4px') fb.dbSelect(dbtable='adm.htmltemplate', value='^.letterhead_id', lbl='!!Letterhead', hasDownArrow=True) fb.checkbox(value='^.as_pdf', label='!!Send as pdf') fb.dataController( "SET .letterhead_id = default_letterhead || null;", _onBuilt=True, default_letterhead=data.getItem('metadata.default_letterhead') or False, _if='default_letterhead') fb.textbox(value='^.mail_code', lbl='Mail code') if data.getItem('parameters'): parameters = data.getItem('parameters') fielddict = { 'T': 'Textbox', 'L': 'NumberTextBox', 'D': 'DateTextBox', 'B': 'Checkbox', 'N': 'NumberTextBox', 'TL': 'Simpletextarea' } for n in parameters: attr = n.attr values = attr.get('values') mandatory = attr.get('mandatory') == 'T' kwargs = dict(value='^.%s' % attr['code'], lbl=attr['description'] or attr['code'], validate_notnull=mandatory) if values: tag = 'filteringSelect' kwargs['values'] = values else: tag = fielddict[attr.get('fieldtype') or 'T'] if tag == 'Simpletextarea': kwargs['lbl_vertical_align'] = 'top' kwargs['colspan'] = 2 kwargs['tag'] = tag fb.child(**kwargs)
class DbStoresHandler(object): """Handler for using multi-database""" def __init__(self, db): self.db = db self.config_folder = os.path.join(db.application.instanceFolder, "dbstores") self.dbstores = {} self.load_config() self.create_stores() def load_config(self): """add???""" self.config = Bag() if os.path.isdir(self.config_folder): self.config = Bag(self.config_folder)["#0"] or Bag() def save_config(self): """add???""" try: shutil.rmtree(self.config_folder, True) except OSError: pass for name, params in self.config.digest("#a.file_name,#v.#0?#"): dbstore_config = Bag() dbstore_config.setItem("db", None, **params) dbstore_config.toXml(os.path.join(self.config_folder, "%s.xml" % name), autocreate=True) def create_stores(self): """add???""" for name in self.config.digest("#a.file_name"): self.add_store(name) def add_store(self, storename, check=False): """add??? :param storename: add??? :param check: add???. Default value is ``False`` """ attr = self.config.getAttr("%s_xml.db" % storename) self.dbstores[storename] = dict( database=attr.get("dbname", storename), host=attr.get("host", self.db.host), user=attr.get("user", self.db.user), password=attr.get("password", self.db.password), port=attr.get("port", self.db.port), ) if check: self.dbstore_align(storename) def drop_dbstore_config(self, storename): """add??? :param storename: add??? """ self.config.pop("%s_xml" % storename) def add_dbstore_config(self, storename, dbname=None, host=None, user=None, password=None, port=None, save=True): """add??? :param storename: add??? :param dbname: add???. Default value is ``None`` :param host: add???. Default value is ``None`` :param user: add???. Default value is ``None`` :param password: add???. Default value is ``None`` :param port: add???. Default value is ``None`` :param save: add???. Default value is ``True`` """ self.config.setItem("%s_xml" % storename, None, file_name=storename) self.config.setItem( "%s_xml.db" % storename, None, dbname=dbname, host=host, user=user, password=password, port=port ) if save: self.save_config() self.load_config() self.add_store(storename, check=True) def dbstore_check(self, storename, verbose=False): """checks if dbstore exists and if it needs to be aligned :param storename: add??? :param verbose: add???. Default value is ``False`` """ with self.db.tempEnv(storename=storename): self.db.use_store(storename) changes = self.db.model.check() if changes and not verbose: return False elif changes and verbose: return changes else: # not changes return True def dbstore_align(self, storename, changes=None): """add??? :param storename: add??? :param changes: add???. Default value is ``None`` """ with self.db.tempEnv(storename=storename): self.db.use_store(storename) changes = changes or self.db.model.check() if changes: self.db.model.applyModelChanges() self.db.use_store("_main_db")
class Main(BaseResourceMail): batch_prefix = 'mail_tpl' batch_cancellable = True batch_delay = 0.5 batch_immediate = True batch_title = None templates = '' def do(self): self.sendmail_selection() def sendmail_selection(self, thermo_selection=None, thermo_record=None): thermo_s = dict(line_code='selection', message='get_record_caption', tblobj=self.tblobj) thermo_s.update(thermo_selection or {}) thermo_r = dict(line_code='record', message='get_record_caption') thermo_r.update(thermo_record or {}) if isinstance(thermo_s['message'], basestring) and hasattr(self, thermo_s['message']): thermo_s['message'] = getattr(self, thermo_s['message']) if isinstance(thermo_r['message'], basestring) and hasattr(self.htmlMaker, thermo_r['message']): thermo_r['message'] = getattr(self.htmlMaker, thermo_r['message']) if not 'templates' in self.batch_parameters: self.batch_parameters['templates'] = self.templates #CONTROLLARE records = self.get_records() pkeyfield = self.tblobj.pkey if not self.get_selection(): return for record in self.btc.thermo_wrapper(records, maximum=len(self.get_selection()), **thermo_s): self.sendmail_record(record=record, thermo=thermo_r, storagekey=record[pkeyfield]) def pre_process(self): extra_parameters = self.batch_parameters.pop('extra_parameters') self.maintable = extra_parameters['table'] pkg,table = self.maintable.split('.') template_address = extra_parameters['template_address'] or extra_parameters['template_id'] data = self.get_template(template_address) self.compiledTemplate = Bag(data['compiled']) self.mail_pars = Bag(data['metadata.email_compiled']) self.mail_pars['attachments'] = self.mail_pars.pop('attachments') if 'attachments' in self.mail_pars else '' self.batch_parameters.setdefault('letterhead_id',data.getItem('metadata.default_letterhead')) self.batch_parameters.setdefault('as_pdf',False) self.batch_title = data['summary'] or 'Mail' self.tblobj = self.db.table(self.maintable) self.virtual_columns = self.compiledTemplate.getItem('main?virtual_columns') self.mail_preference['html'] = not self.batch_parameters['as_pdf'] self.htmlMaker = TableScriptToHtml(self.page,self.tblobj) def sendmail_record(self, record=None, thermo=None, storagekey=None,**kwargs): record.update(self.batch_parameters) as_pdf = self.batch_parameters['as_pdf'] to_address = templateReplace(self.mail_pars.getItem('to_address',''),record) subject = templateReplace(self.mail_pars.getItem('subject',''),record) cc_address = templateReplace(self.mail_pars.getItem('cc_address',''),record) from_address = templateReplace(self.mail_pars.getItem('from_address',''),record) htmlContent=templateReplace(self.compiledTemplate,record, safeMode=True,noneIsBlank=False, locale=self.page.locale, formats=self.compiledTemplate.getItem('main?formats'), masks=self.compiledTemplate.getItem('main?masks'), localizer=self.page.localizer) result = self.htmlMaker(htmlContent=htmlContent, filename='%s.html' %record['id'], record=record, thermo=thermo, pdf=as_pdf, **self.batch_parameters) if result: attachments = templateReplace(self.mail_pars.getItem('attachments',''),record) attachments = attachments.split(',') if attachments else [] body = None if as_pdf: attachments.append(result) body = '' else: body = result attachments = attachments or None self.send_one_email(to_address=to_address,from_address=from_address, cc_address=cc_address, subject=subject,body=body,attachments=attachments,_record_id=record[self.tblobj.pkey]) def table_script_parameters_pane(self,pane,extra_parameters=None,record_count=None,**kwargs): pkg,tbl= extra_parameters['table'].split('.') pane = pane.div(padding='10px',min_height='60px') template_address = extra_parameters['template_address'] or 'adm.userobject.data:%s' %extra_parameters['template_id'] data = self.loadTemplate(template_address,asSource=True)[0] #data,meta = self.db.table('adm.userobject').loadUserObject(pkey=extra_parameters['template_id']) pane.dataFormula('#table_script_runner.dialog_pars.title','dlgtitle', dlgtitle='!!%s (%i)' %(data['metadata.summary'] or 'Mail',record_count),_onBuilt=True) fb = pane.formbuilder(cols=1,fld_width='20em',border_spacing='4px') fb.dbSelect(dbtable='adm.htmltemplate', value='^.letterhead_id',lbl='!!Letterhead',hasDownArrow=True) fb.checkbox(value='^.as_pdf',label='!!Send as pdf') fb.dataController("SET .letterhead_id = default_letterhead || null;",_onBuilt=True, default_letterhead=data.getItem('metadata.default_letterhead') or False,_if='default_letterhead') fb.textbox(value='^.mail_code',lbl='Mail code') if data.getItem('parameters'): parameters = data.getItem('parameters') fielddict = {'T':'Textbox','L':'NumberTextBox','D':'DateTextBox','B':'Checkbox','N':'NumberTextBox', 'TL':'Simpletextarea'} for n in parameters: attr = n.attr values=attr.get('values') mandatory = attr.get('mandatory') == 'T' kwargs = dict(value='^.%s' %attr['code'],lbl=attr['description'] or attr['code'], validate_notnull=mandatory) if values: tag = 'filteringSelect' kwargs['values'] = values else: tag = fielddict[attr.get('fieldtype') or 'T'] if tag == 'Simpletextarea': kwargs['lbl_vertical_align']='top' kwargs['colspan'] = 2 kwargs['tag'] =tag fb.child(**kwargs)
class DbStoresHandler(object): """Handler for using multi-database""" def __init__(self, db): self.db = db self.config_folder = os.path.join(db.application.instanceFolder, 'dbstores') self.dbstores = {} self.load_config() self.create_stores() def load_config(self): """add???""" self.config = Bag() if os.path.isdir(self.config_folder): self.config = Bag(self.config_folder)['#0'] or Bag() def save_config(self): """add???""" try: shutil.rmtree(self.config_folder, True) except OSError: pass for name, params in self.config.digest('#a.file_name,#v.#0?#'): dbstore_config = Bag() dbstore_config.setItem('db', None, **params) dbstore_config.toXml(os.path.join(self.config_folder, '%s.xml' % name), autocreate=True) def create_stores(self): """add???""" for name in self.config.digest('#a.file_name'): self.add_store(name) def add_store(self, storename, check=False): """add??? :param storename: add??? :param check: add???. Default value is ``False`` """ attr = self.config.getAttr('%s_xml.db' % storename) self.dbstores[storename] = dict(database=attr.get('dbname', storename), host=attr.get('host', self.db.host), user=attr.get('user', self.db.user), password=attr.get( 'password', self.db.password), port=attr.get('port', self.db.port)) if check: self.dbstore_align(storename) def drop_dbstore_config(self, storename): """add??? :param storename: add??? """ self.config.pop('%s_xml' % storename) def add_dbstore_config(self, storename, dbname=None, host=None, user=None, password=None, port=None, save=True): """add??? :param storename: add??? :param dbname: add???. Default value is ``None`` :param host: add???. Default value is ``None`` :param user: add???. Default value is ``None`` :param password: add???. Default value is ``None`` :param port: add???. Default value is ``None`` :param save: add???. Default value is ``True`` """ self.config.setItem('%s_xml' % storename, None, file_name=storename) self.config.setItem('%s_xml.db' % storename, None, dbname=dbname, host=host, user=user, password=password, port=port) if save: self.save_config() self.load_config() self.add_store(storename, check=True) def dbstore_check(self, storename, verbose=False): """checks if dbstore exists and if it needs to be aligned :param storename: add??? :param verbose: add???. Default value is ``False`` """ with self.db.tempEnv(storename=storename): self.db.use_store(storename) changes = self.db.model.check() if changes and not verbose: return False elif changes and verbose: return changes else: #not changes return True def dbstore_align(self, storename, changes=None): """add??? :param storename: add??? :param changes: add???. Default value is ``None`` """ with self.db.tempEnv(storename=storename): self.db.use_store(storename) changes = changes or self.db.model.check() if changes: self.db.model.applyModelChanges() self.db.use_store('_main_db')