def create(request,template_name="oa/department_form.html"): """创建部门""" ctx = {} school_id = request.GET.get("sid",0) if request.method == 'POST': school_id = int(request.POST.get("school",0)) form = DepartmentForm(request.POST,user=request.user) if request.is_ajax(): return helpers.ajax_validate_form(form) if form.is_valid(): department = form.save(commit=False) department.creator = request.user department.save() ctx.update({"school":department.school}) if department.id: messages.success(request, u'已成功创建部门 %s ' % department.name) redirect_url = reverse('oa_department_list') + "?sid=" + str(department.school.id) return redirect(redirect_url) else: form = DepartmentForm(user=request.user) try: school = get_object_or_404(School,id=school_id) except: school = None schools = helpers.get_schools(request.user) ctx.update({"school":school,"schools":schools}) ctx.update({'form':form}) return render(request, template_name, ctx)
def update(request, department_id, template_name="oa/department_form.html"): """更新部门""" schools = helpers.get_schools(request.user) department = get_object_or_404(Department,pk=department_id,school__in=schools) school = department.school if request.method == 'POST': form = DepartmentForm(request.POST, instance=department, user=request.user) if request.is_ajax(): return helpers.ajax_validate_form(form) if form.is_valid(): department = form.save(commit=False) department.save() messages.success(request, u"已成功更新部门: %s " % department.name) redirect_url = reverse('oa_department_list') + "?sid=" + str(department.school.id) return redirect(redirect_url) else: form = DepartmentForm(instance=department,user=request.user) ctx = {"form": form, "department": department,"school":school,"schools":schools} return render(request, template_name, ctx)