diff --git a/jasset/asset_api.py b/jasset/asset_api.py index 6968efa7b..10a0075ee 100644 --- a/jasset/asset_api.py +++ b/jasset/asset_api.py @@ -32,6 +32,21 @@ def db_add_group(**kwargs): group_add_asset(group, asset_id) +def db_update_group(**kwargs): + """ + add a asset group in database + 数据库中更新资产 + """ + group_id = kwargs.pop('id') + asset_id_list = kwargs.pop('asset_select') + group = get_object(AssetGroup, id=group_id) + + for asset_id in asset_id_list: + group_add_asset(group, asset_id) + + AssetGroup.objects.filter(id=group_id).update(**kwargs) + + def db_asset_add(**kwargs): """ add asset to db diff --git a/jasset/forms.py b/jasset/forms.py index 648390600..1fd36c40c 100644 --- a/jasset/forms.py +++ b/jasset/forms.py @@ -5,10 +5,27 @@ from jasset.models import IDC, Asset, AssetGroup class AssetForm(forms.ModelForm): + active_choice = ( + (1, "激活"), + (0, "禁用") + ) + is_active = forms.ChoiceField( + label=u"是否激活", required=True, initial = 1, + widget=forms.RadioSelect, choices=active_choice + ) + class Meta: model = Asset fields = [ "ip", "second_ip", "hostname", "port", "group", "username", "password", "use_default_auth", "idc", "mac", "remote_ip", "brand", "cpu", "memory", "disk", "system_type", "system_version", "cabinet", "position", "number", "status", "asset_type", "env", "sn", "is_active", "comment" - ] \ No newline at end of file + ] + + +class AssetGroupForm(forms.ModelForm): + class Meta: + model = AssetGroup + fields = [ + "name", "comment" + ] diff --git a/jasset/models.py b/jasset/models.py index 76757d448..670f3f71d 100644 --- a/jasset/models.py +++ b/jasset/models.py @@ -99,7 +99,7 @@ class Asset(models.Model): ) ip = models.IPAddressField(unique=True, verbose_name=u"主机IP") - second_ip = models.IPAddressField(unique=True, blank=True, null=True, verbose_name=u"IP2") + second_ip = models.CharField(max_length=255, blank=True, null=True, verbose_name=u"其他IP") hostname = models.CharField(max_length=64, blank=True, null=True, verbose_name=u"主机名") port = models.IntegerField(max_length=6, verbose_name=u"端口号") group = models.ManyToManyField(AssetGroup, blank=True, null=True, verbose_name=u"所属主机组") @@ -108,7 +108,7 @@ class Asset(models.Model): use_default_auth = models.BooleanField(default=True, verbose_name=u"使用默认管理账号") idc = models.ForeignKey(IDC, blank=True, null=True, on_delete=models.SET_NULL, verbose_name=u'机房') mac = models.CharField(max_length=20, blank=True, null=True, verbose_name=u"MAC地址") - remote_ip = models.IPAddressField(unique=True, blank=True, null=True, verbose_name=u'远控卡') + remote_ip = models.CharField(max_length=16, blank=True, null=True, verbose_name=u'远控卡') brand = models.CharField(max_length=64, blank=True, null=True, verbose_name=u'硬件厂商型号') cpu = models.CharField(max_length=64, blank=True, null=True, verbose_name=u'CPU') memory = models.CharField(max_length=128, blank=True, null=True, verbose_name=u'内存') @@ -118,9 +118,9 @@ class Asset(models.Model): cabinet = models.CharField(max_length=32, blank=True, null=True, verbose_name=u'机柜号') position = models.IntegerField(max_length=2, blank=True, null=True, verbose_name=u'机器位置') number = models.CharField(max_length=32, blank=True, null=True, verbose_name=u'资产编号') - status = models.IntegerField(max_length=2, choices=SERVER_STATUS, default=1, verbose_name=u"机器状态") + status = models.IntegerField(max_length=2, choices=SERVER_STATUS, blank=True, null=True, default=1, verbose_name=u"机器状态") asset_type = models.IntegerField(max_length=2, choices=ASSET_TYPE, blank=True, null=True, verbose_name=u"主机类型") - env = models.CharField(max_length=32, choices=ENVIRONMENT, blank=True, null=True, verbose_name=u"运行环境") + env = models.IntegerField(max_length=2, choices=ENVIRONMENT, blank=True, null=True, verbose_name=u"运行环境") sn = models.CharField(max_length=32, blank=True, null=True, verbose_name=u"SN编号") date_added = models.DateTimeField(auto_now=True, default=datetime.datetime.now(), null=True) is_active = models.BooleanField(default=True, verbose_name=u"是否激活") @@ -152,4 +152,4 @@ class AssetAlias(models.Model): alias = models.CharField(max_length=100, blank=True, null=True) def __unicode__(self): - return self.alias + return self.alias \ No newline at end of file diff --git a/jasset/models_bak.py b/jasset/models_bak.py new file mode 100644 index 000000000..9c9252a7f --- /dev/null +++ b/jasset/models_bak.py @@ -0,0 +1,151 @@ +# coding: utf-8 + +import datetime +from django.db import models +from juser.models import User, UserGroup + + +class AssetGroup(models.Model): + GROUP_TYPE = ( + ('P', 'PRIVATE'), + ('A', 'ASSET'), + ) + name = models.CharField(max_length=80, unique=True) + comment = models.CharField(max_length=160, blank=True, null=True) + + def __unicode__(self): + return self.name + + def get_asset(self): + return self.asset_set.all() + + def get_asset_info(self, printable=False): + assets = self.get_asset() + ip_comment = {} + for asset in assets: + ip_comment[asset.ip] = asset.comment + + for ip in sorted(ip_comment): + if ip_comment[ip]: + print '%-15s -- %s' % (ip, ip_comment[ip]) + else: + print '%-15s' % ip + print '' + + def get_asset_num(self): + return len(self.get_asset()) + + def get_user_group(self): + perm_list = self.perm_set.all() + user_group_list = [] + for perm in perm_list: + user_group_list.append(perm.user_group) + return user_group_list + + def get_user(self): + user_list = [] + user_group_list = self.get_user_group() + for user_group in user_group_list: + user_list.extend(user_group.user_set.all()) + return user_list + + def is_permed(self, user=None, user_group=None): + if user: + if user in self.get_user(): + return True + + if user_group: + if user_group in self.get_user_group(): + return True + return False + + +class IDC(models.Model): + name = models.CharField(max_length=32, unique=True) + bandwidth = models.CharField(max_length=32, blank=True, null=True) + linkman = models.CharField(max_length=16, blank=True, null=True) + phone = models.CharField(max_length=32, blank=True, null=True) + address = models.CharField(max_length=128, blank=True, null=True) + network = models.TextField(blank=True, null=True) + date_added = models.DateField(auto_now=True, default=datetime.datetime.now()) + operator = models.IntegerField(max_length=32, blank=True, null=True) + comment = models.CharField(max_length=128, blank=True, null=True) + + def __unicode__(self): + return self.name + + +class Asset(models.Model): + """ + asset modle + """ + ENVIRONMENT = ( + (0, U'生产环境'), + (1, U'测试环境') + ) + SERVER_STATUS = ( + (0, u"已使用"), + (1, u"未使用"), + (2, u"报废") + ) + ASSET_TYPE = ( + (0, u"服务器"), + (2, u"网络设备"), + (3, u"其他") + ) + + ip = models.IPAddressField(unique=True) + second_ip = models.CharField(max_length=255, blank=True, null=True) + hostname = models.CharField(max_length=64, blank=True, null=True) + port = models.IntegerField(max_length=6) + group = models.ManyToManyField(AssetGroup, blank=True, null=True) + username = models.CharField(max_length=16, blank=True, null=True) + password = models.CharField(max_length=64, blank=True, null=True) + use_default_auth = models.BooleanField(default=True) + idc = models.ForeignKey(IDC, blank=True, null=True, on_delete=models.SET_NULL) + mac = models.CharField(max_length=20, blank=True, null=True) + remote_ip = models.IPAddressField(unique=True, blank=True, null=True) + brand = models.CharField(max_length=64, blank=True, null=True) + cpu = models.CharField(max_length=64, blank=True, null=True) + memory = models.CharField(max_length=128, blank=True, null=True) + disk = models.CharField(max_length=128, blank=True, null=True) + system_type = models.CharField(max_length=32, blank=True, null=True) + system_version = models.CharField(max_length=8, blank=True, null=True) + cabinet = models.CharField(max_length=32, blank=True, null=True) + position = models.IntegerField(max_length=2, blank=True, null=True) + number = models.CharField(max_length=32, blank=True, null=True) + status = models.IntegerField(max_length=2, choices=SERVER_STATUS, default=1) + asset_type = models.IntegerField(max_length=2, choices=ASSET_TYPE, blank=True, null=True) + env = models.CharField(max_length=32, choices=ENVIRONMENT, blank=True, null=True) + sn = models.CharField(max_length=32, blank=True, null=True) + date_added = models.DateTimeField(auto_now=True, default=datetime.datetime.now()) + is_active = models.BooleanField(default=True) + comment = models.CharField(max_length=128, blank=True, null=True) + + def __unicode__(self): + return self.ip + + def get_user(self): + perm_list = [] + asset_group_all = self.bis_group.all() + for asset_group in asset_group_all: + perm_list.extend(asset_group.perm_set.all()) + + user_group_list = [] + for perm in perm_list: + user_group_list.append(perm.user_group) + + user_permed_list = [] + for user_group in user_group_list: + user_permed_list.extend(user_group.user_set.all()) + user_permed_list = list(set(user_permed_list)) + return user_permed_list + + +class AssetAlias(models.Model): + user = models.ForeignKey(User) + asset = models.ForeignKey(Asset) + alias = models.CharField(max_length=100, null=True) + + def __unicode__(self): + return self.alias diff --git a/jasset/urls.py b/jasset/urls.py index a3df6dbec..3f18f16f2 100644 --- a/jasset/urls.py +++ b/jasset/urls.py @@ -16,9 +16,9 @@ urlpatterns = patterns('', # url(r"^host_detail/$", host_detail), # url(r"^dept_host_ajax/$", dept_host_ajax), # url(r"^show_all_ajax/$", show_all_ajax), - # url(r'^group_edit/$', group_edit), - # url(r'^group_list/$', group_list), - # url(r'^group_detail/$', group_detail), + url(r'^group_edit/$', group_edit), + url(r'^group_list/$', group_list), + url(r'^group_detail/$', group_detail), # url(r'^group_del_host/$', group_del_host), # url(r'^host_edit/batch/$', host_edit_batch), diff --git a/jasset/views.py b/jasset/views.py index d10448b4c..c58d6617e 100644 --- a/jasset/views.py +++ b/jasset/views.py @@ -37,13 +37,69 @@ def group_add(request): except ServerError: pass + else: db_add_group(name=name, comment=comment, asset_select=asset_select) - msg = u"主机组 %s 添加成功" % name + smg = u"主机组 %s 添加成功" % name return my_render('jasset/group_add.html', locals(), request) +@require_role('admin') +def group_edit(request): + """ + Edit asset group + 编辑资产组 + """ + header_title, path1, path2 = u'编辑主机组', u'资产管理', u'编辑主机组' + group_id = request.GET.get('id', '') + group = get_object(AssetGroup, id=group_id) + + asset_all = Asset.objects.all() + asset_select = Asset.objects.filter(group=group) + asset_no_select = [a for a in asset_all if a not in asset_select] + + if request.method == 'POST': + name = request.POST.get('name', '') + asset_select = request.POST.getlist('asset_select', []) + comment = request.POST.get('comment', '') + + try: + if not name: + emg = u'组名不能为空' + raise ServerError(emg) + + if group.name != name: + asset_group_test = get_object(AssetGroup, name=name) + if asset_group_test: + emg = u"该组名 %s 已存在" % name + raise ServerError(emg) + + except ServerError: + pass + + else: + group.asset_set.clear() + db_update_group(id=group_id, name=name, comment=comment, asset_select=asset_select) + smg = u"主机组 %s 添加成功" % name + + return HttpResponseRedirect('/jasset/group_list') + + return my_render('jasset/group_edit.html', locals(), request) + + +@require_role('admin') +def group_detail(request): + """ 主机组详情 """ + header_title, path1, path2 = u'主机组详情', u'资产管理', u'主机组详情' + group_id = request.GET.get('id', '') + group = get_object(AssetGroup, id=group_id) + asset_all = Asset.objects.filter(group=group).order_by('ip') + + contact_list, p, contacts, page_range, current_page, show_first, show_end = pages(asset_all, request) + return my_render('jasset/group_detail.html', locals(), request) + + @require_role('admin') def group_list(request): """ @@ -88,20 +144,8 @@ def asset_add(request): asset_group_all = AssetGroup.objects.all() af = AssetForm() if request.method == 'POST': + af_post = AssetForm(request.POST) ip = request.POST.get('ip') - port = request.POST.get('port') - groups = request.POST.getlist('groups') - use_default_auth = True if request.POST.getlist('use_default_auth', []) else False - is_active = True if request.POST.get('is_active') else False - comment = request.POST.get('comment') - - if not use_default_auth: - username = request.POST.get('username') - password = request.POST.get('password') - password_encode = CRYPTOR.encrypt(password) - else: - username = None - password_encode = None try: if Asset.objects.filter(ip=str(ip)): @@ -110,13 +154,15 @@ def asset_add(request): except ServerError: pass - else: - db_asset_add( - ip=ip, port=port, use_default_auth=use_default_auth, is_active=is_active, comment=comment, - groups=groups, username=username, password=password_encode - ) - msg = u'主机 %s 添加成功' % ip + else: + if af_post.is_valid(): + asset_save = af_post.save(commit=False) + asset_save.save() + af_post.save_m2m() + msg = u'主机 %s 添加成功' % ip + else: + esg = u'主机 %s 添加失败' % ip return my_render('jasset/asset_add.html', locals(), request) # @@ -269,32 +315,38 @@ def asset_del(request): @require_role(role='super') def asset_edit(request): - """ 修改主机 """ + """ + edit a asset + 修改主机 + """ header_title, path1, path2 = u'修改资产', u'资产管理', u'修改资产' asset_id = request.GET.get('id', '') if not asset_id: return HttpResponse('没有该主机') asset = get_object(Asset, id=asset_id) - + af = AssetForm(instance=asset) if request.method == 'POST': - ip = request.POST.get('ip') - port = request.POST.get('port') - groups = request.POST.getlist('groups') - use_default_auth = True if request.POST.getlist('use_default_auth', []) else False - is_active = True if request.POST.get('is_active') else False - comment = request.POST.get('comment') + af_post = AssetForm(request.POST, instance=asset) + ip = request.POST.get('ip', '') - if not use_default_auth: - username = request.POST.get('username') - password = request.POST.get('password') - if password == asset.password: - password_encode = password - else: - password_encode = CRYPTOR.encrypt(password) - else: - username = None - password_encode = None + # ip = request.POST.get('ip') + # port = request.POST.get('port') + # groups = request.POST.getlist('groups') + # use_default_auth = True if request.POST.getlist('use_default_auth', []) else False + # is_active = True if request.POST.get('is_active') else False + # comment = request.POST.get('comment') + + # if not use_default_auth: + # username = request.POST.get('username') + # password = request.POST.get('password') + # if password == asset.password: + # password_encode = password + # else: + # password_encode = CRYPTOR.encrypt(password) + # else: + # username = None + # password_encode = None try: asset_test = get_object(Asset, ip=ip) @@ -304,10 +356,13 @@ def asset_edit(request): except ServerError: pass else: - db_asset_update(id=asset_id, ip=ip, port=port, use_default_auth=use_default_auth, - username=username, password=password_encode, - is_active=is_active, comment=comment) - msg = u'主机 %s 修改成功' % ip + if af_post.is_valid(): + af_save = af_post.save(commit=False) + af_save.save() + af_post.save_m2m() + msg = u'主机 %s 修改成功' % ip + else: + emg = u'主机 %s 修改失败' % ip return HttpResponseRedirect('/jasset/asset_detail/?id=%s' % asset_id) return my_render('jasset/asset_edit.html', locals(), request) diff --git a/jumpserver/settings.py b/jumpserver/settings.py index f6afa68f7..6c4110a18 100644 --- a/jumpserver/settings.py +++ b/jumpserver/settings.py @@ -54,7 +54,7 @@ INSTALLED_APPS = ( 'django.contrib.messages', 'django.contrib.staticfiles', 'django.contrib.humanize', - "bootstrapform", + 'bootstrapform', 'jumpserver', 'juser', 'jasset', diff --git a/templates/jasset/asset_add.html b/templates/jasset/asset_add.html index 53c153740..d36ca880f 100644 --- a/templates/jasset/asset_add.html +++ b/templates/jasset/asset_add.html @@ -39,27 +39,26 @@ {% if msg %}