Пример #1
0
    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']
Пример #2
0
 def saveCurrentMover(self, data):
     moverpath = self.page.site.getStaticPath('user:temp', 'mover')
     indexpath = os.path.join(moverpath, 'index.xml')
     indexbag = Bag()
     if not os.path.isdir(moverpath):
         os.makedirs(moverpath)
     for movercode, table, pkeys, reftable, objtype in data.digest(
             '#k,#a.table,#a.pkeys,#a.reftable,#a.objtype'):
         pkeys = pkeys.keys()
         databag = self.db.table(table).toXml(pkeys=pkeys,
                                              rowcaption=True,
                                              path=os.path.join(
                                                  moverpath, 'data',
                                                  '%s.xml' % movercode))
         indexbag.setItem('movers.%s' % movercode,
                          None,
                          table=table,
                          count=len(pkeys),
                          reftable=reftable,
                          objtype=objtype)
         indexbag.setItem('records.%s' % movercode, None, table=table)
         for n in databag:
             indexbag.setItem('records.%s.%s' % (movercode, n.label),
                              None,
                              pkey=n.attr['pkey'],
                              caption=n.attr.get('caption'))
     indexbag.toXml(indexpath, autocreate=True)
Пример #3
0
    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']
Пример #4
0
    def saveLocalizationFile(self, data=None, **kwargs):
        changesdict = dict()
        for k, v in data['griddata'].items():
            filtered = v.filter(lambda n: '_loadedValue' in n.attr)
            if filtered:
                changesdict[v['_lockey']] = dict(filtered)
        localizer = self.db.application.localizer
        updatablebags = localizer.updatableLocBags['all' if self.isDeveloper(
        ) else 'unprotected']

        def cb(n, changedNodes=None, localizationDict=None):
            if n.label in changesdict:
                n.attr.update(changesdict[n.label])
                changedNodes.append(n.label)
                localizer_item = localizationDict.get(n.label)
                if localizer_item:
                    localizer_item.update(changesdict[n.label])

        for destFolder in updatablebags:
            locbagpath = os.path.join(destFolder, 'localization.xml')
            locbag = Bag(locbagpath) if os.path.exists(locbagpath) else Bag()
            changedNodes = []
            locbag.walk(cb,
                        changedNodes=changedNodes,
                        localizationDict=localizer.localizationDict)
            if changedNodes:
                locbag.toXml(locbagpath)
Пример #5
0
 def importTable(self, tbl):
     if len(tbl) > 23:
         tbl = tbl[:23]
     cmdpath = os.path.join(self.folderdialog4d, 'exp_%s.xml' % tbl)
     b = Bag()
     b['command'] = 'Export4dTables'
     b['t4d.tbl'] = tbl
     b.toXml(cmdpath)
Пример #6
0
 def importTable(self, tbl):
     if len(tbl) > 23:
         tbl = tbl[:23]
     cmdpath = os.path.join(self.folderdialog4d, 'exp_%s.xml' % tbl)
     b = Bag()
     b['command'] = 'Export4dTables'
     b['t4d.tbl'] = tbl
     b.toXml(cmdpath)
Пример #7
0
def build_instanceconfig_xml(path=None):
    instanceconfig_bag = Bag()
    instanceconfig_bag.setItem('packages',None)
    instanceconfig_bag.setItem('authentication.xml_auth',None, dict(defaultTags='user,xml'))
    password = get_random_password(size=6)
    instanceconfig_bag.setItem('authentication.xml_auth.admin',None, dict(pwd=password, tags='_DEV_,admin,user'))
    print "Default password for user admin is %s, you can change it by editing %s" %(password, path)
    instanceconfig_bag.toXml(path,typevalue=False,pretty=True)
Пример #8
0
def build_instanceconfig_xml(path=None):
    instanceconfig_bag = Bag()
    instanceconfig_bag.setItem('packages',None)
    instanceconfig_bag.setItem('authentication.xml_auth',None, dict(defaultTags='user,xml'))
    password = get_random_password(size=6)
    instanceconfig_bag.setItem('authentication.xml_auth.admin',None, dict(pwd=password, tags='_DEV_,admin,user'))
    print "Default password for user admin is %s, you can change it by editing %s" %(password, path)
    instanceconfig_bag.toXml(path,typevalue=False,pretty=True)
Пример #9
0
    def _structFix4D(self, struct, path):
        cnv_file = '%s_conv%s' % os.path.splitext(path)
        if os.path.isfile(cnv_file):
            return cnv_file
        cls = struct.__class__
        b = Bag()
        b.fromXml(path, bagcls=cls, empty=cls)

        convdict = {'ci_relation': None,
                    'o_name': None,
                    'o_name_short': None,
                    'o_name_full': None,
                    'o_name_long': None,
                    'many_name_short': None,
                    'many_name_full': None,
                    'many_name_long': None,
                    'eager_relation': None,
                    'len_max': None,
                    'len_min': None,
                    'len_show': None,
                    'relation': None,
                    'comment': None
        }
        #relate_attrs = set(('ci_relation', 'o_name', 'o_name_short', 'o_name_full', 'o_name_long',
        #                    'many_name_short','many_name_full','many_name_long','eager_relation'))

        for pkg in b['packages']:
            for tbl in pkg.value['tables']:
                for col in tbl.value['columns']:
                    newattrs = {}
                    for k, v in col.attr.items():
                        if v is not None:
                            lbl = convdict.get(k, k)
                            if lbl:
                                newattrs[lbl] = v
                    name_long = newattrs.get('name_long')
                    if name_long:
                        if name_long[0] == name_long[0].lower():
                            newattrs['group'] = '_'
                        if name_long.endswith('_I'):
                            name_long = name_long[:-2]
                        elif not 'indexed' in newattrs:
                            newattrs['group'] = '*'
                        if len(name_long) > 2 and name_long[2] == '_':
                            name_long = name_long[3:]
                        newattrs['name_long'] = name_long.replace('_', ' ')

                    if 'len_max' in col.attr:
                        newattrs['size'] = '%s:%s' % (col.attr.get('len_min', '0'), col.attr['len_max'])
                    if 'relation' in col.attr:
                        mode = None
                        if col.attr.get('ci_relation'):
                            mode = 'insensitive'
                        col.value = Bag()
                        col.value.setItem('relation', None, related_column=col.attr['relation'], mode=mode)
                    col.attr = newattrs
        b.toXml(cnv_file,mode4d=True)
        return cnv_file
Пример #10
0
 def updateLocalizationFiles(self,scan_all=True):
     for s in self.slots:
         if scan_all or s['destFolder'] != self.genroroot:
             locbag = Bag()
             for root in s['roots']:
                 d = DirectoryResolver(root,include='*.py,*.js')()
                 d.walk(self._updateModuleLocalization,locbag=locbag,_mode='deep',destFolder=s['destFolder'] )
             locbag.toXml(os.path.join(s['destFolder'],'localization.xml'),pretty=True,typeattrs=False, typevalue=False)
     self.buildLocalizationDict()
Пример #11
0
def build_siteconfig_xml(path=None, gnrdaemon_password=None):
    siteconfig_bag = Bag()
    siteconfig_bag.setItem('wsgi', None, dict(debug=True, reload=True, port='8080'))
    siteconfig_bag.setItem('gui', None, dict(css_theme='ludo'))
    siteconfig_bag.setItem('jslib', None, dict(dojo_version='11', gnr_version='11'))
    siteconfig_bag.setItem('resources.common', None)
    siteconfig_bag.setItem('resources.js_libs', None)
    siteconfig_bag.setItem('gnrdaemon', None, dict(host='localhost', port='40404', hmac_key=gnrdaemon_password))
    siteconfig_bag.toXml(path,typevalue=False,pretty=True)
Пример #12
0
 def rpc_thermoProgress(self, thermoId, stop=None):
     try:
         thermoBag = Bag(self.pageLocalDocument('thermo_%s' % thermoId))
         if stop:
             thermoBag['stop'] = True
             thermoBag.toXml(self.pageLocalDocument('thermo_%s' % thermoId), autocreate=True)
         return thermoBag
     except:
         pass
