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_handle_anwsers(self, answers=None): answers = Bag(answers or '') for page_id, page_answers in answers.items(): with self.clientPage(page_id) as clientPage: for value, changepath in page_answers.digest('#v,#a.client_path'): clientPage.set(changepath, value) print 'HEY SETTO IN CLIENTPAGE'
def _relationPythonCode(self, relation): relpath = relation.pop('relation') r = relpath.split('.') if len(r) == 3: pkg, table, id = r if pkg == 'main': relpath = '%s.%s' % (table, id) relation_name = relation.pop('relation_name') foreignkey = relation.pop('foreignkey') attributes = Bag() if relation_name: attributes['relation_name'] = relation_name if foreignkey: attributes['mode'] = 'foreignkey' for k, v in relation.items(): if v is not None: attributes[k] = v atlst = [] for k, v in attributes.items(): if v == 'True': v = True if isinstance(v, basestring): if "'" in v: v = '"%s"' % v else: v = "'%s'" % v atlst.append("%s=%s" % (k, v)) return """relation('%s',%s)""" % (relpath, ', '.join(atlst))
def checkFullSyncTables_do(self,errorlog_folder=None,dbstores=None,packages=None,tbllist=None): errors = Bag() master_index = self.db.tablesMasterIndex()['_index_'] for tbl in master_index.digest('#a.tbl'): if tbllist and tbl not in tbllist: continue pkg,tblname = tbl.split('.') if packages and pkg not in packages: continue tbl = self.db.table(tbl) multidb = tbl.multidb if not multidb or multidb=='one' or multidb=='parent': continue main_f = tbl.query(addPkeyColumn=False,bagFields=True, excludeLogicalDeleted=False,ignorePartition=True, excludeDraft=False).fetch() if multidb=='*': tbl.checkSyncAll(dbstores=dbstores,main_fetch=main_f,errors=errors) else: tbl.checkSyncPartial(dbstores=dbstores,main_fetch=main_f,errors=errors) if errorlog_folder: for dbstore,v in errors.items(): p = '%s/%s.xml' %(errorlog_folder,dbstore) if v: v.toXml(p,autocreate=True) else: os.remove(p)
def _relationPythonCode(self,relation): relpath = relation.pop('relation') r = relpath.split('.') if len(r)==3: pkg,table,id = r if pkg == 'main': relpath = '%s.%s' %(table,id) relation_name = relation.pop('relation_name') foreignkey = relation.pop('foreignkey') attributes = Bag() if relation_name: attributes['relation_name'] = relation_name if foreignkey: attributes['mode'] = 'foreignkey' for k, v in relation.items(): if v is not None: attributes[k] = v atlst = [] for k,v in attributes.items(): if v == 'True': v = True if isinstance(v,basestring): if "'" in v: v = '"%s"' %v else: v = "'%s'" %v atlst.append("%s=%s" %(k,v)) return """relation('%s',%s)""" %(relpath,', '.join(atlst))
def loadPreferences(self, filename): basename, ext = os.path.splitext(filename) if ext == '.yaml': preferences = Bag(yaml.load(codecs.open(filename,'rt','utf-8'))) else: preferences = Bag(filename) for package, prefs in preferences.items(): self.db.packages[package].setPreference('', prefs)
def rpc_handle_anwsers(self, answers=None): answers = Bag(answers or '') for page_id, page_answers in answers.items(): with self.clientPage(page_id) as clientPage: for value, changepath in page_answers.digest( '#v,#a.client_path'): clientPage.set(changepath, value) print 'HEY SETTO IN CLIENTPAGE'
def createMessageRecord(self, emailid): new_mail = dict(account_id=self.account_id) new_mail['id'] = getUuid() new_mail['uid'] = emailid resp, data = self.imap.uid('fetch', emailid, "(RFC822)") email_body = data[0][1] mail = email.message_from_string(email_body) #mail = email.message_from_string(unicode(email_body.decode(encoding).encode('utf8'))) onCreatingCallbacs = [ fname for fname in dir(self.messages_table) if fname.startswith('onCreatingMessage_') ] if onCreatingCallbacs: make_message = False for fname in onCreatingCallbacs: make_message = make_message or getattr( self.messages_table, fname)(mail) is not False if make_message is False: return False encoding = mail.get_content_charset() b = Bag(mail) for k, v in b.items(): if isinstance(v, basestring): b[k] = self.smartConverter(v, encoding) new_mail['email_bag'] = b self.fillHeaders(mail, new_mail, encoding) if self.messages_table.spamChecker(new_mail) is True: return if mail.get_content_maintype() not in ('multipart', 'image'): content = mail.get_payload(decode=True) encoding = mail.get_content_charset() #encoding = chardet.detect(content)['encoding'] new_mail['body'] = self.smartConverter(content, encoding) new_mail['body_plain'] = new_mail['body'] else: for part in mail.walk(): part_content_type = part.get_content_type() if part_content_type.startswith('multipart'): continue content_disposition = part.get('Content-Disposition') if content_disposition is None and part_content_type in ( 'text/html', 'text/plain'): self.parseBody(part, new_mail, part_content_type=part_content_type) else: self.parseAttachment(part, new_mail, part_content_type=part_content_type) if new_mail.get('body'): g = re.search("<body(.*?)>(.*?)</body>", new_mail['body'], re.S | re.DOTALL) new_mail['body'] = g.group(2) if g else new_mail['body'] else: new_mail['body'] = new_mail.get('body_plain') return new_mail
def createMessageRecord(self, emailid): new_mail = dict(account_id=self.account_id) new_mail['id'] = getUuid() new_mail['uid'] = emailid resp, data = self.imap.uid('fetch',emailid, "(RFC822)") email_body = data[0][1] mail = email.message_from_string(email_body) #mail = email.message_from_string(unicode(email_body.decode(encoding).encode('utf8'))) onCreatingCallbacs = [fname for fname in dir(self.messages_table) if fname.startswith('onCreatingMessage_')] if onCreatingCallbacs: make_message = False for fname in onCreatingCallbacs: make_message = make_message or getattr(self.messages_table,fname)(mail) is not False if make_message is False: return False encoding = mail.get_content_charset() b = Bag(mail) for k,v in b.items(): if isinstance(v,basestring): b[k] = self.smartConverter(v,encoding) new_mail['email_bag'] = b self.fillHeaders(mail, new_mail,encoding) if self.messages_table.spamChecker(new_mail) is True: return if mail.get_content_maintype() not in ('multipart','image'): content = mail.get_payload(decode=True) encoding = mail.get_content_charset() #encoding = chardet.detect(content)['encoding'] new_mail['body'] = self.smartConverter(content,encoding) new_mail['body_plain'] = new_mail['body'] else: for part in mail.walk(): part_content_type = part.get_content_type() if part_content_type.startswith('multipart'): continue content_disposition = part.get('Content-Disposition') if content_disposition is None and part_content_type in ('text/html','text/plain'): self.parseBody(part, new_mail, part_content_type=part_content_type) else: self.parseAttachment(part, new_mail, part_content_type=part_content_type) if new_mail.get('body'): g = re.search("<body(.*?)>(.*?)</body>", new_mail['body'], re.S|re.DOTALL) new_mail['body'] = g.group(2) if g else new_mail['body'] else: new_mail['body'] = new_mail.get('body_plain') return new_mail
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 checkFullSyncTables_do(self, errorlog_folder=None, dbstores=None, packages=None, tbllist=None): errors = Bag() master_index = self.db.tablesMasterIndex()['_index_'] for tbl in master_index.digest('#a.tbl'): if tbllist and tbl not in tbllist: continue pkg, tblname = tbl.split('.') if packages and pkg not in packages: continue tbl = self.db.table(tbl) multidb = tbl.multidb if not multidb or multidb == 'one' or multidb == 'parent': continue main_f = tbl.query(addPkeyColumn=False, bagFields=True, excludeLogicalDeleted=False, ignorePartition=True, excludeDraft=False).fetch() if multidb == '*': tbl.checkSyncAll(dbstores=dbstores, main_fetch=main_f, errors=errors) else: tbl.checkSyncPartial(dbstores=dbstores, main_fetch=main_f, errors=errors) if errorlog_folder: for dbstore, v in errors.items(): p = '%s/%s.xml' % (errorlog_folder, dbstore) if v: v.toXml(p, autocreate=True) else: os.remove(p)
class Struct4D(object): def __init__(self, app, packages_folder=None): self.app = app self.instance_folder = app.instanceFolder self.folder4dstructBag = Bag(self.folder4dstruct + '/')['structure'] self.names4d = self.buildNames4d() self.packages_folder = packages_folder @property def folder4d(self): return self.app.folder4d @property def folder4dstruct(self): path = os.path.join(self.folder4d, 'structure') if not os.path.isdir(path): os.mkdir(path) return path def areaFolder(self, area): path = os.path.join(self.packages_folder, area) print 'areaFolder:', path if not os.path.isdir(path): os.mkdir(path) return path def modelFolder(self, area): path = os.path.join(self.areaFolder(area), 'model') if not os.path.isdir(path): os.mkdir(path) return path def fromNameAs(self, name): if ' AS ' in name: name4d, nameSql = name.split(' AS ', 1) else: name4d, nameSql = name, name return name4d.strip(), nameSql.strip().lstrip('_').lower() def nameConverter(self, table='', field='', fullname=None, mode='4D'): if not fullname: fullname = '%s_[%s]%s' % (mode, table, field) else: fullname = '%s_%s' % (mode, fullname) return self.names4d.get(fullname.lower()) def get4dTables(self): return [k[4:-1] for k, v in self.names4d.items() if k.startswith('4d') and not v[2]] def buildNames4d(self): result = {} namesdict = {} for sname, sbag in self.folder4dstructBag.items(): sname = sname.lower() tablesbag = sbag['tables4d'] if tablesbag: for tbag in tablesbag.values(): table4D, tableSql = self.fromNameAs(tbag['name']) path4d = '4D_[%s]' % table4D namesdict[ tableSql] = None # in namesdict put the table with area name avoiding multi area duplicates if tableSql.startswith('%s_' % sname): #strip the area code from table name tableSql = tableSql.lower().split('_', 1)[1] result[path4d.lower()] = (sname, tableSql, None) for fldbag in tbag['fields'].values(): name4D, nameSql = self.fromNameAs(fldbag['name']) path4d = '4D_[%s]%s' % (table4D, name4D) result[path4d.lower()] = (sname, tableSql, nameSql) tablesbag = sbag['tablesGnr'] if tablesbag: for tbag in tablesbag.values(): table4D, tableSql = self.fromNameAs(tbag['name']) path4d = 'GNR_[%s]' % table4D if tableSql in namesdict: tableSql = tableSql + '_tbl' if tableSql.startswith('%s_' % sname): #strip the area code from table name tableSql = tableSql.lower().lstrip('_').split('_', 1)[1] result[path4d.lower()] = (sname, tableSql, None) for fldbag in tbag['fields'].values(): name4D, nameSql = self.fromNameAs(fldbag['name']) path4d = 'GNR_[%s]%s' % (table4D, name4D) result[path4d.lower()] = (sname, tableSql, nameSql) return result def build(self, configBag): for sname, sbag in self.folder4dstructBag.items(): sname = sname.lower() area = self.buildArea(sname, sbag) if area: #if an area has no tables don't build the folder at all sqlstructfile = os.path.join(self.modelFolder(sname), 'config_db.xml') area.toXml(sqlstructfile) configBag.setItem('packages.%s' % sname, None, alias=sname) def buildArea(self, sname, sbag): result = DbModelSrc.makeRoot() pkg = result.package(name=sname) tablesbag = sbag['tables4d'] exportArea = False if tablesbag: exportArea = True for tbag in tablesbag.values(): self.buildTable(pkg, tbag) tablesbag = sbag['tablesGnr'] if tablesbag: exportArea = True for tbag in tablesbag.values(): self.buildTable(pkg, tbag, mode='GNR') if exportArea: return result def buildTable(self, pkg, tbag, mode='4D'): name4D, name = self.fromNameAs(tbag['name']) name = self.nameConverter(table=name4D, mode=mode)[1] name_short = name comment = 'imported from 4d %s' % name4D pkey4d = tbag['pkey.name4d'] pkey = None if pkey4d: pkey = self.nameConverter(table=name4D, field=tbag['pkey.name4d'], mode=mode)[2] table = pkg.table(name=name, comment=comment, name_short=name_short, pkey=pkey) for fldbag in tbag['fields'].values(): self.buildField(table, fldbag) if 'extrafields' in tbag: for fldbag in tbag['extrafields'].values(): self.buildField(table, fldbag) def buildField(self, table, fldbag): name4D, name = self.fromNameAs(fldbag['name']) comment = 'imported from 4d %s' % name4D dtype = fldbag['type'] len_max = None size = None if dtype.startswith('A'): len_max = dtype[1:].strip().strip('_') len_max = str(int(len_max) + 2) dtype = 'A' if len_max: size = '0:%s' % len_max fld = table.column(name, dtype=dtype, name_long=name4D, comment=comment, unique=fldbag['unique'], indexed=fldbag['indexed'], size=size) if fldbag['relate']: case_insensitive = False sqltarget = self.nameConverter(table=fldbag['relate.table4d'], field=fldbag['relate.field4d']) if sqltarget: if (dtype == 'A' and int(len_max) > 10 and sqltarget[2].lower() != 'sy_id'): case_insensitive = True else: # no link to 4d table, try genro table sqltarget = self.nameConverter(table=fldbag['relate.tablegnr'], field=fldbag['relate.fieldgnr'], mode='GNR') case_insensitive = True if sqltarget: target = '%s.%s.%s' % (sqltarget[0], sqltarget[1], sqltarget[2]) fld.relation(target) else: print "Error: missing field \n%s" % str(fldbag['relate'])
def applyOnTreeNodeAttr(self, _record=None, **kwargs): docbag = Bag(_record.pop('docbag', None)) result = dict(_record=_record) for lang, content in docbag.items(): result['title_%s' % lang] = content['title'] return result
class Struct4D(object): def __init__(self, instance_folder, packages_folder=None): self.instance_folder = instance_folder self.folder4dstructBag = Bag(self.folder4dstruct + '/')['structure'] self.names4d = self.buildNames4d() self.packages_folder = packages_folder def _get_folder4d(self): path = os.path.join(self.instance_folder, 'sync4d') if not os.path.isdir(path): os.mkdir(path) return path folder4d = property(_get_folder4d) def _get_folder4dstruct(self): path = os.path.join(self.folder4d, 'structure') if not os.path.isdir(path): os.mkdir(path) return path folder4dstruct = property(_get_folder4dstruct) def areaFolder(self, area): path = os.path.join(self.packages_folder, area) print 'areaFolder:', path if not os.path.isdir(path): os.mkdir(path) return path def modelFolder(self, area): path = os.path.join(self.areaFolder(area), 'model') if not os.path.isdir(path): os.mkdir(path) return path def fromNameAs(self, name): if ' AS ' in name: name4d, nameSql = name.split(' AS ', 1) else: name4d, nameSql = name, name return name4d.strip(), nameSql.strip().lstrip('_').lower() def nameConverter(self, table='', field='', fullname=None, mode='4D'): if not fullname: fullname = '%s_[%s]%s' % (mode, table, field) else: fullname = '%s_%s' % (mode, fullname) return self.names4d.get(fullname.lower()) def get4dTables(self): return [ k[4:-1] for k, v in self.names4d.items() if k.startswith('4d') and not v[2] ] def buildNames4d(self): result = {} namesdict = {} for sname, sbag in self.folder4dstructBag.items(): sname = sname.lower() tablesbag = sbag['tables4d'] if tablesbag: for tbag in tablesbag.values(): table4D, tableSql = self.fromNameAs(tbag['name']) path4d = '4D_[%s]' % table4D namesdict[ tableSql] = None # in namesdict put the table with area name avoiding multi area duplicates if tableSql.startswith( '%s_' % sname): #strip the area code from table name tableSql = tableSql.lower().split('_', 1)[1] result[path4d.lower()] = (sname, tableSql, None) for fldbag in tbag['fields'].values(): name4D, nameSql = self.fromNameAs(fldbag['name']) path4d = '4D_[%s]%s' % (table4D, name4D) result[path4d.lower()] = (sname, tableSql, nameSql) tablesbag = sbag['tablesGnr'] if tablesbag: for tbag in tablesbag.values(): table4D, tableSql = self.fromNameAs(tbag['name']) path4d = 'GNR_[%s]' % table4D if tableSql in namesdict: tableSql = tableSql + '_tbl' if tableSql.startswith( '%s_' % sname): #strip the area code from table name tableSql = tableSql.lower().lstrip('_').split('_', 1)[1] result[path4d.lower()] = (sname, tableSql, None) for fldbag in tbag['fields'].values(): name4D, nameSql = self.fromNameAs(fldbag['name']) path4d = 'GNR_[%s]%s' % (table4D, name4D) result[path4d.lower()] = (sname, tableSql, nameSql) return result def build(self, configBag): for sname, sbag in self.folder4dstructBag.items(): sname = sname.lower() area = self.buildArea(sname, sbag) if area: #if an area has no tables don't build the folder at all sqlstructfile = os.path.join(self.modelFolder(sname), 'config_db.xml') area.toXml(sqlstructfile) configBag.setItem('packages.%s' % sname, None, alias=sname) def buildArea(self, sname, sbag): result = DbModelSrc.makeRoot() pkg = result.package(name=sname) tablesbag = sbag['tables4d'] exportArea = False if tablesbag: exportArea = True for tbag in tablesbag.values(): self.buildTable(pkg, tbag) tablesbag = sbag['tablesGnr'] if tablesbag: exportArea = True for tbag in tablesbag.values(): self.buildTable(pkg, tbag, mode='GNR') if exportArea: return result def buildTable(self, pkg, tbag, mode='4D'): name4D, name = self.fromNameAs(tbag['name']) name = self.nameConverter(table=name4D, mode=mode)[1] name_short = name comment = 'imported from 4d %s' % name4D pkey4d = tbag['pkey.name4d'] pkey = None if pkey4d: pkey = self.nameConverter(table=name4D, field=tbag['pkey.name4d'], mode=mode)[2] table = pkg.table(name=name, comment=comment, name_short=name_short, pkey=pkey) for fldbag in tbag['fields'].values(): self.buildField(table, fldbag) if 'extrafields' in tbag: for fldbag in tbag['extrafields'].values(): self.buildField(table, fldbag) def buildField(self, table, fldbag): name4D, name = self.fromNameAs(fldbag['name']) comment = 'imported from 4d %s' % name4D dtype = fldbag['type'] len_max = None size = None if dtype.startswith('A'): len_max = dtype[1:].strip().strip('_') len_max = str(int(len_max) + 2) dtype = 'A' if len_max: size = '0:%s' % len_max fld = table.column(name, dtype=dtype, name_long=name4D, comment=comment, unique=fldbag['unique'], indexed=fldbag['indexed'], size=size) if fldbag['relate']: case_insensitive = False sqltarget = self.nameConverter(table=fldbag['relate.table4d'], field=fldbag['relate.field4d']) if sqltarget: if (dtype == 'A' and int(len_max) > 10 and sqltarget[2].lower() != 'sy_id'): case_insensitive = True else: # no link to 4d table, try genro table sqltarget = self.nameConverter(table=fldbag['relate.tablegnr'], field=fldbag['relate.fieldgnr'], mode='GNR') case_insensitive = True if sqltarget: target = '%s.%s.%s' % (sqltarget[0], sqltarget[1], sqltarget[2]) fld.relation(target) else: print "Error: missing field \n%s" % str(fldbag['relate'])
def test_clear(self): b = Bag(BAG_DATA) b.clear() assert b.items() == []
def applyOnTreeNodeAttr(self,_record=None,**kwargs): docbag = Bag(_record.pop('docbag',None)) result = dict(_record=_record) for lang,content in docbag.items(): result['title_%s' %lang] = content['title'] return result