# -*- coding: utf-8 -*-
from django.contrib.auth.models import User
from django.db.models import Q
from student.models import UserProfile
from openedx.core.djangoapps.ccnu.models import Org
SCHOOL_TUPLE = (
(u'国家数字化学习工程技术研究中心', u'ccnu'),
(u'华中师范大学第一附属中学(初中部)', u'hsyfzcz'),
(u'华中师范大学第一附属中学(高中部)', u'hsyfzgz'),
(u'武汉实验外国语学校', u'whsywgy'),
(u'湖北黄冈中学', u'hgzx'),
(u'武汉市第二中学', u'whzz'),
(u'襄阳市第四中学(襄阳四中)', u'xysz'),
(u'襄阳市第五中学(襄阳五中)', u'xywz'),
(u'湖北省孝感高级中学', u'xggjzx'),
(u'武汉市第一商业学校', u'whysx'),
)
TEACHER_OR_STUDENT_PASSWORD = u'whysx123456'
def account_create(username, email, profile_name, org, password=TEACHER_OR_STUDENT_PASSWORD):
user = User.objects.filter(Q(username=username) | Q(email=email)).first()
if user:
print u'USER EXISTS: {username} - {email}: exists'.format(username=username, email=email)
return user
else:
user = User(
username=username,
email=email,
is_active=True
)
user.set_password(password)
user.save()
profile = UserProfile(
user=user,
name=profile_name,
org=org
)
profile.save()
print u'USER CREATED: {username} - {email}: created'.format(username=username, email=email)
return user
def org_create(org_code, org_name):
org = Org.objects.filter(Q(code=org_code) | Q(name=org_name)).first()
if org:
print u'exists: {org_code} - {org_name}'.format(org_code=org_code, org_name=org_name)
return org
else:
org = Org.objects.create(
code=org_code,
name=org_name
)
print u'org_create: {org_code} - {org_name}: '.format(org_code=org_code, org_name=org_name)
return org
def gen_schools():
# gen school admin accounts
for org_name, org_code in SCHOOL_TUPLE:
# gen school
org_create(org_code, org_name)
def gen_students(school_type, org):
account_attrs = clear_students_account(school_type, account_type='student', max_num=300)
for username, email, profile_name in account_attrs:
account_create(username, email, profile_name, org)
def gen_teachers(school_type, org):
account_attrs = clear_teachers_account(school_type, account_type='teacher', max_num=150)
for username, email, profile_name in account_attrs:
user = account_create(username, email, profile_name, org)
from cms.djangoapps.course_creators.models import CourseCreator
cc, created = CourseCreator.objects.get_or_create(user=user)
if created:
cc.state = CourseCreator.GRANTED
admin = User.objects.filter(is_superuser=True).first()
cc.admin = admin
cc.save()
from student import auth
from student.roles import CourseCreatorRole
auth.add_users(admin, CourseCreatorRole(), user)
def clear_students_account(school_type, account_type, max_num):
"""
return: [(username, email, profile_name), ]
"""
results = []
for i in range(1, max_num + 1):
username = '%s_%s%02d' % (school_type, account_type, i)
email = username + '@ccnu.com'
profile_name = username
account = (username, email, profile_name)
results.append(account)
return results
def clear_teachers_account(school_type, account_type, max_num):
"""
Returns:
[(username, email, profile_name), ]
"""
results = []
for i in range(1, max_num + 1):
username = '%s_%s%02d' % (school_type, account_type, i)
email = username + '@ccnu.com'
profile_name = username
account = (username, email, profile_name)
results.append(account)
return results
def main():
"""
之前解决办法:
a = Org.objects.filter(code='whysx').first()
UserProfile.objects.filter(name__icontains='whysx_').update(org=a)
:return:
"""
import time
s = time.time()
school_type = 'whysx'
# gen org
gen_schools()
# get org
org = Org.objects.filter(code=school_type).first()
# gen students
gen_students(school_type, org)
# gen_teachers
gen_teachers(school_type, org)
e = time.time()
print 'LASTS %s' % (e - s)
main()
batch_accounts_create_scripts
最新推荐文章于 2023-10-19 17:47:22 发布