jumpserver/apps/assets/views.py

724 lines
25 KiB
Python
Raw Normal View History

2016-09-04 10:06:14 +00:00
# coding:utf-8
2016-09-04 08:02:51 +00:00
from __future__ import absolute_import, unicode_literals
2016-08-14 14:10:10 +00:00
2016-09-04 11:05:47 +00:00
from django.utils.translation import ugettext as _
2016-09-06 13:39:21 +00:00
from django.conf import settings
from django.db.models import Q
2016-09-04 08:02:51 +00:00
from django.views.generic import TemplateView, ListView
from django.views.generic.edit import CreateView, DeleteView, FormView, UpdateView
2016-08-14 14:10:10 +00:00
from django.urls import reverse_lazy
2016-09-08 10:12:53 +00:00
from django.contrib.messages.views import SuccessMessageMixin
2016-09-07 12:05:42 +00:00
from django.views.generic.detail import DetailView, SingleObjectMixin
2016-09-19 09:25:41 +00:00
from django.shortcuts import get_object_or_404, reverse, redirect
2016-09-18 16:07:52 +00:00
from common.utils import int_seq
2016-10-12 10:57:51 +00:00
from .utils import CreateAssetTagsMiXin,UpdateAssetTagsMiXin
2016-09-17 15:43:41 +00:00
from .models import Asset, AssetGroup, IDC, AssetExtend, AdminUser, SystemUser, Tag
2016-10-08 10:33:00 +00:00
from .forms import *
2016-09-07 16:40:59 +00:00
from .hands import AdminUserRequiredMixin
2016-08-14 14:10:10 +00:00
2016-09-19 16:39:33 +00:00
class AssetListView(AdminUserRequiredMixin, ListView):
2016-09-17 15:43:41 +00:00
paginate_by = settings.CONFIG.DISPLAY_PER_PAGE
model = Asset
context_object_name = 'asset_list'
template_name = 'assets/asset_list.html'
2016-09-18 16:07:52 +00:00
def get_queryset(self):
queryset = super(AssetListView, self).get_queryset()
2016-09-19 16:39:33 +00:00
queryset = sorted(queryset, key=self.sorted_by_valid_and_ip)
2016-09-18 16:07:52 +00:00
return queryset
2016-09-19 16:39:33 +00:00
@staticmethod
def sorted_by_valid_and_ip(asset):
ip_list = int_seq(asset.ip.split('.'))
ip_list.insert(0, asset.is_valid()[0])
return ip_list
2016-09-17 15:43:41 +00:00
def get_context_data(self, **kwargs):
context = {
'app': 'Assets',
2016-10-12 10:57:51 +00:00
'action': 'asset list',
2016-10-07 15:04:37 +00:00
'tag_list': [(i.id,i.name,i.asset_set.all().count())for i in Tag.objects.all().order_by('name')]
2016-09-17 15:43:41 +00:00
}
kwargs.update(context)
return super(AssetListView, self).get_context_data(**kwargs)
2016-10-12 10:57:51 +00:00
class AssetCreateView(AdminUserRequiredMixin,CreateAssetTagsMiXin,CreateView):
2016-08-14 14:10:10 +00:00
model = Asset
2016-10-07 15:04:37 +00:00
tag_type = 'asset'
2016-09-17 15:43:41 +00:00
form_class = AssetCreateForm
template_name = 'assets/asset_create.html'
2016-08-14 14:10:10 +00:00
success_url = reverse_lazy('assets:asset-list')
2016-10-12 10:57:51 +00:00
def form_valid(self, form):
asset = form.save()
asset.created_by = self.request.user.username or 'Admin'
asset.save()
return super(AssetCreateView, self).form_valid(form)
2016-10-08 10:33:00 +00:00
2016-10-12 10:57:51 +00:00
def form_invalid(self, form):
2016-09-19 16:39:33 +00:00
print(form.errors)
return super(AssetCreateView, self).form_invalid(form)
2016-09-17 06:50:14 +00:00
def get_context_data(self, **kwargs):
2016-09-17 15:43:41 +00:00
context = {
'app': 'Assets',
'action': 'Create asset',
}
kwargs.update(context)
2016-10-07 15:04:37 +00:00
2016-09-17 15:43:41 +00:00
return super(AssetCreateView, self).get_context_data(**kwargs)
2016-08-14 14:10:10 +00:00
2016-10-27 15:04:02 +00:00
class AssetModalCreateView(AdminUserRequiredMixin, CreateAssetTagsMiXin, ListView):
2016-10-21 13:14:49 +00:00
model = Asset
# tag_type = 'asset'
form_class = AssetCreateForm
template_name = 'assets/asset_modal_update.html'
success_url = reverse_lazy('assets:asset-list')
2016-10-27 15:04:02 +00:00
2016-10-24 18:16:20 +00:00
def get_queryset(self):
self.queryset = super(AssetModalCreateView,self).get_queryset()
self.s = self.request.GET.get('plain_id_lists')
if "," in str(self.s):
2016-10-27 15:04:02 +00:00
self.plain_id_lists = [int(x) for x in self.s.split(',')]
2016-10-24 18:16:20 +00:00
else:
self.plain_id_lists = [self.s]
return self.queryset
2016-10-27 15:04:02 +00:00
2016-10-24 18:16:20 +00:00
def get_context_data(self, **kwargs):
asset_on_list = Asset.objects.filter(id__in = self.plain_id_lists)
context = {
'app': 'Assets',
'action': 'Bulk Update asset',
'assets_on_list': asset_on_list,
'assets_count': len(asset_on_list),
'plain_id_lists':self.s,
}
kwargs.update(context)
return super(AssetModalCreateView, self).get_context_data(**kwargs)
2016-10-12 10:57:51 +00:00
2016-10-27 15:04:02 +00:00
class AssetUpdateView(AdminUserRequiredMixin, UpdateAssetTagsMiXin, UpdateView):
2016-08-14 14:10:10 +00:00
model = Asset
2016-09-19 16:39:33 +00:00
form_class = AssetCreateForm
template_name = 'assets/asset_update.html'
2016-08-14 14:10:10 +00:00
success_url = reverse_lazy('assets:asset-list')
2016-09-19 16:39:33 +00:00
def get_context_data(self, **kwargs):
context = {
'app': 'Assets',
'action': 'Update asset',
}
kwargs.update(context)
return super(AssetUpdateView, self).get_context_data(**kwargs)
2016-08-14 14:10:10 +00:00
2016-10-08 10:33:00 +00:00
def form_invalid(self, form):
print(form.errors)
return super(AssetUpdateView, self).form_invalid(form)
2016-08-14 14:10:10 +00:00
class AssetDeleteView(DeleteView):
model = Asset
2016-09-18 16:07:52 +00:00
template_name = 'assets/delete_confirm.html'
2016-08-14 14:10:10 +00:00
success_url = reverse_lazy('assets:asset-list')
class AssetDetailView(DetailView):
model = Asset
context_object_name = 'asset'
template_name = 'assets/asset_detail.html'
2016-08-08 16:43:11 +00:00
2016-09-19 16:39:33 +00:00
def get_context_data(self, **kwargs):
asset_groups = self.object.groups.all()
context = {
'app': 'Assets',
'action': 'Asset detail',
'asset_groups_remain': [asset_group for asset_group in AssetGroup.objects.all()
if asset_group not in asset_groups],
'asset_groups': asset_groups,
}
kwargs.update(context)
return super(AssetDetailView, self).get_context_data(**kwargs)
2016-10-24 11:32:53 +00:00
2016-09-22 10:31:04 +00:00
class AssetModalListView(AdminUserRequiredMixin, ListView):
paginate_by = settings.CONFIG.DISPLAY_PER_PAGE
model = Asset
context_object_name = 'asset_modal_list'
template_name = 'assets/asset_modal_list.html'
2016-09-04 08:02:51 +00:00
2016-09-23 12:22:45 +00:00
def get_context_data(self, **kwargs):
group_id = self.request.GET.get('group_id')
2016-10-08 10:33:00 +00:00
tag_id = self.request.GET.get('tag_id')
2016-10-24 18:16:20 +00:00
plain_id_lists = self.request.GET.get('plain_id_lists')
self.s = self.request.GET.get('plain_id_lists')
if "," in str(self.s):
self.plain_id_lists = [int(x) for x in self.s.split(',')]
else:
self.plain_id_lists = [self.s]
2016-10-27 15:04:02 +00:00
2016-10-24 18:16:20 +00:00
if plain_id_lists:
context = {
'all_assets':plain_id_lists
}
2016-09-23 12:22:45 +00:00
if group_id:
group = AssetGroup.objects.get(id=group_id)
context = {
2016-10-08 10:33:00 +00:00
'all_assets':[x.id for x in group.assets.all()]
}
kwargs.update(context)
if tag_id:
tag = Tag.objects.get(id=tag_id)
context = {
'all_assets':[x.id for x in tag.asset_set.all()]
2016-09-23 12:22:45 +00:00
}
kwargs.update(context)
return super(AssetModalListView, self).get_context_data(**kwargs)
2016-10-24 11:32:53 +00:00
2016-09-07 16:40:59 +00:00
class AssetGroupCreateView(AdminUserRequiredMixin, CreateView):
2016-09-04 10:07:15 +00:00
model = AssetGroup
2016-09-04 11:05:47 +00:00
form_class = AssetGroupForm
template_name = 'assets/asset_group_create.html'
success_url = reverse_lazy('assets:asset-group-list')
2016-10-07 15:04:37 +00:00
#ordering = '-id'
2016-09-04 08:02:51 +00:00
2016-09-07 12:05:42 +00:00
# Todo: Asset group create template select assets so hard, need be resolve next
2016-09-04 11:05:47 +00:00
def get_context_data(self, **kwargs):
context = {
'app': _('Assets'),
'action': _('Create asset group'),
2016-09-23 12:22:45 +00:00
'assets_count': 0,
2016-09-04 11:05:47 +00:00
}
kwargs.update(context)
return super(AssetGroupCreateView, self).get_context_data(**kwargs)
def form_valid(self, form):
2016-09-19 09:25:41 +00:00
asset_group = form.save()
assets_id_list = self.request.POST.getlist('assets', [])
2016-09-22 10:31:04 +00:00
assets = [get_object_or_404(Asset, id=int(asset_id)) for asset_id in assets_id_list]
2016-09-19 09:25:41 +00:00
asset_group.created_by = self.request.user.username or 'Admin'
asset_group.assets.add(*tuple(assets))
asset_group.save()
return super(AssetGroupCreateView, self).form_valid(form)
2016-09-04 11:05:47 +00:00
2016-10-24 11:32:53 +00:00
2016-09-07 16:40:59 +00:00
class AssetGroupListView(AdminUserRequiredMixin, ListView):
2016-09-04 11:05:47 +00:00
model = AssetGroup
2016-09-06 13:39:21 +00:00
paginate_by = settings.CONFIG.DISPLAY_PER_PAGE
context_object_name = 'asset_group_list'
template_name = 'assets/asset_group_list.html'
2016-10-08 10:33:00 +00:00
ordering = '-id'
2016-09-04 11:05:47 +00:00
def get_context_data(self, **kwargs):
context = {
'app': _('Assets'),
2016-09-06 13:39:21 +00:00
'action': _('Asset group list'),
'keyword': self.request.GET.get('keyword', '')
2016-09-04 11:05:47 +00:00
}
kwargs.update(context)
return super(AssetGroupListView, self).get_context_data(**kwargs)
2016-09-04 08:02:51 +00:00
2016-09-06 13:39:21 +00:00
def get_queryset(self):
self.queryset = super(AssetGroupListView, self).get_queryset()
self.keyword = keyword = self.request.GET.get('keyword', '')
self.sort = sort = self.request.GET.get('sort', '-date_created')
if keyword:
self.queryset = self.queryset.filter(Q(name__icontains=keyword) |
Q(comment__icontains=keyword))
if sort:
self.queryset = self.queryset.order_by(sort)
return self.queryset
2016-09-04 08:02:51 +00:00
2016-09-07 16:40:59 +00:00
class AssetGroupDetailView(SingleObjectMixin, AdminUserRequiredMixin, ListView):
2016-09-06 13:39:21 +00:00
template_name = 'assets/asset_group_detail.html'
2016-09-07 12:05:42 +00:00
paginate_by = settings.CONFIG.DISPLAY_PER_PAGE
def get(self, request, *args, **kwargs):
self.object = self.get_object(queryset=AssetGroup.objects.all())
return super(AssetGroupDetailView, self).get(request, *args, **kwargs)
2016-09-04 08:02:51 +00:00
2016-09-07 12:05:42 +00:00
def get_queryset(self):
return self.object.assets.all()
2016-09-04 08:02:51 +00:00
2016-09-06 13:39:21 +00:00
def get_context_data(self, **kwargs):
context = {
'app': _('Assets'),
2016-09-07 12:05:42 +00:00
'action': _('Asset group detail'),
'asset_group': self.object,
2016-09-06 13:39:21 +00:00
}
kwargs.update(context)
return super(AssetGroupDetailView, self).get_context_data(**kwargs)
2016-09-04 08:02:51 +00:00
2016-10-24 11:32:53 +00:00
2016-09-07 16:40:59 +00:00
class AssetGroupUpdateView(AdminUserRequiredMixin, UpdateView):
2016-09-05 16:49:42 +00:00
model = AssetGroup
form_class = AssetGroupForm
template_name = 'assets/asset_group_create.html'
success_url = reverse_lazy('assets:asset-group-list')
2016-09-05 16:49:42 +00:00
2016-09-23 12:22:45 +00:00
def get(self, request, *args, **kwargs):
self.object = self.get_object(queryset=AssetGroup.objects.all())
return super(AssetGroupUpdateView, self).get(request, *args, **kwargs)
2016-09-05 16:49:42 +00:00
def get_context_data(self, **kwargs):
2016-10-14 11:28:58 +00:00
assets_all = self.object.assets.all()
2016-09-05 16:49:42 +00:00
context = {
'app': _('Assets'),
'action': _('Create asset group'),
2016-10-14 11:28:58 +00:00
'assets_on_list': assets_all,
'assets_count': len(assets_all),
2016-09-23 12:22:45 +00:00
'group_id':self.object.id,
2016-09-05 16:49:42 +00:00
}
kwargs.update(context)
return super(AssetGroupUpdateView, self).get_context_data(**kwargs)
2016-09-04 08:02:51 +00:00
2016-09-07 16:40:59 +00:00
class AssetGroupDeleteView(AdminUserRequiredMixin, DeleteView):
2016-09-06 13:39:21 +00:00
template_name = 'assets/delete_confirm.html'
model = AssetGroup
success_url = reverse_lazy('assets:asset-group-list')
2016-09-07 12:51:33 +00:00
2016-09-07 16:40:59 +00:00
class IDCListView(AdminUserRequiredMixin, ListView):
2016-09-07 12:51:33 +00:00
model = IDC
paginate_by = settings.CONFIG.DISPLAY_PER_PAGE
context_object_name = 'idc_list'
template_name = 'assets/idc_list.html'
def get_context_data(self, **kwargs):
context = {
'app': _('Assets'),
'action': _('IDC list'),
'keyword': self.request.GET.get('keyword', '')
}
kwargs.update(context)
return super(IDCListView, self).get_context_data(**kwargs)
def get_queryset(self):
self.queryset = super(IDCListView, self).get_queryset()
self.keyword = keyword = self.request.GET.get('keyword', '')
self.sort = sort = self.request.GET.get('sort', '-date_created')
if keyword:
self.queryset = self.queryset.filter(Q(name__icontains=keyword) |
Q(comment__icontains=keyword))
if sort:
self.queryset = self.queryset.order_by(sort)
return self.queryset
2016-09-07 16:40:59 +00:00
class IDCCreateView(AdminUserRequiredMixin, CreateView):
2016-09-07 12:51:33 +00:00
model = IDC
form_class = IDCForm
2016-09-15 04:20:53 +00:00
template_name = 'assets/idc_create_update.html'
2016-09-07 12:51:33 +00:00
success_url = reverse_lazy('assets:idc-list')
def get_context_data(self, **kwargs):
context = {
2016-09-19 09:25:41 +00:00
'app': _('assets'),
'action': _('Create IDC'),
2016-09-07 12:51:33 +00:00
}
kwargs.update(context)
return super(IDCCreateView, self).get_context_data(**kwargs)
2016-09-19 09:25:41 +00:00
def form_valid(self, form):
IDC = form.save(commit=False)
IDC.created_by = self.request.user.username or 'System'
IDC.save()
# IDC_add_success_next(user)
return super(IDCCreateView, self).form_valid(form)
2016-09-07 12:51:33 +00:00
2016-10-24 11:32:53 +00:00
2016-09-07 16:40:59 +00:00
class IDCUpdateView(AdminUserRequiredMixin, UpdateView):
2016-09-14 07:19:27 +00:00
model = IDC
form_class = IDCForm
2016-09-15 04:20:53 +00:00
template_name = 'assets/idc_create_update.html'
context_object_name = 'idc'
2016-09-14 07:19:27 +00:00
success_url = reverse_lazy('assets:idc-list')
2016-09-15 04:20:53 +00:00
2016-09-14 07:19:27 +00:00
def form_valid(self, form):
2016-09-15 04:20:53 +00:00
idc = form.save(commit=False)
idc.save()
2016-09-14 07:19:27 +00:00
return super(IDCUpdateView, self).form_valid(form)
2016-09-07 12:51:33 +00:00
2016-09-19 09:25:41 +00:00
def get_context_data(self, **kwargs):
context = {
'app': _('assets'),
'action': _('Update IDC'),
}
kwargs.update(context)
return super(IDCUpdateView, self).get_context_data(**kwargs)
2016-09-15 04:20:53 +00:00
2016-09-07 16:40:59 +00:00
class IDCDetailView(AdminUserRequiredMixin, DetailView):
2016-09-04 08:02:51 +00:00
pass
2016-09-07 12:51:33 +00:00
2016-09-14 07:19:27 +00:00
class IDCDeleteView(AdminUserRequiredMixin, DeleteView):
model = IDC
template_name = 'assets/delete_confirm.html'
success_url = reverse_lazy('assets:idc-list')
2016-09-07 16:40:59 +00:00
2016-09-15 04:20:53 +00:00
2016-09-07 16:40:59 +00:00
class AdminUserListView(AdminUserRequiredMixin, ListView):
model = AdminUser
paginate_by = settings.CONFIG.DISPLAY_PER_PAGE
context_object_name = 'admin_user_list'
template_name = 'assets/admin_user_list.html'
def get_context_data(self, **kwargs):
context = {
'app': _('Assets'),
'action': _('Admin user list'),
'keyword': self.request.GET.get('keyword', '')
}
kwargs.update(context)
return super(AdminUserListView, self).get_context_data(**kwargs)
def get_queryset(self):
2016-09-08 12:39:06 +00:00
# Todo: Default order by lose asset connection num
2016-09-07 16:40:59 +00:00
self.queryset = super(AdminUserListView, self).get_queryset()
self.keyword = keyword = self.request.GET.get('keyword', '')
self.sort = sort = self.request.GET.get('sort', '-date_created')
if keyword:
self.queryset = self.queryset.filter(Q(name__icontains=keyword) |
Q(comment__icontains=keyword))
if sort:
self.queryset = self.queryset.order_by(sort)
return self.queryset
2016-09-08 10:12:53 +00:00
class AdminUserCreateView(AdminUserRequiredMixin, SuccessMessageMixin, CreateView):
model = AdminUser
form_class = AdminUserForm
2016-09-08 12:39:06 +00:00
template_name = 'assets/admin_user_create_update.html'
2016-09-08 10:12:53 +00:00
success_url = reverse_lazy('assets:admin-user-list')
def get_context_data(self, **kwargs):
context = {
'app': 'assets',
'action': 'Create admin user'
}
kwargs.update(context)
return super(AdminUserCreateView, self).get_context_data(**kwargs)
def get_success_message(self, cleaned_data):
2016-09-15 04:20:53 +00:00
success_message = _('Create admin user <a href="%s">%s</a> successfully.' %
(
reverse_lazy('assets:admin-user-detail', kwargs={'pk': self.object.pk}),
self.object.name,
))
return success_message
2016-09-07 16:40:59 +00:00
class AdminUserUpdateView(AdminUserRequiredMixin, UpdateView):
2016-09-08 12:39:06 +00:00
model = AdminUser
form_class = AdminUserForm
template_name = 'assets/admin_user_create_update.html'
2016-09-07 16:40:59 +00:00
2016-09-08 12:39:06 +00:00
def get_context_data(self, **kwargs):
context = {
'app': 'assets',
'action': 'Update admin user'
}
kwargs.update(context)
return super(AdminUserUpdateView, self).get_context_data(**kwargs)
2016-09-07 16:40:59 +00:00
2016-09-08 12:39:06 +00:00
def get_success_url(self):
success_url = reverse_lazy('assets:admin-user-detail', pk=self.object.pk)
return success_url
class AdminUserDetailView(AdminUserRequiredMixin, SingleObjectMixin, ListView):
paginate_by = settings.CONFIG.DISPLAY_PER_PAGE
template_name = 'assets/admin_user_detail.html'
context_object_name = 'admin_user'
def get(self, request, *args, **kwargs):
self.object = self.get_object(queryset=AdminUser.objects.all())
return super(AdminUserDetailView, self).get(request, *args, **kwargs)
2016-09-08 14:54:05 +00:00
# Todo: queryset default order by connectivity, need ops support
2016-09-08 12:39:06 +00:00
def get_queryset(self):
return self.object.assets.all()
def get_context_data(self, **kwargs):
context = {
'app': 'assets',
'action': 'Admin user detail'
}
kwargs.update(context)
return super(AdminUserDetailView, self).get_context_data(**kwargs)
2016-09-07 16:40:59 +00:00
class AdminUserDeleteView(AdminUserRequiredMixin, DeleteView):
2016-09-08 12:39:06 +00:00
model = AdminUser
template_name = 'assets/delete_confirm.html'
2016-09-14 07:19:27 +00:00
success_url = reverse_lazy('assets:admin-user-list')
2016-09-08 16:09:49 +00:00
class SystemUserListView(AdminUserRequiredMixin, ListView):
model = SystemUser
paginate_by = settings.CONFIG.DISPLAY_PER_PAGE
context_object_name = 'system_user_list'
template_name = 'assets/system_user_list.html'
def get_context_data(self, **kwargs):
context = {
'app': _('Assets'),
2016-09-09 13:48:44 +00:00
'action': _('System user list'),
2016-09-08 16:09:49 +00:00
'keyword': self.request.GET.get('keyword', '')
}
kwargs.update(context)
return super(SystemUserListView, self).get_context_data(**kwargs)
def get_queryset(self):
# Todo: Default order by lose asset connection num
self.queryset = super(SystemUserListView, self).get_queryset()
self.keyword = keyword = self.request.GET.get('keyword', '')
self.sort = sort = self.request.GET.get('sort', '-date_created')
if keyword:
self.queryset = self.queryset.filter(Q(name__icontains=keyword) |
Q(comment__icontains=keyword))
if sort:
self.queryset = self.queryset.order_by(sort)
return self.queryset
class SystemUserCreateView(AdminUserRequiredMixin, SuccessMessageMixin, CreateView):
model = SystemUser
form_class = SystemUserForm
template_name = 'assets/system_user_create_update.html'
success_url = reverse_lazy('assets:system-user-list')
def get_context_data(self, **kwargs):
context = {
2016-09-09 15:19:39 +00:00
'app': _('Assets'),
'action': _('Create system user'),
2016-09-08 16:09:49 +00:00
}
kwargs.update(context)
return super(SystemUserCreateView, self).get_context_data(**kwargs)
def get_success_message(self, cleaned_data):
2016-09-15 04:20:53 +00:00
success_message = _('Create system user <a href="%s">%s</a> successfully.' %
(
reverse_lazy('assets:system-user-detail', kwargs={'pk': self.object.pk}),
self.object.name,
))
2016-09-16 17:04:52 +00:00
return success_message
2016-09-08 16:09:49 +00:00
2016-09-09 13:48:44 +00:00
class SystemUserUpdateView(AdminUserRequiredMixin, UpdateView):
model = SystemUser
form_class = SystemUserForm
template_name = 'assets/system_user_create_update.html'
def get_context_data(self, **kwargs):
context = {
2016-09-09 15:19:39 +00:00
'app': _('Assets'),
'action': _('Update system user')
2016-09-09 13:48:44 +00:00
}
kwargs.update(context)
return super(SystemUserUpdateView, self).get_context_data(**kwargs)
def get_success_url(self):
2016-09-16 12:53:10 +00:00
success_url = reverse_lazy('assets:system-user-detail', kwargs={'pk': self.object.pk})
2016-09-09 13:48:44 +00:00
return success_url
2016-09-08 16:09:49 +00:00
2016-09-09 15:19:39 +00:00
class SystemUserDetailView(AdminUserRequiredMixin, DetailView):
template_name = 'assets/system_user_detail.html'
context_object_name = 'system_user'
model = SystemUser
def get_context_data(self, **kwargs):
context = {
'app': _('Assets'),
'action': _('System user detail')
}
kwargs.update(context)
return super(SystemUserDetailView, self).get_context_data(**kwargs)
2016-09-08 16:09:49 +00:00
2016-09-09 15:19:39 +00:00
class SystemUserDeleteView(AdminUserRequiredMixin, DeleteView):
model = SystemUser
template_name = 'assets/delete_confirm.html'
2016-09-14 07:19:27 +00:00
success_url = reverse_lazy('assets:system-user-list')
2016-09-09 15:19:39 +00:00
class SystemUserAssetView(AdminUserRequiredMixin, SingleObjectMixin, ListView):
paginate_by = settings.CONFIG.DISPLAY_PER_PAGE
template_name = 'assets/system_user_asset.html'
context_object_name = 'system_user'
def get(self, request, *args, **kwargs):
self.object = self.get_object(queryset=SystemUser.objects.all())
return super(SystemUserAssetView, self).get(request, *args, **kwargs)
2016-09-16 12:53:10 +00:00
def get_asset_groups(self):
return self.object.asset_groups.all()
2016-09-09 15:19:39 +00:00
# Todo: queryset default order by connectivity, need ops support
def get_queryset(self):
2016-09-16 12:53:10 +00:00
return list(self.object.get_assets())
2016-09-09 15:19:39 +00:00
def get_context_data(self, **kwargs):
2016-09-16 12:53:10 +00:00
asset_groups = self.get_asset_groups()
assets = self.get_queryset()
2016-09-09 15:19:39 +00:00
context = {
'app': 'assets',
'action': 'System user asset',
2016-09-16 12:53:10 +00:00
'assets_remain': [asset for asset in Asset.objects.all() if asset not in assets],
'asset_groups': asset_groups,
'asset_groups_remain': [asset_group for asset_group in AssetGroup.objects.all()
if asset_group not in asset_groups]
2016-09-09 15:19:39 +00:00
}
kwargs.update(context)
return super(SystemUserAssetView, self).get_context_data(**kwargs)
2016-09-16 12:53:10 +00:00
# class SystemUserAssetGroupView(AdminUserRequiredMixin, SingleObjectMixin, ListView):
# paginate_by = settings.CONFIG.DISPLAY_PER_PAGE
# template_name = 'assets/system_user_asset_group.html'
# context_object_name = 'system_user'
#
# def get(self, request, *args, **kwargs):
# self.object = self.get_object(queryset=SystemUser.objects.all())
# return super(SystemUserAssetGroupView, self).get(request, *args, **kwargs)
#
2016-09-09 15:19:39 +00:00
# Todo: queryset default order by connectivity, need ops support
2016-09-16 12:53:10 +00:00
# def get_queryset(self):
# return self.object.asset_groups.all()
#
# def get_context_data(self, **kwargs):
# context = {
# 'app': 'assets',
# 'action': 'System user asset group',
# 'asset_groups': self.get_queryset(),
# }
# kwargs.update(context)
# return super(SystemUserAssetGroupView, self).get_context_data(**kwargs)
2016-09-08 16:09:49 +00:00
2016-09-22 10:31:04 +00:00
2016-10-07 15:04:37 +00:00
class TagView(ListView):
context_object_name = 'asset_list'
template_name = 'assets/asset_list.html'
2016-09-22 10:31:04 +00:00
2016-10-07 15:04:37 +00:00
def get_queryset(self):
asset_list = Asset.objects.filter(tags=self.kwargs['tag_id'])
return asset_list
def get_context_data(self, **kwargs):
2016-10-12 10:57:51 +00:00
kwargs['app'] = 'Assets'
kwargs['action']='asset list'
2016-10-07 15:04:37 +00:00
kwargs['tag_list'] = [(i.id,i.name,i.asset_set.all().count() )for i in Tag.objects.all().order_by('name')]
2016-10-12 10:57:51 +00:00
kwargs['tag_id'] = self.kwargs['tag_id']
2016-10-07 15:04:37 +00:00
return super(TagView, self).get_context_data(**kwargs)
2016-10-08 10:33:00 +00:00
class TagsListView(AdminUserRequiredMixin, ListView):
model = Tag
paginate_by = settings.CONFIG.DISPLAY_PER_PAGE
context_object_name = 'asset_tags_list'
template_name = 'assets/asset_tags_list.html'
ordering = '-id'
def get_context_data(self, **kwargs):
context = {
'app': _('Tag'),
'action': _('Asset Tags list'),
'keyword': self.request.GET.get('keyword', '')
}
kwargs.update(context)
return super(TagsListView, self).get_context_data(**kwargs)
class AssetTagCreateView(AdminUserRequiredMixin, CreateView):
model = Tag
form_class = AssetTagForm
template_name = 'assets/asset_tag_create.html'
success_url = reverse_lazy('assets:asset-tag-list')
#ordering = '-id'
# Todo: Asset group create template select assets so hard, need be resolve next
def get_context_data(self, **kwargs):
context = {
'app': _('Tag'),
'action': _('Asset Tags list'),
'assets_count': 0,
}
kwargs.update(context)
return super(AssetTagCreateView, self).get_context_data(**kwargs)
def form_valid(self, form):
asset_tag = form.save()
assets_id_list = self.request.POST.getlist('assets', [])
assets = [get_object_or_404(Asset, id=int(asset_id)) for asset_id in assets_id_list]
asset_tag.created_by = self.request.user.username or 'Admin'
asset_tag.asset_set.add(*tuple(assets))
asset_tag.save()
return super(AssetTagCreateView, self).form_valid(form)
class AssetTagDetailView(SingleObjectMixin, AdminUserRequiredMixin, ListView):
template_name = 'assets/asset_tag_detail.html'
paginate_by = settings.CONFIG.DISPLAY_PER_PAGE
def get(self, request, *args, **kwargs):
self.object = self.get_object(queryset=Tag.objects.all())
return super(AssetTagDetailView, self).get(request, *args, **kwargs)
def get_queryset(self):
return self.object.asset_set.all()
def get_context_data(self, **kwargs):
context = {
'app': _('Tag'),
'action': _('Asset Tags detail'),
'asset_tag': self.object,
}
kwargs.update(context)
return super(AssetTagDetailView, self).get_context_data(**kwargs)
class AssetTagUpdateView(AdminUserRequiredMixin, UpdateView):
model = Tag
form_class = AssetTagForm
template_name = 'assets/asset_tag_create.html'
success_url = reverse_lazy('assets:asset-tag-list')
def get(self, request, *args, **kwargs):
self.object = self.get_object(queryset=Tag.objects.all())
return super(AssetTagUpdateView, self).get(request, *args, **kwargs)
def get_context_data(self, **kwargs):
2016-10-14 11:28:58 +00:00
assets_all = self.object.asset_set.all()
2016-10-08 10:33:00 +00:00
context = {
'app': _('Tag'),
'action': _('Asset Tags detail'),
2016-10-14 11:28:58 +00:00
'assets_count': len(assets_all),
'assets_on_list': assets_all,
2016-10-08 10:33:00 +00:00
'tag_id':self.object.id,
}
kwargs.update(context)
return super(AssetTagUpdateView, self).get_context_data(**kwargs)
class AssetTagDeleteView(AdminUserRequiredMixin, DeleteView):
template_name = 'assets/delete_confirm.html'
model = Tag
success_url = reverse_lazy('assets:asset-tag-list')