batch_account_create script

来源:互联网 发布:php怎么防止sql注入 编辑:程序博客网 时间:2024/05/24 05:52
#!/usr/bin/env python# -*- coding: utf-8 -*-"""说明:    1. 两个用户邮箱重复,则加入问题数组,提示信息    2. 邮箱需要验证, 密码若为空,则默认给123456    3. 前三个属性若为空,则加入问题数组,提示信息"""# start  django 项目中 单独执行脚本必须添加的import jsonfrom django.core.wsgi import get_wsgi_applicationos.environ['DJANGO_SETTINGS_MODULE'] = 'web.settings_local'application = get_wsgi_application()# endimport refrom pyexcel_xls import get_datafrom accounts_create import account_createimport osdef read_xls_file(path):    """    读取excel中数据    :param path:    :return:    """    result = []    xls_data = get_data(path)    for sheet_n in xls_data.keys():        result = xls_data[sheet_n]    return resultdef verif_email(origin):    """    验证邮箱    :param origin:    :return:    """    return True if re.match(        "^.+\\@("        "\\[?)[a-zA-Z0-9\\-\\_\\.]"        "+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$",        origin) else Falsedef format_data(path):    """    格式化数据    :param path:    :return:    """    result = read_xls_file(path=path)    format_datas = []    fine_datas = []    question_datas = []    temp_datas = {}    for i in xrange(1, len(result)):        if result[i][0] and result[i][1] and result[i][2]:            if verif_email(result[i][2]):                if len(result[i]) == 4:                    format_datas.append({                        "username": result[i][0],                        "profile_name": result[i][1],                        "email": result[i][2],                        "password": result[i][3]                    })                else:                    format_datas.append({                        "username": result[i][0],                        "profile_name": result[i][1],                        "email": result[i][2],                        "password": '123456'                    })            else:                print "第{0}行邮箱格式错误!".format(i + 1)                question_datas.append(result[i])        else:            print "第{0}行为无效用户!".format(i + 1)            question_datas.append(result[i])    for item in format_datas:        temp_datas[item.get("email")] = filter(            lambda x: x.get("email") == item.get("email"),            format_datas        )    for k, v in temp_datas.iteritems():        if len(v) != 1:            print "{0} 邮箱重复,为无效用户!".format(k)            question_datas.append(v)        else:            fine_datas.append(v)    return question_datas, fine_datasdef create_user(path):    """    批量创建用户    :param path:    :return:    """    question_datas, fine_datas = format_data(path=path)    for i in fine_datas:        account_create(            username=i[0].get("username"),            email=i[0].get("email"),            profile_name=i[0].get("profile_name"),            password=i[0].get("password")        )if __name__ == "__main__":    path = '20172.xlsx'    create_user(path=path)

python manage.py lms shell --settings=aws
from openedx.core.djangoapps.ccnu.scripts import batch_account_create


原创粉丝点击