Ejemplo n.º 1
0
def user_import(request):
    if request.method == "POST":
        uf = request.FILES.get('user_import')
        with open("/var/opt/alerts/data/user.csv", "wb+") as f:
            for chunk in uf.chunks(chunk_size=1024):
                f.write(chunk)
        try:
            filename = "/var/opt/alerts/data/user.csv"
            with open(filename, "rb") as f:
                title = next(csv.reader(f))
                for data in csv.reader(f):
                    data0 = str2gb2utf8(data[0])
                    if "username" == data0:
                        continue
                    try:
                        user = UserInfo.objects.get(username=data0)
                    except Exception as msg:
                        user = UserInfo()
                        user.username = data0

                    user.email = data[1]
                    user.is_active = data[2]
                    user.is_superuser = data[3]
                    user.nickname = str2gb2utf8(data[4])
                    user.ldap_name = data[5]
                    if data[6]:
                        try:
                            role = str2gb2utf8(data[6])
                            item = RoleList.objects.get(name=role)
                            user.role_id = item.id
                        except Exception as e:
                            print(e)
                            print("role info import error")
                    user.is_alert_enabled = data[7]
                    if data[8]:
                        try:
                            ruleset = str2gb2utf8(data[8])
                            item = RuleSet.objects.get(name=ruleset)
                            user.ruleset_id = item.id
                        except Exception as e:
                            print("ruleset info import error!")
                    user.wid = data[9]
                    user.set_password("password")       # 初始默认密码 "password"
                    user.save()
            os.remove(filename)
            status = 1
        except Exception as e:
            print("import user csv file error!")
            status = 2

    return render(request, 'accounts/import.html', locals())
Ejemplo n.º 2
0
def asset_import(request):
    if request.method == "POST":
        uf = request.FILES.get('asset_import')
        with open("/var/opt/alerts/data/asset.csv", "wb+") as f:
            for chunk in uf.chunks(chunk_size=1024):
                f.write(chunk)
        try:
            filename = "/var/opt/alerts/data/asset.csv"
            with open(filename, "rb") as f:
                title = next(csv.reader(f))
                for data in csv.reader(f):
                    data0 = str2gb2utf8(data[0])
                    if data0 == u"主机名":
                        continue
                    try:
                        host = Host.objects.get(hostname=data0)
                    except Exception as msg:
                        host = Host()
                        host.hostname = data0
                    host.ip = data[1]
                    host.other_ip = str2gb2utf8(data[2])
                    if data[3]:
                        try:
                            idc_name = str2gb2utf8(data[3])
                            print("idc name is : {}".format(idc_name))
                            print("idc name type: {}".format(type(idc_name)))
                            item = Idc.objects.get(name=idc_name)
                            host.idc_id = item.id
                        except Exception as e:
                            print(e)
                            print("idc info import error")
                    host.asset_no = str2gb2utf8(data[4])
                    if data[5]:
                        asset_type = str2gb2utf8(data[5])
                        for x, v in ASSET_TYPE:
                            if v == asset_type:
                                ret = x
                        host.asset_type = ret
                    if data[6]:
                        status = str2gb2utf8(data[6])
                        for x, v in ASSET_STATUS:
                            if v == status:
                                ret = x
                        host.status = ret
                    host.os = str2gb2utf8(data[7])
                    host.vendor = str2gb2utf8(data[8])
                    host.cpu_model = str2gb2utf8(data[9])
                    host.cpu_num = str2gb2utf8(data[10])
                    host.memory = str2gb2utf8(data[11])
                    host.disk = (data[12])
                    host.sn = str2gb2utf8(data[13])
                    host.position = str2gb2utf8(data[14])
                    host.memo = str2gb2utf8(data[15])
                    host.save()
            os.remove(filename)
            status = 1
        except Exception as e:
            print(e)
            print("import asset csv file error!")
            status = 2

    return render(request, 'cmdb/import.html', locals())