Example #1
0
def create_agency_user(request):
    """生成集团用户及集团"""
    school = School()
    school.creator_id = 1
    school.parent_id = 0
    school.save()
   
    prefix = 'u'
    latest = User.objects.latest('id')
    username = "******" % (prefix, latest.id + 1)
    if User.objects.filter(username = username).count():
        username += '_'+str(random.randint(1,999))
    password = 123456
    user = User.objects.create_user(username, '', password)
    data = {'username':username,'password':password}
    teacher = Teacher()
    teacher.user_id = user.id
    teacher.creator_id = 1
    teacher.school_id = school.id
    teacher.save()
    
    role,created = Role.objects.get_or_create(school_id=0,name='集团管理员')
#    role = Role()
#    role.school_id = school.id
#    role.name = "集团管理员"
#    role.save()
    access_list = [a for a in Access.objects.all()]
    role.accesses = access_list
    
    roles = Role.objects.filter(pk=role.id)
    user.roles = roles
    
    return HttpResponse('username:'******',' + 'password:' + str(password))
Example #2
0
def create_teacher(form, request, school=None,batch=False):
    """
    * *form:* 老师的 *form*
    * *request:* 当次访问的 ``request``对象
    * *school:* 角色所在学校
    """
    try:
        password = form.cleaned_data['password']
    except:
        password = 123456
    try:
        username = form.cleaned_data['username']
    except:
        username = None
    try:
        school_id = request.POST['school']
        school = get_object_or_404(School,pk=school_id)
    except:
        school = school if school else None
#     raise
    if batch:
        user = create_user(username=username,password=password,batch=batch)
    else:
        user = School.userObjects.create_user(username=username,password=password)
    
    user.first_name = user.username
    user.save()
    
    f = StarFigure()
    f.user = user
    f.save()
    
    teacher = Teacher()
    teacher.user = user
    teacher.creator = request.user
    try:
        name = request.POST['realname']
    except:
        name = ''
    if name:
        teacher.name = name
    else:
        teacher.name = user.first_name or user.username
    teacher.school = school or get_schools(user)[0]
    teacher.save()
    set_teacher_default_access(teacher)
    return teacher