def set_active(self, active=True): app = CartoviewApp.objects.get(self.name, None) if app: app.active = active app.commit() CartoviewApp.save() return app
def add_carto_app(self): if not CartoviewApp.objects.app_exists(self.name): CartoviewApp({ 'name': self.name, 'active': True, 'order': self.get_app_order(), 'pending': True }) CartoviewApp.save() else: carto_app = CartoviewApp.objects.get(self.name) carto_app.pending = True carto_app.commit() CartoviewApp.save()
def reorder(self, request, **kwargs): ids_list = request.POST.get("apps", None) if ids_list is not None: ids_list = ids_list.split(",") else: ids_list = json.loads(request.body)["apps"] for i in range(0, len(ids_list)): app = App.objects.get(id=ids_list[i]) app.order = i + 1 app.save() cartoview_app = CartoviewApp.objects.get(app.name) if cartoview_app: cartoview_app.order = app.order cartoview_app.commit() if i == (len(ids_list) - 1): CartoviewApp.save() self.log_throttled_access(request) return self.create_response(request, {'success': True})
def load_apps(APPS_DIR): with lock: from cartoview.apps_handler.utils import create_apps_dir from cartoview.apps_handler.config import CartoviewApp global CARTOVIEW_APPS global APPS_SETTINGS create_apps_dir(APPS_DIR) if APPS_DIR not in sys.path: sys.path.append(APPS_DIR) logger.info("Loading Cartoview Apps.....") CartoviewApp.apps_dir = APPS_DIR CartoviewApp.load() for app in CartoviewApp.objects.values(): try: logger.info("Check if {} Healthy.\n".format(app.name)) # ensure that the folder is python module app_module = importlib.import_module(app.name) app_dir = os.path.dirname(app_module.__file__) app_settings_file = os.path.join(app_dir, 'settings.py') libs_dir = os.path.join(app_dir, 'libs') if os.path.exists(app_settings_file): # By doing this instead of import, app/settings.py can # refer to local variables from settings.py without # circular imports. app_settings_file = os.path.realpath(app_settings_file) APPS_SETTINGS += (app_settings_file, ) if os.path.exists(libs_dir) and libs_dir not in sys.path: logger.info( "Append {} libs folder to the system path.\n".format( app.name)) sys.path.append(libs_dir) logger.info("add {} to django INSTALLED_APPS.\n".format( app.name)) if app.name not in CARTOVIEW_APPS: # app_config.name.__str__() because Django don't like # unicode_literals CARTOVIEW_APPS += (app.name.__str__(), ) except Exception as e: print(e) logger.error(e)
def execute_pending(self): from cartoview.apps_handler.config import CartoviewApp CartoviewApp.load() pending_apps = CartoviewApp.objects.get_pending_apps().values() for app in pending_apps: _pending_apps = self.get_pending_apps(app.name) if _pending_apps: for _app in _pending_apps: _app_name = _app.get('name', None) _make_migrations = _app.get('makemigrations', False) _migrate = _app.get('migrate', False) if _app_name: if _make_migrations: self.makemigrations(_app_name) if _migrate: self.migrate(_app_name) else: self.migrate(app) self.collectstatic() carto_app = CartoviewApp.objects.get(app.name) carto_app.pending = False carto_app.commit() CartoviewApp.save()
def completely_remove(self): self.delete_app() self.delete_app_tables() self.delete_app_dir() CartoviewApp.objects.pop(self.name, None) CartoviewApp.save()
def _rollback(self): app = CartoviewApp.objects.pop(self.name, None) if app: CartoviewApp.save() self.delete_app_dir()