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
|
2017-12-01 09:28:47 +00:00
|
|
|
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
|
2017-12-01 09:28:47 +00:00
|
|
|
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)
|
2017-12-01 09:28:47 +00:00
|
|
|
context.update({'app': _('Terminal'), 'action': _('Update terminal')})
|
2017-02-12 08:51:32 +00:00
|
|
|
return context
|
|
|
|
|
|
|
|
|
2017-03-31 03:25:25 +00:00
|
|
|
class TerminalDetailView(LoginRequiredMixin, DetailView):
|
2017-02-12 08:51:32 +00:00
|
|
|
model = Terminal
|
2017-12-01 09:28:47 +00:00
|
|
|
template_name = 'terminal/terminal_detail.html'
|
2017-02-12 08:51:32 +00:00
|
|
|
context_object_name = 'terminal'
|
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = super(TerminalDetailView, self).get_context_data(**kwargs)
|
|
|
|
context.update({
|
2017-12-01 09:28:47 +00:00
|
|
|
'app': _('Terminal'),
|
2017-02-12 08:51:32 +00:00
|
|
|
'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
|
2017-12-14 13:27:14 +00:00
|
|
|
template_name = 'delete_confirm.html'
|
2017-12-01 09:28:47 +00:00
|
|
|
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
|
2017-12-14 13:27:14 +00:00
|
|
|
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)),
|
2017-12-01 09:28:47 +00:00
|
|
|
'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())
|