2017-04-04 03:37:52 +00:00
|
|
|
# coding:utf-8
|
|
|
|
from __future__ import absolute_import, unicode_literals
|
2017-04-08 16:45:28 +00:00
|
|
|
|
|
|
|
import csv
|
2017-04-04 03:37:52 +00:00
|
|
|
import json
|
|
|
|
import uuid
|
2017-04-08 16:45:28 +00:00
|
|
|
import codecs
|
2017-09-24 01:16:47 +00:00
|
|
|
import chardet
|
2017-04-08 16:45:28 +00:00
|
|
|
from io import StringIO
|
2017-04-04 03:37:52 +00:00
|
|
|
|
2018-03-26 07:55:31 +00:00
|
|
|
from django.db import transaction
|
2018-08-13 07:01:56 +00:00
|
|
|
from django.contrib import messages
|
2017-04-08 16:45:28 +00:00
|
|
|
from django.utils.translation import ugettext_lazy as _
|
2017-04-04 03:37:52 +00:00
|
|
|
from django.views.generic import TemplateView, ListView, View
|
|
|
|
from django.views.generic.edit import CreateView, DeleteView, FormView, UpdateView
|
|
|
|
from django.urls import reverse_lazy
|
2017-12-15 07:50:15 +00:00
|
|
|
from django.views.generic.detail import DetailView
|
|
|
|
from django.http import HttpResponse, JsonResponse
|
|
|
|
from django.views.decorators.csrf import csrf_exempt
|
2017-04-04 03:37:52 +00:00
|
|
|
from django.utils.decorators import method_decorator
|
|
|
|
from django.core.cache import cache
|
|
|
|
from django.utils import timezone
|
2017-12-15 07:50:15 +00:00
|
|
|
from django.shortcuts import redirect
|
2018-01-09 15:07:53 +00:00
|
|
|
from django.contrib.messages.views import SuccessMessageMixin
|
2019-06-13 10:58:43 +00:00
|
|
|
from django.forms.formsets import formset_factory
|
2017-12-14 13:27:14 +00:00
|
|
|
|
2017-04-04 03:37:52 +00:00
|
|
|
from common.mixins import JSONResponseMixin
|
2018-08-10 06:46:04 +00:00
|
|
|
from common.utils import get_object_or_none, get_logger
|
2019-06-19 03:31:38 +00:00
|
|
|
from common.permissions import PermissionsMixin, IsOrgAdmin, IsValidUser
|
2019-05-21 08:24:01 +00:00
|
|
|
from common.const import (
|
|
|
|
create_success_msg, update_success_msg, KEY_CACHE_RESOURCES_ID
|
|
|
|
)
|
2017-04-04 03:37:52 +00:00
|
|
|
from .. import forms
|
2018-04-02 10:32:24 +00:00
|
|
|
from ..models import Asset, AdminUser, SystemUser, Label, Node, Domain
|
2017-04-04 03:37:52 +00:00
|
|
|
|
|
|
|
|
2017-12-08 02:15:27 +00:00
|
|
|
__all__ = [
|
2019-03-18 02:15:33 +00:00
|
|
|
'AssetListView', 'AssetCreateView', 'AssetUpdateView', 'AssetUserListView',
|
2017-12-08 02:15:27 +00:00
|
|
|
'UserAssetListView', 'AssetBulkUpdateView', 'AssetDetailView',
|
2019-07-02 06:17:56 +00:00
|
|
|
'AssetDeleteView',
|
2017-12-08 02:15:27 +00:00
|
|
|
]
|
2017-12-15 07:50:15 +00:00
|
|
|
logger = get_logger(__file__)
|
2017-04-04 03:37:52 +00:00
|
|
|
|
|
|
|
|
2019-06-19 02:47:26 +00:00
|
|
|
class AssetListView(PermissionsMixin, TemplateView):
|
2018-02-08 03:34:21 +00:00
|
|
|
template_name = 'assets/asset_list.html'
|
2019-06-19 02:47:26 +00:00
|
|
|
permission_classes = [IsOrgAdmin]
|
2017-04-04 03:37:52 +00:00
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
2018-10-16 04:03:11 +00:00
|
|
|
Node.root()
|
2017-04-04 03:37:52 +00:00
|
|
|
context = {
|
2017-12-30 12:45:54 +00:00
|
|
|
'app': _('Assets'),
|
|
|
|
'action': _('Asset list'),
|
2018-01-26 06:47:10 +00:00
|
|
|
'labels': Label.objects.all().order_by('name'),
|
2018-04-13 07:48:10 +00:00
|
|
|
'nodes': Node.objects.all().order_by('-key'),
|
2017-04-04 03:37:52 +00:00
|
|
|
}
|
|
|
|
kwargs.update(context)
|
2017-12-15 07:50:15 +00:00
|
|
|
return super().get_context_data(**kwargs)
|
2017-04-04 03:37:52 +00:00
|
|
|
|
|
|
|
|
2019-06-19 02:47:26 +00:00
|
|
|
class AssetUserListView(PermissionsMixin, DetailView):
|
2019-03-18 02:15:33 +00:00
|
|
|
model = Asset
|
|
|
|
context_object_name = 'asset'
|
|
|
|
template_name = 'assets/asset_asset_user_list.html'
|
2019-06-19 02:47:26 +00:00
|
|
|
permission_classes = [IsOrgAdmin]
|
2019-03-18 02:15:33 +00:00
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = {
|
|
|
|
'app': _('Assets'),
|
|
|
|
'action': _('Asset user list'),
|
|
|
|
}
|
|
|
|
kwargs.update(context)
|
|
|
|
return super().get_context_data(**kwargs)
|
|
|
|
|
|
|
|
|
2019-06-19 03:31:38 +00:00
|
|
|
class UserAssetListView(PermissionsMixin, TemplateView):
|
2017-04-04 03:37:52 +00:00
|
|
|
template_name = 'assets/user_asset_list.html'
|
2019-06-19 03:31:38 +00:00
|
|
|
permission_classes = [IsValidUser]
|
2017-04-04 03:37:52 +00:00
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = {
|
2018-03-04 05:01:33 +00:00
|
|
|
'action': _('My assets'),
|
2019-06-11 02:09:08 +00:00
|
|
|
'labels': Label.objects.all().order_by('name'),
|
2017-04-04 03:37:52 +00:00
|
|
|
'system_users': SystemUser.objects.all(),
|
|
|
|
}
|
|
|
|
kwargs.update(context)
|
2017-12-15 07:50:15 +00:00
|
|
|
return super().get_context_data(**kwargs)
|
2017-04-04 03:37:52 +00:00
|
|
|
|
|
|
|
|
2019-06-19 02:47:26 +00:00
|
|
|
class AssetCreateView(PermissionsMixin, SuccessMessageMixin, CreateView):
|
2017-04-04 03:37:52 +00:00
|
|
|
model = Asset
|
|
|
|
form_class = forms.AssetCreateForm
|
|
|
|
template_name = 'assets/asset_create.html'
|
|
|
|
success_url = reverse_lazy('assets:asset-list')
|
2019-06-19 02:47:26 +00:00
|
|
|
permission_classes = [IsOrgAdmin]
|
2017-04-04 03:37:52 +00:00
|
|
|
|
2018-02-02 09:06:08 +00:00
|
|
|
def get_form(self, form_class=None):
|
|
|
|
form = super().get_form(form_class=form_class)
|
|
|
|
node_id = self.request.GET.get("node_id")
|
|
|
|
if node_id:
|
|
|
|
node = get_object_or_none(Node, id=node_id)
|
|
|
|
else:
|
|
|
|
node = Node.root()
|
|
|
|
form["nodes"].initial = node
|
|
|
|
return form
|
|
|
|
|
2019-06-13 10:58:43 +00:00
|
|
|
def get_protocol_formset(self):
|
|
|
|
ProtocolFormset = formset_factory(forms.ProtocolForm, extra=0, min_num=1, max_num=5)
|
|
|
|
if self.request.method == "POST":
|
|
|
|
formset = ProtocolFormset(self.request.POST)
|
|
|
|
else:
|
|
|
|
formset = ProtocolFormset()
|
|
|
|
return formset
|
|
|
|
|
|
|
|
def form_valid(self, form):
|
|
|
|
formset = self.get_protocol_formset()
|
|
|
|
valid = formset.is_valid()
|
|
|
|
if not valid:
|
|
|
|
return self.form_invalid(form)
|
|
|
|
protocols = formset.save()
|
|
|
|
instance = super().form_valid(form)
|
|
|
|
instance.protocols.set(protocols)
|
|
|
|
return instance
|
|
|
|
|
2017-04-04 03:37:52 +00:00
|
|
|
def get_context_data(self, **kwargs):
|
2019-06-13 10:58:43 +00:00
|
|
|
formset = self.get_protocol_formset()
|
2017-04-04 03:37:52 +00:00
|
|
|
context = {
|
2018-01-01 07:08:33 +00:00
|
|
|
'app': _('Assets'),
|
|
|
|
'action': _('Create asset'),
|
2019-06-13 10:58:43 +00:00
|
|
|
'formset': formset,
|
2017-04-04 03:37:52 +00:00
|
|
|
}
|
|
|
|
kwargs.update(context)
|
2017-12-12 04:19:45 +00:00
|
|
|
return super().get_context_data(**kwargs)
|
2017-04-04 03:37:52 +00:00
|
|
|
|
2018-01-09 15:07:53 +00:00
|
|
|
def get_success_message(self, cleaned_data):
|
|
|
|
return create_success_msg % ({"name": cleaned_data["hostname"]})
|
|
|
|
|
2017-04-04 03:37:52 +00:00
|
|
|
|
2019-06-19 02:47:26 +00:00
|
|
|
class AssetBulkUpdateView(PermissionsMixin, ListView):
|
2017-04-11 09:29:53 +00:00
|
|
|
model = Asset
|
|
|
|
form_class = forms.AssetBulkUpdateForm
|
|
|
|
template_name = 'assets/asset_bulk_update.html'
|
2017-04-04 03:37:52 +00:00
|
|
|
success_url = reverse_lazy('assets:asset-list')
|
2018-08-13 07:01:56 +00:00
|
|
|
success_message = _("Bulk update asset success")
|
2017-12-18 14:48:19 +00:00
|
|
|
id_list = None
|
|
|
|
form = None
|
2019-06-19 02:47:26 +00:00
|
|
|
permission_classes = [IsOrgAdmin]
|
2017-04-04 03:37:52 +00:00
|
|
|
|
2017-04-11 09:29:53 +00:00
|
|
|
def get(self, request, *args, **kwargs):
|
2019-04-15 06:33:16 +00:00
|
|
|
spm = request.GET.get('spm', '')
|
2019-05-21 08:24:01 +00:00
|
|
|
assets_id = cache.get(KEY_CACHE_RESOURCES_ID.format(spm))
|
2017-04-12 03:50:15 +00:00
|
|
|
if kwargs.get('form'):
|
|
|
|
self.form = kwargs['form']
|
|
|
|
elif assets_id:
|
2019-04-15 06:33:16 +00:00
|
|
|
self.form = self.form_class(initial={'assets': assets_id})
|
2017-04-12 03:50:15 +00:00
|
|
|
else:
|
|
|
|
self.form = self.form_class()
|
2017-12-15 07:50:15 +00:00
|
|
|
return super().get(request, *args, **kwargs)
|
2017-04-11 09:29:53 +00:00
|
|
|
|
|
|
|
def post(self, request, *args, **kwargs):
|
2017-04-12 03:50:15 +00:00
|
|
|
form = self.form_class(request.POST)
|
|
|
|
if form.is_valid():
|
|
|
|
form.save()
|
2018-08-13 07:01:56 +00:00
|
|
|
messages.success(request, self.success_message)
|
2017-04-12 03:50:15 +00:00
|
|
|
return redirect(self.success_url)
|
2017-04-04 03:37:52 +00:00
|
|
|
else:
|
2017-04-12 03:50:15 +00:00
|
|
|
return self.get(request, form=form, *args, **kwargs)
|
2017-04-04 03:37:52 +00:00
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = {
|
2018-01-01 07:08:33 +00:00
|
|
|
'app': _('Assets'),
|
|
|
|
'action': _('Bulk update asset'),
|
2017-04-11 09:29:53 +00:00
|
|
|
'form': self.form,
|
2017-12-18 14:48:19 +00:00
|
|
|
'assets_selected': self.id_list,
|
2017-04-04 03:37:52 +00:00
|
|
|
}
|
|
|
|
kwargs.update(context)
|
2017-12-15 07:50:15 +00:00
|
|
|
return super().get_context_data(**kwargs)
|
2017-04-04 03:37:52 +00:00
|
|
|
|
|
|
|
|
2019-06-19 02:47:26 +00:00
|
|
|
class AssetUpdateView(PermissionsMixin, SuccessMessageMixin, UpdateView):
|
2017-04-04 03:37:52 +00:00
|
|
|
model = Asset
|
|
|
|
form_class = forms.AssetUpdateForm
|
|
|
|
template_name = 'assets/asset_update.html'
|
|
|
|
success_url = reverse_lazy('assets:asset-list')
|
2019-06-19 02:47:26 +00:00
|
|
|
permission_classes = [IsOrgAdmin]
|
2017-04-04 03:37:52 +00:00
|
|
|
|
2019-06-13 10:58:43 +00:00
|
|
|
def get_protocol_formset(self):
|
|
|
|
ProtocolFormset = formset_factory(forms.ProtocolForm, extra=0, min_num=1, max_num=5)
|
|
|
|
if self.request.method == "POST":
|
|
|
|
formset = ProtocolFormset(self.request.POST)
|
|
|
|
else:
|
|
|
|
initial_data = [{"name": p.name, "port": p.port} for p in self.object.protocols.all()]
|
|
|
|
formset = ProtocolFormset(initial=initial_data)
|
|
|
|
return formset
|
|
|
|
|
2017-04-04 03:37:52 +00:00
|
|
|
def get_context_data(self, **kwargs):
|
2019-06-13 10:58:43 +00:00
|
|
|
formset = self.get_protocol_formset()
|
2017-04-04 03:37:52 +00:00
|
|
|
context = {
|
2018-01-01 07:08:33 +00:00
|
|
|
'app': _('Assets'),
|
|
|
|
'action': _('Update asset'),
|
2019-06-13 10:58:43 +00:00
|
|
|
'formset': formset,
|
2017-04-04 03:37:52 +00:00
|
|
|
}
|
|
|
|
kwargs.update(context)
|
2018-01-08 07:15:21 +00:00
|
|
|
return super().get_context_data(**kwargs)
|
2017-04-04 03:37:52 +00:00
|
|
|
|
2018-01-09 15:07:53 +00:00
|
|
|
def get_success_message(self, cleaned_data):
|
|
|
|
return update_success_msg % ({"name": cleaned_data["hostname"]})
|
|
|
|
|
2017-04-04 03:37:52 +00:00
|
|
|
|
2019-06-19 02:47:26 +00:00
|
|
|
class AssetDeleteView(PermissionsMixin, DeleteView):
|
2017-04-04 03:37:52 +00:00
|
|
|
model = Asset
|
2017-12-14 13:27:14 +00:00
|
|
|
template_name = 'delete_confirm.html'
|
2017-04-04 03:37:52 +00:00
|
|
|
success_url = reverse_lazy('assets:asset-list')
|
2019-06-19 02:47:26 +00:00
|
|
|
permission_classes = [IsOrgAdmin]
|
2017-04-04 03:37:52 +00:00
|
|
|
|
|
|
|
|
2019-06-19 03:31:38 +00:00
|
|
|
class AssetDetailView(PermissionsMixin, DetailView):
|
2017-04-04 03:37:52 +00:00
|
|
|
model = Asset
|
|
|
|
context_object_name = 'asset'
|
|
|
|
template_name = 'assets/asset_detail.html'
|
2019-06-19 03:31:38 +00:00
|
|
|
permission_classes = [IsValidUser]
|
2017-04-04 03:37:52 +00:00
|
|
|
|
2019-07-04 07:36:57 +00:00
|
|
|
def get_queryset(self):
|
|
|
|
return super().get_queryset().prefetch_related(
|
|
|
|
"nodes", "labels", "protocols"
|
|
|
|
).select_related('admin_user', 'domain')
|
|
|
|
|
2017-04-04 03:37:52 +00:00
|
|
|
def get_context_data(self, **kwargs):
|
2018-02-09 07:24:44 +00:00
|
|
|
nodes_remain = Node.objects.exclude(assets=self.object)
|
2017-04-04 03:37:52 +00:00
|
|
|
context = {
|
2018-01-01 07:08:33 +00:00
|
|
|
'app': _('Assets'),
|
|
|
|
'action': _('Asset detail'),
|
2018-02-09 07:24:44 +00:00
|
|
|
'nodes_remain': nodes_remain,
|
2017-04-04 03:37:52 +00:00
|
|
|
}
|
|
|
|
kwargs.update(context)
|
2018-01-08 07:15:21 +00:00
|
|
|
return super().get_context_data(**kwargs)
|