Пример #13
0
def build_siteconfig_xml(path=None, gnrdaemon_password=None):
    siteconfig_bag = Bag()
    siteconfig_bag.setItem('wsgi', None, dict(debug=True, reload=True, port='8080'))
    siteconfig_bag.setItem('gui', None, dict(css_theme='ludo'))
    siteconfig_bag.setItem('jslib', None, dict(dojo_version='11', gnr_version='11'))
    siteconfig_bag.setItem('resources.common', None)
    siteconfig_bag.setItem('resources.js_libs', None)
    siteconfig_bag.setItem('gnrdaemon', None, dict(host='localhost', port='40404', hmac_key=gnrdaemon_password))
    siteconfig_bag.toXml(path,typevalue=False,pretty=True)
Пример #14
0
 def onDroppedMover(self,file_path=None):
     import tarfile
     f = tarfile.open(file_path)
     f.extractall(self.page.site.getStaticPath('user:temp'))        
     os.remove(file_path)
     indexpath = self.page.site.getStaticPath('user:temp','mover','index.xml')
     indexbag = Bag(indexpath)
     indexbag.getNode('movers').attr.update(imported=True)
     indexbag.toXml(indexpath)
Пример #15
0
 def rpc_thermoProgress(self, thermoId, stop=None):
     try:
         thermoBag = Bag(self.pageLocalDocument('thermo_%s' % thermoId))
         if stop:
             thermoBag['stop'] = True
             thermoBag.toXml(self.pageLocalDocument('thermo_%s' % thermoId), autocreate=True)
         return thermoBag
     except:
         pass
Пример #16
0
 def onDroppedMover(self, file_path=None):
     import tarfile
     f = tarfile.open(file_path)
     f.extractall(self.page.site.getStaticPath('user:temp'))
     os.remove(file_path)
     indexpath = self.page.site.getStaticPath('user:temp', 'mover',
                                              'index.xml')
     indexbag = Bag(indexpath)
     indexbag.getNode('movers').attr.update(imported=True)
     indexbag.toXml(indexpath)
Пример #17
0
 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)
Пример #18
0
 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)
Пример #19
0
    def do(self):
        """add???"""
        self.site_path = os.path.join(self.base_path, self.site_name)
        pages_path = os.path.join(self.site_path, 'pages')
        root_py_path = os.path.join(self.site_path, 'root.py')
        siteconfig_xml_path = os.path.join(self.site_path, 'siteconfig.xml')
        if not os.path.isdir(self.site_path):
            os.mkdir(self.site_path)
        if not os.path.isdir(pages_path):
            os.mkdir(pages_path)
        if not os.path.isfile(root_py_path):
            root_py = open(root_py_path, 'w')
            root_py.write("""#!/usr/bin/env python2.6
import sys
sys.stdout = sys.stderr
from gnr.web.gnrwsgisite import GnrWsgiSite
site = GnrWsgiSite(__file__)

def application(environ,start_response):
    return site(environ,start_response)

if __name__ == '__main__':
    from gnr.web.server import NewServer
    server=NewServer(__file__)
    server.run()""")
            root_py.close()
        if not os.path.isfile(siteconfig_xml_path):
            if not self.config:
                siteconfig = Bag()
                if self.instance:
                    siteconfig.setItem('instances.%s' % self.instance, None)
                for resource in self.resources:
                    if isinstance(resource, tuple) or isinstance(
                            resource, list):
                        resource, resource_path = resource
                        siteconfig.setItem('resources.%s' % resource,
                                           None,
                                           path=resource_path)
                    else:
                        siteconfig.setItem('resources.%s' % resource, None)
                wsgi_options = dict()
                for option in ('reload', 'debug', 'port', 'mainpackage'):
                    value = getattr(self, 'wsgi_%s' % option, None)
                    if value:
                        wsgi_options[option] = value
                siteconfig.setItem('wsgi', None, **wsgi_options)
                siteconfig['connection_timeout'] = None
                siteconfig['connection_refresh'] = None
                siteconfig.setItem('dojo', None, version=self.dojo_version)
            else:
                siteconfig = self.config
            siteconfig.toXml(siteconfig_xml_path)
Пример #20
0
 def do(self):
     """TODO"""
     self.instance_path = os.path.join(self.base_path, self.instance_name)
     custom_path = os.path.join(self.instance_path, 'custom')
     data_path = os.path.join(self.instance_path, 'data')
     instanceconfig_xml_path = os.path.join(self.instance_path,
                                            'instanceconfig.xml')
     folders_to_make = [self.instance_path, custom_path, data_path]
     if self.use_dbstores:
         dbstores_path = os.path.join(self.instance_path, 'dbstores')
         folders_to_make.append(dbstores_path)
     for path in folders_to_make:
         if not os.path.isdir(path):
             os.mkdir(path)
     if not os.path.isfile(instanceconfig_xml_path):
         if not self.config:
             instanceconfig = Bag()
             db_options = dict()
             for option in ('dbname', 'implementation', 'host', 'port',
                            'username', 'password'):
                 value = getattr(self, 'db_%s' % option, None)
                 if value:
                     db_options[option] = value
             instanceconfig.setItem('db', None, **db_options)
             instanceconfig.setItem('packages', None)
             for package in self.packages:
                 if isinstance(package, tuple) or isinstance(package, list):
                     package, package_path = package
                     instanceconfig.setItem('packages.%s' %
                                            package.replace(':', '_'),
                                            None,
                                            path=package_path,
                                            pkgcode=package)
                 else:
                     instanceconfig.setItem('packages.%s' %
                                            package.replace(':', '_'),
                                            None,
                                            pkgcode=package)
             if self.authentication:
                 instanceconfig.setItem('authentication',
                                        None,
                                        pkg=self.authentication_pkg)
                 instanceconfig.setItem('authentication.py_auth',
                                        None,
                                        defaultTags="user",
                                        pkg="adm",
                                        method="authenticate")
         else:
             instanceconfig = self.config
         instanceconfig.toXml(instanceconfig_xml_path,
                              typevalue=False,
                              pretty=True)
Пример #21
0
 def syncOutTransaction(self, transaction):
     fname = '%s_%s_%s_%s.xml' % (
         transaction['request'].strftime('%Y-%m-%d_%H%M%S'),
         transaction['request'].microsecond, transaction['maintable'],
         transaction['action'])
     fname = os.path.join(self.folderdialog4d, 'test', fname)
     trbag = Bag()
     trbag['command'] = 'sync_in'
     trbag['maintable'] = transaction['maintable']
     trbag['action'] = transaction['action']
     trbag['data'] = Bag(transaction['data'])
     trbag.toXml(fname)
     self.db.table('gnr.sync_out').delete(transaction)
     self.db.commit()
Пример #22
0
 def syncOutTransaction(self, transaction):
     fname = '%s_%s_%s_%s.xml' % (transaction['request'].strftime('%Y-%m-%d_%H%M%S'),
                                  transaction['request'].microsecond,
                                  transaction['maintable'],
                                  transaction['action'])
     fname = os.path.join(self.folderdialog4d, 'test', fname)
     trbag = Bag()
     trbag['command'] = 'sync_in'
     trbag['maintable'] = transaction['maintable']
     trbag['action'] = transaction['action']
     trbag['data'] = Bag(transaction['data'])
     trbag.toXml(fname)
     self.db.table('gnr.sync_out').delete(transaction)
     self.db.commit()
Пример #23
0
 def xmlDump(self, path):
     """add???
     
     :param path: add???
     """
     filepath = os.path.join(path, '%s_dump.xml' % self.name)
     records = self.query(excludeLogicalDeleted=False).fetch()
     result = Bag()
     
     for r in records:
         r = dict(r)
         pkey = r.pop('pkey')
         result['records.%s' % pkey.replace('.', '_')] = Bag(r)
     result.toXml(filepath, autocreate=True)
