Ejemplo n.º 1
0
def user_added(request):
    c = {}
    c.update(csrf(request))
    print(request.POST)
    usertype = request.POST['user_type']
    userid = request.POST['user_id']
    userpasswd = '123456'  #default
    print("haha")
    user = User.objects.create_user(userid, userid + '@zju.edu.cn', userpasswd)
    if usertype == 'Admin':
        user.user_permissions.add()
    elif usertype == 'Student':
        if len(userid) != 10:
            c.update({'idLenInvalid': 1})
        else:
            newStudent = Student_user(id=userid)
            newStudent.save()
            #print 'newStudent:',newStudent
    elif usertype == 'Faculty':
        pass
        #TODO add a faculty to database
    user.save()
    return render_to_response('add_user.html', c)
Ejemplo n.º 2
0
def user_added(request):
    c = {}
    c.update(csrf(request))
    print(request.POST)
    usertype = request.POST['user_type']
    userid = request.POST['user_id']
    userpasswd = '123456' #default
    print("haha")
    user = User.objects.create_user(userid, userid+'@zju.edu.cn', userpasswd)
    if usertype == 'Admin':
        user.user_permissions.add()
    elif usertype == 'Student':
        if len(userid) != 10:
            c.update({'idLenInvalid':1})
        else:
            newStudent = Student_user(id=userid)
            newStudent.save()
            #print 'newStudent:',newStudent
    elif usertype == 'Faculty':
        pass
        #TODO add a faculty to database
    user.save()
    return render_to_response('add_user.html', c)
Ejemplo n.º 3
0
def studentAdd(request):
    group = Group.objects.get(name='student')
    errors = []
    errorImport = []
    addIsDone = False
    userCollege = ""
    if Admin_user.objects.filter(id = request.user.username):
        userCollege = Admin_user.objects.filter(id = request.user.username)[0].college
    elif Faculty_user.objects.filter(id = request.user.username):
        userCollege = Faculty_user.objects.filter(id = request.user.username)[0].college
    else:
        userCollege = Student_user.objects.filter(id = request.user.username)[0].college
    isSuper = False
    if userCollege == 'all':
        isSuper = True

    #get all colleges from Student_user
    stu_colleges = Student_user.objects.all()
    stu_colleges = list(set([row.college for row in stu_colleges]))

    if request.method  ==   'POST':
        if request.POST.get('multiAddCancle') or request.POST.get('first'): #click cancle button or first access
                form = StudentForm(initial = {'college' : userCollege})
        elif 'file' in request.POST and len(request.POST.get('file')) > 0:  # click confirm button
            fileTerms = re.split(',', request.POST.get('file'))
            for x in range(0, len(fileTerms) / LEN_OF_STUDENT_TABLE):
                dbQuery = Student_user(
                    id = fileTerms[0 + LEN_OF_STUDENT_TABLE * x].encode('utf-8'),
                    contact = fileTerms[1 + LEN_OF_STUDENT_TABLE * x].encode('utf-8'),
                    name = fileTerms[2 + LEN_OF_STUDENT_TABLE * x].encode('utf-8'),
                    gender = fileTerms[3 + LEN_OF_STUDENT_TABLE * x].encode('utf-8'),
                    college = fileTerms[4 + LEN_OF_STUDENT_TABLE * x].encode('utf-8'),
                    major = fileTerms[5 + LEN_OF_STUDENT_TABLE * x].encode('utf-8'),
                    grade = fileTerms[6 + LEN_OF_STUDENT_TABLE * x].encode('utf-8'),
                    gpa = fileTerms[7 + LEN_OF_STUDENT_TABLE * x].encode('utf-8'),
                    credits = fileTerms[8 + LEN_OF_STUDENT_TABLE * x].encode('utf-8')
                )
                state = importStudentCheck(fileTerms[0 + LEN_OF_STUDENT_TABLE * x : LEN_OF_STUDENT_TABLE + LEN_OF_STUDENT_TABLE * x])
                if state == 'YEAH'.encode('utf-8'):
                    user = User.objects.create_user(dbQuery.id, dbQuery.id+"@zju.edu.cn", "123456")
                    user.groups.add(group)
                    dbQuery.save()
                else:
                    errorExist = True
                    returnListItem = fileTerms[0 + LEN_OF_STUDENT_TABLE * x : LEN_OF_STUDENT_TABLE + LEN_OF_STUDENT_TABLE * x]
                    returnListItem.append(state)
                    errorImport.append(returnListItem)
            addIsDone = True
            form = StudentForm(initial = {'college' : userCollege})
        elif request.FILES.get('file'):  # dealing with upload
            fileLocation = request.FILES.get('file')
            fileTerms = re.split(',|\n', fileLocation.read())
            multiAdd = True
            terms = []
            for x in range(0, len(fileTerms) / LEN_OF_STUDENT_TABLE):
                terms.append(
                    fileTerms[0 + x * LEN_OF_STUDENT_TABLE: LEN_OF_STUDENT_TABLE + x * LEN_OF_STUDENT_TABLE])
        else:  # regular form submit
            form = StudentForm(request.POST)
            if form.is_valid():
                info = form.cleaned_data
                dbQuery = Student_user(
                    id = info['id'],
                    contact = info['contact'],
                    name = info['name'],
                    gender = info['gender'],
                    college = info['college'],
                    major = info['major'],
                    grade = info['grade'],
                    gpa = info['gpa'],
                    credits = info['credits'],
                    isSpecial = info['isSpecial'],
                    photo = info['photo']
                )
                dbQuery.save()
                user = User.objects.create_user(dbQuery.id, dbQuery.id+"@zju.edu.cn", "123456")
                user.groups.add(group)
                if dbQuery.isSpecial:
                    if info['canManageCourses']:
                        perm = Permission.objects.get(codename='course_manage')
                        user.user_permissions.add(perm)
                    if info['canManageStudents']:
                        perm = Permission.objects.get(codename='student_manage')
                        user.user_permissions.add(perm)
                    if info['canManageFaculties']:
                        perm = Permission.objects.get(codename='faculty_manage')
                        user.user_permissions.add(perm)
                addIsDone = True
                form = StudentForm(initial = {'college' : userCollege})
        return render(request, 'AddStudent.html', locals())
    return render(request, 'AccessFault.html')
