def get_template_sources(self, template_name, template_dirs=None): if not connection.tenant: return if not template_dirs: try: template_dirs = [settings.MULTI_TENANT_DIR] except AttributeError: raise ImproperlyConfigured( 'To use %s.%s you must define the MULTI_TENANT_DIR' % (__name__, FilesystemLoader.__name__)) for template_dir in template_dirs: try: if '%s' in template_dir: yield safe_join( template_dir % connection.tenant.client_name, 'templates', template_name) else: yield safe_join(template_dir, connection.tenant.client_name, 'templates', template_name) except UnicodeDecodeError: # The template dir name was a bytestring that wasn't valid UTF-8. raise except ValueError: # The joined path was located outside of this particular # template_dir (it might be inside another one, so this isn't # fatal). pass
def get_theme_dirs(template_dirs=None): """ Returns all theme directories""" for themes_root in get_theme_root_dirs(): for theme_dir in os.listdir(themes_root): if path.isdir(safe_join(themes_root, theme_dir)) and \ not theme_dir.startswith('.'): yield theme_dir, safe_join(themes_root, theme_dir)
def get_template_sources(self, template_name, template_dirs=None): """ Returns the absolute paths to "template_name", when appended to each directory in "template_dirs". Any paths that don't lie inside one of the template dirs are excluded from the result set, for security reasons. """ if not connection.tenant or isinstance(connection.tenant, FakeTenant): return if not template_dirs: try: template_dirs = settings.MULTITENANT_TEMPLATE_DIRS except AttributeError: raise ImproperlyConfigured('To use %s.%s you must define the MULTITENANT_TEMPLATE_DIRS' % (__name__, FilesystemLoader.__name__)) for template_dir in template_dirs: try: if '%s' in template_dir: yield safe_join(template_dir % connection.tenant.domain_url, template_name) else: yield safe_join(template_dir, connection.tenant.domain_url, template_name) except UnicodeDecodeError: # The template dir name was a bytestring that wasn't valid UTF-8. raise except ValueError: # The joined path was located outside of this particular # template_dir (it might be inside another one, so this isn't # fatal). pass
def get_tree(self, target, ancestors=False, siblings=False): path = self._find_path(target) tree = [self._get_path_info(path)] tree.extend([self._get_path_info(safe_join(self.root, path, child)) for child in os.listdir(path)]) if ancestors: proc_path = path while proc_path != self.root: tree.append(self._get_path_info(proc_path)) proc_path, head = os.path.split(proc_path) for ancestor_sibling in os.listdir(proc_path): ancestor_sibling_abs = safe_join(self.root, proc_path, ancestor_sibling) if os.path.isdir(ancestor_sibling_abs): tree.append(self._get_path_info(ancestor_sibling_abs)) if siblings and not (path == self.root): parent_path, curr_dir = os.path.split(path) for sibling in os.listdir(parent_path): if sibling == curr_dir: continue sibling_abs = safe_join(self.root, parent_path, sibling) tree.append(self._get_path_info(sibling_abs)) # print # print "*******************************************" # print # for t in tree: # print t # print return tree
def get_template_sources(self, template_name, template_dirs=None): """ Returns the absolute paths to "template_name", when appended to each directory in "template_dirs". Any paths that don't lie inside one of the template dirs are excluded from the result set, for security reasons. """ if not template_dirs: template_dirs = settings.TEMPLATE_DIRS for template_dir in template_dirs: try: brand = settings.BRAND if brand in settings.BRANDS and brand not in settings.GM_BRAND: try: mod = import_module('gladminds.{0}'.format(brand)) brand_template_dir = os.path.join(os.path.dirname(mod.__file__), 'templates') yield safe_join(brand_template_dir, template_name) except ImportError as e: yield safe_join(template_dir, template_name) else: yield safe_join(template_dir, template_name) except UnicodeDecodeError: # The template dir name was a bytestring that wasn't valid UTF-8. raise except ValueError: try: yield safe_join(template_dir, template_name) except ValueError: pass # The joined path was located outside of this particular # template_dir (it might be inside another one, so this isn't # fatal). pass
def get_template_sources(self, template_name, template_dirs=None): if not all([settings.DEFAULT_THEME, settings.CURRENT_THEME]): return super(ThemeLoaderMixin, self).get_template_sources( template_name, template_dirs) if not template_dirs: template_dirs = self.get_dirs() _template_dirs = list(template_dirs) template_name = template_name.replace('DEFAULT_THEME', settings.DEFAULT_THEME) # First we find templates in CURRENT_THEME folder template_dirs = [safe_join(t, settings.CURRENT_THEME) for t in template_dirs] if settings.CURRENT_THEME != settings.DEFAULT_THEME: # If this is not default theme - add extra folders for search template_dirs.extend([safe_join(t, settings.DEFAULT_THEME) for t in _template_dirs]) # Add origin templates for callback template_dirs.extend(_template_dirs) return super(ThemeLoaderMixin, self).get_template_sources( template_name, template_dirs)
def dump_data(model, day): prevday = day yday = prevday + datetime.timedelta(1) the_day = datetime.datetime(prevday.year, prevday.month, prevday.day, 0, 0, 0) qs = model.objects.filter(timestamp__gte=the_day, timestamp__lt=the_day + datetime.timedelta(1)) if qs.count() == 0: return data = [] for obj in qs: obj_data = {} for f in obj._meta.fields: obj_data[f.name] = getattr(obj, f.name) data.append(obj_data) dump_dir = safe_join(settings.DUMP_DIR, the_day.strftime("%Y-%m")) dump_file = safe_join(dump_dir, the_day.strftime(model.__name__.lower() + "_%Y-%m-%d.json")) if not os.path.isdir(dump_dir): os.makedirs(dump_dir) f = open(dump_file, "w") simplejson.dump(data, f, default=datetime_encoder) f.close() os.system("/bin/gzip -9 %s" % dump_file) qs.delete()
def copy_to_campaign(filepath, destination): """ Copy file to campaign directory :param filepath: path to file to which be copied :param destination: campaign directory where file should be copied :returns: :param real_path: Path that OS accepts for copying, e.g :returns: URL to copied filed """ original_basename = os.path.basename(filepath) directory = safe_join(settings.MEDIA_ROOT, destination) proposed_path = os.path.join(directory, original_basename) final_destination = create_copy_name(proposed_path) base_name = os.path.basename(final_destination) media_url = safe_join(settings.MEDIA_URL, destination, base_name) try: os.makedirs(directory) except OSError as e: if e.errno != errno.EEXIST: logger.exception("Failed to create a directory [%s]" % directory) raise shutil.copyfile(filepath, final_destination) return media_url
def is_linked(project_root,project_share_root,subpath): share_path = safe_join(project_share_root,subpath) if not os.path.exists(share_path): return False file_path = safe_join(project_root,subpath) if os.path.realpath(file_path) != os.path.realpath(share_path): return False
def get_template_sources(self, template_name, template_dirs=None): if not template_dirs: template_dirs = settings.TEMPLATE_DIRS domain = Site.objects.get_current().domain default_dir = getattr(settings, 'MULTISITE_DEFAULT_TEMPLATE_DIR', 'default') new_template_dirs = [] for template_dir in template_dirs: new_template_dirs.append(safe_join(template_dir, domain)) if default_dir: new_template_dirs.append(safe_join(template_dir, default_dir)) for template_dir in new_template_dirs: try: name = safe_join(template_dir, template_name) except UnicodeDecodeError: # The template dir name was a bytestring that wasn't valid UTF-8. raise except (ValueError, SuspiciousFileOperation): # The joined path was located outside of this particular # template_dir (it might be inside another one, so this isn't # fatal). continue yield Origin( name=name, template_name=template_name, loader=self )
def get_template_sources(self, template_name, template_dirs=None): """ Returns the absolute paths to "template_name", when appended to each directory in "template_dirs". Any paths that don't lie inside one of the template dirs are excluded from the result set, for security reasons. If current language is not default, prepend each path with language name. E.g. 'include/banner.html' would become 'de/include/banner.html' """ if not template_dirs: template_dirs = settings.TEMPLATE_DIRS if get_language() != settings.LANGUAGE_CODE: lang = get_language() new_dirs = [] for i in template_dirs: new_dirs.extend([safe_join(i, lang), i]) template_dirs = new_dirs for template_dir in template_dirs: try: yield safe_join(template_dir, template_name) except UnicodeDecodeError: # The template dir name was a bytestring that wasn't valid UTF-8. raise except ValueError: # The joined path was located outside of this particular # template_dir (it might be inside another one, so this isn't # fatal). pass
def _get_template_sources(template_name, template_dirs=None): """ Returns the absolute paths to "template_name", when appended to each directory in "template_dirs". Any paths that don't lie inside one of the template dirs are excluded from the result set, for security reasons. """ if not template_dirs: template_dirs = APP_TEMPLATE_DIRS app_name = '' if '/' in template_name: app_name, template_name = template_name.split('/', 1) skip_apps = ('admin', 'design',) skip = app_name in skip_apps for app, template_dir in template_dirs: try: if not skip and app_name == app: yield safe_join(template_dir, template_name) #allow index.html to be found in admin and in destiny.design, but not in other apps if app_name == '' and app not in skip_apps: yield safe_join(template_dir, 'design', template_name) continue yield safe_join(template_dir, app_name, template_name) except UnicodeDecodeError: raise except ValueError: pass
def get_template_sources(self, template_name): PROJECT_DIR = dirname(dirname(abspath(__file__))) bits = template_name.split('/') if len(bits) == 2: sub_app, remainder = bits template_dir = '%s/kanban/%s/templates'% (PROJECT_DIR, sub_app,) try: yield safe_join(template_dir, remainder) except UnicodeDecodeError: # The template dir name wasn't valid UTF-8. raise except ValueError: # The joined path was located outside of template_dir. pass if len(bits) == 3: sub_app, view_group, remainder = bits template_dir = '%s/kanban/%s/%s/templates'% (PROJECT_DIR, sub_app, view_group) try: yield safe_join(template_dir, remainder) except UnicodeDecodeError: # The template dir name wasn't valid UTF-8. raise except ValueError: # The joined path was located outside of template_dir. pass
def _data_file(logger_name, filename): datadir = os.path.abspath(os.path.join(os.path.dirname(__file__), 'data')) if not os.path.exists(datadir): os.mkdir(datadir) data_subdir = _os.safe_join(datadir, logger_name.rsplit('.', 1)[1]) if not os.path.exists(data_subdir): os.mkdir(data_subdir) return _os.safe_join(data_subdir, filename)
def test_root_path(self): self.assertEqual( safe_join("/", "path"), "/path", ) self.assertEqual( safe_join("/", ""), "/", )
def staging_list(pathname=settings.STAGING_PATH, dirname=settings.STAGING_PATH, root=False): from django.utils import _os from django.core.files.storage import default_storage """Traverse a path and return an alphabetically by filename sorted nested group of unordered (<ul>) list HTML tags:: <ul> <li id="dir2/file2"><a>file2</a></li> <li id="dir2/file3"><a>file3</a></li> <li id="dir2/subdir"><a>subdir</a> <ul> <li id="dir2/subdir/file4"><a>file4</a></li> </ul> </li> </ul> :param pathname: the directory to traverse :type pathname: string :param dirname: the root directory of the traversal :type dirname: string :rtype: string """ directory_listing = '' # so people aren't malicious with the loading of files in the UI if not path.abspath(pathname).startswith(dirname): return None filelist = listdir(pathname) filelist.sort() for f in filelist: if path.isdir(_os.safe_join(pathname, f)): li = '<li class="jstree-closed" id="%s"><a>%s</a>' \ % (path.relpath(_os.safe_join(pathname, f), dirname), path.basename(f)) directory_listing = directory_listing + li + '<ul></ul></li>' else: if not posixpath.basename(f).startswith('.'): li = '<li class="fileicon" id="%s"><a>%s</a>' \ % (path.relpath(_os.safe_join(pathname, f), dirname), path.basename(f)) directory_listing = directory_listing + li + '</li>' if root: # root call directory_listing = '<ul><li id="phtml_1"><a>'\ + str(path.split(path.dirname(pathname))[1]) \ + '</a><ul>' \ + directory_listing \ + '</ul></li></ul>' return directory_listing
def get_template_sources(template_name, template_dirs=None): if not template_dirs: template_dirs = getattr(settings, "SHPAML_TEMPLATE_DIRS", []) print template_dirs for template_dir in template_dirs: try: print safe_join(template_dir, template_name) yield safe_join(template_dir, template_name) except ValueError: # The joined path was located outside of template_dir. pass
def test_root_path(self): drive, path = os.path.splitdrive(safe_join("/", "path")) self.assertEqual( path, "{0}path".format(os.path.sep), ) drive, path = os.path.splitdrive(safe_join("/", "")) self.assertEqual( path, os.path.sep, )
def get_contents(self, origin): group = get_current_group() template_name: str = origin.template_name if group is None: # If there is no current group .. we have nothing to do .. raise TemplateDoesNotExist(template_name) template_dir: str = sphsettings.get_sph_setting('community_groupaware_template_dir', None) if template_dir is not None: template_path = safe_join(os.path.join(template_dir, group.name), template_name) try: with open(template_path, encoding=self.engine.file_charset) as fp: return fp.read() except FileNotFoundError as e: #logger.debug('File not found', e) pass prefix = 'groups/%s/' % group.name if template_name.startswith(prefix): template_name = template_name.replace(prefix, '') template_path = safe_join(os.path.join(template_dir, group.name), template_name) try: with open(template_path, encoding=self.engine.file_charset) as fp: return fp.read() except FileNotFoundError as e: logger.warning('File no found', e) # Look in the cache . .so we don't have to make unnecessary database # queries cachekey = _get_cache_key(template_name, group) ret = cache.get( cachekey ) if ret is not None: if ret == NOT_EXIST_CACHE_VALUE: raise TemplateDoesNotExist(template_name) return ret while group is not None: try: template = GroupTemplate.objects.get( template_name = template_name, group = group, ) ret = (template.source, "%s:%s" % (group.name, template.template_name)) # Cache for two hours by default .. cache.set(cachekey, ret, TMPL_CACHE_EXPIRE) return ret except GroupTemplate.DoesNotExist: group = group.parent cache.set(cachekey, NOT_EXIST_CACHE_VALUE, TMPL_CACHE_EXPIRE) raise TemplateDoesNotExist(template_name)
def rename(request): """ Rename existing File/Directory. Includes renaming existing Image Versions/Thumbnails. """ from filebrowser.forms import RenameForm # QUERY / PATH CHECK query = request.GET path = get_path(query.get("dir", "")) filename = get_file(query.get("dir", ""), query.get("filename", "")) if path is None or filename is None: if path is None: msg = _("The requested Folder does not exist.") else: msg = _("The requested File does not exist.") request.user.message_set.create(message=msg) return HttpResponseRedirect(reverse("fb_browse")) abs_path = u"%s" % os.path.join(MEDIA_ROOT, DIRECTORY, path) file_extension = os.path.splitext(filename)[1].lower() if request.method == "POST": form = RenameForm(abs_path, file_extension, request.POST) if form.is_valid(): relative_server_path = os.path.join(DIRECTORY, path, filename) new_filename = form.cleaned_data["name"] + file_extension new_relative_server_path = os.path.join(DIRECTORY, path, new_filename) try: # PRE RENAME SIGNAL filebrowser_pre_rename.send(sender=request, path=path, filename=filename, new_filename=new_filename) # RENAME ORIGINAL os.rename(safe_join(MEDIA_ROOT, relative_server_path), safe_join(MEDIA_ROOT, new_relative_server_path)) # DELETE IMAGE VERSIONS/THUMBNAILS # regenerating versions/thumbs will be done automatically for version in VERSIONS: try: os.unlink(os.path.join(MEDIA_ROOT, get_version_path(relative_server_path, version))) except: pass if PRERENDER_VERSIONS: version_generator(new_relative_server_path, version) # POST RENAME SIGNAL filebrowser_post_rename.send(sender=request, path=path, filename=filename, new_filename=new_filename) # MESSAGE & REDIRECT msg = _("Renaming was successful.") request.user.message_set.create(message=msg) redirect_url = reverse("fb_browse") + query_helper(query, "", "filename") return HttpResponseRedirect(redirect_url) except OSError, (errno, strerror): form.errors["name"] = forms.util.ErrorList([_("Error.")])
def dump_data(model, day): from django.db import connection prevday = day yday = prevday + datetime.timedelta(1) the_day = datetime.datetime(prevday.year, prevday.month, prevday.day, 0, 0, 0) qs = model.objects.filter(timestamp__gte=the_day, timestamp__lt=the_day + datetime.timedelta(1)) if qs.count() == 0: return sql, params = qs.query.get_compiler('default').as_sql() class Dumper(list): def __init__(self, sql, params): self.cur = connection.cursor() self.cur.execute(sql, params) self.labels = [i[0] for i in self.cur.cursor.description] def __nonzero__(self): return True def __iter__(self): return self def next(self): rec = self.cur.fetchone() if rec is not None: obj_data = dict(zip(self.labels, rec)) return obj_data else: raise StopIteration dumper = Dumper(sql, params) dump_dir = safe_join(settings.DUMP_DIR, the_day.strftime('%Y-%m')) dump_file = safe_join(dump_dir, the_day.strftime(model.__name__.lower() + '_%Y-%m-%d.json')) if not os.path.isdir(dump_dir): os.makedirs(dump_dir) f = open(dump_file, 'w') for frag in simplejson.JSONEncoder(default=datetime_encoder).iterencode(dumper): f.write(frag) f.close() os.system('/bin/gzip -9 %s' % dump_file) qs.delete()
def find_app_dojo_dir_and_url(app_name): """Returns a list of tuples of dojo modules within an apps 'dojo-media' directory. Each tuple contains the abspath to the module directory and the module name. """ ret = [] media_dir = find_app_dojo_dir(app_name) if not media_dir: return None for d in listdir(media_dir): if path.isdir(safe_join(media_dir, d)): if d not in ("src", "release") and not d.startswith("."): ret.append(tuple([safe_join(media_dir, d), "%(module)s" % { 'module': d }])) return tuple(ret)
def path(self, name): from django.db import connection from django.utils._os import safe_join # FIXME: These imports are inline so that the connection object # can be mocked in tests if connection.tenant: location = safe_join(settings.TENANT_BASE, connection.tenant.schema_name) else: location = self.location try: path = safe_join(location, name) except ValueError: raise SuspiciousFileOperation("Attempted access to '%s' denied." % name) return os.path.normpath(path)
def get_template_sources(template_name, template_dirs=None): from common.models import Theme active_theme = Theme.get_active() if not active_theme: check_themes() for themes_dir in get_theme_root_dirs(template_dirs): active_theme_dir = safe_join(themes_dir, active_theme.directory_name) try: yield safe_join(active_theme_dir, template_name) except UnicodeDecodeError: raise except ValueError: pass
def get_page_or_404(name): """Return page content as a Django template or raise 404 error. Given a page name, try get the absolute file path. If ValueError exception or in case file path doesn't exist raise a 404 error. If exists open each file and instantiates a new Django template object with its contents. Loop through the page's raw nodelist and checks for BlockNode with the name context. If BlockNode is found, it defines a metavariable for us that contains that content. :param name: the page name :raise ValueError: raises an exception :return: render as a template the page content inside page folder """ try: file_path = safe_join(settings.SITE_PAGES_DIRECTORY, name) # absolute file path except ValueError: # exceptional outcome raise Http404('Page Not Found') else: # not exceptional outcome if not os.path.exists(file_path): raise Http404('Page Not found') with open(file_path, 'r') as f: page = Template(f.read()) # instatiates a new <class 'django.template.base.Template'> meta = None for i, node in enumerate(list(page.nodelist)): if isinstance(node, BlockNode) and node.name == 'context': meta = page.nodelist.pop(i) break page._meta = meta return page
def themed(self, name, theme): """ Returns True if given asset override is provided by the given theme otherwise returns False. Args: name: asset name e.g. 'images/logo.png' theme: theme name e.g. 'red-theme', 'edx.org' Returns: True if given asset override is provided by the given theme otherwise returns False """ if not is_comprehensive_theming_enabled(): return False # in debug mode check static asset from within the project directory if settings.DEBUG: themes_location = get_base_themes_dir() # Nothing can be themed if we don't have a theme location or required params. if not all((themes_location, theme, name)): return False themed_path = "/".join([ themes_location, theme, "static/" ]) name = name[1:] if name.startswith("/") else name path = safe_join(themed_path, name) return os.path.exists(path) # in live mode check static asset in the static files dir defined by "STATIC_ROOT" setting else: return self.exists(os.path.join(theme, name))
def find_app_dojo_dir(app_name): """Checks, if a dojo-media directory exists within a given app and returns the absolute path to it.""" base = find_app_dir(app_name) if base: media_dir = safe_join(base, 'dojo-media') if path.isdir(media_dir): return media_dir return None # no dojo-media directory was found within that app
def get_template_dirs(self): """ Returns a list of candidate search paths for templates. """ return [safe_join(theme_dir, theme, 'templates') for theme_dir in settings.THEMES_DIRS for theme in self.get_templates()]
def get_template_sources(self, template_name, template_dirs=None): """ Prefix template_name with layer in use thus enabling template switching. Override entire method since original uses a generator and the extra for loop required introduces syntax complexity. """ ''' Original: keep as reference template_name = os.path.join(settings.FOUNDRY['layers'][0], template_name) return super(AppDirectoriesTypeLoader, self).get_template_sources( template_name, template_dirs ) ''' if not template_dirs: template_dirs = app_template_dirs for template_dir in template_dirs: for layer in settings.FOUNDRY['layers']: l_template_name = os.path.join(layer, template_name) try: yield safe_join(template_dir, l_template_name) except UnicodeDecodeError: # The template dir name was a bytestring that wasn't valid UTF-8. raise except ValueError: # The joined path was located outside of template_dir. pass
def get_template_sources(self, template_name, template_dirs=None): """Return the absolute paths to "template_name", when appended to the selected theme directory in THEMES_DIR. Any paths that don't lie inside one of the template dirs are excluded from the result set, for security reasons. """ theme_templates = [] current_request = get_current_request() if current_request and current_request.mobile: theme_templates.append(os.path.join(get_theme_root(), 'mobile')) theme_templates.append(os.path.join(get_theme_root(), 'templates')) for template_path in theme_templates: try: if settings.USE_S3_THEME: yield os.path.join(template_path, template_name) else: yield safe_join(template_path, template_name) except UnicodeDecodeError: # The template dir name was a bytestring that wasn't valid UTF-8. raise except ValueError: # The joined path was located outside of this particular # template_dir (it might be inside another one, so this isn't # fatal). pass
) MIDDLEWARE_CLASSES = ( 'django.contrib.sessions.middleware.SessionMiddleware', 'django.middleware.common.CommonMiddleware', 'django.middleware.csrf.CsrfViewMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.auth.middleware.SessionAuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware', 'django.middleware.clickjacking.XFrameOptionsMiddleware', ) ROOT_URLCONF = 'jobs.urls' WSGI_APPLICATION = 'jobs.wsgi.application' DATABASES = { 'default': { 'ENGINE': 'django.db.backends.postgresql_psycopg2', 'HOST': os.environ.get('POSTGRES_HOST', 'localhost'), 'NAME': os.environ['POSTGRES_USER'], 'USER': os.environ['POSTGRES_USER'], 'PASSWORD': os.environ['POSTGRES_PASSWORD'], } } LANGUAGE_CODE = 'en-us' TIME_ZONE = 'Asia/Manila' USE_I18N = True USE_L10N = True USE_TZ = True STATIC_URL = '/static/' STATIC_ROOT = safe_join(BASE_DIR, 'static') STATICFILES_DIRS = (('lib', safe_join(BASE_DIR, 'bower_components')), ) TASTYPIE_DEFAULT_FORMATS = ['json']
def handle(self, *args, **options): dest_path = args[0] base_prefix = '/stuff/20letSlovenije/' from proracun.models import Postavka from django.core.urlresolvers import reverse from django.test.client import Client from django.utils._os import safe_join import os, sys pri_od_hodki = { 'odhodki': dict(((i, 1) for i in ('0', '40', '41', '42', '43', '45', '44', '55', '57'))), 'prihodki': dict(((i, 1) for i in ('0', '70', '71', '72', '73', '74', '75', '78', '50'))), } sifre = set([p.sifra for p in Postavka.objects.all()]) sifre.add(0) adj = ['nic', 'inf', 'bdp'] media_prefix = base_prefix + 'media/' urls = [ media_prefix + i for i in [ 'proracun2010.css', 'jquery-1.4.2.js', 'jit-2.0.1.js', 'vir/Drzavni_proracun_1992-2011.xls', 'excanvas.js', 'en.png', 'sl.png' ] ] for p_o, p_o_keys in pri_od_hodki.items(): for a in adj: for s in sifre: if str(s)[:2] in p_o_keys: u = reverse('proracun_areachart_js', args=(p_o, s, a)) urls.append(u) u = reverse('proracun_areachart_js', args=(p_o, 'en', s, a)) urls.append(u) u = reverse('proracun_areachart', args=(p_o, a)) urls.append(u) u = reverse('proracun_areachart', args=('en', p_o, a)) urls.append(u) c = Client() nall = len(urls) for n, u in enumerate(urls): response = c.get(u) u = u.lstrip('/') if u.endswith('/'): u = u + 'index.html' dest_filename = safe_join(dest_path, u) dest_dir = os.path.dirname(dest_filename) if not os.path.isdir(dest_dir): os.makedirs(dest_dir) f = open(dest_filename, 'w') f.write(response.content) f.close() sys.stderr.write('\rProgress: %d/%d' % (n + 1, nall)) sys.stderr.flush() print '' meta_redirect = '''<html><head><meta http-equiv="refresh" content="0;url=%s/"></head><body></body></html>''' for i in [ '', 'prihodki/', 'odhodki/', 'en/', 'en/prihodki/', 'en/odhodki/' ]: u = safe_join(base_prefix, i, 'index.html') where = 'odhodki' in i and 'odhodki' or 'prihodki' dest_filename = safe_join(dest_path, u.lstrip('/')) url = safe_join(base_prefix, where, 'inf') f = open(dest_filename, 'w') f.write(meta_redirect % url) f.close() return
def get_fullpath(self): #NB split out so you could, say, implement a 'delete' method on # a view return Path(safe_join(self.path_root + self.get_id_path()))
# -*- coding: utf-8 -*- # ++ This file `defaults.py` is generated at 3/3/16 6:15 AM ++ from __future__ import unicode_literals from django.apps import apps from django.conf import settings from django.utils._os import safe_join from .globals import HACS_APP_NAME __author__ = "Md Nazrul Islam<*****@*****.**>" HACS_CACHE_SETTING_NAME = 'default' HACS_FALLBACK_URLCONF = settings.ROOT_URLCONF HACS_GENERATED_URLCONF_DIR = safe_join( apps.get_app_config(HACS_APP_NAME).path, 'generated') HACS_SERIALIZED_ROUTING_DIR = None HACS_USER_OBJECT_QUERY_CALLABLE = "hacs.utils.get_user_object" HACS_DEVELOPMENT_MODE = False HACS_AUTO_DISCOVER_URL_MODULE = [ "admin.site.urls", ] HACS_AC_BYPASS = 0 HACS_ANONYMOUS_ROLE_NAME = "Guest" HACS_DEFAULT_STATE = "draft" HACS_DEFAULT_STATES = ("published", "internally_published", "draft", "private")
def dump_data(model, day, use_new=True): from django.db import connection yday = day + datetime.timedelta(1) the_day = datetime.datetime(day.year, day.month, day.day, 0, 0, 0) dump_dir = safe_join(settings.DUMP_DIR, the_day.strftime('%Y-%m')) if not os.path.isdir(dump_dir): os.makedirs(dump_dir) if use_new and the_day >= datetime.datetime(2015, 7, 31): # transition to utc timestamps if the_day == datetime.datetime(2015, 7, 31): start = the_day end = make_aware(start, utc) + datetime.timedelta(1) else: start = make_aware(the_day, utc) end = start + datetime.timedelta(1) qs = model.objects.filter(timestamp__gte=start, timestamp__lt=end) if qs.count() == 0: return sql, params = qs.query.get_compiler('default').as_sql() dump_file = safe_join( dump_dir, the_day.strftime(model.__name__.lower() + '_%Y-%m-%d.csv')) dump_file_relative = os.path.join( the_day.strftime('%Y-%m'), the_day.strftime(model.__name__.lower() + '_%Y-%m-%d.csv')) cur = connection.cursor() copy_sql = 'COPY (' + sql + ') TO stdout WITH CSV HEADER;' full_sql = cur.cursor.mogrify(copy_sql, params) args = ['psql'] if connection.settings_dict['USER']: args += ["-U", connection.settings_dict['USER']] if connection.settings_dict['HOST']: args.extend(["-h", connection.settings_dict['HOST']]) if connection.settings_dict['PORT']: args.extend(["-p", str(connection.settings_dict['PORT'])]) args += [connection.settings_dict['NAME']] cmd = args + ["-c", full_sql] f = open(dump_file, 'w') p = subprocess.Popen(cmd, stdout=f) p.wait() #cur.cursor.copy_expert(full_sql, f) f.close() p = subprocess.Popen(['/bin/gzip', '-9f', dump_file]) p.wait() f = open( safe_join(settings.DUMP_DIR, the_day.strftime('%Y-%m.sha1sums')), 'a') p = subprocess.Popen(['/usr/bin/sha1sum', dump_file_relative + '.gz'], cwd=safe_join(settings.DUMP_DIR, '.'), stdout=f) p.wait() f.close() pks = [i[0] for i in qs.values_list('pk')] qn = connection.ops.quote_name sql = 'DELETE FROM ' + qn(model._meta.db_table) + \ ' WHERE ' + qn(model._meta.pk.name) + \ ' IN (' + ', '.join(['%s' for i in pks]) + ');' cur.execute(sql, pks) cur.execute('COMMIT;') #qs.delete() else: qs = model.objects.filter(timestamp__gte=the_day, timestamp__lt=the_day + datetime.timedelta(1)) if qs.count() == 0: return sql, params = qs.query.get_compiler('default').as_sql() class Dumper(list): def __init__(self, sql, params): self.cur = connection.cursor() self.cur.execute(sql, params) self.labels = [i[0] for i in self.cur.cursor.description] def __nonzero__(self): return True def __iter__(self): return self def next(self): rec = self.cur.fetchone() if rec is not None: obj_data = dict(zip(self.labels, rec)) return obj_data else: raise StopIteration dumper = Dumper(sql, params) dump_file = safe_join( dump_dir, the_day.strftime(model.__name__.lower() + '_%Y-%m-%d.json')) f = open(dump_file, 'w') for frag in simplejson.JSONEncoder( default=datetime_encoder).iterencode(dumper): f.write(frag) f.close() os.system('/bin/gzip -9 %s' % dump_file) qs.delete()
def _full_path(self, name): if name == '/': name = '' return safe_join(self.root_path, name)
def path(self, name): return safe_join(self.location, name)
def path(self, name): return safe_join(os.path.abspath(self.base_location), self.location, name)
# coding: utf-8 # @Time : 2021/5/10 1:19 下午 # @Author : DB.Bruce Dong from django.utils._os import safe_join # safe_join,暂时理解为,根据不同的操作系统,返回适应当前系统的文件路径 # windows路径: "/" # linux路径:"C:\\" location = '' safe_join(location, 'fi.txt')
def _full_path(self, name): if name == "/": name = "" return safe_join(self.root_path, name).replace("\\", "/")
def get_data_folder_path(challenge_short_name): """ Returns physical base path to the root of the folder where all files for this project are kept """ return safe_join(settings.MEDIA_ROOT, challenge_short_name)
def output_path(self): return safe_join("/output", self.relative_path)
# get the path of model 'dashboard' fs_encoding = sys.getfilesystemencoding() or sys.getdefaultencoding() app = 'dashboard' try: mod = import_module(app) except ImportError, e: raise ImproperlyConfigured('ImportError %s: %s' % (app, e.args[0])) template_dir = os.path.dirname(mod.__file__) template_dir = template_dir.decode(fs_encoding) # list dashboard_app directories in this directories dashboard_app_template_dirs = [] apps_name = os.listdir(template_dir) for app_name in apps_name: app_path = safe_join(template_dir, app_name) if os.path.isdir(app_path) and app_name != 'api': dashboard_app_template_dirs.append(safe_join(app_path)) # It won't change, so convert it to a tuple to save memory. dashboard_app_template_dirs = tuple(dashboard_app_template_dirs) class TemplateLoader(BaseLoader): is_usable = True def get_template_sources(self, template_name, template_dirs=None): if not template_dirs: template_dirs = dashboard_app_template_dirs for template_dir in template_dirs: try:
def get_template_sources(self, template_name): >>>>>>> 37c99181c9a6b95433d60f8c8ef9af5731096435 """ Return an Origin object pointing to an absolute path in each directory in template_dirs. For security reasons, if a path doesn't lie inside one of the template_dirs it is excluded from the result set. """ <<<<<<< HEAD if not template_dirs: template_dirs = self.get_dirs() for template_dir in template_dirs: ======= for template_dir in self.get_dirs(): >>>>>>> 37c99181c9a6b95433d60f8c8ef9af5731096435 try: name = safe_join(template_dir, template_name) except SuspiciousFileOperation: # The joined path was located outside of this template_dir # (it might be inside another one, so this isn't fatal). continue yield Origin( name=name, template_name=template_name, loader=self, ) <<<<<<< HEAD def load_template_source(self, template_name, template_dirs=None): warnings.warn( 'The load_template_sources() method is deprecated. Use '
def _full_path(self, name): if name == '/': name = '' return safe_join(self.root_path, name).replace('\\', '/')
def input_path(self): return safe_join("/input", self.relative_path)
def get_app_template_path(self, app, template_path): """ Return the full path of a template located in an app. """ return safe_join(self.app_templates_dirs[app], template_path)
def get_project_data_folder(self): """ Full path to root folder for all data belonging to this project """ return safe_join(settings.MEDIA_ROOT, self.short_name)
def file_exist(path): full_path = safe_join(settings.MEDIA_ROOT, path) result = os.path.exists(full_path) return result
'default': { 'ENGINE': 'django.db.backends.postgresql_psycopg2', 'HOST': os.environ.get('POSTGRES_HOST', 'localhost'), 'NAME': os.environ['POSTGRES_USER'], 'USER': os.environ['POSTGRES_USER'], 'PASSWORD': os.environ['POSTGRES_PASSWORD'], }, } LANGUAGE_CODE = 'en-us' TIME_ZONE = 'Asia/Manila' USE_I18N = True USE_L10N = True USE_TZ = True STATIC_ROOT = safe_join(BASE_DIR, 'static') MEDIA_ROOT = safe_join(BASE_DIR, 'media') STATICFILES_FINDERS = ( 'django.contrib.staticfiles.finders.FileSystemFinder', 'django.contrib.staticfiles.finders.AppDirectoriesFinder', 'compressor.finders.CompressorFinder', ) STATIC_URL = '/static/' MEDIA_URL = '/media/' LOGIN_URL = "login" LOGIN_REDIRECT_URL = "landing" COMPRESS_ENABLED = True
def test_base_path_ends_with_sep(self): drive, path = os.path.splitdrive(safe_join("/abc/", "abc")) self.assertEqual( path, "{0}abc{0}abc".format(os.path.sep) )
def test_parent_path(self): with self.assertRaises(SuspiciousFileOperation): safe_join("/abc/", "../def")
def path(self, name): return safe_join('/tmp', name)
def install_theme_fileobj(theme_name, zip_file, force=False): """ Extract resources and templates from an opened ``ZipFile`` and install them at a place they can be picked by the multitier logic in ``template_loader.Loader.get_template_sources``. """ #pylint:disable=too-many-statements,too-many-locals assert theme_name is not None LOGGER.info("install theme %s%s", theme_name, " (force)" if force else "") theme_dir = get_theme_dir(theme_name) public_dir = safe_join(settings.PUBLIC_ROOT, theme_name) templates_dir = safe_join(theme_dir, 'templates') if not force and os.path.exists(public_dir): LOGGER.warning("install theme '%s' but '%s' already exists.", theme_name, public_dir) raise PermissionDenied("Theme public assets already exists.") if not force and os.path.exists(templates_dir): LOGGER.warning("install theme '%s' but '%s' already exists.", theme_name, templates_dir) raise PermissionDenied("Theme templates already exists.") # We rely on the assumption that ``public_dir`` and ``templates_dir`` # are on the same filesystem. We create a temporary directory on that # common filesystem, which guarentees that: # 1. If the disk is full, we will find on extract, not when we try # to move the directory in place. # 2. If the filesystem is encrypted, we don't inadvertently leak # information by creating "temporary" files. tmp_base = safe_join( os.path.commonprefix([public_dir, templates_dir]), '.cache') if not os.path.exists(tmp_base): os.makedirs(tmp_base) if not os.path.isdir(os.path.dirname(templates_dir)): os.makedirs(os.path.dirname(templates_dir)) tmp_dir = tempfile.mkdtemp(dir=tmp_base) #pylint: disable=too-many-nested-blocks try: for info in zip_file.infolist(): if info.file_size == 0: # Crude way to detect directories continue tmp_path = None test_parts = os.path.normpath(info.filename).split(os.sep)[1:] if test_parts: base = test_parts.pop(0) if base == 'public': if settings.PUBLIC_WHITELIST is not None: if (os.path.join(*test_parts) in settings.PUBLIC_WHITELIST): tmp_path = safe_join(tmp_dir, base, *test_parts) else: tmp_path = safe_join(tmp_dir, base, *test_parts) if tmp_path: if not os.path.isdir(os.path.dirname(tmp_path)): os.makedirs(os.path.dirname(tmp_path)) with open(tmp_path, 'wb') as extracted_file: extracted_file.write(zip_file.read(info.filename)) elif base == 'templates': relative_path = os.path.join(*test_parts) if relative_path in settings.TEMPLATES_BLACKLIST: continue if settings.TEMPLATES_WHITELIST is not None: if (os.path.join(*test_parts) in settings.TEMPLATES_WHITELIST): tmp_path = safe_join(tmp_dir, base, relative_path) else: tmp_path = safe_join(tmp_dir, base, relative_path) if tmp_path: if not os.path.isdir(os.path.dirname(tmp_path)): os.makedirs(os.path.dirname(tmp_path)) template_string = zip_file.read(info.filename) if hasattr(template_string, 'decode'): template_string = template_string.decode('utf-8') template_string = force_text(template_string) try: check_template(template_string) with open(tmp_path, 'w') as extracted_file: extracted_file.write(template_string) try: default_path = get_template_path( relative_path=relative_path) if (default_path and not default_path.startswith(theme_dir)): cmdline = [ 'diff', '-u', default_path, tmp_path] cmd = subprocess.Popen( cmdline, stdout=subprocess.PIPE) lines = cmd.stdout.readlines() cmd.wait() # Non-zero error codes are ok here. # That's how diff indicates the files # are different. if not lines: LOGGER.info( "%s: no differences", relative_path) os.remove(tmp_path) except TemplateDoesNotExist: # We are installing a template which is not # in the default theme, so no diff is ok. pass except TemplateSyntaxError as err: LOGGER.info("error:%s: %s", relative_path, err) # Should be safe to move in-place at this point. # Templates are necessary while public resources (css, js) # are optional. tmp_public = safe_join(tmp_dir, 'public') tmp_templates = safe_join(tmp_dir, 'templates') mkdirs = [] renames = [] for paths in [(tmp_templates, templates_dir), (tmp_public, public_dir)]: if os.path.exists(paths[0]): if not os.path.exists(os.path.dirname(paths[1])): mkdirs += [os.path.exists(os.path.dirname(paths[1]))] renames += [paths] for path in mkdirs: os.makedirs(path) for paths in renames: if os.path.exists(paths[1]): LOGGER.info("remove previous path %s", paths[1]) shutil.rmtree(paths[1]) os.rename(paths[0], paths[1]) finally: # Always delete the temporary directory, exception raised or not. shutil.rmtree(tmp_dir)
def go(): import re, os import csv import datetime from django.core.files import File from django.utils._os import safe_join from proracun.models import Proracun, Postavka, TIPI_PRORACUNOV try: rdr = csv.reader(open(args[0])) except IndexError: raise CommandError("please pass seznam.csv") datadir = os.path.dirname(args[0]) tipi = dict([(i[1], i[0]) for i in TIPI_PRORACUNOV]) #re.match('^si_proracun_(\d{4})_(\d{4})_(\d\d)_(\d\d)_([^\.]+).csv$') for line in rdr: if line[0].startswith('Prora'): # new year year = line[0].split(' ')[1].strip() continue else: print line tip = tipi[line[1].decode('utf-8')] data = { 'datum_sprejetja': datetime.datetime.strptime(line[0], '%d.%m.%Y'), 'proracunsko_leto': year, 'tip_proracuna': tip, 'del_proracuna': 1, 'epa': re.sub('^\s*EPA\s*', '', line[2]), } pr = Proracun(**data) pr.pdf.save(line[3], File(open(safe_join(datadir, line[3])))) pr.csv.save(line[4], File(open(safe_join(datadir, line[4])))) pr.save() postavke_csv = csv.reader(open(safe_join(datadir, line[4]))) for pline in postavke_csv: try: int(pline[0]) except: continue if pr.tip_proracuna == 'ZR': znesek = pline[4] else: znesek = pline[2] p = Postavka(proracun=pr, sifra=pline[0], naziv=pline[1], znesek=znesek) p.save() Postavka(proracun=pr, sifra=57, naziv=u'Plačila obresti', znesek=0).save() print 'Loaded:', line[4] print 'Postprocessing...' from django.db import connection cur = connection.cursor() cur.execute('''UPDATE proracun_postavka SET znesek = proracun_postavka.znesek - N.sum FROM ( SELECT a.sum, a.proracun_id, b.znesek FROM proracun_postavka AS b, ( SELECT proracun_id, sum(znesek) FROM proracun_postavka WHERE sifra IN (403,404) GROUP BY proracun_id ) AS a WHERE a.proracun_id = b.proracun_id AND b.sifra = 40 ) AS N WHERE proracun_postavka.proracun_id = N.proracun_id AND proracun_postavka.sifra = 40;''') cur.execute('''UPDATE proracun_postavka SET znesek = proracun_postavka.znesek + N.sum FROM ( SELECT a.sum, a.proracun_id, b.znesek FROM proracun_postavka AS b, ( SELECT proracun_id, sum(znesek) FROM proracun_postavka WHERE sifra in (403,404) GROUP BY proracun_id ) AS a WHERE a.proracun_id = b.proracun_id AND b.sifra = 57 ) AS N WHERE proracun_postavka.proracun_id = N.proracun_id AND proracun_postavka.sifra = 57;''') cur.execute('''UPDATE proracun_postavka SET naziv = naziv || ' (' || sifra || ')', sifra = regexp_replace(sifra::text, '^40([34])', '57\\\\1')::integer WHERE sifra::text ~ E'^40[34]';''') cur.execute('''UPDATE proracun_postavka SET znesek = 0.0 WHERE id IN (SELECT a.id FROM proracun_postavka a, proracun_proracun b WHERE a.proracun_id = b.id AND b.tip_proracuna != 'ZR' AND sifra::text ~ E'^50.$');''')
def mkfile(self, name, parent): parent_path = self._find_path(parent) new_abs_path = safe_join(self.root, parent_path, name) return FileWrapper.mkfile(new_abs_path, self.root).get_info()
def path(self, name): return safe_join(MEDIA_ROOT, name)
def mkdir(self, name, parent): parent_path = self._find_path(parent) new_abs_path = safe_join(self.root, parent_path, name) return DirectoryWrapper.mkdir(new_abs_path, self.root).get_info()
# Build paths inside the project like this: os.path.join(BASE_DIR, ...) BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) TEMPLATE_PATH = 'templates' SESSION_ENGINE = 'django.contrib.sessions.backends.signed_cookies' # Quick-start development settings - unsuitable for production # See https://docs.djangoproject.com/en/2.0/howto/deployment/checklist/ # SECURITY WARNING: keep the secret key used in production secret! SECRET_KEY = '' # SECURITY WARNING: don't run with debug turned on in production! DEBUG = True if os.path.exists(safe_join(BASE_DIR, 'eos_accounts/live.py')): # if this file exists, we're on the live server DEBUG = False LIVE = True STAGING = False ALLOWED_HOSTS = [ 'localhost', 'eos-account-creator.com', 'preview.eos-account-creator.com', '192.168.0.26', '192.168.0.73', '192.168.0.206', '192.168.0.23', '127.0.0.1' ] CANONICAL_BASE_URL = 'https://eos-account-creator.com/' # Application definition INSTALLED_APPS = [
def path(self, name): try: path = safe_join(self.location, name) except ValueError: raise SuspiciousOperation("Attempted access to '%s' denied." % name) return os.path.normpath(path)