Пример #24
0
    def do(self):
        """TODO"""
        self.site_path = os.path.join(self.base_path, self.site_name)
        pages_path = os.path.join(self.site_path, 'pages')
        root_py_path = os.path.join(self.site_path, 'root.py')
        siteconfig_xml_path = os.path.join(self.site_path, 'siteconfig.xml')
        if not os.path.isdir(self.site_path):
            os.mkdir(self.site_path)
        if not os.path.isdir(pages_path):
            os.mkdir(pages_path)
        if not os.path.isfile(root_py_path):
            root_py = open(root_py_path, 'w')
            root_py.write("""#!/usr/bin/env python2.6
import sys
sys.stdout = sys.stderr
from gnr.web.gnrwsgisite import GnrWsgiSite
site = GnrWsgiSite(__file__)

def application(environ,start_response):
    return site(environ,start_response)

if __name__ == '__main__':
    from gnr.web.server import NewServer
    server=NewServer(__file__)
    server.run()""")
            root_py.close()
        if not os.path.isfile(siteconfig_xml_path):
            if not self.config:
                siteconfig = Bag()
                if self.instance:
                    siteconfig.setItem('instances.%s' % self.instance, None)
                for resource in self.resources:
                    if isinstance(resource, tuple) or isinstance(resource, list):
                        resource, resource_path = resource
                        siteconfig.setItem('resources.%s' % resource, None, path=resource_path)
                    else:
                        siteconfig.setItem('resources.%s' % resource, None)
                wsgi_options = dict()
                for option in ('reload', 'debug', 'port', 'mainpackage'):
                    value = getattr(self, 'wsgi_%s' % option, None)
                    if value:
                        wsgi_options[option] = value
                siteconfig.setItem('wsgi', None, **wsgi_options)
                siteconfig['connection_timeout'] = None
                siteconfig['connection_refresh'] = None
                siteconfig.setItem('dojo', None, version=self.dojo_version)
            else:
                siteconfig = self.config
            siteconfig.toXml(siteconfig_xml_path,typevalue=False,pretty=True)
Пример #25
0
 def saveCurrentMover(self,data):
     moverpath = self.page.site.getStaticPath('user:temp','mover')
     indexpath = os.path.join(moverpath,'index.xml')
     indexbag = Bag()
     if not os.path.isdir(moverpath):
         os.makedirs(moverpath)
     for movercode,table,pkeys,reftable,objtype in data.digest('#k,#a.table,#a.pkeys,#a.reftable,#a.objtype'):
         pkeys = pkeys.keys()
         databag = self.db.table(table).toXml(pkeys=pkeys,rowcaption=True,
                                                 path=os.path.join(moverpath,'data','%s.xml' %movercode))
         indexbag.setItem('movers.%s' %movercode,None,table=table,count=len(pkeys),reftable=reftable,objtype=objtype)
         indexbag.setItem('records.%s' %movercode,None,table=table)
         for n in databag:
             indexbag.setItem('records.%s.%s' %(movercode,n.label),None,pkey=n.attr['pkey'],caption=n.attr.get('caption')) 
     indexbag.toXml(indexpath,autocreate=True)
Пример #26
0
 def _result_write(self, result=None, result_attr=None, error=None, error_attr=None):
     result_doc = Bag()
     result_doc['title'] = self.title
     result_doc['batch_id'] = self.batch_id
     result_doc['owner_page_id'] = self.page.page_id
     result_doc['note'] = self.note
     result_doc['start_ts'] = self.start_ts
     result_doc['end_ts'] = datetime.now()
     result_doc['time_delta'] = str(result_doc['end_ts'] - result_doc['start_ts']).split('.')[0]
     if result is not None:
         result_doc.setItem('result', result, _attributes=result_attr)
     if error is not None:
         result_doc.setItem('error', error, _attributes=error_attr)
     result_doc.toXml(self.result_doc_path, autocreate=True)
     return result_doc
Пример #27
0
 def updateLocalizationFiles(self, scan_all=True):
     for s in self.slots:
         if scan_all or s['destFolder'] != self.genroroot:
             locbag = Bag()
             for root in s['roots']:
                 d = DirectoryResolver(root, include='*.py,*.js')()
                 d.walk(self._updateModuleLocalization,
                        locbag=locbag,
                        _mode='deep',
                        destFolder=s['destFolder'])
             locbag.toXml(os.path.join(s['destFolder'], 'localization.xml'),
                          pretty=True,
                          typeattrs=False,
                          typevalue=False)
     self.buildLocalizationDict()
Пример #28
0
 def extdb_buildTableModules(self,project=None,package=None,instance=None,data=None,connection_params=None,**kwargs):
     p = PathResolver()
     project_path = p.project_name_to_path(project)
     modelpath = os.path.join(project_path,'packages',package,'model')
     instance_path = os.path.join(p.instance_name_to_path(instance),'instanceconfig.xml')
     instance_bag = Bag(instance_path)
     dbname = connection_params['dbname'] or connection_params['filename']
     legacydb,ext = os.path.splitext(os.path.basename(dbname))
     parsdict = dict()
     for k,v in connection_params.items():
         if v not in ('',None):
             parsdict[k] = v
     instance_bag.setItem('legacy_db.%s' %legacydb,None,**parsdict)
     instance_bag.toXml(instance_path,typevalue=False,pretty=True)
     for srcpkg,tables in data.items():
         for tablename,columns in self.utils.quickThermo(tables.items(),labelcb=lambda r: '%s.%s' %(srcpkg,r[0]),maxidx=len(tables)):
             firstColAttr = columns.getAttr('#0')
             tablename = tablename.lower()
             tablename = tablename.replace(' ','_').replace('.','_')
             table_data = Bag()
             table_data['name'] = tablename
             table_data['legacy_name'] = firstColAttr.get('table_fullname')
             table_data['legacy_db'] = legacydb
             pkey = firstColAttr.get('table_pkey')
             if pkey:
                 pkey = pkey.lower()
             table_data['pkey'] = pkey or None
             columns_bag = Bag()
             table_data['_columns'] = columns_bag
             caption_field = None
             for colattr in columns.digest('#a'):
                 legacy_name = colattr['name']
                 if legacy_name=='_multikey':
                     legacy_name = None
                 colname = colattr['name'].lower()
                 dtype=colattr.get('dtype')
                 if not caption_field and dtype in ('A','T','C') and colname!=pkey:
                     caption_field = colname
                 b = Bag(dict(name=colname,legacy_name=legacy_name,
                                                     name_long=None,dtype=dtype,
                                                     size=str(colattr.get('size')) if colattr.get('size') else None,
                                                     indexed=colattr.get('indexed'),
                                                     unique=colattr.get('unique')))
                 columns_bag.setItem(colname,b)
                 if colattr.get('relate_to'):
                     b.setItem('_relation',Bag(dict(relation=colattr['relate_to'].lower(),onDelete='raise')))
             table_data['caption_field'] = caption_field
             self.makeOneTable(os.path.join(modelpath,'%s.py' %tablename),table_data=table_data)
Пример #29
0
 def audit(self,tblobj,event,audit_mode=None,record=None,old_record=None,username=None,page=None):
     if event=='I' and audit_mode=='lazy':
         return
     if not username:
         username = self.db.currentEnv.get('user') 
     version = record.get('__version',0)
     audit_record = dict(tablename=tblobj.fullname,event=event,username=username,
                   record_pkey=record[tblobj.pkey],version=version,transaction_id=self.db.currentEnv.get('env_transaction_id'))
     if event == 'I' or event=='D':
         audit_record['data'] = self.getXmlRecord(record)
     if event == 'U':
         assert old_record, 'Missing old_record in an update that uses audit feature tbl: %s' %tblobj.fullname
         changes = Bag()
         for k in record.keys():
             if k in ('__version','__mod_ts'):
                 continue
             if record[k] != old_record.get(k):
                 changes[k] = record[k]
         audit_record['data'] = changes.toXml()
         if audit_mode=='lazy' and version==1:
             first_audit = dict(audit_record)
             first_audit['version'] = 0
             first_audit['event'] = 'I'
             first_audit['data'] = self.getXmlRecord(old_record)
             self.insert(first_audit)
     self.insert(audit_record)
