Exemple #1
0
def create_provider(request,template_name="supply/provider_form.html"):
    """创建供应商"""
    ctx = {}
    school_id = request.GET.get("sid",0)

    if request.method == 'POST':
        school_id = int(request.POST.get("school",0))
        form = ProviderForm(request.POST)

        if form.is_valid():
            provider = form.save(commit=False)
            provider.creator = request.user
            provider.save()
            ctx.update({"school":provider.school})
            if provider.id:
                messages.success(request, u'已成功创建供应商%s ' % provider.name)
                redirect_url = reverse('oa_provider_index') + "?sid=" + str(provider.school.id)
                return redirect(redirect_url)
    else:
        form = ProviderForm()
    try:
        school = School.objects.get(id=school_id)
    except:
        school = None
    
    schools = get_schools(request.user)
    ctx.update({"school":school,"schools":schools})
    ctx.update({'form':form})
    return render(request, template_name, ctx)
Exemple #2
0
def update_provider(request, provider_id, template_name="supply/provider_form.html"):
    """更新供应商"""
    schools = get_schools(request.user)
    provider = get_object_or_404(Provider,pk=provider_id,school__in=schools)
    school = provider.school

    if request.method == 'POST':
        form = ProviderForm(request.POST, instance=provider)
        
        if form.is_valid():
            provider = form.save(commit=False)
            provider.save()
            messages.success(request, u"已成功更新供应商: %s " % provider.name)
            redirect_url = reverse('oa_provider_index') + "?sid=" + str(provider.school.id)
            return redirect(redirect_url)
    else:
        form = ProviderForm(instance=provider)
    ctx = {"form": form, "provider": provider,"school":school,"schools":schools,'sid':school.id}
    return render(request, template_name, ctx)