jumpserver/apps/terminal/views/session.py

142 lines
4.7 KiB
Python
Raw Normal View History

2017-12-01 13:22:32 +00:00
# -*- coding: utf-8 -*-
#
import time
from datetime import datetime
from django.views.generic import ListView, UpdateView, DeleteView, DetailView, TemplateView
from django.views.generic.edit import SingleObjectMixin
from django.utils.translation import ugettext as _
from django.utils import timezone
from django.utils.module_loading import import_string
from django.urls import reverse_lazy
from django.http import HttpResponse
from django.conf import settings
from django.db.models import Q
from users.utils import AdminUserRequiredMixin
from ..models import Session, Command, Terminal
from ..backends import get_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
]
command_store = get_command_store()
2017-12-01 13:22:32 +00:00
class SessionListView(AdminUserRequiredMixin, ListView):
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'
paginate_by = settings.CONFIG.DISPLAY_PER_PAGE
2017-12-04 08:41:00 +00:00
user = asset = system_user = date_from_s = date_to_s = ''
2017-12-01 13:22:32 +00:00
date_format = '%m/%d/%Y'
def get_queryset(self):
2017-12-04 08:41:00 +00:00
date_to_default = timezone.now()
date_from_default = timezone.now() - timezone.timedelta(7)
date_to_default_s = date_to_default.strftime(self.date_format)
date_from_default_s = date_from_default.strftime(self.date_format)
2017-12-01 13:22:32 +00:00
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')
2017-12-04 08:41:00 +00:00
self.date_from_s = self.request.GET.get('date_from', date_from_default_s)
self.date_to_s = self.request.GET.get('date_to', date_to_default_s)
2017-12-01 13:22:32 +00:00
filter_kwargs = {}
if self.date_from_s:
date_from = datetime.strptime(self.date_from_s, self.date_format)
date_from = date_from.replace(tzinfo=timezone.get_current_timezone())
filter_kwargs['date_start__gt'] = date_from
if self.date_to_s:
date_to = timezone.datetime.strptime(
self.date_to_s + ' 23:59:59', '%m/%d/%Y %H:%M:%S')
date_to = date_to.replace(tzinfo=timezone.get_current_timezone())
filter_kwargs['date_start__lt'] = 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 = {
'app': _('Audits'),
'action': _('Proxy log list'),
2017-12-04 08:41:00 +00:00
'user_list': utils.get_user_list_from_cache(),
'asset_list': utils.get_asset_list_from_cache(),
'system_user_list': utils.get_system_user_list_from_cache(),
2017-12-01 13:22:32 +00:00
'date_from': self.date_from_s,
'date_to': self.date_to_s,
'username': 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'),
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)
class SessionDetailView(SingleObjectMixin, 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