#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
说明:
1. 两个用户邮箱重复,则加入问题数组,提示信息
2. 邮箱需要验证, 密码若为空,则默认给123456
3. 前三个属性若为空,则加入问题数组,提示信息
"""
# start django 项目中 单独执行脚本必须添加的
import json
from django.core.wsgi import get_wsgi_application
os.environ['DJANGO_SETTINGS_MODULE'] = 'web.settings_local'
application = get_wsgi_application()
# end
import re
from pyexcel_xls import get_data
from accounts_create import account_create
import os
def 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 result
def 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 False
def 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_datas
def 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