jumpserver/apps/assets/views/label.py

90 lines
2.6 KiB
Python
Raw Normal View History

2018-01-16 08:32:06 +00:00
# -*- coding: utf-8 -*-
#
2018-01-23 11:37:22 +00:00
from django.views.generic import TemplateView, CreateView, \
2018-01-16 08:32:06 +00:00
UpdateView, DeleteView, DetailView
from django.utils.translation import ugettext_lazy as _
2018-01-23 11:37:22 +00:00
from django.urls import reverse_lazy
2018-01-16 08:32:06 +00:00
from common.permissions import PermissionsMixin, IsOrgAdmin
2018-01-26 07:32:23 +00:00
from common.const import create_success_msg, update_success_msg
from ..models import Label
2018-01-23 11:37:22 +00:00
from ..forms import LabelForm
2018-01-16 08:32:06 +00:00
__all__ = (
"LabelListView", "LabelCreateView", "LabelUpdateView",
"LabelDetailView", "LabelDeleteView",
)
class LabelListView(PermissionsMixin, TemplateView):
2018-01-16 08:32:06 +00:00
template_name = 'assets/label_list.html'
permission_classes = [IsOrgAdmin]
2018-01-16 08:32:06 +00:00
def get_context_data(self, **kwargs):
context = {
'app': _('Assets'),
'action': _('Label list'),
}
kwargs.update(context)
return super().get_context_data(**kwargs)
class LabelCreateView(PermissionsMixin, CreateView):
2018-01-26 07:32:23 +00:00
model = Label
2018-01-23 11:37:22 +00:00
template_name = 'assets/label_create_update.html'
form_class = LabelForm
success_url = reverse_lazy('assets:label-list')
success_message = create_success_msg
disable_name = ['draw', 'search', 'limit', 'offset', '_']
permission_classes = [IsOrgAdmin]
2018-01-23 11:37:22 +00:00
def get_context_data(self, **kwargs):
context = {
'app': _('Assets'),
'action': _('Create label'),
'type': 'create'
2018-01-23 11:37:22 +00:00
}
kwargs.update(context)
return super().get_context_data(**kwargs)
2018-01-16 08:32:06 +00:00
def form_valid(self, form):
name = form.cleaned_data.get('name')
if name in self.disable_name:
msg = _(
'Tips: Avoid using label names reserved internally: {}'
).format(', '.join(self.disable_name))
form.add_error("name", msg)
return self.form_invalid(form)
return super().form_valid(form)
2018-01-16 08:32:06 +00:00
class LabelUpdateView(PermissionsMixin, UpdateView):
2018-01-26 07:32:23 +00:00
model = Label
template_name = 'assets/label_create_update.html'
form_class = LabelForm
success_url = reverse_lazy('assets:label-list')
success_message = update_success_msg
permission_classes = [IsOrgAdmin]
2018-01-26 07:32:23 +00:00
def get_context_data(self, **kwargs):
context = {
'app': _('Assets'),
'action': _('Update label'),
'type': 'update'
2018-01-26 07:32:23 +00:00
}
kwargs.update(context)
return super().get_context_data(**kwargs)
2018-01-16 08:32:06 +00:00
class LabelDetailView(PermissionsMixin, DetailView):
2018-01-16 08:32:06 +00:00
pass
class LabelDeleteView(PermissionsMixin, DeleteView):
2018-01-29 08:25:30 +00:00
model = Label
template_name = 'delete_confirm.html'
success_url = reverse_lazy('assets:label-list')
permission_classes = [IsOrgAdmin]