Пример #1
0
def create(request,template_name="oa/position_form.html"):
    """创建职务"""
    ctx = {}
    school_id = request.GET.get("sid",0)
    if request.method == 'POST':
        form = PositionForm(request.POST)
        school_id = int(request.POST.get('school'))
        
        if request.is_ajax():
            return helpers.ajax_validate_form(form)
        
        if form.is_valid():
            position = form.save(commit=False)
            position.creator = request.user
            position.save()
            ctx.update({"school":position.school})
            if position.id:
                messages.success(request, u'已成功创建职务 %s ' % position.name)
#                 redirect_url = reverse('oa_position_list') + "?sid=" + str(position.school.id)
                return redirect('oa_position_list')
    else:
        form = PositionForm()
    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)
Пример #2
0
def update(request, position_id, template_name="oa/position_form.html"):
    """更新职务"""
    schools = helpers.get_schools(request.user)
    position = get_object_or_404(Position,pk=position_id,school__in=schools)
    school = position.school

    if request.method == 'POST':
        form = PositionForm(request.POST, instance=position)
        
        if request.is_ajax():
            return helpers.ajax_validate_form(form)
        
        if form.is_valid():
            position = form.save(commit=False)
            position.save()
            messages.success(request, u"已成功更新职务: %s " % position.name)
            redirect_url = reverse('oa_position_list') + "?sid=" + str(position.school.id)
            return redirect(redirect_url)
    else:
        form = PositionForm(instance=position)
    ctx = {"form": form, "position": position,"school":school,"schools":schools}
    return render(request, template_name, ctx)