jumpserver/apps/terminal/views/session.py

121 lines
3.7 KiB
Python
Raw Normal View History

2017-12-01 13:22:32 +00:00
# -*- coding: utf-8 -*-
#
2018-01-08 07:15:21 +00:00
from django.views.generic import ListView
2017-12-01 13:22:32 +00:00
from django.views.generic.edit import SingleObjectMixin
from django.utils.translation import ugettext as _
from django.utils import timezone
from django.conf import settings
from users.utils import AdminUserRequiredMixin
from common.mixins import DatetimeSearchMixin
2017-12-01 13:22:32 +00:00
from ..models import Session, Command, Terminal
2018-01-21 09:27:27 +00:00
from ..backends import get_multi_command_store
2017-12-04 08:41:00 +00:00
from .. import utils
2017-12-01 13:22:32 +00:00
__all__ = [
'SessionOnlineListView', 'SessionOfflineListView',
'SessionDetailView',
2017-12-01 13:22:32 +00:00
]
2018-01-21 09:27:27 +00:00
command_store = get_multi_command_store()
2017-12-01 13:22:32 +00:00
class SessionListView(AdminUserRequiredMixin, DatetimeSearchMixin, ListView):
2017-12-01 13:22:32 +00:00
model = Session
2017-12-04 08:41:00 +00:00
template_name = 'terminal/session_list.html'
2017-12-01 13:22:32 +00:00
context_object_name = 'session_list'
2018-01-12 07:43:26 +00:00
paginate_by = settings.DISPLAY_PER_PAGE
user = asset = system_user = ''
date_from = date_to = None
2017-12-01 13:22:32 +00:00
def get_queryset(self):
self.queryset = super().get_queryset()
2017-12-04 08:41:00 +00:00
self.user = self.request.GET.get('user')
self.asset = self.request.GET.get('asset')
2017-12-01 13:22:32 +00:00
self.system_user = self.request.GET.get('system_user')
filter_kwargs = dict()
filter_kwargs['date_start__gt'] = self.date_from
filter_kwargs['date_start__lt'] = 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
2017-12-01 13:22:32 +00:00
if self.system_user:
filter_kwargs['system_user'] = self.system_user
if filter_kwargs:
self.queryset = self.queryset.filter(**filter_kwargs)
return self.queryset
def get_context_data(self, **kwargs):
context = {
2018-01-08 07:15:21 +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(),
'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,
2017-12-01 13:22:32 +00:00
'system_user': self.system_user,
}
kwargs.update(context)
return super().get_context_data(**kwargs)
class SessionOnlineListView(SessionListView):
def get_queryset(self):
queryset = super().get_queryset().filter(is_finished=False)
return queryset
def get_context_data(self, **kwargs):
context = {
'app': _('Terminal'),
2017-12-01 13:22:32 +00:00
'action': _('Session online list'),
2018-03-07 03:28:42 +00:00
'type': 'online',
2017-12-04 08:41:00 +00:00
'now': timezone.now(),
2017-12-01 13:22:32 +00:00
}
kwargs.update(context)
return super().get_context_data(**kwargs)
class SessionOfflineListView(SessionListView):
def get_queryset(self):
queryset = super().get_queryset()
queryset = queryset.filter(is_finished=True)
return queryset
def get_context_data(self, **kwargs):
context = {
'app': _('Terminal'),
2017-12-01 13:22:32 +00:00
'action': _('Session offline list'),
2017-12-04 08:41:00 +00:00
'now': timezone.now(),
2017-12-01 13:22:32 +00:00
}
kwargs.update(context)
return super().get_context_data(**kwargs)
2018-03-04 05:01:33 +00:00
class SessionDetailView(SingleObjectMixin, AdminUserRequiredMixin, ListView):
template_name = 'terminal/session_detail.html'
model = Session
object = None
2017-12-15 09:07:52 +00:00
def get(self, request, *args, **kwargs):
self.object = self.get_object(queryset=self.model.objects.all())
return super().get(request, *args, **kwargs)
def get_queryset(self):
return command_store.filter(session=self.object.id)
def get_context_data(self, **kwargs):
context = {
'app': _('Terminal'),
'action': _('Session detail'),
}
kwargs.update(context)
return super().get_context_data(**kwargs)
2017-12-01 13:22:32 +00:00