jumpserver/apps/terminal/views/terminal.py

124 lines
3.9 KiB
Python
Raw Normal View History

2016-10-16 14:12:13 +00:00
# ~*~ coding: utf-8 ~*~
#
2017-03-31 03:25:25 +00:00
from django.views.generic import ListView, UpdateView, DeleteView, \
2017-12-26 16:14:46 +00:00
DetailView, View
2017-03-31 03:25:25 +00:00
from django.contrib.auth.mixins import LoginRequiredMixin
2016-10-16 14:12:13 +00:00
from django.utils.translation import ugettext as _
2017-12-26 16:14:46 +00:00
from django.shortcuts import redirect
2017-03-31 03:25:25 +00:00
from django.urls import reverse_lazy, reverse
2016-10-16 14:12:13 +00:00
2016-12-26 16:59:52 +00:00
from common.mixins import JSONResponseMixin
2017-12-01 13:22:32 +00:00
from ..models import Terminal
from ..forms import TerminalForm
from ..hands import AdminUserRequiredMixin
__all__ = [
"TerminalListView", "TerminalUpdateView", "TerminalDetailView",
"TerminalDeleteView", "TerminalConnectView", "TerminalAcceptView",
2017-12-26 16:14:46 +00:00
"WebTerminalView",
2017-12-01 13:22:32 +00:00
]
2016-10-16 14:12:13 +00:00
2018-01-01 07:08:33 +00:00
class TerminalListView(AdminUserRequiredMixin, ListView):
2016-10-16 14:12:13 +00:00
model = Terminal
template_name = 'terminal/terminal_list.html'
2016-12-26 16:59:52 +00:00
form_class = TerminalForm
2016-10-16 14:12:13 +00:00
def get_context_data(self, **kwargs):
context = super(TerminalListView, self).get_context_data(**kwargs)
2016-12-26 16:59:52 +00:00
context.update({
'app': _('Terminal'),
'action': _('Terminal list'),
'form': self.form_class()
})
2016-10-16 14:12:13 +00:00
return context
2016-10-19 10:33:14 +00:00
2017-03-31 03:25:25 +00:00
class TerminalUpdateView(AdminUserRequiredMixin, UpdateView):
2016-10-19 10:33:14 +00:00
model = Terminal
form_class = TerminalForm
template_name = 'terminal/terminal_update.html'
success_url = reverse_lazy('terminal:terminal-list')
2016-10-19 10:33:14 +00:00
def get_context_data(self, **kwargs):
context = super(TerminalUpdateView, self).get_context_data(**kwargs)
context.update({'app': _('Terminal'), 'action': _('Update terminal')})
return context
2017-03-31 03:25:25 +00:00
class TerminalDetailView(LoginRequiredMixin, DetailView):
model = Terminal
template_name = 'terminal/terminal_detail.html'
context_object_name = 'terminal'
def get_context_data(self, **kwargs):
context = super(TerminalDetailView, self).get_context_data(**kwargs)
context.update({
'app': _('Terminal'),
'action': _('Terminal detail')
})
2016-10-19 10:33:14 +00:00
return context
2016-10-24 11:32:53 +00:00
2017-03-31 03:25:25 +00:00
class TerminalDeleteView(AdminUserRequiredMixin, DeleteView):
2016-10-24 11:32:53 +00:00
model = Terminal
template_name = 'delete_confirm.html'
success_url = reverse_lazy('terminal:terminal-list')
2016-12-26 16:59:52 +00:00
2017-12-01 13:22:32 +00:00
class TerminalAcceptView(AdminUserRequiredMixin, JSONResponseMixin, UpdateView):
2016-12-26 16:59:52 +00:00
model = Terminal
form_class = TerminalForm
template_name = 'terminal/terminal_modal_accept.html'
2016-12-26 16:59:52 +00:00
def form_valid(self, form):
terminal = form.save()
2017-12-25 04:22:49 +00:00
terminal.create_app_user()
2016-12-26 16:59:52 +00:00
terminal.is_accepted = True
2016-12-27 16:28:52 +00:00
terminal.is_active = True
2016-12-26 16:59:52 +00:00
terminal.save()
data = {
'success': True,
'msg': 'success'
}
return self.render_json_response(data)
def form_invalid(self, form):
data = {
'success': False,
2016-12-29 16:19:47 +00:00
'msg': str(form.errors),
2016-12-26 16:59:52 +00:00
}
return self.render_json_response(data)
2017-03-31 03:25:25 +00:00
class TerminalConnectView(LoginRequiredMixin, DetailView):
template_name = 'flash_message_standalone.html'
model = Terminal
def get_context_data(self, **kwargs):
if self.object.type == 'Web':
context = {
'title': _('Redirect to web terminal'),
2017-04-07 08:59:09 +00:00
'messages': _('Redirect to web terminal') + self.object.url,
2017-03-31 03:25:25 +00:00
'auto_redirect': True,
'interval': 3,
'redirect_url': self.object.url
}
else:
context = {
'title': _('Connect ssh terminal'),
'messages': _('You should use your ssh client tools '
'connect terminal: {} <br /> <br />'
'{}'.format(self.object.name, self.object.url)),
'redirect_url': reverse('terminal:terminal-list')
2017-03-31 03:25:25 +00:00
}
kwargs.update(context)
return super(TerminalConnectView, self).get_context_data(**kwargs)
2017-12-26 16:14:46 +00:00
class WebTerminalView(LoginRequiredMixin, View):
def get(self, request, *args, **kwargs):
return redirect('/luna/?' + request.GET.urlencode())