Ejemplo n.º 4
0
def studentAdd(request):
    errors = []
    errorImport = []
    addIsDone = False
    userCollege = ""
    if Admin_user.objects.filter(id=request.user.username):
        userCollege = Admin_user.objects.filter(
            id=request.user.username)[0].college
    elif Faculty_user.objects.filter(id=request.user.username):
        userCollege = Admin_user.objects.filter(
            id=request.user.username)[0].college
    else:
        userCollege = Student_user.objects.filter(
            id=request.user.username)[0].college
    isSuper = False
    if userCollege == 'all':
        isSuper = True

    if request.method == 'POST':
        if request.POST.get('multiAddCancle') or request.POST.get(
                'first'):  #click cancle button or first access
            form = StudentForm(initial={'college': userCollege})
        elif 'file' in request.POST and len(
                request.POST.get('file')) > 0:  # click confirm button
            fileTerms = re.split(',', request.POST.get('file'))
            for x in range(0, len(fileTerms) / LEN_OF_STUDENT_TABLE):
                dbQuery = Student_user(
                    id=fileTerms[0 + LEN_OF_STUDENT_TABLE * x].encode('utf-8'),
                    contact=fileTerms[1 + LEN_OF_STUDENT_TABLE *
                                      x].encode('utf-8'),
                    name=fileTerms[2 +
                                   LEN_OF_STUDENT_TABLE * x].encode('utf-8'),
                    gender=fileTerms[3 +
                                     LEN_OF_STUDENT_TABLE * x].encode('utf-8'),
                    college=fileTerms[4 + LEN_OF_STUDENT_TABLE *
                                      x].encode('utf-8'),
                    major=fileTerms[5 +
                                    LEN_OF_STUDENT_TABLE * x].encode('utf-8'),
                    grade=fileTerms[6 +
                                    LEN_OF_STUDENT_TABLE * x].encode('utf-8'),
                    gpa=fileTerms[7 +
                                  LEN_OF_STUDENT_TABLE * x].encode('utf-8'),
                    credits=fileTerms[8 + LEN_OF_STUDENT_TABLE *
                                      x].encode('utf-8'))
                state = importStudentCheck(
                    fileTerms[0 +
                              LEN_OF_STUDENT_TABLE * x:LEN_OF_STUDENT_TABLE +
                              LEN_OF_STUDENT_TABLE * x])
                if state == 'YEAH'.encode('utf-8'):
                    user = User.objects.create_user(dbQuery.id,
                                                    dbQuery.id + "@zju.edu.cn",
                                                    "123456")
                    dbQuery.save()
                else:
                    errorExist = True
                    returnListItem = fileTerms[0 + LEN_OF_STUDENT_TABLE *
                                               x:LEN_OF_STUDENT_TABLE +
                                               LEN_OF_STUDENT_TABLE * x]
                    returnListItem.append(state)
                    errorImport.append(returnListItem)
            addIsDone = True
            form = StudentForm(initial={'college': userCollege})
        elif request.FILES.get('file'):  # dealing with upload
            fileLocation = request.FILES.get('file')
            fileTerms = re.split(',|\n', fileLocation.read())
            multiAdd = True
            terms = []
            for x in range(0, len(fileTerms) / LEN_OF_STUDENT_TABLE):
                terms.append(
                    fileTerms[0 +
                              x * LEN_OF_STUDENT_TABLE:LEN_OF_STUDENT_TABLE +
                              x * LEN_OF_STUDENT_TABLE])
        else:  # regular form submit
            form = StudentForm(request.POST)
            if form.is_valid():
                info = form.cleaned_data
                dbQuery = Student_user(id=info['id'],
                                       contact=info['contact'],
                                       name=info['name'],
                                       gender=info['gender'],
                                       college=info['college'],
                                       major=info['major'],
                                       grade=info['grade'],
                                       gpa=info['gpa'],
                                       credits=info['credits'],
                                       isSpecial=info['isSpecial'],
                                       photo=info['photo'])
                dbQuery.save()
                user = User.objects.create_user(dbQuery.id,
                                                dbQuery.id + "@zju.edu.cn",
                                                "123456")
                if dbQuery.isSpecial:
                    if info['canManageCourses']:
                        perm = Permission.objects.get(codename='course_manage')
                        user.user_permissions.add(perm)
                    if info['canManageStudents']:
                        perm = Permission.objects.get(
                            codename='student_manage')
                        user.user_permissions.add(perm)
                    if info['canManageFaculties']:
                        perm = Permission.objects.get(
                            codename='faculty_manage')
                        user.user_permissions.add(perm)
                addIsDone = True
                form = StudentForm(initial={'college': userCollege})
        return render(request, 'AddStudent.html', locals())
    return render(request, 'AccessFault.html')