diff --git a/jasset/models.py b/jasset/models.py index 253fcbf70..16c276d7d 100644 --- a/jasset/models.py +++ b/jasset/models.py @@ -1,3 +1,4 @@ +import datetime from django.db import models from juser.models import Group, User @@ -9,6 +10,13 @@ class IDC(models.Model): def __unicode__(self): return self.name +class Group(models.Model): + name = models.CharField(max_length=80, unique=True) + comment = models.CharField(max_length=160, blank=True, null=True) + + def __unicode__(self): + return self.name + class Asset(models.Model): LOGIN_TYPE_CHOICES = ( @@ -26,7 +34,7 @@ class Asset(models.Model): password_common = models.CharField(max_length=80, blank=True, null=True) username_super = models.CharField(max_length=20, blank=True, null=True) password_super = models.CharField(max_length=80, blank=True, null=True) - date_added = models.IntegerField(max_length=12) + date_added = models.DateTimeField(auto_now=True,default=datetime.datetime.now(), null=True) is_active = models.BooleanField(default=True) comment = models.CharField(max_length=100, blank=True, null=True) diff --git a/jasset/urls.py b/jasset/urls.py index 48c32db05..bf108e16b 100644 --- a/jasset/urls.py +++ b/jasset/urls.py @@ -5,4 +5,7 @@ from jasset.views import * urlpatterns = patterns('', url(r'^$', index), url(r'jadd', jadd), + url(r'jlist', jlist), + url(r'jadd_idc', jadd_idc), + url(r'jlist_idc', jlist_idc), ) \ No newline at end of file diff --git a/jasset/views.py b/jasset/views.py index 820b4b315..52a95d32e 100644 --- a/jasset/views.py +++ b/jasset/views.py @@ -1,16 +1,110 @@ -#coding:utf-8 +# coding:utf-8 +import datetime from django.shortcuts import render from django.http import HttpResponse from django.template import RequestContext from django.shortcuts import render_to_response from django.http import HttpResponseRedirect +from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger + +from models import IDC, Asset, Group +from connect import PyCrypt, KEY def index(request): - return render_to_response('jasset/jasset.html',) + return render_to_response('jasset/jasset.html', ) def jadd(request): + global j_passwd + groups = [] + cryptor = PyCrypt(KEY) + eidc = IDC.objects.all() + egroup = Group.objects.all() + is_actived = {'active': 1, 'no_active': 0} + login_typed = {'LDAP': 'L', 'SSH_KEY': 'S', 'PASSWORD': 'P', 'MAP': 'M'} + if request.method == 'POST': - pass - return render_to_response('jasset/jadd.html',) \ No newline at end of file + j_ip = request.POST.get('j_ip') + j_idc = request.POST.get('j_idc') + j_port = request.POST.get('j_port') + j_type = request.POST.get('j_type') + j_group = request.POST.getlist('j_group') + j_active = request.POST.get('j_active') + j_comment = request.POST.get('j_comment') + if j_type == 'MAP': + j_user = request.POST.get('j_user') + j_password = cryptor.encrypt(request.POST.get('j_password')) + j_root = request.POST.get('j_root') + j_passwd = cryptor.encrypt(request.POST.get('j_passwd')) + + j_idc = IDC.objects.get(name=j_idc) + for group in j_group: + c = Group.objects.get(name=group) + groups.append(c) + + if Asset.objects.filter(ip=str(j_ip)): + emg = u'该IP已存在!' + return render_to_response('jasset/jadd.html', {'emg': emg, 'j_ip': j_ip}) + + elif j_type == 'MAP': + a = Asset(ip=j_ip, + port=j_port, + login_type=login_typed[j_type], + idc=j_idc, + is_active=int(is_actived[j_active]), + comment=j_comment, + username_common=j_user, + password_common=j_password, + username_super=j_root, + password_super=j_passwd,) + else: + a = Asset(ip=j_ip, + port=j_port, + login_type=login_typed[j_type], + idc=j_idc, + is_active=int(is_actived[j_active]), + comment=j_comment) + a.save() + a.group = groups + a.save() + + return render_to_response('jasset/jadd.html', + {'header_title': u'添加主机 | Add Host', + 'path1': '资产管理', + 'path2': '添加主机', + 'eidc': eidc, + 'egroup': egroup, } + ) + + +def jlist(request): + posts = contact_list = Asset.objects.all().order_by('ip') + print posts + paginator = Paginator(contact_list, 5) + try: + page = int(request.GET.get('page', '1')) + except ValueError: + page = 1 + + try: + contacts = paginator.page(page) + except (EmptyPage, InvalidPage): + contacts = paginator.page(paginator.num_pages) + + return render_to_response('jasset/jlist.html', + {"contacts": contacts, + 'p': paginator, + 'posts': posts, + 'header_title': u'查看主机 | List Host', + 'path1': '资产管理', + 'path2': '查看主机', }, + context_instance=RequestContext(request)) + + +def jadd_idc(request): + pass + + +def jlist_idc(request): + pass \ No newline at end of file diff --git a/jumpserver/settings.py b/jumpserver/settings.py index c4e6ed3ce..67e9bc776 100644 --- a/jumpserver/settings.py +++ b/jumpserver/settings.py @@ -111,7 +111,7 @@ USE_I18N = True USE_L10N = True -USE_TZ = True +USE_TZ = False # Static files (CSS, JavaScript, Images) diff --git a/templates/jasset/jadd.html b/templates/jasset/jadd.html index 81f4fa931..6d3985beb 100644 --- a/templates/jasset/jadd.html +++ b/templates/jasset/jadd.html @@ -1,94 +1,122 @@ {% extends 'base.html' %} {% block content %} -