def addapiuser(request): apiusername = request.POST.get('add_api_username', None) password = request.POST.get('add_api_password', None) name = request.POST.get('add_api_name', None) department = request.POST.get('add_api_department', None) description = request.POST.get('add_api_description', None) try: username = request.session.get('username') permess = Userperm(username, 'operate') if permess['isSuccess']: if apiusername and password: insert_apiusers_profiles = insert_apiusers_profile( apiusername, password, name, department, description) if insert_apiusers_profiles: result = {'isSuccess': True, "message": '新建成功'} else: result = {'isSuccess': False, "message": '写入数据库失败'} else: result = {'isSuccess': False, "message": '用户名或密码未填写'} else: result = {'isSuccess': False, "message": '权限不足'} except Exception as e: result = {'isSuccess': False, "message": str(e)} insert_log(username, request, str(result['isSuccess']), str(result), '') response = HttpResponse() response['Content-Type'] = "text/javascript" response.write(json.dumps(result)) return response
def addContact(request): distinguishedName = request.POST.get('add_contact_distinguishedName') cn = request.POST.get('add_contact_cn') sn = request.POST.get('add_contact_sn') givenName = request.POST.get('add_contact_givenName') displayName = request.POST.get('add_contact_displayName') description = request.POST.get('add_contact_description') mail = request.POST.get('add_contact_mail') name = request.POST.get('add_contact_name') smtpvalue = request.POST.get('add_contact_smtpvalue') try: username = request.session.get('username') permess = Userperm(username, 'operate') if permess['isSuccess']: result = newContact(distinguishedName, cn, sn=sn, givenName=givenName, displayName=displayName, description=description, mail=mail, name=name, smtpvalue=smtpvalue, attributes={}) else: result = {'isSuccess': False, "message": '权限不足'} except Exception as e: result = {'isSuccess': False, "message": str(e)} insert_log(username, request, str(result['isSuccess']), str(result), '') response = HttpResponse() response['Content-Type'] = "text/javascript" response.write(json.dumps(result)) return response
def modifyuser(request): field = request.POST.get('field', None) row = request.POST.get('row', None) oldValue = request.POST.get('oldValue', None) try: username = request.session.get('username') permess = Userperm(username, 'changelw') if permess['isSuccess']: rowjson = json.loads(row) if field == 'sAMAccountName': sAMAccountName = oldValue else: sAMAccountName = rowjson['sAMAccountName'] sAMAccountName_repeace = repeace(sAMAccountName) with ldap3RESTARTABLE as conn: conn.search( search_base=ladp3search_base, search_filter='(&(sAMAccountName=' + sAMAccountName_repeace + ')(&(objectCategory=person)(objectClass=user)))', ) response = conn.response[0] dn = response.get('dn', '') if dn: if field == 'cn': cn = "CN=" + rowjson['cn'] modify_dn = conn.modify_dn(dn, cn) else: attributesVaule = rowjson.get(field, None) if attributesVaule: attributesVaule = [attributesVaule] else: attributesVaule = [] modify_dn = conn.modify(dn=dn, changes={ field: [(MODIFY_REPLACE, attributesVaule)] }) if modify_dn: result = { 'isSuccess': True, "message": str(field) + '的属性值' + str(oldValue) + '修改为' + rowjson.get(field, '') } else: result = { 'isSuccess': False, "message": str(field) + '的属性修改失败' } else: result = {'isSuccess': False, "message": '权限不足'} except Exception as e: result = {'isSuccess': False, "message": str(e)} insert_log(username, request, str(result['isSuccess']), str(result), '') response = HttpResponse() response['Content-Type'] = "text/javascript" response.write(json.dumps(result).encode("UTF-8")) return response
def delObject(request): dn = request.POST.get('dn') controls = request.POST.get('controls') checkboxtext = request.POST.get('checkboxtext') try: username = request.session.get('username') permess = Userperm(username, 'operate') if permess['isSuccess']: if controls == 'true': if checkboxtext == '删除下面所有账户或对象': controls = [('1.2.840.113556.1.4.805', False, None)] result = delete_object(dn, controls) else: result = { 'isSuccess': False, "message": '请输入"删除下面所有账户或对象"' } else: controls = None result = delete_object(dn, controls) else: result = {'isSuccess': False, "message": '权限不足'} except Exception as e: result = {'isSuccess': False, "message": str(e)} insert_log(username, request, str(result['isSuccess']), str(result), '') response = HttpResponse() response['Content-Type'] = "text/javascript" response.write(json.dumps(result)) return response
def addGroup(request): distinguishedName = request.POST.get('add_group_distinguishedName') cn = request.POST.get('add_group_cn') scope = request.POST.get('group_scope') type = request.POST.get('group_type') sAMAccountName = request.POST.get('add_group_sAMAccountName') displayName = request.POST.get('add_group_displayName') description = request.POST.get('add_group_description') try: username = request.session.get('username') permess = Userperm(username, 'operate') if permess['isSuccess']: groupType = int(scope) - int(type) attributes = {} if displayName: attributes.update({'displayName': displayName}) if description: attributes.update({'description': description}) result = newGroup(distinguishedName, cn, sAMAccountName, groupType, attributes=attributes) else: result = {'isSuccess': False, "message": '权限不足'} except Exception as e: result = {'isSuccess': False, "message": str(e)} insert_log(username, request, str(result['isSuccess']), str(result), '') response = HttpResponse() response['Content-Type'] = "text/javascript" response.write(json.dumps(result)) return response
def delprojects(request): username = request.session.get('username') user = request.POST.get("user") id = request.POST.get("id") if username == user: try: delvalue = onedeltable(id) if delvalue == (): result = {'isSuccess': True, "message": '删除成功'} else: result = {'isSuccess': False, "message": '删除失败'} except Exception as e: result = {'isSuccess': False, "message": str(e)} else: permess = Userperm(username, 'operate') if permess['isSuccess']: try: delvalue = onedeltable(id) if delvalue == (): result = {'isSuccess': True, "message": '删除成功'} else: result = {'isSuccess': False, "message": '删除失败'} except Exception as e: result = {'isSuccess': False, "message": str(e)} else: result = {'isSuccess': False, "message": '权限不足'} insert_log(username, request, str(result['isSuccess']), str(result), '删除文档') response = HttpResponse() response['Content-Type'] = "application/json" response.write(json.dumps(result, default=str).encode("UTF-8")) return response
def addapipermissions(request): username_id = request.POST.get('username_id', None) apinames = request.POST.get('apiname', None) try: username = request.session.get('username') permess = Userperm(username, 'operate') if permess['isSuccess']: if username_id and apinames: apinamelist = apinames.split(',') for apiname in apinamelist: apiname_s = apiname.strip() if apiname_s: apinamepermissions = True get_apinamepermissions_iss = get_apinamepermissions_is( username_id, apiname_s) if not get_apinamepermissions_iss: apinamepermissions = insert_apinamepermissions( username_id, apiname_s) if apinamepermissions: result = {'isSuccess': True, "message": '添加成功'} else: result = {'isSuccess': False, "message": '写入数据库失败'} else: result = {'isSuccess': False, "message": 'apiname栏位未填写,或其他'} else: result = {'isSuccess': False, "message": '权限不足'} except Exception as e: result = {'isSuccess': False, "message": str(e)} insert_log(username, request, str(result['isSuccess']), str(result), '') response = HttpResponse() response['Content-Type'] = "text/javascript" response.write(json.dumps(result)) return response
def setRenameObject(request): distinguishedName = request.POST.get('distinguishedName') cn = request.POST.get('cn') sn = request.POST.get('sn') givenName = request.POST.get('givenName') displayName = request.POST.get('displayName') userPrincipalName = request.POST.get('userPrincipalName') sAMAccountName = request.POST.get('sAMAccountName') objectClass = request.POST.get('objectClass') try: username = request.session.get('username') permess = Userperm(username, 'operate') if permess['isSuccess']: objectClass = objectClass.split(',') result = set_rename_object(distinguishedName, cn, sn, givenName, displayName, userPrincipalName, sAMAccountName, objectClass) else: result = {'isSuccess': False, "message": '权限不足'} except Exception as e: result = {'isSuccess': False, "message": str(e)} insert_log(username, request, str(result['isSuccess']), str(result), '') response = HttpResponse() response['Content-Type'] = "text/javascript" response.write(json.dumps(result)) return response
def resetUserPassword(request): sAMAccountName = request.POST.get('sAMAccountName', None) distinguishedName = request.POST.get('distinguishedName', None) newpassword = request.POST.get('newpassword') pwdLastSet = request.POST.get('pwdLastSet') unlock = request.POST.get('unlock') ip = request.POST.get('ip', None) try: username = request.session.get('username') permess = Userperm(username, 'operate') if permess['isSuccess']: if pwdLastSet == 'true': pwdLastSet = True else: pwdLastSet = None if unlock == 'true': unlock = True else: unlock = None result = resetPassword(newpassword, distinguishedName=distinguishedName, sAMAccountName=sAMAccountName, pwdLastSet=pwdLastSet, unlock=unlock, ip=ip) else: result = {'isSuccess': False, "message": '权限不足'} except Exception as e: result = {'isSuccess': False, "message": str(e)} insert_log(username, request, str(result['isSuccess']), str(result), '') response = HttpResponse() response['Content-Type'] = "text/javascript" response.write(json.dumps(result)) return response
def setAccidentallyDeleted(request): distinguishedName = request.POST.get('distinguishedName', None) sAMAccountName = request.POST.get('sAMAccountName', None) prevent = request.POST.get('prevent', 'true') try: username = request.session.get('username') permess = Userperm(username, 'operate') if permess['isSuccess']: if prevent == 'true': result = check_accidentally_deleted( sAMAccountName=sAMAccountName, distinguishedName=distinguishedName) else: result = uncheck_accidentally_deleted( sAMAccountName=sAMAccountName, distinguishedName=distinguishedName) else: result = {'isSuccess': False, "message": '权限不足'} except Exception as e: result = {'isSuccess': False, "message": str(e)} insert_log(username, request, str(result['isSuccess']), str(result), '') response = HttpResponse() response['Content-Type'] = "text/javascript" response.write(json.dumps(result)) return response
def executemodifyuser(request): getData = request.POST.get('getData', {}) username = request.session.get('username') try: username = request.session.get('username') permess = Userperm(username, 'operate') if permess['isSuccess']: getDatatojson = json.loads(getData) modifyalluser = thrmodifyalluser(getDatatojson, username) if modifyalluser: if '' in getDatatojson[0]: getDatatojson[0].pop('') if '0' in getDatatojson[0]: getDatatojson[0].pop('0') attributesldap = list(getDatatojson[0].keys()) columns = [] for c in range(0, len(attributesldap)): columns.append({ 'field': attributesldap[c], 'title': attributesldap[c] }) result = { 'isSuccess': True, "message": modifyalluser, 'columns': columns } else: result = {'isSuccess': False, "message": '权限不足'} except Exception as e: result = {'isSuccess': False, "message": str(e)} insert_log(username, request, str(result['isSuccess']), str(result), '') response = HttpResponse() response['Content-Type'] = "text/javascript" response.write(json.dumps(result)) return response
def addComputer(request): distinguishedName = request.POST.get('add_computer_distinguishedName') cn = request.POST.get('add_computer_cn') displayName = request.POST.get('add_computer_displayName') description = request.POST.get('add_computer_description') try: username = request.session.get('username') permess = Userperm(username, 'operate') if permess['isSuccess']: attributes = {} if displayName: attributes.update({'displayName': displayName}) if description: attributes.update({'description': description}) result = newComputer(distinguishedName, cn, userAccountControl=4128, attributes=attributes) else: result = {'isSuccess': False, "message": '权限不足'} except Exception as e: result = {'isSuccess': False, "message": str(e)} insert_log(username, request, str(result['isSuccess']), str(result), '') response = HttpResponse() response['Content-Type'] = "text/javascript" response.write(json.dumps(result)) return response
def upmesserro(request): username = request.session.get('username') permess = Userperm(username, 'operate') if permess['isSuccess']: try: post = request.POST texid = post.get('texid') title = post.get('titel') tab = post.get('tab1') name = post.get('markup') userper = post.get('userper') affect = post.get('affect') inpendtimeid = post.get('inpendtimeid') remessage=upsaveerr(title, userper,affect,inpendtimeid, tab,name, username,texid) if remessage==(): result = {'isSuccess': True, "message": '保存成功'} else: result = {'isSuccess': False, "message": '保存失败'} except Exception as e: result = {'isSuccess': False, "message": str(e)} else: result = {'isSuccess': False, "message": '权限不足'} insert_log(username, request, str(result['isSuccess']), str(result), '新建文档') response = HttpResponse() response['Content-Type'] = "application/json" response.write(json.dumps(result, default=str).encode("UTF-8")) return response
def executecreatmail(request): getData = request.POST.get('getData', {}) maildb = request.POST.get('maildb', '') mailarchive = request.POST.get('mailarchive', '') mailarchivedb = request.POST.get('mailarchivedb', '') username = request.session.get('username') try: permess = Userperm(username, 'operate') if permess['isSuccess']: getDatatojson = json.loads(getData) if getDatatojson and maildb: modifyalluser = threxecutecreatmail(getDatatojson, username, maildb, mailarchive, mailarchivedb) if modifyalluser: if '' in getDatatojson[0]: getDatatojson[0].pop('') if '0' in getDatatojson[0]: getDatatojson[0].pop('0') if 'status' in getDatatojson[0]: getDatatojson[0].pop('status') attributesldap = list(getDatatojson[0].keys()) columns = [] for c in range(0, len(attributesldap)): columns.append({ 'field': attributesldap[c], 'title': attributesldap[c] }) for modifuser in modifyalluser: if modifuser.get('description', ''): modifuser['description'] = str( modifuser['description'][0]) result = { 'isSuccess': True, "message": modifyalluser, 'columns': columns } else: result = { 'isSuccess': False, "message": '传入空值', } else: result = {'isSuccess': False, "message": '权限不足'} except Exception as e: result = {'isSuccess': False, "message": str(e)} insert_log(username, request, str(result['isSuccess']), str(result), '') response = HttpResponse() response['Content-Type'] = "text/javascript" response.write(json.dumps(result)) return response
def addUser(request): distinguishedName = request.POST.get('add_user_distinguishedName') cn = request.POST.get('add_user_cn') sn = request.POST.get('add_user_sn') givenName = request.POST.get('add_user_givenName') password = request.POST.get('add_user_password') userPrincipalName1 = request.POST.get('add_user_userPrincipalName1') userPrincipalName2 = request.POST.get('add_user_userPrincipalName2') sAMAccountName1 = request.POST.get('add_user_sAMAccountName1') sAMAccountName2 = request.POST.get('add_user_sAMAccountName2') displayName = request.POST.get('add_user_displayName') description = request.POST.get('add_user_description') userAccountControl = request.POST.get('add_user_userAccountControl') mail = request.POST.get('add_user_mail') maildb = request.POST.get('add_user_mail_db') try: userPrincipalName = userPrincipalName1 + userPrincipalName2 username = request.session.get('username') permess = Userperm(username, 'operate') if permess['isSuccess']: if userAccountControl == 'yes': userAccountControl = 544 else: userAccountControl = 546 attributes = {} if displayName: attributes.update({'displayName': displayName}) if description: attributes.update({'description': description}) result = newUser(distinguishedName=distinguishedName, cn=cn, sn=sn, givenName=givenName, displayName=None, userPrincipalName=userPrincipalName, sAMAccountName=sAMAccountName2, password=password, userAccountControl=userAccountControl, mail=mail, maildb=maildb, attributes=attributes) else: result = {'isSuccess': False, "message": '权限不足'} except Exception as e: result = {'isSuccess': False, "message": str(e)} insert_log(username, request, str(result['isSuccess']), str(result), '') response = HttpResponse() response['Content-Type'] = "text/javascript" response.write(json.dumps(result)) return response
def userautodow(request): username = request.session.get('username') try: permess = Userperm(username, 'operate') if permess['isSuccess']: result = {'isSuccess': True, "message": '正常权限'} else: result = {'isSuccess': False, "message": '权限失败'} except Exception as e: result = {'isSuccess': False, "message": str(e)} response = HttpResponse() response['Content-Type'] = "application/json" response.write(json.dumps(result, default=str).encode("UTF-8")) return response
def executemodifypasswd(request): getData = request.POST.get('getData', {}) radiovaule = request.POST.get('radiovaule', None) pwdLastSet = request.POST.get('pwdLastSet', None) add_passwd_count = request.POST.get('add_passwd_count', None) add_passwd1 = request.POST.get('add_passwd1', None) add_passwd2 = request.POST.get('add_passwd2', None) username = request.session.get('username') try: username = request.session.get('username') permess = Userperm(username, 'operate') if permess['isSuccess']: getDatatojson = json.loads(getData) if getDatatojson and radiovaule and (add_passwd1 == add_passwd2): modifyalluser = thrmodifyalluserpasswd(getDatatojson, username, radiovaule, pwdLastSet, add_passwd_count, add_passwd1) columns = [{ 'field': 'sAMAccountName', 'title': 'sAMAccountName' }, { 'field': 'password', 'title': 'password' }, { 'field': 'pwdLastSet', 'title': 'pwdLastSet' }] result = { 'isSuccess': True, "message": modifyalluser, 'columns': columns } else: result = { 'isSuccess': False, "message": '传入空值或2次密码不一致', } else: result = {'isSuccess': False, "message": '权限不足'} except Exception as e: result = {'isSuccess': False, "message": str(e)} insert_log(username, request, str(result['isSuccess']), str(result), '') response = HttpResponse() response['Content-Type'] = "text/javascript" response.write(json.dumps(result)) return response
def moveDnsToOu(request): dns = request.POST.getlist('dns[]') new_superior = request.POST.get('new_superior') try: username = request.session.get('username') permess = Userperm(username, 'operate') if permess['isSuccess']: result = dnMoveToOu(dns, new_superior) else: result = {'isSuccess': False, "message": '权限不足'} except Exception as e: result = {'isSuccess': False, "message": str(e)} insert_log(username, request, str(result['isSuccess']), str(result), '') response = HttpResponse() response['Content-Type'] = "text/javascript" response.write(json.dumps(result)) return response
def uploadfiletemplate(request): files = request.FILES.get('file') try: username = request.session.get('username') permess = Userperm(username, 'operate') if permess['isSuccess']: filename = handle_uploaded_file(files) data = xlrd.open_workbook(filename) table = data.sheet_by_index(0) nrows = table.nrows # 行数 ncols = table.ncols # 列数 heard = [] heardfield = table.row_values(0) #获取抬头 for c in heardfield: if c: heard.append({'field': c, 'title': c}) # 从第2行开始读取 EXCEL tabledata = [] for i in range(1, nrows): data1 = {} row_data = table.row_values(i) for j in range(0, len(heardfield)): if isinstance(row_data[j], str): row_data1 = row_data[j].replace(" ", '') else: row_data1 = row_data[j] data1.update({heardfield[j]: row_data1}) tabledata.append(data1) result = { 'isSuccess': True, "message": '', 'heard': heard, 'tabledata': tabledata } else: result = {'isSuccess': False, "message": '权限不足'} except Exception as e: result = {'isSuccess': False, "message": str(e)} insert_log(username, request, str(result['isSuccess']), str(result), '') response = HttpResponse() response['Content-Type'] = "text/javascript" response.write(json.dumps(result)) return response
def setObjectAttributes(request): distinguishedName = request.POST.get('distinguishedName') attributesName = request.POST.get('attributesName') attributesVaule = request.POST.get('attributesVaule') try: username = request.session.get('username') permess = Userperm(username, 'changelw') if permess['isSuccess']: result = setAccount(distinguishedName, attributesName, attributesVaule) else: result = {'isSuccess': False, "message": '权限不足'} except Exception as e: result = {'isSuccess': False, "message": str(e)} insert_log(username, request, str(result['isSuccess']), str(result), '') response = HttpResponse() response['Content-Type'] = "text/javascript" response.write(json.dumps(result)) return response
def deluserpermissions(request): ids = request.POST.getlist('ids', None) try: username = request.session.get('username') permess = Userperm(username, 'operate') if permess['isSuccess']: for id in ids: delect_apiusers_profiles = delect_apinamepermissions(id) if delect_apiusers_profiles: result = {'isSuccess': True, "message": str(ids) + '删除成功'} else: result = {'isSuccess': False, "message": str(ids) + '删除失败'} else: result = {'isSuccess': False, "message": '权限不足'} except Exception as e: result = {'isSuccess': False, "message": str(e)} insert_log(username, request, str(result['isSuccess']), str(result), '') response = HttpResponse() response['Content-Type'] = "text/javascript" response.write(json.dumps(result).encode("UTF-8")) return response
def modifyapiattributesleve(request): id = request.POST.get('add_api_apiname', None) attributes = request.POST.get('add_api_attributes', None) try: username = request.session.get('username') permess = Userperm(username, 'operate') if permess['isSuccess']: update_attributeslevels = update_attributeslevel(attributes, id) if update_attributeslevels: result = {'isSuccess': True, "message": '更新成功'} else: result = {'isSuccess': False, "message": '更新数据库失败'} else: result = {'isSuccess': False, "message": '权限不足'} except Exception as e: result = {'isSuccess': False, "message": str(e)} insert_log(username, request, str(result['isSuccess']), str(result), '') response = HttpResponse() response['Content-Type'] = "text/javascript" response.write(json.dumps(result).encode("UTF-8")) return response
def addorganizationalUnit(request): distinguishedName = request.POST.get('distinguishedName') cn = request.POST.get('cn') prevent = request.POST.get('checkbox') try: username = request.session.get('username') permess = Userperm(username, 'operate') if permess['isSuccess']: result = newOrganizationalUnit(distinguishedName, cn, prevent, attributes={}) else: result = {'isSuccess': False, "message": '权限不足'} except Exception as e: result = {'isSuccess': False, "message": str(e)} insert_log(username, request, str(result['isSuccess']), str(result), '') response = HttpResponse() response['Content-Type'] = "text/javascript" response.write(json.dumps(result)) return response
def creatalluser(request): get_user_distinguishedName = request.POST.get('get_user_distinguishedName', None) getData = request.POST.get('getData', {}) try: username = request.session.get('username') permess = Userperm(username, 'operate') if permess['isSuccess']: getDatatojson = json.loads(getData) creatallusers = thrcreatalluser(getDatatojson, get_user_distinguishedName) if creatallusers: if '' in getDatatojson[0]: getDatatojson[0].pop('') if '0' in getDatatojson[0]: getDatatojson[0].pop('0') attributesldap = list(creatallusers[0].keys()) columns = [] for c in range(0, len(attributesldap)): if not attributesldap[c] == 'status': columns.append({ 'field': attributesldap[c], 'title': attributesldap[c] }) result = { 'isSuccess': True, "message": creatallusers, "columns": columns } else: result = {'isSuccess': False, "message": '都未新建成功或者没有数据'} else: result = {'isSuccess': False, "message": '权限不足'} except Exception as e: result = {'isSuccess': False, "message": str(e)} insert_log(username, request, str(result['isSuccess']), str(result), '') response = HttpResponse() response['Content-Type'] = "text/javascript" response.write(json.dumps(result)) return response
def modifyapiuser(request): field = request.POST.get('field', None) row = request.POST.get('row', None) oldValue = request.POST.get('oldValue', None) password = request.POST.get('password', None) new_active = request.POST.get('new_active', None) try: username = request.session.get('username') permess = Userperm(username, 'operate') if permess['isSuccess']: if field == 'password': password = make_password(password) update_apiusers_profiles = update_apiusers_profile( row, field, password) elif field == 'is_active': update_apiusers_profiles = update_apiusers_profile( row, field, new_active) else: rowjson = json.loads(row) id = rowjson.get('id') attributesVaule = rowjson.get(field, None) update_apiusers_profiles = update_apiusers_profile( id, field, attributesVaule) if update_apiusers_profiles: result = {'isSuccess': True, "message": field + '更新成功'} else: result = {'isSuccess': False, "message": '更新失败'} else: result = {'isSuccess': False, "message": '权限不足'} except Exception as e: result = {'isSuccess': False, "message": str(e)} insert_log(username, request, str(result['isSuccess']), str(result), '') response = HttpResponse() response['Content-Type'] = "text/javascript" response.write(json.dumps(result).encode("UTF-8")) return response
def groupexport(request): username = request.session.get('username') filter = request.GET.get('filter') name = str(request.GET.get('samename')) """导出数据至excel""" # 如果是POST请求,则根据提交的表单数据,判断导出哪些数据 try: username = request.session.get('username') permess = Userperm(username, 'operate') if permess['isSuccess']: if request.method == 'GET': output = io.BytesIO() # 将xlsx数据写入数据流中 workbook = xlsxwriter.Workbook(output) worksheet = workbook.add_worksheet() # 创建一个工作表对象 # 定义数据表头列表 title = [u'账号', u'显示名称', u'工号', u'邮箱地址', u'GUID'] datas = getgroup(filter) format = workbook.add_format() # 定义format格式对象 format.set_border(1) # 定义format对象单元格边框加粗(1像素)的格式 format_title = workbook.add_format() # 定义format_title格式对象 format_title.set_border(1) # 定义format_title对象单元格边框加粗(1像素)的格式 format_title.set_bg_color( '#cccccc') # 定义format_title对象单元格背景颜色为 # '#cccccc'的格式 format_title.set_align('center') # 定义format_title对象单元格居中对齐的格式 format_title.set_bold() # 定义format_title对象单元格内容加粗的格式 format_ave = workbook.add_format() # 定义format_ave格式对象 format_ave.set_border(1) # 定义format_ave对象单元格边框加粗(1像素)的格式 format_ave.set_num_format('0.00') # 定义format_ave对象单元格数字类别显示格式 # 下面分别以行或列写入方式将标题、业务名称、流量数据写入起初单元格,同时引用不同格式对象 worksheet.set_column('A:J', 19) worksheet.write_row('A1', title, format_title) nowdate = datetime.now().strftime('%Y-%m-%d') if datas['isSuccess']: deatmessage = datas['message'] i = 2 for data22 in deatmessage: date = (str(data22['sAMAccountName']), str(data22['displayName']), str(data22['wWWHomePage']), str(data22['mail']), str(data22['physicalDeliveryOfficeName'])) row = 'A' + str(i) worksheet.write_row(row, date, format) i = i + 1 workbook.close() # 将数据流的游标指向起始位置 output.seek(0) result = {'isSuccess': True, "message": name + '组成员'} insert_log(username, request, str(result['isSuccess']), str(result), '') response = HttpResponse( output.read(), content_type= "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" ) response[ 'Content-Disposition'] = 'attachment; filename="{0}{1}.xlsx"'.format( name + '组成员', nowdate).encode('utf-8') return response else: result = {'isSuccess': False, "message": '导出出现异常'} insert_log(username, request, str(result['isSuccess']), str(result), '') response = HttpResponse( output.read(), content_type= "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" ) response[ 'Content-Disposition'] = 'attachment; filename="{0}{1}.xlsx"'.format( '导出出现异常', nowdate).encode('utf-8') return response else: result = {'isSuccess': False, "message": '权限不足'} except Exception as e: result = {'isSuccess': False, "message": str(e)} insert_log(username, request, str(result['isSuccess']), str(result), '') response = HttpResponse() response['Content-Type'] = "text/javascript" response.write(json.dumps(result)) return response
def exportfiletemplate(request): userPrincipalName = request.GET.get('userPrincipalName', '') cn = request.GET.get('cn', '') givenName = request.GET.get('givenName') initials = request.GET.get('initials') sn = request.GET.get('sn') displayName = request.GET.get('displayName') description = request.GET.get('description') physicalDeliveryOfficeName = request.GET.get('physicalDeliveryOfficeName') telephoneNumber = request.GET.get('telephoneNumber') mail = request.GET.get('mail') maildb = request.GET.get('maildb') wWWHomePage = request.GET.get('wWWHomePage') password = request.GET.get('password') userAccountControl = request.GET.get('userAccountControl') template = request.GET.get('template', '用户模板') try: username = request.session.get('username') permess = Userperm(username, 'operate') if permess['isSuccess']: """导出数据至excel""" # 如果是POST请求,则根据提交的表单数据,判断导出哪些数据 output = io.BytesIO() # 将xlsx数据写入数据流中 workbook = xlsxwriter.Workbook(output) worksheet = workbook.add_worksheet() # 创建一个工作表对象 # 定义数据表头列表 format = workbook.add_format() # 定义format格式对象 format.set_border(1) # 定义format对象单元格边框加粗(1像素)的格式 format_title = workbook.add_format() # 定义format_title格式对象 format_title.set_border(1) # 定义format_title对象单元格边框加粗(1像素)的格式 format_title.set_bg_color('#cccccc') # 定义format_title对象单元格背景颜色为 # '#cccccc'的格式 format_title.set_align('center') # 定义format_title对象单元格居中对齐的格式 format_title.set_bold() # 定义format_title对象单元格内容加粗的格式 format_ave = workbook.add_format() # 定义format_ave格式对象 format_ave.set_border(1) # 定义format_ave对象单元格边框加粗(1像素)的格式 format_ave.set_num_format('0.00') # 定义format_ave对象单元格数字类别显示格式 # 下面分别以行或列写入方式将标题、业务名称、流量数据写入起初单元格,同时引用不同格式对象 worksheet.set_column('A:O', 25) ldapattributess = ldapattributes( userPrincipalName=userPrincipalName, cn=cn, givenName=givenName, initials=initials, sn=sn, displayName=displayName, description=description, physicalDeliveryOfficeName=physicalDeliveryOfficeName, telephoneNumber=telephoneNumber, mail=mail, maildb=maildb, wWWHomePage=wWWHomePage, password=password, userAccountControl=userAccountControl, ) worksheet.write_row('A1', ldapattributess['titleldap'], format_title) nowdate = datetime.now().strftime('%Y-%m-%d') workbook.close() # 将数据流的游标指向起始位置 output.seek(0) response = HttpResponse( output.read(), content_type= "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" ) response[ 'Content-Disposition'] = 'attachment; filename="{0}.xlsx"'.format( template).encode('utf-8') return response else: result = {'isSuccess': False, "message": '权限不足'} except Exception as e: result = {'isSuccess': False, "message": str(e)}
def exportListToOU(request): distinguishedName = request.GET.get('distinguishedName') search_scope = request.GET.get('search_scope') try: username = request.session.get('username') permess = Userperm(username, 'operate') if permess['isSuccess']: if distinguishedName: if search_scope == 'true': search_scope = 'SUBTREE' else: search_scope = 'LEVEL' """导出数据至excel""" # 如果是POST请求,则根据提交的表单数据,判断导出哪些数据 output = io.BytesIO() # 将xlsx数据写入数据流中 workbook = xlsxwriter.Workbook(output) worksheet = workbook.add_worksheet() # 创建一个工作表对象 # 定义数据表头列表 title = [u'名称', u'登录名', u'类型', u'描述', u'显示名称', u'路径'] datas = getObjectToDn(distinguishedName, search_scope) name = distinguishedName.split(',')[0].split('=')[1] format = workbook.add_format() # 定义format格式对象 format.set_border(1) # 定义format对象单元格边框加粗(1像素)的格式 format_title = workbook.add_format() # 定义format_title格式对象 format_title.set_border(1) # 定义format_title对象单元格边框加粗(1像素)的格式 format_title.set_bg_color( '#cccccc') # 定义format_title对象单元格背景颜色为 # '#cccccc'的格式 format_title.set_align('center') # 定义format_title对象单元格居中对齐的格式 format_title.set_bold() # 定义format_title对象单元格内容加粗的格式 format_ave = workbook.add_format() # 定义format_ave格式对象 format_ave.set_border(1) # 定义format_ave对象单元格边框加粗(1像素)的格式 format_ave.set_num_format('0.00') # 定义format_ave对象单元格数字类别显示格式 # 下面分别以行或列写入方式将标题、业务名称、流量数据写入起初单元格,同时引用不同格式对象 worksheet.set_column('A:J', 19) worksheet.set_column('F:F', 60) worksheet.write_row('A1', title, format_title) nowdate = datetime.now().strftime('%Y-%m-%d') if datas['isSuccess']: deatmessageList = datas['message'] i = 2 for attributes in deatmessageList: date = (str(attributes['name']), str(attributes['sAMAccountName']), str( objectClassFrom( attributes['objectClass'], attributes['groupType'])['types']), str(attributes['description']), str(attributes['displayName']), str(attributes['distinguishedName'])) row = 'A' + str(i) worksheet.write_row(row, date, format) i = i + 1 workbook.close() # 将数据流的游标指向起始位置 output.seek(0) result = {'isSuccess': True, "message": name + '导出列表'} insert_log(username, request, str(result['isSuccess']), str(result), '') response = HttpResponse( output.read(), content_type= "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" ) response[ 'Content-Disposition'] = 'attachment; filename="{0}{1}.xlsx"'.format( name + '导出列表', nowdate).encode('utf-8') return response else: result = {'isSuccess': False, "message": '导出出现异常'} insert_log(username, request, str(result['isSuccess']), str(result), '') response = HttpResponse( output.read(), content_type= "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" ) response[ 'Content-Disposition'] = 'attachment; filename="{0}{1}.xlsx"'.format( '导出出现异常', nowdate).encode('utf-8') return response else: result = {'isSuccess': False, "message": '权限不足'} except Exception as e: result = {'isSuccess': False, "message": str(e)}