Compare commits
2 Commits
094597e2f6
...
8158be3139
Author | SHA1 | Date |
---|---|---|
Arthur Hanson | 8158be3139 | |
Arthur | 80802a4357 |
|
@ -96,6 +96,7 @@ class ScriptModule(PythonModuleMixin, JobsMixin, ManagedFile):
|
|||
Proxy model for script module files.
|
||||
"""
|
||||
objects = ScriptModuleManager()
|
||||
error = None
|
||||
|
||||
class Meta:
|
||||
proxy = True
|
||||
|
@ -118,6 +119,7 @@ class ScriptModule(PythonModuleMixin, JobsMixin, ManagedFile):
|
|||
try:
|
||||
module = self.get_module()
|
||||
except Exception as e:
|
||||
self.error = e
|
||||
logger.debug(f"Failed to load script: {self.python_name} error: {e}")
|
||||
module = None
|
||||
|
||||
|
|
|
@ -1043,12 +1043,27 @@ class ScriptListView(ContentTypePermissionRequiredMixin, View):
|
|||
})
|
||||
|
||||
|
||||
class ScriptView(generic.ObjectView):
|
||||
class BaseScriptView(generic.ObjectView):
|
||||
queryset = Script.objects.all()
|
||||
|
||||
def _get_script_class(self, script):
|
||||
script_class = script.python_class
|
||||
if script_class:
|
||||
script_class = script_class()
|
||||
|
||||
return script_class
|
||||
|
||||
|
||||
class ScriptView(BaseScriptView):
|
||||
|
||||
def get(self, request, **kwargs):
|
||||
script = self.get_object(**kwargs)
|
||||
script_class = script.python_class()
|
||||
script_class = self._get_script_class(script)
|
||||
if not script_class:
|
||||
return render(request, 'extras/script.html', {
|
||||
'script': script,
|
||||
})
|
||||
|
||||
form = script_class.as_form(initial=normalize_querydict(request.GET))
|
||||
|
||||
return render(request, 'extras/script.html', {
|
||||
|
@ -1060,11 +1075,16 @@ class ScriptView(generic.ObjectView):
|
|||
|
||||
def post(self, request, **kwargs):
|
||||
script = self.get_object(**kwargs)
|
||||
script_class = script.python_class()
|
||||
|
||||
if not request.user.has_perm('extras.run_script', obj=script):
|
||||
return HttpResponseForbidden()
|
||||
|
||||
script_class = self._get_script_class(script)
|
||||
if not script_class:
|
||||
return render(request, 'extras/script.html', {
|
||||
'script': script,
|
||||
})
|
||||
|
||||
form = script_class.as_form(request.POST, request.FILES)
|
||||
|
||||
# Allow execution only if RQ worker process is running
|
||||
|
@ -1094,21 +1114,22 @@ class ScriptView(generic.ObjectView):
|
|||
})
|
||||
|
||||
|
||||
class ScriptSourceView(generic.ObjectView):
|
||||
class ScriptSourceView(BaseScriptView):
|
||||
queryset = Script.objects.all()
|
||||
|
||||
def get(self, request, **kwargs):
|
||||
script = self.get_object(**kwargs)
|
||||
script_class = self._get_script_class(script)
|
||||
|
||||
return render(request, 'extras/script/source.html', {
|
||||
'script': script,
|
||||
'script_class': script.python_class(),
|
||||
'script_class': script_class,
|
||||
'job_count': script.jobs.count(),
|
||||
'tab': 'source',
|
||||
})
|
||||
|
||||
|
||||
class ScriptJobsView(generic.ObjectView):
|
||||
class ScriptJobsView(BaseScriptView):
|
||||
queryset = Script.objects.all()
|
||||
|
||||
def get(self, request, **kwargs):
|
||||
|
|
|
@ -14,38 +14,43 @@
|
|||
{% trans "You do not have permission to run scripts" %}.
|
||||
</div>
|
||||
{% endif %}
|
||||
<form action="" method="post" enctype="multipart/form-data" class="object-edit">
|
||||
{% csrf_token %}
|
||||
<div class="field-group my-4">
|
||||
{# Render grouped fields according to declared fieldsets #}
|
||||
{% for group, fields in script_class.get_fieldsets %}
|
||||
{% if fields %}
|
||||
<div class="field-group mb-5">
|
||||
<div class="row">
|
||||
<h5 class="col-9 offset-3">{{ group }}</h5>
|
||||
{% if form %}
|
||||
<form action="" method="post" enctype="multipart/form-data" class="object-edit">
|
||||
{% csrf_token %}
|
||||
<div class="field-group my-4">
|
||||
{# Render grouped fields according to declared fieldsets #}
|
||||
{% for group, fields in script_class.get_fieldsets %}
|
||||
{% if fields %}
|
||||
<div class="field-group mb-5">
|
||||
<div class="row">
|
||||
<h5 class="col-9 offset-3">{{ group }}</h5>
|
||||
</div>
|
||||
{% for name in fields %}
|
||||
{% with field=form|getfield:name %}
|
||||
{% render_field field %}
|
||||
{% endwith %}
|
||||
{% endfor %}
|
||||
</div>
|
||||
{% for name in fields %}
|
||||
{% with field=form|getfield:name %}
|
||||
{% render_field field %}
|
||||
{% endwith %}
|
||||
{% endfor %}
|
||||
</div>
|
||||
{% endif %}
|
||||
{% endfor %}
|
||||
</div>
|
||||
<div class="text-end">
|
||||
<a href="{% url 'extras:script_list' %}" class="btn btn-outline-secondary">{% trans "Cancel" %}</a>
|
||||
{% if not request.user|can_run:script or not script.is_executable %}
|
||||
<button class="btn btn-primary" disabled>
|
||||
<i class="mdi mdi-play"></i> {% trans "Run Script" %}
|
||||
</button>
|
||||
{% else %}
|
||||
<button type="submit" name="_run" class="btn btn-primary">
|
||||
<i class="mdi mdi-play"></i> {% trans "Run Script" %}
|
||||
</button>
|
||||
{% endif %}
|
||||
{% endfor %}
|
||||
</div>
|
||||
<div class="text-end">
|
||||
<a href="{% url 'extras:script_list' %}" class="btn btn-outline-secondary">{% trans "Cancel" %}</a>
|
||||
{% if not request.user|can_run:script or not script.is_executable %}
|
||||
<button class="btn btn-primary" disabled>
|
||||
<i class="mdi mdi-play"></i> {% trans "Run Script" %}
|
||||
</button>
|
||||
{% else %}
|
||||
<button type="submit" name="_run" class="btn btn-primary">
|
||||
<i class="mdi mdi-play"></i> {% trans "Run Script" %}
|
||||
</button>
|
||||
{% endif %}
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
</form>
|
||||
{% else %}
|
||||
<p>{% trans "Error loading script" %}.</p>
|
||||
<pre class="block">{{ script.module.error }}</pre>
|
||||
{% endif %}
|
||||
</div>
|
||||
</div>
|
||||
{% endblock content %}
|
||||
|
|
|
@ -1,6 +1,14 @@
|
|||
{% extends 'extras/script/base.html' %}
|
||||
{% load i18n %}
|
||||
|
||||
{% block content %}
|
||||
<code class="h6 my-3 d-block">{{ script_class.filename }}</code>
|
||||
<pre class="block">{{ script_class.source }}</pre>
|
||||
|
||||
{% if script_class %}
|
||||
<code class="h6 my-3 d-block">{{ script_class.filename }}</code>
|
||||
<pre class="block">{{ script_class.source }}</pre>
|
||||
{% else %}
|
||||
<p>{% trans "Error loading script" %}.</p>
|
||||
<pre class="block">{{ script.module.error }}</pre>
|
||||
{% endif %}
|
||||
|
||||
{% endblock %}
|
||||
|
|
Loading…
Reference in New Issue