jumpserver/apps/assets/views/cluster.py

112 lines
3.6 KiB
Python
Raw Normal View History

2017-12-07 08:25:50 +00:00
# coding:utf-8
from django.utils.translation import ugettext as _
from django.views.generic import TemplateView, ListView, View
from django.views.generic.edit import CreateView, DeleteView, FormView, UpdateView
from django.urls import reverse_lazy
from django.views.generic.detail import DetailView, SingleObjectMixin
from django.contrib.messages.views import SuccessMessageMixin
from common.const import create_success_msg, update_success_msg
2017-12-07 08:25:50 +00:00
from .. import forms
from ..models import Asset, AssetGroup, AdminUser, Cluster, SystemUser
from ..hands import AdminUserRequiredMixin
__all__ = [
'ClusterListView', 'ClusterCreateView', 'ClusterUpdateView',
'ClusterDetailView', 'ClusterDeleteView', 'ClusterAssetsView',
]
2017-12-07 08:25:50 +00:00
class ClusterListView(AdminUserRequiredMixin, TemplateView):
template_name = 'assets/cluster_list.html'
def get_context_data(self, **kwargs):
context = {
'app': _('Assets'),
'action': _('Cluster list'),
}
kwargs.update(context)
2017-12-19 04:41:00 +00:00
return super().get_context_data(**kwargs)
2017-12-07 08:25:50 +00:00
class ClusterCreateView(AdminUserRequiredMixin, SuccessMessageMixin, CreateView):
2017-12-07 08:25:50 +00:00
model = Cluster
form_class = forms.ClusterForm
template_name = 'assets/cluster_create_update.html'
success_url = reverse_lazy('assets:cluster-list')
success_message = create_success_msg
2017-12-07 08:25:50 +00:00
def get_context_data(self, **kwargs):
context = {
'app': _('assets'),
2018-01-09 16:38:21 +00:00
'action': _('Create cluster'),
2017-12-07 08:25:50 +00:00
}
kwargs.update(context)
2017-12-19 04:41:00 +00:00
return super().get_context_data(**kwargs)
2017-12-07 08:25:50 +00:00
def form_valid(self, form):
2018-01-14 10:02:11 +00:00
cluster = form.save()
cluster.created_by = self.request.user.username
2017-12-07 08:25:50 +00:00
cluster.save()
2017-12-19 04:41:00 +00:00
return super().form_valid(form)
2017-12-07 08:25:50 +00:00
class ClusterUpdateView(AdminUserRequiredMixin, SuccessMessageMixin, UpdateView):
2017-12-07 08:25:50 +00:00
model = Cluster
form_class = forms.ClusterForm
template_name = 'assets/cluster_create_update.html'
context_object_name = 'cluster'
success_url = reverse_lazy('assets:cluster-list')
success_message = update_success_msg
2017-12-07 08:25:50 +00:00
def get_context_data(self, **kwargs):
context = {
'app': _('assets'),
'action': _('Update Cluster'),
}
kwargs.update(context)
2017-12-19 04:41:00 +00:00
return super().get_context_data(**kwargs)
2017-12-07 08:25:50 +00:00
class ClusterDetailView(AdminUserRequiredMixin, DetailView):
model = Cluster
template_name = 'assets/cluster_detail.html'
context_object_name = 'cluster'
2017-12-19 04:41:00 +00:00
def get_context_data(self, **kwargs):
admin_user_list = AdminUser.objects.exclude()
context = {
'app': _('Assets'),
'action': _('Cluster detail'),
'admin_user_list': admin_user_list,
}
kwargs.update(context)
return super().get_context_data(**kwargs)
2017-12-07 08:25:50 +00:00
class ClusterAssetsView(AdminUserRequiredMixin, DetailView):
model = Cluster
template_name = 'assets/cluster_assets.html'
context_object_name = 'cluster'
def get_context_data(self, **kwargs):
assets_remain = Asset.objects.exclude(id__in=self.object.assets.all())
context = {
'app': _('Assets'),
'action': _('Asset detail'),
'groups': AssetGroup.objects.all(),
'system_users': SystemUser.objects.all(),
'assets_remain': assets_remain,
'assets': [asset for asset in Asset.objects.all() if asset not in assets_remain],
}
kwargs.update(context)
2017-12-19 04:41:00 +00:00
return super().get_context_data(**kwargs)
2017-12-07 08:25:50 +00:00
class ClusterDeleteView(AdminUserRequiredMixin, DeleteView):
model = Cluster
template_name = 'delete_confirm.html'
2017-12-07 08:25:50 +00:00
success_url = reverse_lazy('assets:cluster-list')