batch_accounts_create_scripts

# -*- 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()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值