def update(request, teacher_id, template_name="oa/teacher_form.html"): """update a teacher""" schools = helpers.get_schools(request.user) teacher = get_object_or_404(Teacher, pk=teacher_id, school__in=schools) roles = Role.objects.all() role_pks = [r.id for r in teacher.user.roles.all()] user_role_pks = ','.join([str(r.pk) for r in teacher.user.roles.all()]) school = teacher.school try: postjob = teacher.postjob except: postjob = PostJob(teacher=teacher,school=school) postjob.save() if request.method == 'POST': password = teacher.user.password form1 = TeacherUserForm(request.POST,instance=teacher.user) form2 = UserProfileForm(request.POST, request.FILES,instance=teacher.user.get_profile()) form3 = PostJobForm(request.POST,instance=teacher.postjob) error_list = form1.errors.items() + form2.errors.items() + form3.errors.items() if request.is_ajax(): return helpers.ajax_validate_form_error_list(error_list) print form1.errors,'11111111' print form2.errors,'22222222' print form3.errors,'33333333' if form1.is_valid() and form2.is_valid() and form3.is_valid(): username = form1.clean_username() if username: teacher.user.username = username teacher.user.password = password teacher.user.save() profile = form2.save(commit=False) realname = request.POST['realname'] if realname: teacher.name = realname teacher.save() form3.save() role_pks = request.POST.getlist('role_list') role_list = [r for r in Role.objects.filter(pk__in=role_pks)] teacher.user.roles = role_list messages.success(request, u"教师 %s 已成功修改" % teacher.name) return redirect("oa_teacher_list") else: form1 = TeacherUserForm(instance=teacher.user) form2 = UserProfileForm(instance=teacher.user.get_profile()) form3 = PostJobForm(instance=teacher.postjob,user=request.user) ctx = {'form1': form1,'form2': form2,'form3': form3,'teacher':teacher,\ 'user_role_pks':user_role_pks,'roles':roles,'role_pks':role_pks} ctx.update(csrf(request)) return render(request, template_name, ctx)
def create(request, template_name="oa/teacher_form.html"): roles = Role.objects.all() if request.method == 'POST': form1 = TeacherUserForm(request.POST) is_auto = request.POST.get('signup') is_valid = form1.is_valid() form1_error_list = form1.errors.items() if is_auto == "auto": form1_error_list = [] is_valid = True print is_valid,'v11111111111' form2 = UserProfileForm(request.POST, request.FILES) print form2.errors,'2222222' form3 = PostJobForm(request.POST,user=request.user) print form3.errors,'3333333' error_list = form1_error_list + form2.errors.items() + form3.errors.items() if request.is_ajax(): return helpers.ajax_validate_form_error_list(error_list) if is_valid and form2.is_valid() and form3.is_valid(): #新建用户 teacher = helpers.create_teacher(form1, request) print teacher,'vvvvvvvv' role_pks = request.POST.getlist('role_list') role_list = [r for r in Role.objects.filter(pk__in=role_pks)] if teacher.id: for role in role_list: teacher.user.roles.add(role) form2 = UserProfileForm(request.POST, request.FILES, instance=teacher.user.get_profile()) form2.save() form3 = PostJobForm(request.POST,user=request.user) postjob = form3.save(commit=False) postjob.teacher = teacher postjob.save() messages.success(request, u"教师 %s 已成功创建" % teacher.name) return redirect("oa_teacher_list") else: form1 = TeacherUserForm() form2 = UserProfileForm() form3 = PostJobForm(user=request.user) # form4 = TeachernameForm() ctx = {'form1': form1,'form2': form2,'form3': form3,'roles':roles} ctx.update(csrf(request)) return render(request, template_name, ctx)