Пример #30
0
 def audit(self,tblobj,event,audit_mode=None,record=None,old_record=None,username=None,page=None):
     if event=='I' and audit_mode=='lazy':
         return
     if not username:
         username = self.db.currentEnv.get('user') 
     version = record.get('__version',0)
     audit_record = dict(tablename=tblobj.fullname,event=event,username=username,
                   record_pkey=record[tblobj.pkey],version=version,transaction_id=self.db.currentEnv.get('env_transaction_id'))
     if event == 'I' or event=='D':
         audit_record['data'] = self.getXmlRecord(record)
     if event == 'U':
         assert old_record, 'Missing old_record in an update that uses audit feature tbl: %s' %tblobj.fullname
         changes = Bag()
         for k in record.keys():
             if k in ('__version','__mod_ts'):
                 continue
             if record[k] != old_record.get(k):
                 changes[k] = record[k]
         audit_record['data'] = changes.toXml()
         if audit_mode=='lazy' and version==1:
             first_audit = dict(audit_record)
             first_audit['version'] = 0
             first_audit['event'] = 'I'
             first_audit['data'] = self.getXmlRecord(old_record)
             self.insert(first_audit)
     self.insert(audit_record)
Пример #31
0
    def result_bag(self, result):
        error = getattr(self, 'error', None)
        page = self.page
        envelope = Bag()
        resultAttrs = {}
        if isinstance(result, tuple):
            resultAttrs = result[1]
            if len(result) == 3 and isinstance(result[2], Bag):
                page.setInClientData(result[2])
            result = result[0]
            if resultAttrs is not None:
                envelope['resultType'] = 'node'
        if error:
            envelope['error'] = error
        if isinstance(result, page.domSrcFactory):
            resultAttrs['__cls'] = 'domsource'
        if page.isLocalizer():
            envelope['_localizerStatus'] = '*_localizerStatus*'
        envelope.setItem('result', result, _attributes=resultAttrs)
        dataChanges = self.page.collectClientDatachanges()
        if dataChanges:
            envelope.setItem('dataChanges', dataChanges)
        page.response.content_type = "text/xml"
        xmlresult = envelope.toXml(unresolved=True,
                                   translate_cb=page.localizer.translateText,
                                   omitUnknownTypes=True,
                                   catalog=page.catalog)
        if page.isLocalizer():
            xmlresult = xmlresult.replace('*_localizerStatus*',
                                          page.localizer.status)

        return xmlresult
Пример #32
0
 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)
Пример #33
0
    def result_bag(self, result):
        error = getattr(self, 'error', None)
        page = self.page
        envelope = Bag()
        resultAttrs = {}
        if isinstance(result, tuple):
            resultAttrs = result[1]
            if len(result) == 3 and isinstance(result[2], Bag):
                page.setInClientData(result[2])
            result = result[0]
            if resultAttrs is not None:
                envelope['resultType'] = 'node'
        if error:
            envelope['error'] = error
        if isinstance(result, page.domSrcFactory):
            resultAttrs['__cls'] = 'domsource'
        if page.isLocalizer():
            envelope['_localizerStatus'] = '*_localizerStatus*'
        envelope.setItem('result', result, _attributes=resultAttrs)
        dataChanges = self.page.collectClientDatachanges()
        if dataChanges:
            envelope.setItem('dataChanges', dataChanges)
        page.response.content_type = "text/xml"
        xmlresult = envelope.toXml(unresolved=True,
                                   translate_cb=page.localizer.translateText, omitUnknownTypes=True,
                                   catalog=page.catalog)
        if page.isLocalizer():
            xmlresult = xmlresult.replace('*_localizerStatus*', page.localizer.status)

        return xmlresult
Пример #34
0
    def _translateMissing(self, txt):
        if not self.page.packageId: return
        missingpath = os.path.join(self.page.siteFolder, 'data', '_missingloc', self.page.packageId)
        if isinstance(txt, unicode):
            txtmd5 = txt.encode('utf8', 'ignore')
        else:
            txtmd5 = txt
        fname = os.path.join(missingpath, '%s.xml' % hashlib.md5(txtmd5).hexdigest())

        if not os.path.isfile(fname):
            b = Bag()
            b['txt'] = txt
            b['pkg'] = self.page.packageId
            old_umask = os.umask(2)
            b.toXml(fname, autocreate=True)
            os.umask(old_umask)
Пример #35
0
    def _translateMissing(self, txt):
        if not self.page.packageId: return
        missingpath = os.path.join(self.page.siteFolder, 'data', '_missingloc',
                                   self.page.packageId)
        if isinstance(txt, unicode):
            txtmd5 = txt.encode('utf8', 'ignore')
        else:
            txtmd5 = txt
        fname = os.path.join(missingpath,
                             '%s.xml' % hashlib.md5(txtmd5).hexdigest())

        if not os.path.isfile(fname):
            b = Bag()
            b['txt'] = txt
            b['pkg'] = self.page.packageId
            old_umask = os.umask(2)
            b.toXml(fname, autocreate=True)
            os.umask(old_umask)
Пример #36
0
def build_environment_xml(path=None, gnrpy_path=None, gnrdaemon_password=None):
    genropy_home = os.path.dirname(gnrpy_path)
    genropy_projects = os.path.join(genropy_home,'projects')
    genropy_tutorial_projects = os.path.join(genropy_home,'tutorial','projects')
    genropy_packages = os.path.join(genropy_home,'packages')
    genropy_resources = os.path.join(genropy_home,'resources')
    genropy_webtools = os.path.join(genropy_home,'webtools')
    dojo_11_path = os.path.join(genropy_home, 'dojo_libs', 'dojo_11')
    gnr_d11_path = os.path.join(genropy_home,'gnrjs', 'gnr_d11')
    environment_bag = Bag()
    environment_bag.setItem('environment.gnrhome', None, dict(value=genropy_home))
    environment_bag.setItem('projects.genropy', None, dict(path=genropy_projects))
    environment_bag.setItem('projects.genropy_tutorial', None, dict(path=genropy_tutorial_projects))
    environment_bag.setItem('packages.genropy', None, dict(path=genropy_packages))
    environment_bag.setItem('static.js.dojo_11',None, dict(path=dojo_11_path, cdn=""))
    environment_bag.setItem('static.js.gnr_11', None, dict(path=gnr_d11_path))
    environment_bag.setItem('resources.genropy', None, dict(path=genropy_resources))
    environment_bag.setItem('webtools.genropy', None, dict(path=genropy_webtools))
    environment_bag.setItem('gnrdaemon', None, dict(host='localhost', port='40404', hmac_key=gnrdaemon_password))
    environment_bag.toXml(path,typevalue=False,pretty=True)
Пример #37
0
 def _result_write(self,
                   result=None,
                   result_attr=None,
                   error=None,
                   error_attr=None):
     result_doc = Bag()
     result_doc['title'] = self.title
     result_doc['batch_id'] = self.batch_id
     result_doc['owner_page_id'] = self.page.page_id
     result_doc['note'] = self.note
     result_doc['start_ts'] = self.start_ts
     result_doc['end_ts'] = datetime.now()
     result_doc['time_delta'] = str(result_doc['end_ts'] -
                                    result_doc['start_ts']).split('.')[0]
     if result is not None:
         result_doc.setItem('result', result, _attributes=result_attr)
     if error is not None:
         result_doc.setItem('error', error, _attributes=error_attr)
     result_doc.toXml(self.result_doc_path, autocreate=True)
     return result_doc
Пример #38
0
 def saveCurrentMover(self, data):
     moverpath = self.page.site.getStaticPath("user:temp", "mover")
     indexpath = os.path.join(moverpath, "index.xml")
     indexbag = Bag()
     if not os.path.isdir(moverpath):
         os.makedirs(moverpath)
     for movercode, table, pkeys, reftable, objtype in data.digest("#k,#a.table,#a.pkeys,#a.reftable,#a.objtype"):
         pkeys = pkeys.keys()
         databag = self.db.table(table).toXml(
             pkeys=pkeys, rowcaption=True, path=os.path.join(moverpath, "data", "%s.xml" % movercode)
         )
         indexbag.setItem(
             "movers.%s" % movercode, None, table=table, count=len(pkeys), reftable=reftable, objtype=objtype
         )
         indexbag.setItem("records.%s" % movercode, None, table=table)
         for n in databag:
             indexbag.setItem(
                 "records.%s.%s" % (movercode, n.label), None, pkey=n.attr["pkey"], caption=n.attr.get("caption")
             )
     indexbag.toXml(indexpath, autocreate=True)
