def create_apps_dir(apps_dir=getattr(settings, 'APPS_DIR', None)): if not apps_dir: project_dir = getattr(settings, 'BASE_DIR', settings.PROJECT_DIR) apps_dir = os.path.abspath( os.path.join(os.path.dirname(project_dir), "apps")) if not os.path.exists(apps_dir): create_direcotry(apps_dir) if not os.access(apps_dir, os.W_OK): change_path_permission(apps_dir)
def test_helpers(self): apps_dir_perm = get_perm(settings.APPS_DIR) self.assertEqual(apps_dir_perm, 511) apps_dir_perm = get_path_permission(settings.APPS_DIR) self.assertEqual(apps_dir_perm, ('0o777', '0o777')) try: make_writeable_recursive(settings.APPS_DIR) except OSError: self.fail("Failed to change permission") test_dir = os.path.join(settings.CARTOVIEW_DIR, 'test_dir') create_direcotry(test_dir) self.assertTrue(os.path.exists(test_dir)) shutil.rmtree(test_dir)
def download_layers(request): today = datetime.now() date_as_path = today.strftime("%Y/%m/%d") file_suff = today.strftime("%Y_%m_%d-%H_%M_%S") file_name = 'download_{}.gpkg'.format(file_suff) file_dir = os.path.join("packages_download", date_as_path, request.user.username) package_url = os.path.join(file_dir, file_name) package_dir = os.path.join(settings.MEDIA_ROOT, file_dir) create_direcotry(package_dir) if not os.path.isdir(package_dir) or not os.access(package_dir, os.W_OK): return HttpResponseForbidden('maybe destination is not writable\ or not a directory') package_path = os.path.join(package_dir, file_name) layernames = request.GET.get('layers', None) if not layernames: return HttpResponseForbidden("No layers provided") layernames = [str(layername) for layername in layernames.split(',')] permitted = get_objects_for_user(request.user, 'base.download_resourcebase') permitted_layers = Layer.objects.filter(id__in=permitted) permitted_layers = [ layer for layer in permitted_layers if layer.alternate in layernames ] print(permitted_layers) with DataManager.open_source(get_connection(), is_postgres=True) as ds: if not ds: return HttpResponseForbidden("Cannot connect to database") layer_styles = [] table_names = [] for layer in permitted_layers: typename = str(layer.alternate) table_name = typename.split(":").pop() if DataManager.source_layer_exists(ds, table_name): table_names.append(table_name) gattr = str( layer.attribute_set.filter( attribute_type__contains='gml').first().attribute) layer_style = layer.default_style sld_url = layer_style.sld_url style_name = str(layer_style.name) layer_styles.append( (table_name, gattr, style_name, get_sld_body(sld_url))) DataManager.postgis_as_gpkg(get_connection(), package_path, layernames=table_names) stm = StyleManager(package_path) stm.create_table() for style in layer_styles: stm.add_style(*style, default=True) return redirect(os.path.join(settings.MEDIA_URL, package_url))
def setup_apps(options): from cartoview.app_manager.helpers import (create_direcotry, change_path_permission) try: f = urlopen(TEST_DATA_URL) zip_ref = zipfile.ZipFile(BytesIO(f.read())) create_direcotry(APPS_DIR) if not os.access(APPS_DIR, os.W_OK): change_path_permission(APPS_DIR) zip_ref.extractall(APPS_DIR) zip_ref.close() except urllib2.HTTPError as e: print ("HTTP Error:", e.code) except urllib2.URLError as e: print ("URL Error:", e.reason)
def create_apps_dir(APPS_DIR): if not os.path.exists(APPS_DIR): create_direcotry(APPS_DIR) if not os.access(APPS_DIR, os.W_OK): change_path_permission(APPS_DIR)
from cartoview.app_manager.helpers import (change_path_permission, create_direcotry) standard_library.install_aliases() formatter = logging.Formatter( '[%(asctime)s] p%(process)s { %(name)s %(pathname)s:%(lineno)d} \ %(levelname)s - %(message)s', '%m-%d %H:%M:%S') logger = logging.getLogger(__name__) handler = logging.StreamHandler(stdout) handler.setFormatter(formatter) logger.addHandler(handler) # BASE_DIR must be defined in project.settings APPS_DIR = os.path.abspath(os.path.join(BASE_DIR, "apps")) if not os.path.exists(APPS_DIR): create_direcotry(APPS_DIR) if not os.access(APPS_DIR, os.W_OK): change_path_permission(APPS_DIR) if APPS_DIR not in sys.path: sys.path.append(APPS_DIR) apps_file_path = os.path.join(APPS_DIR, "apps.yml") PENDING_APPS = os.path.join(APPS_DIR, "pendingOperation.yml") apps_config = AppsConfig(apps_file_path) CARTOVIEW_APPS = () for app_config in apps_config: if app_config.active: try: # ensure that the folder is python module app_module = importlib.import_module(app_config.name) app_dir = os.path.dirname(app_module.__file__)
def get_new_dir(base_dir=_temp_dir): rand_str = uuid4().__str__().replace('-', '')[:8] timestr = time.strftime("%Y/%m/%d/%H/%M/%S") target = os.path.join(base_dir, timestr, rand_str) create_direcotry(target) return target
# -*- coding: utf-8 -*- import os from collections import namedtuple from cartoview.app_manager.helpers import create_direcotry LayerPostgisOptions = namedtuple( 'LayerPostgisOptions', ['skipfailures', 'overwrite', 'append', 'update']) POSTGIS_OPTIONS = LayerPostgisOptions(True, True, False, False) _temp_dir = os.path.join( os.path.dirname(os.path.realpath(__file__)), 'tmp_generator') _downloads_dir = os.path.join( os.path.dirname(os.path.realpath(__file__)), 'downloads') create_direcotry(_temp_dir) create_direcotry(_downloads_dir)