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 account_setting(request, template_name="oa/account_setting.html"): """个人设置""" if request.method == 'POST': password = request.user.password form1 = TeacherUserForm(request.POST,instance=request.user) form2 = UserProfileForm(request.POST, request.FILES,instance=request.user.get_profile()) if form1.is_valid() and form2.is_valid(): u = form1.save(commit=False) u.password = password u.save() profile = form2.save(commit=False) realname = request.POST['realname'] profile.realname = realname profile.save() if helpers.is_teacher(request.user): teacher = request.user.teacher teacher.name = profile.realname teacher.save() if helpers.is_student(request.user): student = request.user.student student.name = profile.realname student.save() messages.success(request, _('Your profile has been updated.')) return redirect("oa_account_setting") else: form1 = TeacherUserForm(instance=request.user) form2 = UserProfileForm(instance=request.user.get_profile()) try: postjob = request.user.teacher.postjob except: postjob = None ctx = {'form1': form1,'form2': form2,'postjob':postjob} 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)
def create(request, template_name="oa/student_form.html"): """添加学籍""" extra = int(request.GET.get("extra", 1)) 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 form2 = UserProfileForm(request.POST, request.FILES) form3 = OaStudentForm(request.POST,user=request.user) extra = request.POST['form-TOTAL_FORMS'] formset = formset_factory(GuardianForm, extra=extra) form4 = formset(request.POST) form5 = BirthControlForm(request.POST) print form2.errors,'22222222' print form3.errors,'3333333' print form4.errors,'444444444' print form5.errors,'55555555' form4_error_list = [] for fo in form4: form4_error_list = form4_error_list + fo.errors.items() error_list = form1_error_list + form2.errors.items() + form3.errors.items() + form4_error_list + form5.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() and form4.is_valid() and form5.is_valid(): student = helpers.create_student(form1, request) if student.id: form2 = UserProfileForm(request.POST, request.FILES, instance=student.user.get_profile()) form2.save() form3 = OaStudentForm(request.POST,instance=student,user=None) form3.save() for f in form4: guardian = f.save(commit=False) if guardian.name == "empty_name" and guardian.mobile == "13900000000" and guardian.address == "empty_address": pass else: if guardian.name: guardian.student = student guardian.save() birthcontrol = form5.save(commit=False) birthcontrol.student = student birthcontrol.save() profile = student.user.profile if not profile.mobile: try: mobile = Guardian.objects.filter(student=student)[0].mobile profile.mobile = mobile profile.save() except: pass messages.success(request, u"学生 %s 已成功创建" % student.name) return redirect("oa_student_list") else: form1 = TeacherUserForm() form2 = UserProfileForm() form3 = OaStudentForm(user=request.user) form4 = formset_factory(GuardianForm, extra=extra) form5 = BirthControlForm() ctx = {'form1': form1,'form2': form2,'form3': form3,'form4':form4,'form5':form5,'extra':extra} ctx.update(csrf(request)) return render(request, template_name, ctx)
def update(request, student_id, template_name="oa/student_form.html"): """更新学籍""" extra = int(request.GET.get("extra", 0)) schools = helpers.get_schools(request.user) schools = [s for s in schools if not s.parent_id==0] student = get_object_or_404(Student, pk=student_id,school__in=schools) school = student.school guardians = student.guardians.all() guardian_list = [] for g in guardians: guardian_list.append({'name':g.name,'relation':g.relation,'mobile':g.mobile,'office_phone':g.office_phone,\ 'other_phone':g.other_phone,'office_email':g.office_email,'other_email':g.other_email,'address':g.address}) if not guardians.count(): extra = 1 if request.method == 'POST': password = student.user.password form1 = TeacherUserForm(request.POST,instance=student.user) form2 = UserProfileForm(request.POST, request.FILES, instance=student.user.get_profile()) form3 = OaStudentForm(request.POST,user=request.user,instance=student) extra = request.POST['form-TOTAL_FORMS'] formset = formset_factory(GuardianForm, extra=extra) form4 = formset(request.POST,initial=guardian_list) form5 = BirthControlForm(request.POST,instance=student.birth) form4_error_list = [] for fo in form4: form4_error_list = form4_error_list + fo.errors.items() error_list = form2.errors.items() + form2.errors.items() + form3.errors.items() + form4_error_list + form5.errors.items() # if request.is_ajax(): # return helpers.ajax_validate_form_error_list(error_list) if form1.is_valid() and form2.is_valid() and form3.is_valid() and form4.is_valid(): username = form1.clean_username() if username: student.user.username = username student.user.password = password student.user.save() form2.save() form3 = OaStudentForm(request.POST,instance=student,user=request.user) student = form3.save(commit=False) realname = request.POST['realname'] if realname: student.name = realname student.save() guardians.delete() for f in form4: guardian = f.save(commit=False) if guardian.name == "empty_name" and guardian.mobile == "13900000000" and guardian.address == "empty_address": pass else: if guardian.name: guardian.student = student guardian.save() form5.save() messages.success(request, u"学生 %s 已成功修改" % student.name) return redirect("oa_student_list") else: form1 = TeacherUserForm(instance=student.user) form2 = UserProfileForm(instance=student.user.get_profile()) form3 = OaStudentForm(instance=student,user=request.user) formset = formset_factory(GuardianForm,extra=extra) form4 = formset(initial=guardian_list) try: birth = student.birth except: birth = BirthControl() birth.student = student birth.save() form5 = BirthControlForm(instance=birth) # print form3['group'],form3['school'] ctx = {'form1': form1,'form2': form2,'form3': form3,'form4':form4,'form5':form5,'extra':extra,'student':student} ctx.update(csrf(request)) return render(request, template_name, ctx)