Пример #39
0
def build_environment_xml(path=None, gnrpy_path=None, gnrdaemon_password=None):
    genropy_home = os.path.dirname(gnrpy_path)
    genropy_projects = os.path.join(genropy_home,'projects')
    custom_projects = os.path.normpath(os.path.join(genropy_home,'..','genropy_projects'))
    create_folder(custom_projects)
    genropy_packages = os.path.join(genropy_home,'packages')
    genropy_resources = os.path.join(genropy_home,'resources')
    genropy_webtools = os.path.join(genropy_home,'webtools')
    dojo_11_path = os.path.join(genropy_home, 'dojo_libs', 'dojo_11')
    gnr_d11_path = os.path.join(genropy_home,'gnrjs', 'gnr_d11')
    environment_bag = Bag()
    environment_bag.setItem('environment.gnrhome', None, dict(value=genropy_home))
    environment_bag.setItem('projects.genropy', None, dict(path=genropy_projects))
    environment_bag.setItem('projects.custom', None, dict(path=custom_projects))
    environment_bag.setItem('packages.genropy', None, dict(path=genropy_packages))
    environment_bag.setItem('static.js.dojo_11',None, dict(path=dojo_11_path, cdn=""))
    environment_bag.setItem('static.js.gnr_11', None, dict(path=gnr_d11_path))
    environment_bag.setItem('resources.genropy', None, dict(path=genropy_resources))
    environment_bag.setItem('webtools.genropy', None, dict(path=genropy_webtools))
    environment_bag.setItem('gnrdaemon', None, dict(host='localhost', port='40404', hmac_key=gnrdaemon_password))
    environment_bag.toXml(path,typevalue=False,pretty=True)
Пример #40
0
 def makeNewPackage(self,package_name=None,name_long=None,is_main_package=None,project_name=None):
     path_resolver = PathResolver()
     project_path = path_resolver.project_name_to_path(project_name)
     packagespath = os.path.join(project_path,'packages')
     instances = os.path.join(project_path,'instances')
     sites = os.path.join(project_path,'sites')
     package_maker = PackageMaker(package_name,base_path=packagespath,helloworld=True,name_long=name_long)
     package_maker.do()
     if os.path.exists(instances):
         for d in os.listdir(instances):
             configpath = os.path.join(instances,d,'instanceconfig.xml')
             if os.path.isfile(configpath):
                 b = Bag(configpath)
                 b.setItem('packages.%s' %package_name,'')
                 b.toXml(configpath,typevalue=False,pretty=True)
     if os.path.exists(sites):
         for d in os.listdir(sites):
             configpath = os.path.join(sites,d,'siteconfig.xml')
             if os.path.isfile(configpath):
                 b = Bag(configpath)
                 n = b.getNode('wsgi')
                 n.attr['mainpackage'] = package_name
                 b.toXml(configpath,typevalue=False,pretty=True)
     return package_name
Пример #41
0
 def do(self):
     """TODO"""
     self.instance_path = os.path.join(self.base_path, self.instance_name)
     custom_path = os.path.join(self.instance_path, 'custom')
     data_path = os.path.join(self.instance_path, 'data')
     instanceconfig_xml_path = os.path.join(self.instance_path, 'instanceconfig.xml')
     folders_to_make = [self.instance_path, custom_path, data_path]
     if self.use_dbstores:
         dbstores_path = os.path.join(self.instance_path, 'dbstores')
         folders_to_make.append(dbstores_path)
     for path in folders_to_make:
         if not os.path.isdir(path):
             os.mkdir(path)
     if not os.path.isfile(instanceconfig_xml_path):
         if not self.config:
             instanceconfig = Bag()
             db_options = dict()
             for option in ('dbname', 'implementation', 'host', 'port', 'username', 'password'):
                 value = getattr(self, 'db_%s' % option, None)
                 if value:
                     db_options[option] = value
             instanceconfig.setItem('db', None, **db_options)
             instanceconfig.setItem('packages', None)
             for package in self.packages:
                 if isinstance(package, tuple) or isinstance(package, list):
                     package, package_path = package
                     instanceconfig.setItem('packages.%s' % package.replace(':','_'), None, path=package_path,pkgcode=package)
                 else:
                     instanceconfig.setItem('packages.%s' % package.replace(':','_'), None,pkgcode=package)
             if self.authentication:
                 instanceconfig.setItem('authentication', None, pkg=self.authentication_pkg)
                 instanceconfig.setItem('authentication.py_auth', None, defaultTags="user", pkg="adm",
                                        method="authenticate")
         else:
             instanceconfig = self.config
         instanceconfig.toXml(instanceconfig_xml_path,typevalue=False,pretty=True)
Пример #42
0
    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()
Пример #43
0
 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)
Пример #44
0
 def result_bag(self, result):
     error = getattr(self, 'error', None)
     page = self.page
     envelope = Bag()
     resultAttrs = {}
     if isinstance(result, BagNode):
         envelope['resultType'] = 'node'
         resultAttrs = result.attr
         result = result.getValue()
     if isinstance(result, tuple):
         resultAttrs = result[1]
         if len(result) == 3 and isinstance(result[2], Bag):
             page.setInClientData(result[2])
         result = result[0]
         if resultAttrs is not None:
             envelope['resultType'] = 'node'
     if error:
         envelope['error'] = error
     if isinstance(result, page.domSrcFactory):
         resultAttrs['__cls'] = 'domsource'
         self.checkNotAllowed(result)
     if page.isLocalizer():
         envelope['_localizerStatus'] = '*_localizerStatus*'
     for k in dir(page):
         if k.startswith('envelope_'):
             v = getattr(page, k)
             if v is not None:
                 envelope.setItem(k[9:], v)
     envelope.setItem('result', result, _attributes=resultAttrs)
     if not getattr(page, '_closed', False):
         dataChanges = self.page.collectClientDatachanges()
         if dataChanges:
             envelope.setItem('dataChanges', dataChanges)
     page.response.content_type = "text/xml"
     t0 = time()
     xmlresult = envelope.toXml(unresolved=True,
                                translate_cb=page.localize,
                                omitUnknownTypes=True,
                                catalog=page.catalog)
     page.xml_deltatime = int((time() - t0) * 1000)
     page.xml_size = len(xmlresult)
     return xmlresult
Пример #45
0
 def result_bag(self, result):
     error = getattr(self, 'error', None)
     page = self.page
     envelope = Bag()
     resultAttrs = {}
     if isinstance(result, BagNode):
         envelope['resultType'] = 'node'
         resultAttrs = result.attr
         result = result.getValue()
     if isinstance(result, tuple):
         resultAttrs = result[1]
         if len(result) == 3 and isinstance(result[2], Bag):
             page.setInClientData(result[2])
         result = result[0]
         if resultAttrs is not None:
             envelope['resultType'] = 'node'
     if error:
         envelope['error'] = error
     if isinstance(result, page.domSrcFactory):
         resultAttrs['__cls'] = 'domsource'
         self.checkNotAllowed(result)
     if page.isLocalizer():
         envelope['_localizerStatus'] = '*_localizerStatus*'
     for k in dir(page):
         if k.startswith('envelope_'):
             v = getattr(page,k)
             if v is not None:
                 envelope.setItem(k[9:],v)
     envelope.setItem('result', result, _attributes=resultAttrs)
     if not getattr(page,'_closed',False):
         dataChanges = self.page.collectClientDatachanges()
         if dataChanges:
             envelope.setItem('dataChanges', dataChanges)
     page.response.content_type = "text/xml"
     t0 = time()
     xmlresult = envelope.toXml(unresolved=True,
                                translate_cb=page.localize, omitUnknownTypes=True,
                                catalog=page.catalog)
     page.xml_deltatime = int((time()-t0)*1000)
     page.xml_size = len(xmlresult)
     return xmlresult
