jumpserver/apps/applications/views.py

116 lines
3.7 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, \
DetailView, TemplateView
from django.contrib.auth.mixins import LoginRequiredMixin
2016-10-16 14:12:13 +00:00
from django.utils.translation import ugettext as _
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-03-31 03:25:25 +00:00
from .models import Terminal
2016-10-19 10:33:14 +00:00
from .forms import TerminalForm
2017-03-31 03:25:25 +00:00
from .hands import AdminUserRequiredMixin
2016-10-16 14:12:13 +00:00
2017-03-31 03:25:25 +00:00
class TerminalListView(LoginRequiredMixin, ListView):
2016-10-16 14:12:13 +00:00
model = Terminal
template_name = 'applications/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 = 'applications/terminal_update.html'
2017-03-31 03:25:25 +00:00
success_url = reverse_lazy('applications: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': _('Applications'), 'action': _('Update terminal')})
return context
2017-03-31 03:25:25 +00:00
class TerminalDetailView(LoginRequiredMixin, DetailView):
model = Terminal
template_name = 'applications/terminal_detail.html'
context_object_name = 'terminal'
def get_context_data(self, **kwargs):
context = super(TerminalDetailView, self).get_context_data(**kwargs)
context.update({
'app': _('Applications'),
'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 = 'assets/delete_confirm.html'
2016-12-26 16:59:52 +00:00
success_url = reverse_lazy('applications:applications-list')
2016-12-30 14:21:50 +00:00
class TerminalModelAccept(AdminUserRequiredMixin, JSONResponseMixin, UpdateView):
2016-12-26 16:59:52 +00:00
model = Terminal
form_class = TerminalForm
2016-12-30 14:21:50 +00:00
template_name = 'applications/terminal_modal_test.html'
2016-12-26 16:59:52 +00:00
2016-12-29 16:19:47 +00:00
def post(self, request, *args, **kwargs):
print(request.POST)
return super(TerminalModelAccept, self).post(request, *args, **kwargs)
2016-12-26 16:59:52 +00:00
def form_valid(self, form):
terminal = form.save()
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):
2016-12-29 16:19:47 +00:00
print('form.data')
2016-12-26 16:59:52 +00:00
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)),
2017-03-31 15:46:00 +00:00
'redirect_url': reverse('applications:terminal-list')
2017-03-31 03:25:25 +00:00
}
kwargs.update(context)
return super(TerminalConnectView, self).get_context_data(**kwargs)