Merge branch 'cmdb' of code.simcu.com:jumpserver/jumpserver into cmdb

pull/530/head
ibuler 2016-09-04 18:07:19 +08:00
commit d1c96cd4b2
9 changed files with 237 additions and 174 deletions

View File

@ -22,7 +22,8 @@ class IDCSerializer(serializers.ModelSerializer):
class Meta:
model = IDC
#fields = ('id', 'title', 'code', 'linenos', 'language', 'style')
class AssetGroupViewSet(viewsets.ModelViewSet):
"""
API endpoint that allows AssetGroup to be viewed or edited.
@ -30,6 +31,7 @@ class AssetGroupViewSet(viewsets.ModelViewSet):
queryset = AssetGroup.objects.all()
serializer_class = AssetGroupSerializer
class AssetViewSet(viewsets.ModelViewSet):
"""
API endpoint that allows Asset to be viewed or edited.
@ -37,6 +39,7 @@ class AssetViewSet(viewsets.ModelViewSet):
queryset = Asset.objects.all()
serializer_class = AssetSerializer
class IDCViewSet(viewsets.ModelViewSet):
"""
API endpoint that allows IDC to be viewed or edited.

View File

@ -10,8 +10,8 @@ class AssetForm(forms.ModelForm):
model = Asset
fields = [
"ip", "other_ip", "hostname", "port", "group", "username", "password", "idc", "mac_addr",
"remote_card_ip", "brand", "cpu", "memory", "disk", "os", "cabinet_no", "cabinet_pos",
"ip", "other_ip", "remote_card_ip", "hostname", "port", "groups", "username", "password",
"idc", "mac_addr", "brand", "cpu", "memory", "disk", "os", "cabinet_no", "cabinet_pos",
"number", "status", "type", "env", "sn", "is_active", "comment"
]

View File

@ -6,9 +6,9 @@ from django.utils.translation import ugettext_lazy as _
class AssetGroup(models.Model):
name = models.CharField(max_length=64, unique=True, verbose_name=_('Name'))
created_by = models.CharField(max_length=32, blank=True, verbose_name=_('Created by'))
comment = models.CharField(max_length=128, blank=True, verbose_name=_('Comment'))
name = models.CharField(max_length=64, unique=True, null=True, blank=True, verbose_name=_('Name'))
created_by = models.CharField(max_length=32, null=True, blank=True, verbose_name=_('Created by'))
comment = models.CharField(max_length=128, null=True, blank=True, verbose_name=_('Comment'))
def __unicode__(self):
return self.name
@ -34,44 +34,97 @@ class IDC(models.Model):
class Meta:
db_table = 'idc'
verbose_name = _('IDC')
verbose_name_plural = verbose_name
class AssetExtend(models.Model):
pass
key = models.CharField(max_length=64, null=True, blank=True, verbose_name=u'key')
value = models.CharField(max_length=64, null=True, blank=True, verbose_name=u'value')
created_by = models.CharField(max_length=32, blank=True, verbose_name=u"Created by")
date_added = models.DateTimeField(auto_now=True, null=True, blank=True)
comment = models.CharField(max_length=128, blank=True, verbose_name=u"Comment")
def __unicode__(self):
return self.name
class Meta:
db_table = 'assetextend'
class AdminUser(models.Model):
name = models.CharField(max_length=128, unique=True, null=True, blank=True, verbose_name=u"用户名称")
username = models.CharField(max_length=16, null=True, blank=True, verbose_name=u"用户名")
password = models.CharField(max_length=256, null=True, blank=True, verbose_name=u"密码")
private_key = models.CharField(max_length=4096, null=True, blank=True, verbose_name=u"私钥")
is_default = models.BooleanField(default=True, verbose_name=u"是否默认")
auto_update = models.BooleanField(default=True, verbose_name=u"自动更新")
date_added = models.DateTimeField(auto_now=True, null=True, blank=True)
create_by = models.CharField(max_length=32, null=True, blank=True, verbose_name=u"创建者")
comment = models.CharField(max_length=128, blank=True, verbose_name=u"备注")
def __unicode__(self):
return self.name
class Meta:
db_table = 'adminuser'
class SysUser(models.Model):
name = models.CharField(max_length=128, unique=True, null=True, blank=True, verbose_name=u"用户名称")
username = models.CharField(max_length=16, null=True, blank=True, verbose_name=u"用户名")
password = models.CharField(max_length=256, null=True, blank=True, verbose_name=u"密码")
protocol = models.CharField(max_length=16, null=True, blank=True, verbose_name=u"协议")
private_key = models.CharField(max_length=4096, null=True, blank=True, verbose_name=u"私钥")
public_key = models.CharField(max_length=4096, null=True, blank=True, verbose_name=u"公钥")
is_default = models.BooleanField(default=True, verbose_name=u"是否显示")
auto_push = models.BooleanField(default=True, verbose_name=u"自动推送")
auto_update = models.BooleanField(default=True, verbose_name=u"自动更新")
sudo = models.CharField(max_length=4096, null=True, blank=True, verbose_name=u"私钥")
shell = models.CharField(max_length=64, null=True, blank=True, verbose_name=u"shell环境")
home = models.CharField(max_length=64, null=True, blank=True, verbose_name=u"home目录")
uid = models.IntegerField(null=True, blank=True, verbose_name=u"uid")
date_added = models.DateTimeField(auto_now=True, null=True, blank=True)
create_by = models.CharField(max_length=32, null=True, blank=True, verbose_name=u"创建者")
comment = models.CharField(max_length=128, blank=True, verbose_name=u"备注")
def __unicode__(self):
return self.name
class Meta:
db_table = 'sysuser'
class Asset(models.Model):
ip = models.CharField(max_length=32, blank=True, verbose_name=_('IP'))
other_ip = models.CharField(max_length=255, blank=True, verbose_name=_('Other IP'))
remote_card_ip = models.CharField(max_length=16, blank=True, verbose_name=_('Remote card IP'))
hostname = models.CharField(max_length=128, unique=True, blank=True, verbose_name=_('Hostname'))
port = models.IntegerField(blank=True, verbose_name=_('Port'))
groups = models.ManyToManyField(AssetGroup, blank=True, verbose_name=_('Asset groups'))
username = models.CharField(max_length=16, blank=True, verbose_name=_('Admin user'))
password = models.CharField(max_length=256, blank=True, verbose_name=_("Admin password"))
idc = models.ForeignKey(IDC, blank=True, null=True, on_delete=models.SET_NULL, verbose_name=_('IDC'))
mac_addr = models.CharField(max_length=20, blank=True, verbose_name=_("Mac address"))
brand = models.CharField(max_length=64, blank=True, verbose_name=_('Brand'))
cpu = models.CharField(max_length=64, blank=True, verbose_name=_('CPU'))
memory = models.CharField(max_length=128, blank=True, verbose_name=_('Memory'))
disk = models.CharField(max_length=1024, blank=True, verbose_name=_('Disk'))
os = models.CharField(max_length=128, blank=True, verbose_name=_('OS'))
cabinet_no = models.CharField(max_length=32, blank=True, verbose_name=_('Cabinet number'))
ip = models.CharField(max_length=32, null=True, blank=True, verbose_name=_('IP'))
other_ip = models.CharField(max_length=255, null=True, blank=True, verbose_name=_('Other IP'))
remote_card_ip = models.CharField(max_length=16, null=True, blank=True, verbose_name=_('Remote card IP'))
hostname = models.CharField(max_length=128, unique=True, null=True, blank=True, verbose_name=_('Hostname'))
port = models.IntegerField(null=True, blank=True, verbose_name=_('Port'))
groups = models.ManyToManyField(AssetGroup, null=True, blank=True, verbose_name=_('Asset groups'))
username = models.CharField(max_length=16, null=True, blank=True, verbose_name=_('Admin user'))
password = models.CharField(max_length=256, null=True, blank=True, verbose_name=_("Admin password"))
admin_user = models.ForeignKey(AdminUser, null=True, blank=True, on_delete=models.SET_NULL, verbose_name=_("Admin User"))
sys_user = models.ManyToManyField(SysUser, null=True, blank=True, verbose_name=_("Sys User"))
idc = models.ForeignKey(IDC, null=True, blank=True, on_delete=models.SET_NULL, verbose_name=_('IDC'))
mac_addr = models.CharField(max_length=20, null=True, blank=True, verbose_name=_("Mac address"))
brand = models.CharField(max_length=64, null=True, blank=True, verbose_name=_('Brand'))
cpu = models.CharField(max_length=64, null=True, blank=True, verbose_name=_('CPU'))
memory = models.CharField(max_length=128, null=True, blank=True, verbose_name=_('Memory'))
disk = models.CharField(max_length=1024, null=True, blank=True, verbose_name=_('Disk'))
os = models.CharField(max_length=128, null=True, blank=True, verbose_name=_('OS'))
cabinet_no = models.CharField(max_length=32, null=True, blank=True, verbose_name=_('Cabinet number'))
cabinet_pos = models.IntegerField(null=True, blank=True, verbose_name=_('Cabinet position'))
number = models.CharField(max_length=32, blank=True, unique=True, verbose_name=_('Asset number'))
status = models.ManyToManyField(AssetExtend, blank=True,
number = models.CharField(max_length=32, null=True, blank=True, unique=True, verbose_name=_('Asset number'))
status = models.ManyToManyField(AssetExtend, null=True, blank=True,
related_name="asset_status_extend", verbose_name=_('Asset status'))
type = models.ManyToManyField(AssetExtend, blank=True,
type = models.ManyToManyField(AssetExtend, null=True, blank=True,
related_name="asset_type_extend", verbose_name=_('Asset type'))
env = models.ManyToManyField(AssetExtend, blank=True,
env = models.ManyToManyField(AssetExtend, null=True, blank=True,
related_name="asset_env_extend", verbose_name=_('Asset environment'))
sn = models.CharField(max_length=128, blank=True, unique=True, verbose_name=_('Serial number'))
created_by = models.CharField(max_length=32, blank=True, verbose_name=_('Created by'))
sn = models.CharField(max_length=128, null=True, blank=True, unique=True, verbose_name=_('Serial number'))
created_by = models.CharField(max_length=32, null=True, blank=True, verbose_name=_('Created by'))
is_active = models.BooleanField(default=True, verbose_name=_('Is active'))
date_added = models.DateTimeField(auto_now=True, null=True, verbose_name=_('Date added'))
comment = models.CharField(max_length=128, blank=True, verbose_name=_('Comment'))
date_added = models.DateTimeField(auto_now=True, null=True, blank=True, verbose_name=_('Date added'))
comment = models.CharField(max_length=128, null=True, blank=True, verbose_name=_('Comment'))
def __unicode__(self):
return self.ip
@ -79,3 +132,19 @@ class Asset(models.Model):
class Meta:
db_table = 'asset'
class Label(models.Model):
key = models.CharField(max_length=64, null=True, blank=True, verbose_name=u'key')
value = models.CharField(max_length=64, null=True, blank=True, verbose_name=u'value')
asset = models.ForeignKey(Asset, null=True, blank=True, on_delete=models.SET_NULL, verbose_name=u'label')
created_by = models.CharField(max_length=32, blank=True, verbose_name=_("Created by"))
date_added = models.DateTimeField(auto_now=True, null=True, blank=True)
comment = models.CharField(max_length=128, blank=True, verbose_name=_('Comment'))
def __unicode__(self):
return self.name
class Meta:
db_table = 'label'

View File

@ -24,45 +24,33 @@
<div class="panel blank-panel">
<div class="panel-body">
<div class="tab-content">
<div id="tab-1" class="ibox float-e-margins tab-pane active">
{% if error %}
<div class="alert alert-warning text-center">{{ error }}</div>
{% endif %}
{% if msg %}
<div class="alert alert-success text-center">{{ msg }}</div>
{% endif %}
<div id="tab-1" class="ibox float-e-margins tab-pane active"></div>
<form id="assetForm" method="post" class="form-horizontal">
{% csrf_token %}
<h2 class="widget-head-color-box">基本信息</h2>
<h3 class="widget-head-color-box">基本信息</h3>
{{ form.hostname|bootstrap_horizontal }}
<div class="hr-line-dashed"></div>
{{ form.ip|bootstrap_horizontal }}
{# <p class="col-sm-offset-2">Tips: 如果IP地址不填写, IP默认会设置与主机名一致</p>#}
<div class="form-group" id="id_port">
<div class="hr-line-dashed"></div>
<label class="col-sm-2 control-label">端口</label>
<div class="col-sm-9">
<input type="text" placeholder="" name="port" class="form-control">
</div>
</div>
{# <div class="form-group" id="id_type">#}
{# <div class="hr-line-dashed"></div>#}
{# <label class="col-sm-2 control-label">资产类型</label>#}
{# <div class="col-sm-9">#}
{# <input type="text" placeholder="" name="type" class="form-control">#}
{# </div>#}
{# </div>#}
<div class="form-group" id="id_type">
<label class="col-sm-2 control-label">资产类型</label>
<div class="col-sm-9">
<input type="text" placeholder="" name="type" class="form-control">
</div>
</div>
<div class="hr-line-dashed"></div>
{{ form.comment|bootstrap_horizontal }}
<div class="hr-line-dashed"></div>
<h2>关联资产用户</h2>
<h3>关联资产用户</h3>
<div class="form-group">
<label for="j_group" class="col-sm-2 control-label">管理用户</label>
<div class="col-sm-9">
@ -73,7 +61,7 @@
</div>
</div>
<div class="form-group" id="id_port">
<div class="form-group" id="id_manager_user">
<div class="col-sm-offset-2 col-sm-9">
<input type="text" placeholder="请选择管理用户" name="manager_user" class="form-control">
</div>
@ -81,7 +69,6 @@
<p class="col-sm-offset-2">Tips: 管理用户是服务器存在的root或拥有sudo的用户用来推送系统用户</p>
<div class="form-group">
<div class="hr-line-dashed"></div>
<label for="system_user" class="col-sm-2 control-label">系统用户</label>
<div class="col-sm-9">
<input type="text" placeholder="" name="system_user" class="form-control">
@ -89,14 +76,13 @@
</div>
<div class="hr-line-dashed"></div>
<h2>所属</h2>
<h3>所属</h3>
{{ form.idc|bootstrap_horizontal }}
<div class="hr-line-dashed"></div>
{{ form.group|bootstrap_horizontal }}
<div class="hr-line-dashed"></div>
<h2>标签</h2>
<h3>标签</h3>
<div class="hr-line-dashed"></div>
<div class="form-group">
@ -118,84 +104,84 @@
{% endblock %}
{% block self_footer_js %}
<script>
$('document').ready(function(){
var check_default = "{{ default_setting.name }}";
console.log(check_default);
if (check_default != 'default'){
$('#id_use_default_auth').attr('disabled', true);
$('#id_use_default_auth').attr('checked', false);
$('#admin_account').css('display', 'block');
} else {
$('#id_use_default_auth').click(function(){
if ($(this).is(':checked')){
$('#admin_account').css('display', 'none');
}
else {
$('#admin_account').css('display', 'block');
}
})
}
});
var required_fields = ["id_ip", "id_hostname", "id_port"];
required_fields.forEach(function(field) {
$('label[for="' + field + '"]').parent().addClass("required");
});
$('#assetForm').validator({
timely: 2,
theme: "yellow_right_effect",
rules: {
check_ip: [/^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])(\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])){3}$/, 'ip地址不正确'],
check_port: [/^\d{1,5}$/, '端口号不正确'],
use_default_auth: function() {
var str1 = $("#id_use_default_auth").is(":checked");
if (str1 == true){
var decide = false;
} else {
var decide = true;
}
return decide}
},
fields: {
"ip": {
rule: "check_ip;",
tip: "输入IP",
ok: "",
msg: {required: "必须填写!"}
},
"hostname": {
rule: "required;length[0~53]",
tip: "填写主机名",
ok: "",
msg: {required: "必须填写!"}
},
"port": {
rule: "required",
tip: "输入端口号",
ok: "",
msg: {required: "必须填写!"}
},
"username": {
rule: "required(use_default_auth)",
tip: "输入用户名",
ok: "",
msg: {required: "必须填写!"}
},
"password": {
rule: "required(use_default_auth);length[0~64]",
tip: "输入密码",
ok: "",
msg: {required: "必须填写!"}
}
},
valid: function(form) {
form.submit();
}
});
</script>
{#<script>#}
{# $('document').ready(function(){#}
{# var check_default = "{{ default_setting.name }}";#}
{# console.log(check_default);#}
{# if (check_default != 'default'){#}
{# $('#id_use_default_auth').attr('disabled', true);#}
{# $('#id_use_default_auth').attr('checked', false);#}
{# $('#admin_account').css('display', 'block');#}
{# } else {#}
{# $('#id_use_default_auth').click(function(){#}
{# if ($(this).is(':checked')){#}
{# $('#admin_account').css('display', 'none');#}
{# }#}
{# else {#}
{# $('#admin_account').css('display', 'block');#}
{# }#}
{# })#}
{# }#}
{##}
{# });#}
{##}
{# var required_fields = ["id_ip", "id_hostname", "id_port"];#}
{# required_fields.forEach(function(field) {#}
{# $('label[for="' + field + '"]').parent().addClass("required");#}
{# });#}
{##}
{# $('#assetForm').validator({#}
{# timely: 2,#}
{# theme: "yellow_right_effect",#}
{# rules: {#}
{# check_ip: [/^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])(\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])){3}$/, 'ip地址不正确'],#}
{# check_port: [/^\d{1,5}$/, '端口号不正确'],#}
{# use_default_auth: function() {#}
{# var str1 = $("#id_use_default_auth").is(":checked");#}
{# if (str1 == true){#}
{# var decide = false;#}
{# } else {#}
{# var decide = true;#}
{# }#}
{# return decide}#}
{# },#}
{# fields: {#}
{# "ip": {#}
{# rule: "check_ip;",#}
{# tip: "输入IP",#}
{# ok: "",#}
{# msg: {required: "必须填写!"}#}
{# },#}
{# "hostname": {#}
{# rule: "required;length[0~53]",#}
{# tip: "填写主机名",#}
{# ok: "",#}
{# msg: {required: "必须填写!"}#}
{# },#}
{# "port": {#}
{# rule: "required",#}
{# tip: "输入端口号",#}
{# ok: "",#}
{# msg: {required: "必须填写!"}#}
{# },#}
{# "username": {#}
{# rule: "required(use_default_auth)",#}
{# tip: "输入用户名",#}
{# ok: "",#}
{# msg: {required: "必须填写!"}#}
{# },#}
{# "password": {#}
{# rule: "required(use_default_auth);length[0~64]",#}
{# tip: "输入密码",#}
{# ok: "",#}
{# msg: {required: "必须填写!"}#}
{# }#}
{# },#}
{# valid: function(form) {#}
{# form.submit();#}
{# }#}
{# });#}
{##}
{#</script>#}
{% endblock %}

View File

@ -1,4 +1,9 @@
# coding:utf-8
from django.conf.urls import url, include
from .views import *
# from .api import (
# AssetGroupViewSet, AssetViewSet, IDCViewSet
# )
from django.conf.urls import url,include
import views
# from rest_framework import routers
@ -9,6 +14,9 @@ import views
app_name = 'assets'
urlpatterns = [
url(r'^$', AssetListView.as_view(), name='asset-list'),
url(r'^(?P<pk>[0-9]+)/delete/$', AssetDeleteView.as_view(), name='asset-delete'),
url(r'^(?P<pk>[0-9]+)/detail/$', AssetDetailView.as_view(), name='asset-detail'),
url(r'^asset', views.AssetListView.as_view(), name='asset-list'),
url(r'^asset/add$', views.AssetAddView.as_view(), name='asset-add'),
url(r'^asset/(?P<pk>[0-9]+)$', views.AssetDetailView.as_view(), name='asset-detail'),

View File

@ -1,2 +1,23 @@
# ~*~ coding: utf-8 ~*~
#
from django.contrib.auth.mixins import UserPassesTestMixin
from django.urls import reverse_lazy
from common.tasks import send_mail_async
from common.utils import reverse
from users.models import User
try:
import cStringIO as StringIO
except ImportError:
import StringIO
class AdminUserRequiredMixin(UserPassesTestMixin):
login_url = reverse_lazy('users:login')
def test_func(self):
return self.request.user.is_staff

View File

@ -1,5 +1,10 @@
# coding:utf-8
from __future__ import absolute_import, unicode_literals
from django.views.generic import TemplateView, ListView
from django.urls import reverse_lazy
from django.views.generic.edit import CreateView, DeleteView, FormView, UpdateView
from django.views.generic import TemplateView, ListView
from django.views.generic.edit import CreateView, DeleteView, FormView, UpdateView
from django.urls import reverse_lazy
@ -7,6 +12,8 @@ from django.views.generic.detail import DetailView
from .models import Asset, AssetGroup, IDC, AssetExtend
from .forms import AssetForm
from .utils import AdminUserRequiredMixin
class AssetAddView(CreateView):
model = Asset
@ -14,6 +21,10 @@ class AssetAddView(CreateView):
template_name = 'assets/asset_add.html'
success_url = reverse_lazy('assets:asset-list')
def form_invalid(self, form):
print(form.errors)
return super(AssetAddView, self).form_invalid(form)
class AssetEditView(UpdateView):
pass

View File

@ -132,7 +132,7 @@ else:
# Password validation
# https://docs.djangoproject.com/en/1.10/ref/settings/#auth-password-validators
#
AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',

View File

@ -1,35 +0,0 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.10 on 2016-08-21 09:54
from __future__ import unicode_literals
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('users', '0003_auto_20160814_1758'),
]
operations = [
migrations.AlterField(
model_name='user',
name='email',
field=models.EmailField(max_length=30, unique=True, verbose_name='\u90ae\u4ef6'),
),
migrations.AlterField(
model_name='user',
name='groups',
field=models.ManyToManyField(to='users.UserGroup', verbose_name='\u7528\u6237\u7ec4'),
),
migrations.AlterField(
model_name='user',
name='name',
field=models.CharField(max_length=20, verbose_name='\u59d3\u540d'),
),
migrations.AlterField(
model_name='user',
name='username',
field=models.CharField(max_length=20, unique=True, verbose_name='\u7528\u6237\u540d'),
),
]