Пример #46
0
    def sendCommandToPage(self,page_id,command,data):
        headers = {'Content-type': 'application/x-www-form-urlencoded'}
        envelope=Bag(dict(command=command,data=data))

        body = urllib.urlencode(dict(page_id=page_id,envelope=envelope.toXml(unresolved=True)))
        #self.socketConnection.request('POST',self.proxyurl,headers=headers, body=body)

        n = MAX_CONNECTION_ATTEMPT
        error = CONNECTION_REFUSED
        while n>0 and error==CONNECTION_REFUSED:
            try:
                self.socketConnection.request('POST',self.proxyurl,headers=headers, body=body)
                error = False
                if n!=MAX_CONNECTION_ATTEMPT:
                    print 'SUCCEED'
            except socket.error, e:
                error = e.errno
                if error == CONNECTION_REFUSED:
                    n -= 1
                    print 'attempting',n
                    sleep(CONNECTION_ATTEMPT_DELAY)
                else:
                    raise
Пример #47
0
    def tableImporterCheck(self,table=None,file_path=None,limit=None,**kwargs):
        result = Bag()

        result['imported_file_path'] = file_path
        reader = self.getReader(file_path)
        columns = Bag()
        rows = Bag()
        match_data = Bag()
        result['columns'] = columns
        result['rows'] = rows
        result['match_data'] = match_data
        table_col_list = []
        legacy_match = dict()
        if table:
            tblobj = self.page.db.table(table)
            for colname,colobj in tblobj.model.columns.items():
                table_col_list.append(colname)
                if colobj.attributes.get('legacy_name'):
                    legacy_match[colobj.attributes['legacy_name']] = colname
            result['methodlist'] = ','.join([k[9:] for k in dir(tblobj) if k.startswith('importer_')])
        for k,i in sorted(reader.index.items(),key=lambda tup:tup[1]):
            columns.setItem(k,None,name=k,field=k,width='10em')
            if k in table_col_list:
                dest_field = k 
                do_import = True
            elif k in legacy_match:
                dest_field = legacy_match[k]
                do_import = True
            else:
                dest_field = None
                do_import = not table
            match_data.setItem(k,Bag(dict(do_import=do_import,source_field=k,dest_field=dest_field)))
        for i,r in enumerate(reader()):
            if limit and i>=limit:
                break
            rows.setItem('r_%i' %i,Bag(dict(r)))
        return result.toXml()
Пример #48
0
 def serve_ping(self, response, environ, start_response, page_id=None, reason=None, **kwargs):
     """add???
     
     :param response: add???
     :param environ: add???
     :param start_response: add???
     :param page_id: add???. Default value is ``None``
     :param reason: add???. Default value is ``None``
     :returns: add???
     """
     kwargs = self.parse_kwargs(kwargs)
     _lastUserEventTs = kwargs.get('_lastUserEventTs')
     page_item = self.register.refresh(page_id, _lastUserEventTs)
     if not page_item:
         return self.failed_exception('no longer existing page %s' % page_id, environ, start_response)
         
     self.handle_clientchanges(page_id, kwargs)
     envelope = Bag(dict(result=None))
     datachanges = self.get_datachanges(page_id, user=page_item['user'])
     if datachanges:
         envelope.setItem('dataChanges', datachanges)
     response.content_type = "text/xml"
     result = envelope.toXml(unresolved=True, omitUnknownTypes=True)
     return result
Пример #49
0
 def sendCommandToPage(self,page_id,command,data):
     envelope = Bag(dict(command=command,data=data))
     self.server.channels.get(page_id).write_message(envelope.toXml(unresolved=True))
