Skip to content

Commit

Permalink
Fixes #14572 - Constrains JobView (and related views) badge to specif…
Browse files Browse the repository at this point in the history
…ic named job (#14754)

* Fixes #14572 - Constrains JobView (and related views) badge to specific named job

* Adjust report views to resolve same problem

* Fixed PEP8 error

* Update netbox/templates/extras/script/base.html

Co-authored-by: Jeremy Stretch <jstretch@netboxlabs.com>

* Move function to method on PythonModuleMixin

* Update netbox/extras/views.py

Co-authored-by: Jeremy Stretch <jstretch@netboxlabs.com>

* Update netbox/extras/views.py

Co-authored-by: Jeremy Stretch <jstretch@netboxlabs.com>

* Update netbox/extras/views.py

Co-authored-by: Jeremy Stretch <jstretch@netboxlabs.com>

* Update netbox/extras/views.py

Co-authored-by: Jeremy Stretch <jstretch@netboxlabs.com>

* Update to mixin and view

---------

Co-authored-by: Jeremy Stretch <jstretch@netboxlabs.com>
  • Loading branch information
DanSheps and jeremystretch authored Jan 22, 2024
1 parent 79e0d3a commit fd53925
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 27 deletions.
10 changes: 10 additions & 0 deletions netbox/extras/models/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,16 @@

class PythonModuleMixin:

def get_jobs(self, name):
"""
Returns a list of Jobs associated with this specific script or report module
:param name: The class name of the script or report
:return: List of Jobs associated with this
"""
return self.jobs.filter(
name=name
)

@property
def path(self):
return os.path.splitext(self.file_path)[0]
Expand Down
44 changes: 19 additions & 25 deletions netbox/extras/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -1057,16 +1057,14 @@ def get_required_permission(self):
def get(self, request, module, name):
module = get_report_module(module, request)
report = module.reports[name]()
jobs = module.get_jobs(report.class_name)

object_type = ContentType.objects.get(app_label='extras', model='reportmodule')
report.result = Job.objects.filter(
object_type=object_type,
object_id=module.pk,
name=report.name,
report.result = jobs.filter(
status__in=JobStatusChoices.TERMINAL_STATE_CHOICES
).first()

return render(request, 'extras/report.html', {
'job_count': jobs.count(),
'module': module,
'report': report,
'form': ReportForm(scheduling_enabled=report.scheduling_enabled),
Expand All @@ -1078,6 +1076,7 @@ def post(self, request, module, name):

module = get_report_module(module, request)
report = module.reports[name]()
jobs = module.get_jobs(report.class_name)
form = ReportForm(request.POST, scheduling_enabled=report.scheduling_enabled)

if form.is_valid():
Expand All @@ -1086,6 +1085,7 @@ def post(self, request, module, name):
if not get_workers_for_queue('default'):
messages.error(request, "Unable to run report: RQ worker process not running.")
return render(request, 'extras/report.html', {
'job_count': jobs.count(),
'report': report,
})

Expand All @@ -1103,6 +1103,7 @@ def post(self, request, module, name):
return redirect('extras:report_result', job_pk=job.pk)

return render(request, 'extras/report.html', {
'job_count': jobs.count(),
'module': module,
'report': report,
'form': form,
Expand All @@ -1117,8 +1118,10 @@ def get_required_permission(self):
def get(self, request, module, name):
module = get_report_module(module, request)
report = module.reports[name]()
jobs = module.get_jobs(report.class_name)

return render(request, 'extras/report/source.html', {
'job_count': jobs.count(),
'module': module,
'report': report,
'tab': 'source',
Expand All @@ -1133,13 +1136,7 @@ def get_required_permission(self):
def get(self, request, module, name):
module = get_report_module(module, request)
report = module.reports[name]()

object_type = ContentType.objects.get(app_label='extras', model='reportmodule')
jobs = Job.objects.filter(
object_type=object_type,
object_id=module.pk,
name=report.class_name
)
jobs = module.get_jobs(report.class_name)

jobs_table = JobTable(
data=jobs,
Expand All @@ -1149,6 +1146,7 @@ def get(self, request, module, name):
jobs_table.configure(request)

return render(request, 'extras/report/jobs.html', {
'job_count': jobs.count(),
'module': module,
'report': report,
'table': jobs_table,
Expand Down Expand Up @@ -1232,19 +1230,16 @@ def get_required_permission(self):
def get(self, request, module, name):
module = get_script_module(module, request)
script = module.scripts[name]()
jobs = module.get_jobs(script.class_name)
form = script.as_form(initial=normalize_querydict(request.GET))

# Look for a pending Job (use the latest one by creation timestamp)
object_type = ContentType.objects.get(app_label='extras', model='scriptmodule')
script.result = Job.objects.filter(
object_type=object_type,
object_id=module.pk,
name=script.name,
).exclude(
script.result = module.get_jobs(script.class_name).exclude(
status__in=JobStatusChoices.TERMINAL_STATE_CHOICES
).first()

return render(request, 'extras/script.html', {
'job_count': jobs.count(),
'module': module,
'script': script,
'form': form,
Expand All @@ -1256,6 +1251,7 @@ def post(self, request, module, name):

module = get_script_module(module, request)
script = module.scripts[name]()
jobs = module.get_jobs(script.class_name)
form = script.as_form(request.POST, request.FILES)

# Allow execution only if RQ worker process is running
Expand All @@ -1279,6 +1275,7 @@ def post(self, request, module, name):
return redirect('extras:script_result', job_pk=job.pk)

return render(request, 'extras/script.html', {
'job_count': jobs.count(),
'module': module,
'script': script,
'form': form,
Expand All @@ -1293,8 +1290,10 @@ def get_required_permission(self):
def get(self, request, module, name):
module = get_script_module(module, request)
script = module.scripts[name]()
jobs = module.get_jobs(script.class_name)

return render(request, 'extras/script/source.html', {
'job_count': jobs.count(),
'module': module,
'script': script,
'tab': 'source',
Expand All @@ -1309,13 +1308,7 @@ def get_required_permission(self):
def get(self, request, module, name):
module = get_script_module(module, request)
script = module.scripts[name]()

object_type = ContentType.objects.get(app_label='extras', model='scriptmodule')
jobs = Job.objects.filter(
object_type=object_type,
object_id=module.pk,
name=script.class_name
)
jobs = module.get_jobs(script.class_name)

jobs_table = JobTable(
data=jobs,
Expand All @@ -1325,6 +1318,7 @@ def get(self, request, module, name):
jobs_table.configure(request)

return render(request, 'extras/script/jobs.html', {
'job_count': jobs.count(),
'module': module,
'script': script,
'table': jobs_table,
Expand Down
2 changes: 1 addition & 1 deletion netbox/templates/extras/report/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
</li>
<li class="nav-item" role="presentation">
<a class="nav-link{% if tab == 'jobs' %} active{% endif %}" href="{% url 'extras:report_jobs' module=report.module name=report.class_name %}">
{% trans "Jobs" %} {% badge module.jobs.count %}
{% trans "Jobs" %} {% badge job_count %}
</a>
</li>
</ul>
Expand Down
2 changes: 1 addition & 1 deletion netbox/templates/extras/script/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
</li>
<li class="nav-item" role="presentation">
<a class="nav-link{% if tab == 'jobs' %} active{% endif %}" href="{% url 'extras:script_jobs' module=script.module name=script.class_name %}">
{% trans "Jobs" %} {% badge module.jobs.count %}
{% trans "Jobs" %} {% badge job_count %}
</a>
</li>
</ul>
Expand Down

0 comments on commit fd53925

Please sign in to comment.