def run_command_view(self, request): """ This view has the user running the command and commiting changes to the database. """ if not request.user.is_superuser: return redirect("admin:login") context = dict(self.admin_site.each_context(request)) if request.method == "POST": tool = acltools.get_tool_from_data(request.POST) context.update(tool=tool) if tool.form_instance.is_valid(): tool.run(request.user, commit=True) form = tool.form_instance else: raise Exception(_("Only POST requests allowed.")) context.update( { "adminform": helpers.AdminForm( form, list([(None, {"fields": form.base_fields})]), self.get_prepopulated_fields(request), ), "action": "run", "app_label": self.model._meta.app_label, "opts": self.model._meta, "title": tool.name, } ) return TemplateResponse( request, "admin/peeringdb_server/commandlinetool/run_command.html", context )
def handle(self, *args, **options): command = CommandLineTool.objects.filter(status="waiting").order_by("-created").first() if command: self.log("Running {}".format(command)) command.set_running() command.save() try: tool = get_tool_from_data({"tool":command.tool}) arguments = json.loads(command.arguments) tool.kwargs = arguments.get("kwargs") tool.args = arguments.get("args") tool._run(command.user, commit=True) command.delete() except Exception as exc: command.status = "done" command.result = "Command ended with error: {}".format(exc) command.save()
def preview_command_view(self, request): """ This view has the user preview the result of running the command """ if not self.has_add_permission(request): return HttpResponseForbidden() context = dict(self.admin_site.each_context(request)) if request.method == "POST": tool = acltools.get_tool_from_data(request.POST) context.update(tool=tool) if tool.form_instance.is_valid(): action = "run" tool.run(request.user, commit=False) else: action = "run" form = tool.form_instance else: raise Exception(_("Only POST requests allowed.")) context.update({ "adminform": helpers.AdminForm( form, list([(None, { "fields": form.base_fields })]), self.get_prepopulated_fields(request), ), "action": action, "app_label": self.model._meta.app_label, "opts": self.model._meta, "title": _("{} (Preview)").format(tool.name), }) return TemplateResponse( request, "admin/peeringdb_server/commandlinetool/preview_command.html", context, )
def handle(self, *args, **options): command = (CommandLineTool.objects.filter( status="waiting").order_by("-created").first()) if command: self.log(f"Running {command}") command.set_running() command.save() try: tool = get_tool_from_data({"tool": command.tool}) arguments = json.loads(command.arguments) tool.kwargs = arguments.get("kwargs") tool.args = arguments.get("args") tool._run(command.user, commit=True) command.delete() except Exception as exc: command.status = "done" command.result = f"Command ended with error: {exc}" command.save()