Пример #50
0
class GnrBaseWebPage(GnrObject):
    """TODO"""
    def newCookie(self, name, value, **kw):
        """TODO
        
        :param name: TODO
        :param value: TODO"""
        return self.request.newCookie(name, value, **kw)
        
    def newMarshalCookie(self, name, value, secret=None, **kw):
        """TODO
        
        :param name: TODO
        :param value: TODO
        :param secret: TODO"""
        return self.request.newMarshalCookie(name, value, secret=secret, **kw)
        
    def get_cookie(self, cookieName, cookieType, secret=None, path=None):
        """TODO
        
        :param cookieName: TODO
        :param cookieType: TODO
        :param secret: TODO
        :param path: TODO"""
        return self.request.get_cookie(cookieName, cookieType, secret=secret, path=path)
        
    def add_cookie(self, cookie):
        """TODO
        
        :param cookie: TODO"""
        self.response.add_cookie(cookie)
        
    def _get_clientContext(self):
        cookie = self.get_cookie('genroContext', 'simple')
        if cookie:
            return Bag(urllib.unquote(cookie.value))
        else:
            return Bag()
            
    def _set_clientContext(self, bag):
        value = urllib.quote(bag.toXml())
        cookie = self.get_cookie('genroContext', 'simple', path=self.site.default_uri)
        cookie.value = value
        self.add_cookie(cookie)
        
    clientContext = property(_get_clientContext, _set_clientContext)
        
    def _get_filename(self):
        try:
            return self._filename
        except AttributeError:
            self._filename = os.path.basename(self.filepath)
            return self._filename
            
    filename = property(_get_filename)
        
    def _get_canonical_filename(self):
        return self.filename
        
    canonical_filename = property(_get_canonical_filename)
    
    @public_method
    def decodeDatePeriod(self, datestr, workdate=None, locale=None,min_date=None,max_date=None):
        """TODO
        
        :param datestr: a date string. For the string format, please check the :meth:`decodeDatePeriod()
        <gnr.core.gnrdate.decodeDatePeriod>` method docstrings
        :param workdate: the :ref:`workdate`
        :param locale: the current locale (e.g: en, en_us, it)"""
        workdate = workdate or self.workdate
        locale = locale or self.locale
        period = datestr
        valid = False
        try:
            returnDate = gnrdate.decodeDatePeriod(datestr, workdate=workdate, locale=locale, returnDate=True,min_date=min_date,max_date=max_date)
            valid = True
        except:
            returnDate = (None, None)
            period = None
        result = Bag()
        result['from'] = returnDate[0]
        result['to'] = returnDate[1]
        result['prev_from'] = gnrdate.dateLastYear(returnDate[0])
        result['prev_to'] = gnrdate.dateLastYear(returnDate[1])
        result['period'] = period
        result['valid'] = valid
        result['period_string'] = gnrdate.periodCaption(locale=locale, *returnDate)
        return result
        
    def mixins(self):
        """Implement this method in your page for mixin the page with methods from the
        public :ref:`public_resources` folder
        
        :returns: a list of mixin names with the following syntax: ``moduleName:className``"""
        return []
        
    def requestWrite(self, txt, encoding='utf-8'):
        """TODO
        
        :param txt: TODO
        :param encoding: the encoding type"""
        self.responseWrite(txt, encoding=encoding)
        
    def responseWrite(self, txt, encoding='utf-8'):
        """TODO
        
        :param txt: TODO
        :param encoding: the encoding type"""
        self.response.write(txt.encode(encoding))
        
    def _get_siteStatus(self):
        if not hasattr(self, '_siteStatus'):
            path = os.path.join(self.siteFolder, 'data', '_siteStatus.xml')
            
            if os.path.isfile(path):
                self._siteStatus = Bag(path)
            else:
                self._siteStatus = Bag()
        return self._siteStatus
        
    siteStatus = property(_get_siteStatus)
        
    def siteStatusSave(self):
        """TODO"""
        if hasattr(self, '_siteStatus'):
            path = os.path.join(self.siteFolder, 'data', '_siteStatus.xml')
            self._siteStatus.toXml(path)
            
    def pageLocalDocument(self, docname, page_id=None):
        """TODO
        
        :param docname: TODO
        :param page_id: TODO"""
        page_id = page_id or self.page_id
        folder = os.path.join(self.connectionFolder, page_id)
        if not os.path.isdir(folder):
            os.makedirs(folder)
        return os.path.join(folder, docname)
        
    def freezeSelection(self, selection, name,**kwargs):
        """TODO
        
        :param selection: TODO
        :param name: TODO"""
        path = self.pageLocalDocument(name)
        selection.freeze(path, autocreate=True,**kwargs)
        return path

    def freezeSelectionUpdate(self,selection):
        selection.freezeUpdate()
        
    def unfreezeSelection(self, dbtable=None, name=None, page_id=None):
        """TODO
        
        :param dbtable: specify the :ref:`database table <table>`. More information in the
                        :ref:`dbtable` section (:ref:`dbselect_examples_simple`)
        :param name: TODO
        :param page_id: TODO"""
        assert name, 'name is mandatory'
        if isinstance(dbtable, basestring):
            dbtable = self.db.table(dbtable)
        selection = self.db.unfreezeSelection(self.pageLocalDocument(name,page_id=page_id))
        if dbtable and selection is not None:
            assert dbtable == selection.dbtable, 'unfrozen selection does not belong to the given table'
        return selection
    
    def freezedPkeys(self,dbtable=None,name=None,page_id=None):
        assert name, 'name is mandatory'
        if isinstance(dbtable, basestring):
            dbtable = self.db.table(dbtable)
        return self.db.freezedPkeys(self.pageLocalDocument(name,page_id=page_id))

    @public_method
    def getUserSelection(self, selectionName=None, selectedRowidx=None, filterCb=None, columns=None,
                         sortBy=None,condition=None, table=None, condition_args=None):
        """TODO
        
        :param selectionName: TODO
        :param selectedRowidx: TODO
        :param filterCb: TODO
        :param columns: it represents the :ref:`columns` to be returned by the "SELECT"
                        clause in the traditional sql query. For more information, check the
                        :ref:`sql_columns` section
        :param condition: set a :ref:`sql_condition` for the selection
        :param table: the :ref:`database table <table>` name on which the query will be executed,
                      in the form ``packageName.tableName`` (packageName is the name of the
                      :ref:`package <packages>` to which the table belongs to)
        :param condition_args: the arguments of the *condition* parameter. Their syntax
                               is ``condition_`` followed by the name of the argument"""
        # table is for checking if the selection belong to the table
        assert selectionName, 'selectionName is mandatory'
        page_id = self.sourcepage_id or self.page_id
        if isinstance(table, basestring):
            table = self.db.table(table)
        selection = self.unfreezeSelection(dbtable=table, name=selectionName,page_id=page_id)
        table = table or selection.dbtable
        if filterCb:
            filterCb = self.getPublicMethod('rpc',filterCb)
            selection.filter(filterCb)
        elif selectedRowidx:
            if isinstance(selectedRowidx, basestring):
                selectedRowidx = [int(x) for x  in selectedRowidx.split(',')]
                selectedRowidx = set(selectedRowidx) #use uniquify (gnrlang) instead
            selection.filter(lambda r: r['rowidx'] in selectedRowidx)
        if sortBy:
            selection.sort(sortBy)
        if not columns:
            return selection
        if columns=='pkey':
            return selection.output('pkeylist')
        condition_args = condition_args or {}
        pkeys = selection.output('pkeylist')
        where = 't0.%s in :pkeys' % table.pkey
        if condition:
            where = '%s AND %s' % (where, condition)
        selection = table.query(columns=columns, where=where,
                                pkeys=pkeys, addPkeyColumn=False,
                                excludeDraft=False,
                                **condition_args).selection()
        return selection
        
    def getAbsoluteUrl(self, path, **kwargs):
        """Get TODO. Return an external link to the page
        
        :param path: the path to the page from the :ref:`sites_pages` folder
                     of a :ref:`project`"""
        return self.request.construct_url(self.getDomainUrl(path, **kwargs))
        
    def resolvePathAsUrl(self, *args, **kwargs):
        """TODO"""
        return self.diskPathToUri(self.resolvePath(*args, **kwargs))
        
    def resolvePath(self, *args, **kwargs):
        """TODO"""
        folder = kwargs.pop('folder', None)
        sitefolder = self.siteFolder
        if folder == '*data':
            diskpath = os.path.join(sitefolder, 'pages', '..', 'data', *args)
            return diskpath
        elif folder == '*users':
            diskpath = os.path.join(sitefolder, 'pages', '..', 'data', '_users', *args)
            return diskpath
        elif folder == '*home':
            diskpath = os.path.join(sitefolder, 'pages', '..', 'data', '_users', self.user, *args)
            return diskpath
        elif folder == '*pages':
            diskpath = os.path.join(sitefolder, 'pages', *args)
        elif folder == '*lib':
            diskpath = os.path.join(sitefolder, 'pages', '_lib', *args)
        elif folder == '*static':
            diskpath = os.path.join(sitefolder, 'pages', 'static', *args)
        else:
            diskpath = os.path.join(os.path.dirname(self.filename), *args)
        diskpath = os.path.normpath(diskpath)
        return diskpath
        
    def diskPathToUri(self, tofile, fromfile=None):
        """TODO
        
        :param tofile: TODO
        :param fromfile: TODO"""
        fromfile = fromfile or self.filename
        pagesfolder = self.folders['pages']
        relUrl = tofile.replace(pagesfolder, '').lstrip('/')
        path = fromfile.replace(pagesfolder, '')
        rp = '../' * (len(path.split('/')) - 1)
        
        path_info = self.request.path_info
        if path_info: # != '/index'
            rp = rp + '../' * (len(path_info.split('/')) - 1)
        return '%s%s' % (rp, relUrl)
        

    def _get_dbconnection(self):
        if not self._dbconnection:
            self._dbconnection = self.db.adapter.connect()
        return
        
    dbconnection = property(_get_dbconnection)
        
    def _get_packages(self):
        return self.db.packages
        
    packages = property(_get_packages)
    
    @property
    def package(self):
        """TODO"""
        pkgId = self.packageId
        if pkgId:
            return self.db.package(pkgId)
            
        
    def _get_tblobj(self):
        if self.maintable:
            return self.db.table(self.maintable)
            
    tblobj = property(_get_tblobj)
        
    def formSaver(self, formId, table=None, method=None, _fired='', datapath=None,
                  resultPath='dummy', changesOnly=True, onSaving=None, onSaved=None, saveAlways=False, **kwargs):
        """TODO
        
        :param formId: the id of the :ref:`form`
        :param table: the :ref:`database table <table>` name on which the query will be executed,
                      in the form ``packageName.tableName`` (packageName is the name of the
                      :ref:`package <packages>` to which the table belongs to)
        :param method: TODO
        :param _fired: TODO
        :param datapath: the :ref:`datapath` form
        :param resultPath: TODO
        :param changesOnly: boolean. TODO
        :param onSaving: TODO
        :param onSaved: TODO 
        :param saveAlways: boolean. TODO"""
        method = method or 'saveRecordCluster'
        controller = self.pageController()
        data = '==genro.getFormCluster("%s");'
        onSaved = onSaved or ''
        _onCalling = kwargs.pop('_onCalling', None)
        if onSaving:
            _onCalling = """var currform = genro.formById("%s");
                            var onSavingCb = function(record,form){%s};
                             %s
                            var result = onSavingCb.call(this, data.getItem('record'), currform);
                            if(result===false){
                                currform.status = null;
                                return false;
                            }else if(result instanceof gnr.GnrBag){
                                $1.data.setItem('record',result);
                            }""" % (formId, onSaving, _onCalling or '')
                            
        _onError = """var cb = function(){genro.formById("%s").status=null;};
                    genro.dlg.ask(kwargs._errorTitle,error,{"confirm":"Confirm"},
                                  {"confirm":cb});""" % formId
        if changesOnly:
            data = '==genro.getFormChanges("%s");'
        controller.dataController('genro.formById("%s").save(always)' % formId, _fired=_fired,
                                  datapath=datapath, always=saveAlways)
        kwargs['fireModifiers'] = _fired.replace('^', '=')
        controller.dataRpc(resultPath, method=method, nodeId='%s_saver' % formId, _POST=True,
                           datapath=datapath, data=data % formId, _onCalling=_onCalling,
                           _onResult='genro.formById("%s").saved();%s;' % (formId, onSaved),
                           _onError=_onError, _errorTitle='!!Saving error',
                           table=table, **kwargs)
                           
    def formLoader(self, formId, resultPath, table=None, pkey=None, datapath=None,
                   _fired=None, loadOnStart=False, lock=False,
                   method=None, onLoading=None, onLoaded=None, loadingParameters=None, **kwargs):
        """TODO
        
        :param formId: the id of the :ref:`form`
        :param resultPath: TODO
        :param table: the :ref:`database table <table>` name on which the query will be executed,
                      in the form ``packageName.tableName`` (packageName is the name of the
                      :ref:`package <packages>` to which the table belongs to)
        :param pkey: the record :ref:`primary key <pkey>`
        :param datapath: the :ref:`datapath` form
        :param _fired: TODO
        :param loadOnStart: boolean TODO
        :param lock: boolean. TODO
        :param method: TODO
        :param onLoading: TODO
        :param onLoaded: TODO
        :param loadingParameters: TODO"""
        pkey = pkey or '*newrecord*'
        method = method or 'loadRecordCluster'
        onResultScripts = []
        onResultScripts.append('genro.formById("%s").loaded()' % formId)
        if onLoaded:
            onResultScripts.append(onLoaded)
        loadingParameters = loadingParameters or '=gnr.tables.maintable.loadingParameters'
        controller = self.pageController()
        controller.dataController('genro.formById(_formId).load();',
                                  _fired=_fired, _onStart=loadOnStart, _delay=1, _formId=formId,
                                  datapath=datapath)
                                  
        controller.dataRpc(resultPath, method=method, pkey=pkey, table=table,
                           nodeId='%s_loader' % formId, datapath=datapath, _onCalling=onLoading,
                           _onResult=';'.join(onResultScripts), lock=lock,
                           loadingParameters=loadingParameters,
                           virtual_columns='==genro.formById(_formId).getVirtualColumns()',
                           _formId=formId, **kwargs)
                           
    @public_method                      
    def loadRecordCluster(self, table=None, pkey=None, recordGetter='app.getRecord', **kwargs):
        """TODO
        
        :param table: the :ref:`database table <table>` name on which the query will be executed,
                      in the form ``packageName.tableName`` (packageName is the name of the
                      :ref:`package <packages>` to which the table belongs to)
        :param pkey: the record :ref:`primary key <pkey>`
        :param recordGetter: TODO"""
        table = table or self.maintable
        getterHandler = self.getPublicMethod('rpc', recordGetter)
        record, recinfo = getterHandler(table=table, pkey=pkey, **kwargs)
        return record, recinfo
    
    @public_method
    def saveRecordCluster(self, data, table=None, _nocommit=False, rowcaption=None, _autoreload=False,
                          onSavingHandler=None, onSavedHandler=None,**kwargs):
        """TODO
        
        :param data: TODO
        :param table: the :ref:`database table <table>` name on which the query will be executed,
                      in the form ``packageName.tableName`` (packageName is the name of the
                      :ref:`package <packages>` to which the table belongs to)
        :param rowcaption: the textual representation of a record in a user query.
                           For more information, check the :ref:`rowcaption` section
        :param onSavingHandler: TODO
        :param onSavedHandler: TODO
        """
        #resultAttr = None #todo define what we put into resultAttr
        resultAttr = {}
        gridsChanges = data.pop('grids')
        onSavingMethod = 'onSaving'
        onSavedMethod = 'onSaved'
        maintable = getattr(self, 'maintable')
        table = table or maintable
        tblobj = self.db.table(table)
        if table != maintable:
            onSavingMethod = 'onSaving_%s' % table.replace('.', '_')
            onSavedMethod = 'onSaved_%s' % table.replace('.', '_')
        onSavingHandler =  self.getPublicMethod('rpc', onSavingHandler) if onSavingHandler else getattr(self, onSavingMethod, None)
        onSavedHandler = self.getPublicMethod('rpc', onSavedHandler) if onSavedHandler else getattr(self, onSavedMethod, None)
        node = data.getNode('record')
        recordCluster = node.value
        recordClusterAttr = node.getAttr()
        promisedFields = dict()
        if recordClusterAttr.get('_newrecord'):
            for k, promised in recordCluster.digest('#k,#a.promised'):
                if promised:
                    promisedFields[k] = recordCluster.pop(k)
        onSavedKwargs = dict()
        if onSavingHandler:
            onSavedKwargs = onSavingHandler(recordCluster, recordClusterAttr, resultAttr=resultAttr) or {}
        virtual_columns = self.pageStore().getItem('tables.%s.virtual_columns' % table)
        if virtual_columns:
            for virtual_col in virtual_columns.keys():
                recordCluster.pop(virtual_col, None)
        record = tblobj.writeRecordCluster(recordCluster, recordClusterAttr)
        if gridsChanges:
            fkey = record[tblobj.pkey]
            for gridchange in gridsChanges:
                grid_changeset = gridchange.value
                if recordClusterAttr.get('_newrecord'):
                    if grid_changeset['inserted']:
                        for row in grid_changeset['inserted'].values():
                            for k,v in row.items():
                                if v == '*newrecord*':
                                    row[k] = fkey
                self.app.saveEditedRows(table=gridchange.attr['table'],changeset=grid_changeset,commit=False)
        if promisedFields:
            msg = ['Saved record']
            for f in promisedFields:
                if promisedFields[f]!=record[f]:
                    pars = getattr(tblobj,'counter_%s' %f)()
                    fieldname = tblobj.column(f).name_long or f
                    fieldname.replace('!!','')
                    msgpars = dict(sequence=record[f],promised_sequence=promisedFields[f],fieldname=fieldname)
                    msg.append(dict(message=pars.get('message_failed',"!!%(fieldname)s: %(sequence)s instead of %(promised_sequence)s") %msgpars,messageType='warning'))
            resultAttr['saved_message'] = msg
                
        if onSavedHandler:
            onSavedHandler(record, resultAttr=resultAttr, **onSavedKwargs)
        if not _nocommit:
            self.db.commit()
        if not 'caption' in resultAttr:
            resultAttr['caption'] = tblobj.recordCaption(record, rowcaption=rowcaption)
        pkey = record[tblobj.pkey]
        resultAttr['lastTS'] = str(record[tblobj.lastTS]) if tblobj.lastTS else None
        for k,v in recordClusterAttr.items():
            if k.startswith('lastTS_'):
                resultAttr[k] = v
        if _autoreload:
            result = Bag()
            result.setItem('pkey',pkey,**resultAttr)
            keyToLoad=pkey if _autoreload  is  True else _autoreload
            if keyToLoad!='*dismiss*':
                record,recInfo = self.app.getRecord(pkey=keyToLoad,table=table)
                result.setItem('loadedRecord',record,**recInfo)
            return result
        else:
            return (pkey, resultAttr)
            
    @public_method    
    def deleteRecordCluster(self, data, table=None, **kwargs):
        """TODO
        
        :param data: TODO
        :param table: the :ref:`database table <table>` name on which the query will be executed,
                      in the form ``packageName.tableName`` (packageName is the name of the
                      :ref:`package <packages>` to which the table belongs to)"""
        maintable = getattr(self, 'maintable')
        table = table or maintable
        tblobj = self.db.table(table)
        node = data.getNode('record')
        recordCluster = node.value
        recordClusterAttr = node.getAttr()
        try: #try:
            self.onDeleting(recordCluster, recordClusterAttr)
            recordClusterAttr['_deleterecord'] = True
            record = tblobj.writeRecordCluster(recordCluster, recordClusterAttr)
            self.onDeleted(record)
            self.db.commit()
            return 'ok'
        except GnrSqlDeleteException, e:
            return ('delete_error', {'msg': e.message})
Пример #51
0
 def sendCommandToPage(self,page_id,command,data):
     headers = {'Content-type': 'application/x-www-form-urlencoded'}
     envelope=Bag(dict(command=command,data=data))
     body=urllib.urlencode(dict(page_id=page_id,envelope=envelope.toXml(unresolved=True)))
     self.socketConnection.request('POST',self.proxyurl,headers=headers, body=body)