def test_app_installer(self): with lock: user = Profile.objects.filter(is_superuser=True).first() store_id = 1 app_name = "cartoview_test_app" app_version = "0.1" app_installer = AppInstaller(app_name, store_id, app_version, user) installed_apps = app_installer.install() self.assertEqual(len(installed_apps), 1) apps_admin_url = reverse( "admin:%s_%s_changelist" % (App._meta.app_label, App._meta.model_name)) data = { 'action': 'suspend_selected', ACTION_CHECKBOX_NAME: [str(app.pk) for app in installed_apps] } resp = self.client.post(apps_admin_url, data) self.assertNotEqual(resp.status_code, 500) data = { 'action': 'activate_selected', ACTION_CHECKBOX_NAME: [str(app.pk) for app in installed_apps] } resp = self.client.post(apps_admin_url, data) self.assertNotEqual(resp.status_code, 500) uninstalled = app_installer.uninstall() self.assertEqual(uninstalled, True)
def install(self, request): data = request.data app_name = data.get("app_name", None) store_id = data.get("store_id", None) version = data.get("app_version", None) if not app_name or not store_id or not version: return Response({"details": "invalid data"}, status=400) qs = App.objects.filter(name=app_name) if qs.count() > 0: permitted = request.user.has_perm("install_app", qs.first()) if not permitted: return Response( { "details": "You don't have permission to install <> app".format( app_name) }, status=403) if parse_version(version) <= parse_version(qs.first().version): return Response({"details": "app already installed"}, status=400) try: installer = AppInstaller(app_name, store_id, version, user=request.user) installer.install() except BaseException as e: if isinstance(e, AppAlreadyInstalledException): logger.warn(e) return Response({"details": str(e)}, status=500) serializer = AppSerializer(App.objects.get(name=app_name)) return Response(serializer.data, status=200)
def handle(self, *args, **options): app_name = options.get('name') app_version = options.get('app_version') store = AppStore.objects.get(is_default=True) q = App.objects.filter(name=app_name) try: if q.count() == 0 or (q.first() and compare_version( q.first().version, app_version)): installer = AppInstaller(app_name, store.id, app_version) installer.install() except Exception as ex: logger.error(ex.message)
def uninstall(self, request, pk=None): try: app = App.objects.get(pk=pk) except ObjectDoesNotExist: return Response("App Not Found to uninstall", status=404) try: installer = AppInstaller(app.name, app.store.id, app.version, user=request.user) installer.uninstall() except BaseException as e: return Response({"details": str(e)}, status=500) serializer = AppSerializer(app) return Response(serializer.data, status=200)
def delete_application_on_fail(self, appname): from cartoview.app_manager.installer import AppInstaller AppInstaller(appname).uninstall(restart=True)