2020-07-13 15:11:58 -04:00
|
|
|
from django.contrib import messages
|
2018-06-14 16:15:14 -04:00
|
|
|
from django.contrib.contenttypes.models import ContentType
|
2021-04-05 13:53:25 -04:00
|
|
|
from django.db.models import Count, Q
|
2019-08-12 11:39:36 -04:00
|
|
|
from django.http import Http404, HttpResponseForbidden
|
2018-07-03 09:47:44 -04:00
|
|
|
from django.shortcuts import get_object_or_404, redirect, render
|
2021-03-16 15:00:08 -04:00
|
|
|
from django.urls import reverse
|
2017-09-21 16:32:05 -04:00
|
|
|
from django.views.generic import View
|
2020-07-13 15:11:58 -04:00
|
|
|
from django_rq.queues import get_connection
|
|
|
|
from rq import Worker
|
2017-03-30 21:55:57 -04:00
|
|
|
|
2020-11-11 16:07:38 -05:00
|
|
|
from netbox.views import generic
|
2017-09-28 12:51:10 -04:00
|
|
|
from utilities.forms import ConfirmationForm
|
2021-03-26 13:02:55 -04:00
|
|
|
from utilities.tables import paginate_table
|
2020-12-17 14:47:49 -05:00
|
|
|
from utilities.utils import copy_safe_request, count_related, shallow_compare_dict
|
2020-11-11 16:07:38 -05:00
|
|
|
from utilities.views import ContentTypePermissionRequiredMixin
|
2020-06-12 09:48:23 -04:00
|
|
|
from . import filters, forms, tables
|
2020-06-29 03:50:05 -04:00
|
|
|
from .choices import JobResultStatusChoices
|
2021-03-16 15:00:08 -04:00
|
|
|
from .models import ConfigContext, ImageAttachment, JournalEntry, ObjectChange, JobResult, Tag, TaggedItem
|
2020-06-29 03:50:05 -04:00
|
|
|
from .reports import get_report, get_reports, run_report
|
2019-08-12 13:51:25 -04:00
|
|
|
from .scripts import get_scripts, run_script
|
2018-05-22 12:22:46 -04:00
|
|
|
|
|
|
|
|
|
|
|
#
|
|
|
|
# Tags
|
|
|
|
#
|
|
|
|
|
2020-11-11 16:07:38 -05:00
|
|
|
class TagListView(generic.ObjectListView):
|
2020-07-23 14:20:56 -04:00
|
|
|
queryset = Tag.objects.annotate(
|
2020-12-17 14:47:49 -05:00
|
|
|
items=count_related(TaggedItem, 'tag')
|
2020-11-25 15:49:18 -05:00
|
|
|
)
|
2020-01-09 20:57:13 -05:00
|
|
|
filterset = filters.TagFilterSet
|
|
|
|
filterset_form = forms.TagFilterForm
|
2020-06-12 09:48:23 -04:00
|
|
|
table = tables.TagTable
|
2018-05-22 12:22:46 -04:00
|
|
|
|
|
|
|
|
2021-03-26 15:25:18 -04:00
|
|
|
class TagView(generic.ObjectView):
|
|
|
|
queryset = Tag.objects.all()
|
|
|
|
|
|
|
|
def get_extra_context(self, request, instance):
|
|
|
|
tagged_items = TaggedItem.objects.filter(tag=instance)
|
2021-03-29 16:53:41 -04:00
|
|
|
taggeditem_table = tables.TaggedItemTable(
|
|
|
|
data=tagged_items,
|
|
|
|
orderable=False
|
|
|
|
)
|
|
|
|
paginate_table(taggeditem_table, request)
|
2021-03-26 15:25:18 -04:00
|
|
|
|
2021-04-05 13:53:25 -04:00
|
|
|
object_types = [
|
|
|
|
{
|
|
|
|
'content_type': ContentType.objects.get(pk=ti['content_type']),
|
|
|
|
'item_count': ti['item_count']
|
|
|
|
} for ti in tagged_items.values('content_type').annotate(item_count=Count('pk'))
|
|
|
|
]
|
|
|
|
|
2021-03-26 15:25:18 -04:00
|
|
|
return {
|
2021-03-29 16:53:41 -04:00
|
|
|
'taggeditem_table': taggeditem_table,
|
2021-03-26 15:25:18 -04:00
|
|
|
'tagged_item_count': tagged_items.count(),
|
2021-04-05 13:53:25 -04:00
|
|
|
'object_types': object_types,
|
2021-03-26 15:25:18 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-11-11 16:07:38 -05:00
|
|
|
class TagEditView(generic.ObjectEditView):
|
2020-07-23 14:20:56 -04:00
|
|
|
queryset = Tag.objects.all()
|
2019-08-28 11:56:00 -04:00
|
|
|
model_form = forms.TagForm
|
2018-05-22 12:22:46 -04:00
|
|
|
|
|
|
|
|
2020-11-11 16:07:38 -05:00
|
|
|
class TagDeleteView(generic.ObjectDeleteView):
|
2020-07-23 14:20:56 -04:00
|
|
|
queryset = Tag.objects.all()
|
2018-05-22 12:22:46 -04:00
|
|
|
|
|
|
|
|
2020-11-11 16:07:38 -05:00
|
|
|
class TagBulkImportView(generic.BulkImportView):
|
2020-07-23 14:20:56 -04:00
|
|
|
queryset = Tag.objects.all()
|
2020-06-12 09:48:23 -04:00
|
|
|
model_form = forms.TagCSVForm
|
|
|
|
table = tables.TagTable
|
|
|
|
|
|
|
|
|
2020-11-11 16:07:38 -05:00
|
|
|
class TagBulkEditView(generic.BulkEditView):
|
2020-07-23 14:20:56 -04:00
|
|
|
queryset = Tag.objects.annotate(
|
2020-12-17 14:47:49 -05:00
|
|
|
items=count_related(TaggedItem, 'tag')
|
2020-11-25 15:49:18 -05:00
|
|
|
)
|
2020-06-12 09:48:23 -04:00
|
|
|
table = tables.TagTable
|
2019-08-28 11:56:00 -04:00
|
|
|
form = forms.TagBulkEditForm
|
|
|
|
|
|
|
|
|
2020-11-11 16:07:38 -05:00
|
|
|
class TagBulkDeleteView(generic.BulkDeleteView):
|
2020-07-23 14:20:56 -04:00
|
|
|
queryset = Tag.objects.annotate(
|
2020-12-17 14:47:49 -05:00
|
|
|
items=count_related(TaggedItem, 'tag')
|
2020-11-25 15:49:18 -05:00
|
|
|
)
|
2020-06-12 09:48:23 -04:00
|
|
|
table = tables.TagTable
|
2017-03-30 21:55:57 -04:00
|
|
|
|
|
|
|
|
2018-06-27 16:02:34 -04:00
|
|
|
#
|
|
|
|
# Config contexts
|
|
|
|
#
|
|
|
|
|
2020-11-11 16:07:38 -05:00
|
|
|
class ConfigContextListView(generic.ObjectListView):
|
2018-06-27 16:02:34 -04:00
|
|
|
queryset = ConfigContext.objects.all()
|
2020-01-09 20:57:13 -05:00
|
|
|
filterset = filters.ConfigContextFilterSet
|
|
|
|
filterset_form = forms.ConfigContextFilterForm
|
2020-06-12 09:48:23 -04:00
|
|
|
table = tables.ConfigContextTable
|
2020-02-13 16:39:38 -05:00
|
|
|
action_buttons = ('add',)
|
2018-06-27 16:02:34 -04:00
|
|
|
|
|
|
|
|
2020-11-11 16:07:38 -05:00
|
|
|
class ConfigContextView(generic.ObjectView):
|
2020-05-21 15:39:07 -04:00
|
|
|
queryset = ConfigContext.objects.all()
|
2018-06-27 16:02:34 -04:00
|
|
|
|
2020-11-19 15:59:11 -05:00
|
|
|
def get_extra_context(self, request, instance):
|
2020-04-24 12:01:41 -04:00
|
|
|
# Determine user's preferred output format
|
|
|
|
if request.GET.get('format') in ['json', 'yaml']:
|
|
|
|
format = request.GET.get('format')
|
2020-05-06 23:44:06 -04:00
|
|
|
if request.user.is_authenticated:
|
|
|
|
request.user.config.set('extras.configcontext.format', format, commit=True)
|
|
|
|
elif request.user.is_authenticated:
|
2020-04-24 12:01:41 -04:00
|
|
|
format = request.user.config.get('extras.configcontext.format', 'json')
|
2020-05-06 23:44:06 -04:00
|
|
|
else:
|
|
|
|
format = 'json'
|
2020-04-24 12:01:41 -04:00
|
|
|
|
2020-11-19 15:59:11 -05:00
|
|
|
return {
|
2020-04-24 12:01:41 -04:00
|
|
|
'format': format,
|
2020-11-19 15:59:11 -05:00
|
|
|
}
|
2018-06-27 16:02:34 -04:00
|
|
|
|
|
|
|
|
2020-11-11 16:07:38 -05:00
|
|
|
class ConfigContextEditView(generic.ObjectEditView):
|
2020-05-11 12:37:22 -04:00
|
|
|
queryset = ConfigContext.objects.all()
|
2019-08-28 11:56:00 -04:00
|
|
|
model_form = forms.ConfigContextForm
|
2018-06-27 16:02:34 -04:00
|
|
|
template_name = 'extras/configcontext_edit.html'
|
|
|
|
|
|
|
|
|
2020-11-11 16:07:38 -05:00
|
|
|
class ConfigContextBulkEditView(generic.BulkEditView):
|
2018-11-13 15:08:55 -05:00
|
|
|
queryset = ConfigContext.objects.all()
|
2020-01-09 20:57:13 -05:00
|
|
|
filterset = filters.ConfigContextFilterSet
|
2020-06-12 09:48:23 -04:00
|
|
|
table = tables.ConfigContextTable
|
2019-08-28 11:56:00 -04:00
|
|
|
form = forms.ConfigContextBulkEditForm
|
2018-11-13 15:08:55 -05:00
|
|
|
|
|
|
|
|
2020-11-11 16:07:38 -05:00
|
|
|
class ConfigContextDeleteView(generic.ObjectDeleteView):
|
2020-05-11 12:47:01 -04:00
|
|
|
queryset = ConfigContext.objects.all()
|
2018-06-27 16:02:34 -04:00
|
|
|
|
|
|
|
|
2020-11-11 16:07:38 -05:00
|
|
|
class ConfigContextBulkDeleteView(generic.BulkDeleteView):
|
2018-06-27 16:02:34 -04:00
|
|
|
queryset = ConfigContext.objects.all()
|
2020-06-12 09:48:23 -04:00
|
|
|
table = tables.ConfigContextTable
|
2018-06-27 16:02:34 -04:00
|
|
|
|
|
|
|
|
2020-11-11 16:07:38 -05:00
|
|
|
class ObjectConfigContextView(generic.ObjectView):
|
2018-06-28 13:48:12 -04:00
|
|
|
base_template = None
|
2020-11-19 15:59:11 -05:00
|
|
|
template_name = 'extras/object_configcontext.html'
|
2018-06-28 13:48:12 -04:00
|
|
|
|
2020-11-19 15:59:11 -05:00
|
|
|
def get_extra_context(self, request, instance):
|
|
|
|
source_contexts = ConfigContext.objects.restrict(request.user, 'view').get_for_object(instance)
|
2018-06-28 13:48:12 -04:00
|
|
|
|
2020-04-24 12:01:41 -04:00
|
|
|
# Determine user's preferred output format
|
|
|
|
if request.GET.get('format') in ['json', 'yaml']:
|
|
|
|
format = request.GET.get('format')
|
2020-05-06 23:44:06 -04:00
|
|
|
if request.user.is_authenticated:
|
|
|
|
request.user.config.set('extras.configcontext.format', format, commit=True)
|
|
|
|
elif request.user.is_authenticated:
|
2020-04-24 12:01:41 -04:00
|
|
|
format = request.user.config.get('extras.configcontext.format', 'json')
|
2020-05-06 23:44:06 -04:00
|
|
|
else:
|
|
|
|
format = 'json'
|
2020-04-24 12:01:41 -04:00
|
|
|
|
2020-11-19 15:59:11 -05:00
|
|
|
return {
|
|
|
|
'rendered_context': instance.get_config_context(),
|
2018-06-28 13:48:12 -04:00
|
|
|
'source_contexts': source_contexts,
|
2020-04-24 12:01:41 -04:00
|
|
|
'format': format,
|
2018-06-28 13:48:12 -04:00
|
|
|
'base_template': self.base_template,
|
|
|
|
'active_tab': 'config-context',
|
2020-11-19 15:59:11 -05:00
|
|
|
}
|
2018-06-28 13:48:12 -04:00
|
|
|
|
|
|
|
|
2018-06-14 16:15:14 -04:00
|
|
|
#
|
|
|
|
# Change logging
|
|
|
|
#
|
|
|
|
|
2020-11-11 16:07:38 -05:00
|
|
|
class ObjectChangeListView(generic.ObjectListView):
|
2020-10-30 16:52:40 -04:00
|
|
|
queryset = ObjectChange.objects.all()
|
2020-01-09 20:57:13 -05:00
|
|
|
filterset = filters.ObjectChangeFilterSet
|
|
|
|
filterset_form = forms.ObjectChangeFilterForm
|
2020-06-12 09:48:23 -04:00
|
|
|
table = tables.ObjectChangeTable
|
2018-06-20 13:52:54 -04:00
|
|
|
template_name = 'extras/objectchange_list.html'
|
2020-02-13 17:11:39 -05:00
|
|
|
action_buttons = ('export',)
|
2018-06-20 13:52:54 -04:00
|
|
|
|
|
|
|
|
2020-11-11 16:07:38 -05:00
|
|
|
class ObjectChangeView(generic.ObjectView):
|
2020-05-21 15:39:07 -04:00
|
|
|
queryset = ObjectChange.objects.all()
|
2018-06-20 13:52:54 -04:00
|
|
|
|
2020-11-19 15:59:11 -05:00
|
|
|
def get_extra_context(self, request, instance):
|
2020-06-01 11:43:49 -04:00
|
|
|
related_changes = ObjectChange.objects.restrict(request.user, 'view').filter(
|
2020-11-19 15:59:11 -05:00
|
|
|
request_id=instance.request_id
|
2020-06-01 11:43:49 -04:00
|
|
|
).exclude(
|
2020-11-19 15:59:11 -05:00
|
|
|
pk=instance.pk
|
2020-06-01 11:43:49 -04:00
|
|
|
)
|
2020-06-12 09:48:23 -04:00
|
|
|
related_changes_table = tables.ObjectChangeTable(
|
2018-06-20 13:52:54 -04:00
|
|
|
data=related_changes[:50],
|
|
|
|
orderable=False
|
|
|
|
)
|
|
|
|
|
2020-06-01 11:43:49 -04:00
|
|
|
objectchanges = ObjectChange.objects.restrict(request.user, 'view').filter(
|
2020-11-19 15:59:11 -05:00
|
|
|
changed_object_type=instance.changed_object_type,
|
|
|
|
changed_object_id=instance.changed_object_id,
|
2020-02-15 22:39:08 +00:00
|
|
|
)
|
|
|
|
|
2020-11-19 15:59:11 -05:00
|
|
|
next_change = objectchanges.filter(time__gt=instance.time).order_by('time').first()
|
|
|
|
prev_change = objectchanges.filter(time__lt=instance.time).order_by('-time').first()
|
2020-02-15 22:39:08 +00:00
|
|
|
|
2021-03-04 13:06:04 -05:00
|
|
|
if instance.prechange_data and instance.postchange_data:
|
2020-02-15 22:39:08 +00:00
|
|
|
diff_added = shallow_compare_dict(
|
2021-03-04 13:06:04 -05:00
|
|
|
instance.prechange_data or dict(),
|
|
|
|
instance.postchange_data or dict(),
|
2020-02-15 22:39:08 +00:00
|
|
|
exclude=['last_updated'],
|
|
|
|
)
|
2021-03-04 13:06:04 -05:00
|
|
|
diff_removed = {
|
|
|
|
x: instance.prechange_data.get(x) for x in diff_added
|
|
|
|
} if instance.prechange_data else {}
|
2020-02-15 22:39:08 +00:00
|
|
|
else:
|
2021-03-04 13:06:04 -05:00
|
|
|
diff_added = None
|
|
|
|
diff_removed = None
|
2020-02-15 22:39:08 +00:00
|
|
|
|
2020-11-19 15:59:11 -05:00
|
|
|
return {
|
2020-02-15 22:39:08 +00:00
|
|
|
'diff_added': diff_added,
|
|
|
|
'diff_removed': diff_removed,
|
|
|
|
'next_change': next_change,
|
|
|
|
'prev_change': prev_change,
|
2018-06-20 13:52:54 -04:00
|
|
|
'related_changes_table': related_changes_table,
|
|
|
|
'related_changes_count': related_changes.count()
|
2020-11-19 15:59:11 -05:00
|
|
|
}
|
2018-06-20 13:52:54 -04:00
|
|
|
|
|
|
|
|
|
|
|
class ObjectChangeLogView(View):
|
2018-06-14 16:15:14 -04:00
|
|
|
"""
|
2018-06-20 13:52:54 -04:00
|
|
|
Present a history of changes made to a particular object.
|
2020-11-19 11:49:37 -05:00
|
|
|
|
|
|
|
base_template: The name of the template to extend. If not provided, "<app>/<model>.html" will be used.
|
2018-06-14 16:15:14 -04:00
|
|
|
"""
|
2020-11-19 11:49:37 -05:00
|
|
|
base_template = None
|
2018-06-14 16:15:14 -04:00
|
|
|
|
|
|
|
def get(self, request, model, **kwargs):
|
|
|
|
|
2020-06-25 15:58:13 -04:00
|
|
|
# Handle QuerySet restriction of parent object if needed
|
|
|
|
if hasattr(model.objects, 'restrict'):
|
|
|
|
obj = get_object_or_404(model.objects.restrict(request.user, 'view'), **kwargs)
|
|
|
|
else:
|
|
|
|
obj = get_object_or_404(model, **kwargs)
|
2018-06-14 16:15:14 -04:00
|
|
|
|
2018-06-22 15:05:40 -04:00
|
|
|
# Gather all changes for this object (and its related objects)
|
2018-06-14 16:15:14 -04:00
|
|
|
content_type = ContentType.objects.get_for_model(model)
|
2020-06-01 11:43:49 -04:00
|
|
|
objectchanges = ObjectChange.objects.restrict(request.user, 'view').prefetch_related(
|
2018-06-22 15:05:40 -04:00
|
|
|
'user', 'changed_object_type'
|
2018-06-20 13:52:54 -04:00
|
|
|
).filter(
|
2018-06-22 15:05:40 -04:00
|
|
|
Q(changed_object_type=content_type, changed_object_id=obj.pk) |
|
|
|
|
Q(related_object_type=content_type, related_object_id=obj.pk)
|
2018-06-20 13:52:54 -04:00
|
|
|
)
|
2020-06-12 09:48:23 -04:00
|
|
|
objectchanges_table = tables.ObjectChangeTable(
|
2018-06-20 13:52:54 -04:00
|
|
|
data=objectchanges,
|
|
|
|
orderable=False
|
|
|
|
)
|
2021-03-26 13:02:55 -04:00
|
|
|
paginate_table(objectchanges_table, request)
|
2019-07-18 21:40:36 -04:00
|
|
|
|
2020-11-19 13:31:59 -05:00
|
|
|
# Default to using "<app>/<model>.html" as the template, if it exists. Otherwise,
|
|
|
|
# fall back to using base.html.
|
2020-11-19 11:49:37 -05:00
|
|
|
if self.base_template is None:
|
|
|
|
self.base_template = f"{model._meta.app_label}/{model._meta.model_name}.html"
|
2018-06-14 16:15:14 -04:00
|
|
|
|
2018-06-20 13:52:54 -04:00
|
|
|
return render(request, 'extras/object_changelog.html', {
|
2020-11-19 11:49:37 -05:00
|
|
|
'object': obj,
|
2019-07-18 21:40:36 -04:00
|
|
|
'table': objectchanges_table,
|
2020-11-19 11:49:37 -05:00
|
|
|
'base_template': self.base_template,
|
2018-06-14 16:15:14 -04:00
|
|
|
'active_tab': 'changelog',
|
|
|
|
})
|
|
|
|
|
|
|
|
|
2017-09-21 16:32:05 -04:00
|
|
|
#
|
|
|
|
# Image attachments
|
|
|
|
#
|
|
|
|
|
2020-11-11 16:07:38 -05:00
|
|
|
class ImageAttachmentEditView(generic.ObjectEditView):
|
2020-05-11 12:37:22 -04:00
|
|
|
queryset = ImageAttachment.objects.all()
|
2019-08-28 11:56:00 -04:00
|
|
|
model_form = forms.ImageAttachmentForm
|
2017-03-30 21:55:57 -04:00
|
|
|
|
|
|
|
def alter_obj(self, imageattachment, request, args, kwargs):
|
|
|
|
if not imageattachment.pk:
|
|
|
|
# Assign the parent object based on URL kwargs
|
|
|
|
model = kwargs.get('model')
|
2017-04-03 15:51:45 -04:00
|
|
|
imageattachment.parent = get_object_or_404(model, pk=kwargs['object_id'])
|
2017-03-30 21:55:57 -04:00
|
|
|
return imageattachment
|
|
|
|
|
2017-04-27 11:32:08 -04:00
|
|
|
def get_return_url(self, request, imageattachment):
|
2017-04-03 15:51:45 -04:00
|
|
|
return imageattachment.parent.get_absolute_url()
|
2017-03-30 21:55:57 -04:00
|
|
|
|
|
|
|
|
2020-11-11 16:07:38 -05:00
|
|
|
class ImageAttachmentDeleteView(generic.ObjectDeleteView):
|
2020-05-11 12:47:01 -04:00
|
|
|
queryset = ImageAttachment.objects.all()
|
2017-03-30 21:55:57 -04:00
|
|
|
|
2017-04-27 11:32:08 -04:00
|
|
|
def get_return_url(self, request, imageattachment):
|
2017-04-28 14:05:02 -04:00
|
|
|
return imageattachment.parent.get_absolute_url()
|
2017-09-21 16:32:05 -04:00
|
|
|
|
|
|
|
|
2021-03-16 15:00:08 -04:00
|
|
|
#
|
|
|
|
# Journal entries
|
|
|
|
#
|
|
|
|
|
2021-03-16 15:57:23 -04:00
|
|
|
class JournalEntryListView(generic.ObjectListView):
|
|
|
|
queryset = JournalEntry.objects.all()
|
|
|
|
filterset = filters.JournalEntryFilterSet
|
|
|
|
filterset_form = forms.JournalEntryFilterForm
|
|
|
|
table = tables.JournalEntryTable
|
|
|
|
action_buttons = ('export',)
|
|
|
|
|
|
|
|
|
2021-03-16 15:00:08 -04:00
|
|
|
class JournalEntryEditView(generic.ObjectEditView):
|
|
|
|
queryset = JournalEntry.objects.all()
|
|
|
|
model_form = forms.JournalEntryForm
|
|
|
|
|
|
|
|
def alter_obj(self, obj, request, args, kwargs):
|
|
|
|
if not obj.pk:
|
|
|
|
obj.created_by = request.user
|
|
|
|
return obj
|
|
|
|
|
|
|
|
def get_return_url(self, request, instance):
|
2021-03-16 16:47:35 -04:00
|
|
|
if not instance.assigned_object:
|
|
|
|
return reverse('extras:journalentry_list')
|
2021-03-16 15:00:08 -04:00
|
|
|
obj = instance.assigned_object
|
|
|
|
viewname = f'{obj._meta.app_label}:{obj._meta.model_name}_journal'
|
|
|
|
return reverse(viewname, kwargs={'pk': obj.pk})
|
|
|
|
|
|
|
|
|
|
|
|
class JournalEntryDeleteView(generic.ObjectDeleteView):
|
|
|
|
queryset = JournalEntry.objects.all()
|
|
|
|
|
|
|
|
def get_return_url(self, request, instance):
|
|
|
|
obj = instance.assigned_object
|
|
|
|
viewname = f'{obj._meta.app_label}:{obj._meta.model_name}_journal'
|
|
|
|
return reverse(viewname, kwargs={'pk': obj.pk})
|
|
|
|
|
|
|
|
|
2021-03-17 10:41:06 -04:00
|
|
|
class JournalEntryBulkEditView(generic.BulkEditView):
|
|
|
|
queryset = JournalEntry.objects.prefetch_related('created_by')
|
|
|
|
filterset = filters.JournalEntryFilterSet
|
|
|
|
table = tables.JournalEntryTable
|
|
|
|
form = forms.JournalEntryBulkEditForm
|
|
|
|
|
|
|
|
|
|
|
|
class JournalEntryBulkDeleteView(generic.BulkDeleteView):
|
|
|
|
queryset = JournalEntry.objects.prefetch_related('created_by')
|
|
|
|
filterset = filters.JournalEntryFilterSet
|
|
|
|
table = tables.JournalEntryTable
|
|
|
|
|
|
|
|
|
2021-03-16 15:00:08 -04:00
|
|
|
class ObjectJournalView(View):
|
|
|
|
"""
|
|
|
|
Show all journal entries for an object.
|
|
|
|
|
|
|
|
base_template: The name of the template to extend. If not provided, "<app>/<model>.html" will be used.
|
|
|
|
"""
|
|
|
|
base_template = None
|
|
|
|
|
|
|
|
def get(self, request, model, **kwargs):
|
|
|
|
|
|
|
|
# Handle QuerySet restriction of parent object if needed
|
|
|
|
if hasattr(model.objects, 'restrict'):
|
|
|
|
obj = get_object_or_404(model.objects.restrict(request.user, 'view'), **kwargs)
|
|
|
|
else:
|
|
|
|
obj = get_object_or_404(model, **kwargs)
|
|
|
|
|
|
|
|
# Gather all changes for this object (and its related objects)
|
|
|
|
content_type = ContentType.objects.get_for_model(model)
|
|
|
|
journalentries = JournalEntry.objects.restrict(request.user, 'view').prefetch_related('created_by').filter(
|
|
|
|
assigned_object_type=content_type,
|
|
|
|
assigned_object_id=obj.pk
|
|
|
|
)
|
2021-03-29 09:44:04 -04:00
|
|
|
journalentry_table = tables.ObjectJournalTable(journalentries)
|
2021-03-26 13:02:55 -04:00
|
|
|
paginate_table(journalentry_table, request)
|
2021-03-16 15:00:08 -04:00
|
|
|
|
|
|
|
if request.user.has_perm('extras.add_journalentry'):
|
|
|
|
form = forms.JournalEntryForm(
|
|
|
|
initial={
|
|
|
|
'assigned_object_type': ContentType.objects.get_for_model(obj),
|
|
|
|
'assigned_object_id': obj.pk
|
|
|
|
}
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
form = None
|
|
|
|
|
|
|
|
# Default to using "<app>/<model>.html" as the template, if it exists. Otherwise,
|
|
|
|
# fall back to using base.html.
|
|
|
|
if self.base_template is None:
|
|
|
|
self.base_template = f"{model._meta.app_label}/{model._meta.model_name}.html"
|
|
|
|
|
|
|
|
return render(request, 'extras/object_journal.html', {
|
|
|
|
'object': obj,
|
|
|
|
'form': form,
|
|
|
|
'table': journalentry_table,
|
|
|
|
'base_template': self.base_template,
|
|
|
|
'active_tab': 'journal',
|
|
|
|
})
|
|
|
|
|
|
|
|
|
2017-09-21 16:32:05 -04:00
|
|
|
#
|
|
|
|
# Reports
|
|
|
|
#
|
|
|
|
|
2020-06-29 03:50:05 -04:00
|
|
|
class ReportListView(ContentTypePermissionRequiredMixin, View):
|
2017-09-21 16:32:05 -04:00
|
|
|
"""
|
2020-06-29 03:50:05 -04:00
|
|
|
Retrieve all of the available reports from disk and the recorded JobResult (if any) for each.
|
2017-09-21 16:32:05 -04:00
|
|
|
"""
|
2020-06-02 15:40:39 -04:00
|
|
|
def get_required_permission(self):
|
2020-09-23 13:40:15 -04:00
|
|
|
return 'extras.view_report'
|
2017-09-21 16:32:05 -04:00
|
|
|
|
|
|
|
def get(self, request):
|
|
|
|
|
|
|
|
reports = get_reports()
|
2020-06-29 03:50:05 -04:00
|
|
|
report_content_type = ContentType.objects.get(app_label='extras', model='report')
|
|
|
|
results = {
|
|
|
|
r.name: r
|
|
|
|
for r in JobResult.objects.filter(
|
|
|
|
obj_type=report_content_type,
|
|
|
|
status__in=JobResultStatusChoices.TERMINAL_STATE_CHOICES
|
|
|
|
).defer('data')
|
|
|
|
}
|
2017-09-22 12:11:10 -04:00
|
|
|
|
2017-09-27 17:39:22 -04:00
|
|
|
ret = []
|
2017-09-22 12:11:10 -04:00
|
|
|
for module, report_list in reports:
|
|
|
|
module_reports = []
|
2017-09-26 16:36:43 -04:00
|
|
|
for report in report_list:
|
2017-09-27 17:39:22 -04:00
|
|
|
report.result = results.get(report.full_name, None)
|
|
|
|
module_reports.append(report)
|
|
|
|
ret.append((module, module_reports))
|
2017-09-21 16:32:05 -04:00
|
|
|
|
|
|
|
return render(request, 'extras/report_list.html', {
|
2017-09-27 17:39:22 -04:00
|
|
|
'reports': ret,
|
|
|
|
})
|
|
|
|
|
|
|
|
|
2020-08-10 15:56:55 -04:00
|
|
|
class ReportView(ContentTypePermissionRequiredMixin, View):
|
2017-09-27 17:39:22 -04:00
|
|
|
"""
|
2020-06-29 03:50:05 -04:00
|
|
|
Display a single Report and its associated JobResult (if any).
|
2017-09-27 17:39:22 -04:00
|
|
|
"""
|
2020-06-02 15:40:39 -04:00
|
|
|
def get_required_permission(self):
|
2020-09-23 13:40:15 -04:00
|
|
|
return 'extras.view_report'
|
2017-09-27 17:39:22 -04:00
|
|
|
|
2020-07-03 11:55:04 -04:00
|
|
|
def get(self, request, module, name):
|
2017-09-27 17:39:22 -04:00
|
|
|
|
2020-08-10 15:56:55 -04:00
|
|
|
report = get_report(module, name)
|
|
|
|
if report is None:
|
|
|
|
raise Http404
|
2020-06-29 03:50:05 -04:00
|
|
|
|
|
|
|
report_content_type = ContentType.objects.get(app_label='extras', model='report')
|
2020-07-03 11:55:04 -04:00
|
|
|
report.result = JobResult.objects.filter(
|
2020-06-29 03:50:05 -04:00
|
|
|
obj_type=report_content_type,
|
|
|
|
name=report.full_name,
|
|
|
|
status__in=JobResultStatusChoices.TERMINAL_STATE_CHOICES
|
|
|
|
).first()
|
2017-09-27 17:39:22 -04:00
|
|
|
|
|
|
|
return render(request, 'extras/report.html', {
|
|
|
|
'report': report,
|
2017-09-28 12:51:10 -04:00
|
|
|
'run_form': ConfirmationForm(),
|
2017-09-21 16:32:05 -04:00
|
|
|
})
|
2017-09-28 12:51:10 -04:00
|
|
|
|
2020-07-03 11:55:04 -04:00
|
|
|
def post(self, request, module, name):
|
2017-09-28 12:51:10 -04:00
|
|
|
|
2020-07-03 11:55:04 -04:00
|
|
|
# Permissions check
|
|
|
|
if not request.user.has_perm('extras.run_report'):
|
|
|
|
return HttpResponseForbidden()
|
2017-09-28 12:51:10 -04:00
|
|
|
|
2020-08-10 15:56:55 -04:00
|
|
|
report = get_report(module, name)
|
|
|
|
if report is None:
|
|
|
|
raise Http404
|
2020-07-13 15:11:58 -04:00
|
|
|
|
|
|
|
# Allow execution only if RQ worker process is running
|
|
|
|
if not Worker.count(get_connection('default')):
|
|
|
|
messages.error(request, "Unable to run report: RQ worker process not running.")
|
2020-08-10 15:56:55 -04:00
|
|
|
return render(request, 'extras/report.html', {
|
|
|
|
'report': report,
|
|
|
|
})
|
2020-07-13 15:11:58 -04:00
|
|
|
|
2020-08-10 15:56:55 -04:00
|
|
|
# Run the Report. A new JobResult is created.
|
|
|
|
report_content_type = ContentType.objects.get(app_label='extras', model='report')
|
|
|
|
job_result = JobResult.enqueue_job(
|
|
|
|
run_report,
|
|
|
|
report.full_name,
|
|
|
|
report_content_type,
|
|
|
|
request.user
|
|
|
|
)
|
2020-07-03 11:55:04 -04:00
|
|
|
|
2020-08-10 15:56:55 -04:00
|
|
|
return redirect('extras:report_result', job_result_pk=job_result.pk)
|
2019-08-09 12:33:33 -04:00
|
|
|
|
2020-07-03 11:55:04 -04:00
|
|
|
|
2020-08-10 15:56:55 -04:00
|
|
|
class ReportResultView(ContentTypePermissionRequiredMixin, View):
|
|
|
|
"""
|
|
|
|
Display a JobResult pertaining to the execution of a Report.
|
|
|
|
"""
|
2020-07-03 11:55:04 -04:00
|
|
|
def get_required_permission(self):
|
2020-09-23 13:40:15 -04:00
|
|
|
return 'extras.view_report'
|
2020-07-03 11:55:04 -04:00
|
|
|
|
|
|
|
def get(self, request, job_result_pk):
|
|
|
|
report_content_type = ContentType.objects.get(app_label='extras', model='report')
|
2020-08-10 15:56:55 -04:00
|
|
|
jobresult = get_object_or_404(JobResult.objects.all(), pk=job_result_pk, obj_type=report_content_type)
|
2020-07-03 11:55:04 -04:00
|
|
|
|
2020-08-10 15:56:55 -04:00
|
|
|
# Retrieve the Report and attach the JobResult to it
|
|
|
|
module, report_name = jobresult.name.split('.')
|
|
|
|
report = get_report(module, report_name)
|
|
|
|
report.result = jobresult
|
2020-07-03 11:55:04 -04:00
|
|
|
|
|
|
|
return render(request, 'extras/report_result.html', {
|
|
|
|
'report': report,
|
2020-08-10 15:56:55 -04:00
|
|
|
'result': jobresult,
|
2020-07-03 11:55:04 -04:00
|
|
|
})
|
2019-08-09 12:33:33 -04:00
|
|
|
|
2020-07-06 10:44:36 -04:00
|
|
|
|
2019-08-09 12:33:33 -04:00
|
|
|
#
|
|
|
|
# Scripts
|
|
|
|
#
|
|
|
|
|
2020-06-29 03:50:05 -04:00
|
|
|
class GetScriptMixin:
|
2020-07-03 11:55:04 -04:00
|
|
|
def _get_script(self, name, module=None):
|
|
|
|
if module is None:
|
|
|
|
module, name = name.split('.', 1)
|
2020-06-29 03:50:05 -04:00
|
|
|
scripts = get_scripts()
|
|
|
|
try:
|
|
|
|
return scripts[module][name]()
|
|
|
|
except KeyError:
|
|
|
|
raise Http404
|
|
|
|
|
|
|
|
|
|
|
|
class ScriptListView(ContentTypePermissionRequiredMixin, View):
|
2020-06-02 15:40:39 -04:00
|
|
|
|
|
|
|
def get_required_permission(self):
|
|
|
|
return 'extras.view_script'
|
2019-08-09 12:33:33 -04:00
|
|
|
|
|
|
|
def get(self, request):
|
|
|
|
|
2020-07-06 01:58:28 -04:00
|
|
|
scripts = get_scripts(use_names=True)
|
|
|
|
script_content_type = ContentType.objects.get(app_label='extras', model='script')
|
|
|
|
results = {
|
|
|
|
r.name: r
|
|
|
|
for r in JobResult.objects.filter(
|
|
|
|
obj_type=script_content_type,
|
|
|
|
status__in=JobResultStatusChoices.TERMINAL_STATE_CHOICES
|
|
|
|
).defer('data')
|
|
|
|
}
|
|
|
|
|
|
|
|
for _scripts in scripts.values():
|
|
|
|
for script in _scripts.values():
|
|
|
|
script.result = results.get(script.full_name)
|
|
|
|
|
2019-08-09 12:33:33 -04:00
|
|
|
return render(request, 'extras/script_list.html', {
|
2020-07-06 01:58:28 -04:00
|
|
|
'scripts': scripts,
|
2019-08-09 12:33:33 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
|
2020-06-29 03:50:05 -04:00
|
|
|
class ScriptView(ContentTypePermissionRequiredMixin, GetScriptMixin, View):
|
2020-06-02 15:40:39 -04:00
|
|
|
|
|
|
|
def get_required_permission(self):
|
|
|
|
return 'extras.view_script'
|
2019-08-09 12:33:33 -04:00
|
|
|
|
|
|
|
def get(self, request, module, name):
|
2020-07-03 11:55:04 -04:00
|
|
|
script = self._get_script(name, module)
|
2020-01-09 09:41:10 -05:00
|
|
|
form = script.as_form(initial=request.GET)
|
2019-08-09 12:33:33 -04:00
|
|
|
|
2020-06-29 03:50:05 -04:00
|
|
|
# Look for a pending JobResult (use the latest one by creation timestamp)
|
|
|
|
script_content_type = ContentType.objects.get(app_label='extras', model='script')
|
|
|
|
script.result = JobResult.objects.filter(
|
|
|
|
obj_type=script_content_type,
|
|
|
|
name=script.full_name,
|
2020-07-03 11:55:04 -04:00
|
|
|
).exclude(
|
|
|
|
status__in=JobResultStatusChoices.TERMINAL_STATE_CHOICES
|
2020-06-29 03:50:05 -04:00
|
|
|
).first()
|
|
|
|
|
2019-08-09 12:33:33 -04:00
|
|
|
return render(request, 'extras/script.html', {
|
|
|
|
'module': module,
|
|
|
|
'script': script,
|
|
|
|
'form': form,
|
|
|
|
})
|
|
|
|
|
|
|
|
def post(self, request, module, name):
|
|
|
|
|
2019-08-12 11:39:36 -04:00
|
|
|
# Permissions check
|
|
|
|
if not request.user.has_perm('extras.run_script'):
|
|
|
|
return HttpResponseForbidden()
|
|
|
|
|
2020-07-03 11:55:04 -04:00
|
|
|
script = self._get_script(name, module)
|
2019-08-16 15:27:58 -04:00
|
|
|
form = script.as_form(request.POST, request.FILES)
|
2019-08-09 12:33:33 -04:00
|
|
|
|
2020-07-13 15:11:58 -04:00
|
|
|
# Allow execution only if RQ worker process is running
|
|
|
|
if not Worker.count(get_connection('default')):
|
|
|
|
messages.error(request, "Unable to run script: RQ worker process not running.")
|
|
|
|
|
|
|
|
elif form.is_valid():
|
2019-08-12 14:28:06 -04:00
|
|
|
commit = form.cleaned_data.pop('_commit')
|
2020-06-29 03:50:05 -04:00
|
|
|
|
|
|
|
script_content_type = ContentType.objects.get(app_label='extras', model='script')
|
|
|
|
job_result = JobResult.enqueue_job(
|
|
|
|
run_script,
|
|
|
|
script.full_name,
|
|
|
|
script_content_type,
|
|
|
|
request.user,
|
|
|
|
data=form.cleaned_data,
|
|
|
|
request=copy_safe_request(request),
|
|
|
|
commit=commit
|
|
|
|
)
|
2019-08-09 12:33:33 -04:00
|
|
|
|
2020-07-03 11:55:04 -04:00
|
|
|
return redirect('extras:script_result', job_result_pk=job_result.pk)
|
|
|
|
|
|
|
|
return render(request, 'extras/script.html', {
|
|
|
|
'module': module,
|
|
|
|
'script': script,
|
|
|
|
'form': form,
|
|
|
|
})
|
2020-06-29 03:50:05 -04:00
|
|
|
|
|
|
|
|
|
|
|
class ScriptResultView(ContentTypePermissionRequiredMixin, GetScriptMixin, View):
|
|
|
|
|
|
|
|
def get_required_permission(self):
|
|
|
|
return 'extras.view_script'
|
|
|
|
|
2020-07-03 11:55:04 -04:00
|
|
|
def get(self, request, job_result_pk):
|
2020-06-29 03:50:05 -04:00
|
|
|
result = get_object_or_404(JobResult.objects.all(), pk=job_result_pk)
|
2020-07-03 11:55:04 -04:00
|
|
|
script_content_type = ContentType.objects.get(app_label='extras', model='script')
|
2020-06-29 03:50:05 -04:00
|
|
|
if result.obj_type != script_content_type:
|
|
|
|
raise Http404
|
|
|
|
|
2020-07-03 11:55:04 -04:00
|
|
|
script = self._get_script(result.name)
|
|
|
|
|
2020-06-29 03:50:05 -04:00
|
|
|
return render(request, 'extras/script_result.html', {
|
2019-08-09 12:33:33 -04:00
|
|
|
'script': script,
|
2020-06-29 03:50:05 -04:00
|
|
|
'result': result,
|
2020-07-03 11:55:04 -04:00
|
|
|
'class_name': script.__class__.__name__
|
2019-08-09 12:33:33 -04:00
|
|
|
})
|