2016-10-16 14:12:13 +00:00
|
|
|
# ~*~ coding: utf-8 ~*~
|
|
|
|
#
|
2019-08-22 04:41:28 +00:00
|
|
|
import time
|
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
|
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
|
2019-06-19 02:47:26 +00:00
|
|
|
from common.permissions import PermissionsMixin, IsSuperUser
|
2017-12-01 13:22:32 +00:00
|
|
|
|
|
|
|
|
|
|
|
__all__ = [
|
|
|
|
"TerminalListView", "TerminalUpdateView", "TerminalDetailView",
|
|
|
|
"TerminalDeleteView", "TerminalConnectView", "TerminalAcceptView",
|
2018-10-10 07:37:20 +00:00
|
|
|
"WebTerminalView", 'WebSFTPView',
|
2017-12-01 13:22:32 +00:00
|
|
|
]
|
2016-10-16 14:12:13 +00:00
|
|
|
|
|
|
|
|
2019-06-19 02:47:26 +00:00
|
|
|
class TerminalListView(PermissionsMixin, 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
|
2019-06-19 02:47:26 +00:00
|
|
|
permission_classes = [IsSuperUser]
|
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({
|
2019-06-20 10:10:44 +00:00
|
|
|
'app': _('Sessions'),
|
2016-12-26 16:59:52 +00:00
|
|
|
'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
|
|
|
|
|
|
|
|
2019-06-19 02:47:26 +00:00
|
|
|
class TerminalUpdateView(PermissionsMixin, 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')
|
2019-06-19 02:47:26 +00:00
|
|
|
permission_classes = [IsSuperUser]
|
2016-10-19 10:33:14 +00:00
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = super(TerminalUpdateView, self).get_context_data(**kwargs)
|
2019-06-20 10:10:44 +00:00
|
|
|
context.update({'app': _('Sessions'), 'action': _('Update terminal')})
|
2017-02-12 08:51:32 +00:00
|
|
|
return context
|
|
|
|
|
|
|
|
|
2019-06-19 03:31:38 +00:00
|
|
|
class TerminalDetailView(PermissionsMixin, 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'
|
2019-06-19 02:47:26 +00:00
|
|
|
permission_classes = [IsSuperUser]
|
2017-02-12 08:51:32 +00:00
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = super(TerminalDetailView, self).get_context_data(**kwargs)
|
|
|
|
context.update({
|
2019-06-20 10:10:44 +00:00
|
|
|
'app': _('Sessions'),
|
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
|
|
|
|
|
|
|
|
2019-06-19 02:47:26 +00:00
|
|
|
class TerminalDeleteView(PermissionsMixin, 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')
|
2019-06-19 02:47:26 +00:00
|
|
|
permission_classes = [IsSuperUser]
|
2016-12-26 16:59:52 +00:00
|
|
|
|
|
|
|
|
2019-06-19 02:47:26 +00:00
|
|
|
class TerminalAcceptView(PermissionsMixin, 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'
|
2019-06-19 02:47:26 +00:00
|
|
|
permission_classes = [IsSuperUser]
|
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)
|
|
|
|
|
|
|
|
|
2019-06-19 03:31:38 +00:00
|
|
|
class TerminalConnectView(PermissionsMixin, DetailView):
|
2018-09-29 02:44:52 +00:00
|
|
|
"""
|
|
|
|
Abandon
|
|
|
|
"""
|
2017-03-31 03:25:25 +00:00
|
|
|
template_name = 'flash_message_standalone.html'
|
|
|
|
model = Terminal
|
2019-06-19 02:47:26 +00:00
|
|
|
permission_classes = [IsSuperUser]
|
2017-03-31 03:25:25 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
2019-06-19 03:31:38 +00:00
|
|
|
class WebTerminalView(View):
|
2017-12-26 16:14:46 +00:00
|
|
|
def get(self, request, *args, **kwargs):
|
2019-08-22 04:41:28 +00:00
|
|
|
redirect_url = '/luna/?_={}&'.format(int(time.time()))
|
|
|
|
return redirect(redirect_url + request.GET.urlencode())
|
2018-10-10 07:37:20 +00:00
|
|
|
|
|
|
|
|
2019-06-19 03:31:38 +00:00
|
|
|
class WebSFTPView(View):
|
2018-10-10 07:37:20 +00:00
|
|
|
def get(self, request, *args, **kwargs):
|
2019-09-25 06:40:19 +00:00
|
|
|
return redirect('/koko/elfinder/sftp/?' + request.GET.urlencode())
|