2017-12-04 08:41:00 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
|
|
|
|
2018-07-19 10:36:52 +00:00
|
|
|
from django.views.generic import ListView, View
|
2017-12-04 08:41:00 +00:00
|
|
|
from django.conf import settings
|
|
|
|
from django.utils.translation import ugettext as _
|
2018-07-19 10:36:52 +00:00
|
|
|
from django.http import HttpResponse
|
|
|
|
from django.template import loader
|
|
|
|
import time
|
2017-12-04 08:41:00 +00:00
|
|
|
|
2018-07-20 10:42:01 +00:00
|
|
|
from common.mixins import DatetimeSearchMixin
|
|
|
|
from common.permissions import AdminUserRequiredMixin
|
2017-12-04 08:41:00 +00:00
|
|
|
from ..models import Command
|
|
|
|
from .. import utils
|
2018-05-22 10:22:06 +00:00
|
|
|
from ..backends import get_multi_command_storage
|
2017-12-04 08:41:00 +00:00
|
|
|
|
2018-07-19 10:36:52 +00:00
|
|
|
__all__ = ['CommandListView', 'CommandExportView']
|
2018-05-22 10:22:06 +00:00
|
|
|
common_storage = get_multi_command_storage()
|
2017-12-04 08:41:00 +00:00
|
|
|
|
|
|
|
|
2018-03-04 05:01:33 +00:00
|
|
|
class CommandListView(DatetimeSearchMixin, AdminUserRequiredMixin, ListView):
|
2017-12-04 08:41:00 +00:00
|
|
|
model = Command
|
|
|
|
template_name = "terminal/command_list.html"
|
|
|
|
context_object_name = 'command_list'
|
2018-01-12 07:43:26 +00:00
|
|
|
paginate_by = settings.DISPLAY_PER_PAGE
|
2017-12-26 17:29:23 +00:00
|
|
|
command = user = asset = system_user = ""
|
|
|
|
date_from = date_to = None
|
2017-12-04 08:41:00 +00:00
|
|
|
|
|
|
|
def get_queryset(self):
|
2017-12-29 18:29:29 +00:00
|
|
|
self.command = self.request.GET.get('command', '')
|
|
|
|
self.user = self.request.GET.get("user", '')
|
|
|
|
self.asset = self.request.GET.get('asset', '')
|
|
|
|
self.system_user = self.request.GET.get('system_user', '')
|
2017-12-26 17:29:23 +00:00
|
|
|
filter_kwargs = dict()
|
|
|
|
filter_kwargs['date_from'] = self.date_from
|
|
|
|
filter_kwargs['date_to'] = self.date_to
|
2017-12-04 08:41:00 +00:00
|
|
|
if self.user:
|
|
|
|
filter_kwargs['user'] = self.user
|
|
|
|
if self.asset:
|
|
|
|
filter_kwargs['asset'] = self.asset
|
|
|
|
if self.system_user:
|
|
|
|
filter_kwargs['system_user'] = self.system_user
|
|
|
|
if self.command:
|
2018-04-25 09:48:44 +00:00
|
|
|
filter_kwargs['input'] = self.command
|
2018-01-21 09:27:27 +00:00
|
|
|
queryset = common_storage.filter(**filter_kwargs)
|
2017-12-29 18:29:29 +00:00
|
|
|
return queryset
|
2017-12-04 08:41:00 +00:00
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = {
|
|
|
|
'app': _('Terminal'),
|
|
|
|
'action': _('Command list'),
|
2017-12-29 18:29:29 +00:00
|
|
|
'user_list': utils.get_session_user_list(),
|
|
|
|
'asset_list': utils.get_session_asset_list(),
|
|
|
|
'system_user_list': utils.get_session_system_user_list(),
|
2017-12-04 08:41:00 +00:00
|
|
|
'command': self.command,
|
2017-12-26 17:29:23 +00:00
|
|
|
'date_from': self.date_from,
|
|
|
|
'date_to': self.date_to,
|
2017-12-29 18:29:29 +00:00
|
|
|
'user': self.user,
|
2017-12-04 08:41:00 +00:00
|
|
|
'asset': self.asset,
|
|
|
|
'system_user': self.system_user,
|
|
|
|
}
|
|
|
|
kwargs.update(context)
|
|
|
|
return super().get_context_data(**kwargs)
|
|
|
|
|
|
|
|
|
2018-07-19 10:36:52 +00:00
|
|
|
class CommandExportView(DatetimeSearchMixin, AdminUserRequiredMixin, View):
|
|
|
|
model = Command
|
|
|
|
command = user = asset = system_user = action = ''
|
|
|
|
date_from = date_to = None
|
2017-12-04 08:41:00 +00:00
|
|
|
|
2018-07-19 10:36:52 +00:00
|
|
|
def get(self, request, *args, **kwargs):
|
|
|
|
queryset = self.get_queryset()
|
|
|
|
template = 'terminal/command_report.html'
|
|
|
|
context = {
|
|
|
|
'queryset': queryset,
|
|
|
|
'total_count': len(queryset),
|
|
|
|
'now': time.time(),
|
|
|
|
}
|
|
|
|
content = loader.render_to_string(template, context, request)
|
|
|
|
content_type = 'application/octet-stream'
|
|
|
|
response = HttpResponse(content, content_type)
|
|
|
|
filename = 'command-report-{}.html'.format(int(time.time()))
|
|
|
|
response['Content-Disposition'] = 'attachment; filename="%s"' % filename
|
|
|
|
return response
|
2017-12-04 08:41:00 +00:00
|
|
|
|
2018-07-19 10:36:52 +00:00
|
|
|
def get_queryset(self):
|
|
|
|
self.get_date_range()
|
|
|
|
self.action = self.request.GET.get('action', '')
|
|
|
|
self.command = self.request.GET.get('command', '')
|
|
|
|
self.user = self.request.GET.get("user", '')
|
|
|
|
self.asset = self.request.GET.get('asset', '')
|
|
|
|
self.system_user = self.request.GET.get('system_user', '')
|
|
|
|
filter_kwargs = dict()
|
|
|
|
filter_kwargs['date_from'] = self.date_from
|
|
|
|
filter_kwargs['date_to'] = self.date_to
|
|
|
|
if self.user:
|
|
|
|
filter_kwargs['user'] = self.user
|
|
|
|
if self.asset:
|
|
|
|
filter_kwargs['asset'] = self.asset
|
|
|
|
if self.system_user:
|
|
|
|
filter_kwargs['system_user'] = self.system_user
|
|
|
|
if self.command:
|
|
|
|
filter_kwargs['input'] = self.command
|
|
|
|
queryset = common_storage.filter(**filter_kwargs)
|
|
|
|
return queryset
|