def edit_language(): """ Edit language file """ #app = get_app() lang = ("zh-tw",T("Mandarin")) args = ['devicedb','languages',lang[0]+'.py'] filename = '/'.join(args) response.title = args[-1] strings = read_dict(apath(filename, r=request)) if '__corrupted__' in strings: form = SPAN(strings['__corrupted__'], _class='error') return dict(filename=filename, form=form) keys = sorted(strings.keys(), lambda x, y: cmp( unicode(x, 'utf-8').lower(), unicode(y, 'utf-8').lower())) rows = [] rows.append(H2(T('Original/Translation'))) for key in keys: name = md5_hash(key) s = strings[key] (prefix, sep, key) = key.partition('\x01') if sep: prefix = SPAN(prefix + ': ', _class='tm_ftag') k = key else: (k, prefix) = (prefix, '') _class = 'untranslated' if k == s else 'translated' if len(s) <= 40: elem = INPUT(_type='text', _name=name, value=s, _size=70, _class=_class) else: elem = TEXTAREA(_name=name, value=s, _cols=70, _rows=5, _class=_class) # Making the short circuit compatible with <= python2.4 k = (s != k) and k or B(k) new_row = DIV( LABEL(prefix, k, _style="font-weight:normal;"), CAT(elem, '\n', TAG.BUTTON( T('delete'), _onclick='return delkey("%s")' % name, _class='btn' )), _id=name, _class='span6 well well-small') rows.append(DIV(new_row,_class="row-fluid")) rows.append(DIV(INPUT(_type='submit', _value=T('update'), _class="btn btn-primary"), _class='controls')) form = FORM(*rows) if form.accepts(request.vars, keepvalues=True): strs = dict() for key in keys: name = md5_hash(key) if form.vars[name] == chr(127): continue strs[key] = form.vars[name] write_dict(apath(filename, r=request), strs) session.flash = T('saved on UTC') + request.utcnow.strftime(" %Y-%m-%d %H:%M") redirect(URL(r=request, args=request.args)) return dict(app=args[0], filename=filename, form=form, lang=lang)
def errors(): """ Error handler """ from gluon.admin import apath from gluon.fileutils import listdir for item in request.vars: if item[:7] == "delete_": os.unlink(apath("%s/errors/%s" % (appname, item[7:]), r=request)) func = lambda p: os.stat(apath("%s/errors/%s" % (appname, p), r=request)).st_mtime tickets = sorted(listdir(apath("%s/errors/" % appname, r=request), "^\w.*"), key=func, reverse=True) return dict(app=appname, tickets=tickets)
def get_applications(request): """ @param request: The application request. Return all the web2py applications running on the server as a list. """ return [f for f in os.listdir(apath(r=request)) if f.find('.') < 0]
session_db = DAL('gae') session.connect(request, response, db=session_db) hosts = (http_host, ) is_gae = True else: is_gae = False if request.is_https: session.secure() elif not request.is_local and not DEMO_MODE: raise HTTP(200, T('Admin is disabled because insecure channel')) try: _config = {} port = int(request.env.server_port or 0) restricted(read_file(apath('../parameters_%i.py' % port, request)), _config) if not 'password' in _config or not _config['password']: raise HTTP(200, T('admin disabled because no admin password')) except IOError: import gluon.fileutils if is_gae: if gluon.fileutils.check_credentials(request): session.authorized = True session.last_time = time.time() else: raise HTTP( 200, T('admin disabled because not supported on google app engine')) else:
def create_portable_app(web2py_source, copy_database=False, copy_uploads=False): """Function to create the portable app based on the parameters""" from gluon.admin import apath import shutil,tempfile,os import zipfile import contenttype cachedir = os.path.join(apath("%s" % appname, r=request), "cache") tempdir = tempfile.mkdtemp("", "eden-", cachedir) workdir = os.path.join(tempdir, "web2py") if copy_uploads: ignore = shutil.ignore_patterns("*.db", "*.log", "*.table", "errors", "sessions", "compiled" , "cache", ".bzr", "*.pyc") else: ignore = shutil.ignore_patterns("*.db", "*.log", "*.table", "errors", "sessions", "compiled" , "uploads", "cache", ".bzr", "*.pyc") appdir = os.path.join(workdir, "applications", appname) shutil.copytree(apath("%s" % appname, r=request),\ appdir, \ ignore = ignore) os.mkdir(os.path.join(appdir, "errors")) os.mkdir(os.path.join(appdir, "sessions")) os.mkdir(os.path.join(appdir, "cache")) if not copy_uploads: os.mkdir(os.path.join(appdir, "uploads")) shutil.copy(os.path.join(appdir, "deployment-templates", "cron", "crontab"),\ os.path.join(appdir, "cron", "crontab")) if copy_database: # Copy the db for the portable app s3db.load_all_models() # Load all modules to copy everything portable_db = DAL("sqlite://storage.db", folder=os.path.join(appdir, "databases")) for table in db: portable_db.define_table(table._tablename, *[field for field in table]) portable_db.commit() temp_csv_file=tempfile.mkstemp() db.export_to_csv_file(open(temp_csv_file[1], "wb")) portable_db.import_from_csv_file(open(temp_csv_file[1], "rb")) os.unlink(temp_csv_file[1]) portable_db.commit() # Replace the following with a more specific config config_template = open(os.path.join(appdir, "deployment-templates", "models", "000_config.py"), "r") new_config = open(os.path.join(appdir, "models", "000_config.py"), "w") # Replace first occurance of False with True new_config.write(config_template.read().replace("False", "True", 1)) new_config.close() # Embedded the web2py source with eden for download shutil.copy(os.path.join(cachedir, web2py_source), os.path.join(cachedir, "download.zip")) portable_app = os.path.join(cachedir, "download.zip") zip = zipfile.ZipFile(portable_app, "a", zipfile.ZIP_DEFLATED) tozip = os.path.join(tempdir, "web2py") rootlen = len(tempdir) + 1 for base, dirs, files in os.walk(tozip): for directory in dirs: directory = os.path.join(base, directory) zip.write(directory, directory[rootlen:]) # Create empty directories for file in files: fn = os.path.join(base, file) zip.write(fn, fn[rootlen:]) zip.close() shutil.rmtree(tempdir) response.headers["Content-Type"] = contenttype.contenttype(portable_app) response.headers["Content-Disposition"] = \ "attachment; filename=portable-sahana.zip" return response.stream(portable_app)
def portable(): """ Portable app creator""" from gluon.admin import apath import os from operator import itemgetter, attrgetter uploadfolder=os.path.join(apath("%s" % appname, r=request), "cache") web2py_source = None web2py_source_exists = False last_build_exists = False for base, dirs, files in os.walk(uploadfolder): for filename in files: if "web2py_source" in filename: web2py_source_exists = True web2py_source = filename break for base, dirs, files in os.walk(uploadfolder): for filename in files: if "download.zip" in filename: last_build_exists = True break web2py_form = SQLFORM.factory( Field("web2py_source", "upload", uploadfolder=uploadfolder, requires=IS_UPLOAD_FILENAME(extension="zip"), ), table_name="web2py_source", ) if web2py_form.accepts(request.vars, keepvalues=True, session=None): # Make sure only one web2py source file exists files_to_remove = {} for base, dirs, files in os.walk(uploadfolder): for filename in files: if "web2py_source" in filename: files_to_remove[filename] = os.stat(os.path.join(uploadfolder, filename)).st_mtime sorted_files = sorted(files_to_remove.items(), key=itemgetter(1)) for i in range(0, len(sorted_files) - 1): # 1 indicates leave one file os.remove(os.path.join(uploadfolder,sorted_files[i][0])) web2py_source = sorted_files[len(sorted_files) - 1][0] web2py_source_exists = True session.flash = T("Web2py executable zip file found - Upload to replace the existing file") else: # Lets throw an error message if this the zip file isn't found if not web2py_source_exists: session.error = T("Web2py executable zip file needs to be uploaded to use this function.") else: session.flash = T("Web2py executable zip file found - Upload to replace the existing file") # Since the 2nd form depends on having uploaded the zip # in order to work we only show it if the upload was successfully # completed. if web2py_source_exists: generator_form = SQLFORM.factory( Field("copy_database", "boolean"), Field("copy_uploads", "boolean"), ) if generator_form.accepts(request.vars, keepvalues=True, session=None): if web2py_source_exists: create_portable_app(web2py_source=web2py_source,\ copy_database = request.vars.copy_database,\ copy_uploads = request.vars.copy_uploads) else: session.error = T("Web2py executable zip file needs to be uploaded first to use this function.") else: generator_form = None if last_build_exists: download_last_form = SQLFORM.factory() if download_last_form.accepts(request.vars, keepvalues=True, session=None): portable_app = os.path.join(cachedir, "download.zip") response.headers["Content-Type"] = contenttype.contenttype(portable_app) response.headers["Content-Disposition"] = \ "attachment; filename=portable-sahana.zip" return response.stream(portable_app) else: download_last_form = None return dict( web2py_form=web2py_form, generator_form=generator_form, download_last_form=download_last_form )
session_db = DAL('gae') session.connect(request, response, db=session_db) hosts = (http_host, ) is_gae = True else: is_gae = False if request.env.http_x_forwarded_for or request.is_https: session.secure() elif not request.is_local and not DEMO_MODE: raise HTTP(200, T('Admin is disabled because insecure channel')) try: _config = {} port = int(request.env.server_port or 0) restricted(read_file(apath('../parameters_%i.py' % port, request)), _config) if not 'password' in _config or not _config['password']: raise HTTP(200, T('admin disabled because no admin password')) except IOError: import gluon.fileutils if is_gae: if gluon.fileutils.check_credentials(request): session.authorized = True session.last_time = time.time() else: raise HTTP(200, T('admin disabled because not supported on google app engine')) else: raise HTTP(200, T('admin disabled because unable to access password file'))
socket.gethostbyname(http_host), '::1','127.0.0.1','::ffff:127.0.0.1') remote_addr = request.env.remote_addr if request.env.http_x_forwarded_for \ or request.env.wsgi_url_scheme in ['https', 'HTTPS'] \ or request.env.https == 'on': session.secure() elif not remote_addr in hosts and not DEMO_MODE: raise HTTP(200, T('Admin is disabled because insecure channel')) try: _config = {} port = int(request.env.server_port or 0) restricted(open(apath('../parameters_%i.py' % port, request), 'r').read(), _config) if not 'password' in _config or not _config['password']: raise HTTP(200, T('admin disabled because no admin password')) except IOError: import gluon.fileutils if request.env.web2py_runtime_gae: if gluon.fileutils.check_credentials(request): session.authorized = True session.last_time = time.time() else: raise HTTP(200, T('admin disabled because not supported on google app engine')) else: raise HTTP(200, T('admin disabled because unable to access password file'))
session.connect(request, response, db=session_db) hosts = (http_host, ) is_gae = True else: is_gae = False if request.is_https: session.secure() elif not request.is_local and not DEMO_MODE: raise HTTP(200, T('Admin is disabled because insecure channel')) try: _config = {} port = int(request.env.server_port or 0) restricted( read_file(apath('../parameters_%i.py' % port, request)), _config) if not 'password' in _config or not _config['password']: raise HTTP(200, T('admin disabled because no admin password')) except IOError: import gluon.fileutils if is_gae: if gluon.fileutils.check_credentials(request): session.authorized = True session.last_time = time.time() else: raise HTTP(200, T('admin disabled because not supported on google app engine')) else: raise HTTP( 200, T('admin disabled because unable to access password file'))
def portable(): """ Portable app creator""" from gluon.admin import apath import os from operator import itemgetter, attrgetter uploadfolder = os.path.join(apath("%s" % appname, r=request), "cache") web2py_source = None web2py_source_exists = False last_build_exists = False for base, dirs, files in os.walk(uploadfolder): for filename in files: if "web2py_source" in filename: web2py_source_exists = True web2py_source = filename break for base, dirs, files in os.walk(uploadfolder): for filename in files: if "download.zip" in filename: last_build_exists = True break web2py_form = SQLFORM.factory( Field( "web2py_source", "upload", uploadfolder=uploadfolder, requires=IS_UPLOAD_FILENAME(extension="zip"), ), table_name="web2py_source", ) if web2py_form.accepts(request.vars, keepvalues=True, session=None): # Make sure only one web2py source file exists files_to_remove = {} for base, dirs, files in os.walk(uploadfolder): for filename in files: if "web2py_source" in filename: files_to_remove[filename] = os.stat( os.path.join(uploadfolder, filename)).st_mtime sorted_files = sorted(files_to_remove.items(), key=itemgetter(1)) for i in range(0, len(sorted_files) - 1): # 1 indicates leave one file os.remove(os.path.join(uploadfolder, sorted_files[i][0])) web2py_source = sorted_files[len(sorted_files) - 1][0] web2py_source_exists = True session.flash = T( "Web2py executable zip file found - Upload to replace the existing file" ) else: # Lets throw an error message if this the zip file isn't found if not web2py_source_exists: session.error = T( "Web2py executable zip file needs to be uploaded to use this function." ) else: session.flash = T( "Web2py executable zip file found - Upload to replace the existing file" ) generator_form = SQLFORM.factory( Field("copy_database", "boolean"), Field("copy_uploads", "boolean"), ) if generator_form.accepts(request.vars, keepvalues=True, session=None): if web2py_source_exists: create_portable_app(web2py_source=web2py_source,\ copy_database = request.vars.copy_database,\ copy_uploads = request.vars.copy_uploads) else: session.error = T( "Web2py executable zip file needs to be uploaded to use this function." ) if last_build_exists: download_last_form = SQLFORM.factory() if download_last_form.accepts(request.vars, keepvalues=True, session=None): portable_app = os.path.join(cachedir, "download.zip") response.headers["Content-Type"] = contenttype.contenttype( portable_app) response.headers["Content-Disposition"] = \ "attachment; filename=portable-sahana.zip" return response.stream(portable_app) else: download_last_form = None return dict(web2py_form=web2py_form, generator_form=generator_form, download_last_form=download_last_form)
def verify_admin_password(password): _config = {} port = int(request.env.server_port or 0) restricted(read_file(apath('../parameters_%i.py' % port, request)), _config) return _config['password'] == CRYPT()(password)[0]
def _get_statics(): statics = listdir(apath('sqlabs/static/%s/' % plugin_name, r=request), '[^\.#].*') statics = [x.replace('\\', '/') for x in statics] statics.sort() return statics
def _get_statics(): statics = listdir(apath('sqlabs/static/%s/' % plugin_name, r=request), '[^\.#].*') statics = [x.replace('\\','/') for x in statics] statics.sort() return statics
except TypeError: safe_file = open(a, b) try: return safe_file.read() finally: safe_file.close() def find_exposed_functions(dt): dt = regex_longcomments.sub('', dt) return regex_expose.findall(dt) def beauty_text(t): parts = t.split('_') return ' '.join([x.title() for x in parts]) response.tmpmenu = UL(_class='collapsible') controllers = sorted( listdir(apath('ecommerce/controllers/', r=request), '.*\.py$')) controllers = [x.replace('\\', '/') for x in controllers] for c in controllers: li = LI() _c = c[:-3] data = safe_read(apath('ecommerce/controllers/%s' % c, r=request)) items = find_exposed_functions(data) li.append(DIV(beauty_text(_c), _class='collapsible-header')) body = UL() for i in sorted(items): body.append(LI(A(beauty_text(i), _href=URL(_c, i)))) li.append(DIV( body, _class='collapsible-body')) response.tmpmenu.append(li)
session_db = DAL('gae') session.connect(request, response, db=session_db) hosts = (http_host, ) is_gae = True else: is_gae = False if request.is_https: session.secure() elif not request.is_local and not DEMO_MODE: raise HTTP(200, T('Admin is disabled because insecure channel')) try: _config = {} port = int(request.env.server_port or 0) exec read_file(apath('../parameters_%i.py' % port, request)) in _config if not 'password' in _config or not _config['password']: raise HTTP(200, T('admin disabled because no admin password')) except IOError: import gluon.fileutils if is_gae: if gluon.fileutils.check_credentials(request): session.authorized = True session.last_time = time.time() else: raise HTTP(200, T('admin disabled because not supported on google app engine')) else: raise HTTP( 200, T('admin disabled because unable to access password file'))
'::1', '127.0.0.1', '::ffff:127.0.0.1') remote_addr = request.env.remote_addr if request.env.http_x_forwarded_for \ or request.env.wsgi_url_scheme in ['https', 'HTTPS'] \ or request.env.https == 'on': session.secure() elif not remote_addr in hosts and not DEMO_MODE: raise HTTP(200, T('Admin is disabled because insecure channel')) try: _config = {} port = int(request.env.server_port or 0) restricted( open(apath('../parameters_%i.py' % port, request), 'r').read(), _config) if not 'password' in _config or not _config['password']: raise HTTP(200, T('admin disabled because no admin password')) except IOError: import gluon.fileutils if request.env.web2py_runtime_gae: if gluon.fileutils.check_credentials(request): session.authorized = True session.last_time = time.time() else: raise HTTP( 200, T('admin disabled because not supported on google app engine')) else:
def create_portable_app(web2py_source, copy_database=False, copy_uploads=False): """Function to create the portable app based on the parameters""" from gluon.admin import apath import shutil, tempfile, os import zipfile import contenttype cachedir = os.path.join(apath("%s" % appname, r=request), "cache") tempdir = tempfile.mkdtemp("", "eden-", cachedir) workdir = os.path.join(tempdir, "web2py") if copy_uploads: ignore = shutil.ignore_patterns("*.db", "*.log", "*.table", "errors", "sessions", "compiled", "cache", ".bzr", "*.pyc") else: ignore = shutil.ignore_patterns("*.db", "*.log", "*.table", "errors", "sessions", "compiled", "uploads", "cache", ".bzr", "*.pyc") appdir = os.path.join(workdir, "applications", appname) shutil.copytree(apath("%s" % appname, r=request),\ appdir, \ ignore = ignore) os.mkdir(os.path.join(appdir, "errors")) os.mkdir(os.path.join(appdir, "sessions")) os.mkdir(os.path.join(appdir, "cache")) if not copy_uploads: os.mkdir(os.path.join(appdir, "uploads")) shutil.copy(os.path.join(appdir, "deployment-templates", "cron", "crontab"),\ os.path.join(appdir, "cron", "crontab")) if copy_database: # Copy the db for the portable app s3db.load_all_models() # Load all modules to copy everything portable_db = DAL("sqlite://storage.db", folder=os.path.join(appdir, "databases")) for table in db: portable_db.define_table(table._tablename, *[field for field in table]) portable_db.commit() temp_csv_file = tempfile.mkstemp() db.export_to_csv_file(open(temp_csv_file[1], "wb")) portable_db.import_from_csv_file(open(temp_csv_file[1], "rb")) os.unlink(temp_csv_file[1]) portable_db.commit() # Replace the following with a more specific config config_template = open( os.path.join(appdir, "deployment-templates", "models", "000_config.py"), "r") new_config = open(os.path.join(appdir, "models", "000_config.py"), "w") # Replace first occurance of False with True new_config.write(config_template.read().replace("False", "True", 1)) new_config.close() # Embedded the web2py source with eden for download shutil.copy(os.path.join(cachedir, web2py_source), os.path.join(cachedir, "download.zip")) portable_app = os.path.join(cachedir, "download.zip") zip = zipfile.ZipFile(portable_app, "a", zipfile.ZIP_DEFLATED) tozip = os.path.join(tempdir, "web2py") rootlen = len(tempdir) + 1 for base, dirs, files in os.walk(tozip): for directory in dirs: directory = os.path.join(base, directory) zip.write(directory, directory[rootlen:]) # Create empty directories for file in files: fn = os.path.join(base, file) zip.write(fn, fn[rootlen:]) zip.close() shutil.rmtree(tempdir) response.headers["Content-Type"] = contenttype.contenttype(portable_app) response.headers["Content-Disposition"] = \ "attachment; filename=portable-sahana.zip" return response.stream(portable_app)