Пример #1
0
def add_edit_institute(request, institute_id=None):

    if institute_id:
        institute = get_object_or_404(Institute, pk=institute_id)
        flag = 2
    else:
        institute = None
        flag = 1

    DelegateFormSet = inlineformset_factory(Institute, InstituteDelegate, extra=3)

    if request.method == 'POST':
        form = InstituteForm(request.POST, instance=institute)
        delegate_formset = DelegateFormSet(request.POST, instance=institute)

        if form.is_valid() and delegate_formset.is_valid():
            institute = form.save()
            if flag == 1:
                delegate_formset = DelegateFormSet(request.POST, instance=institute)
                delegate_formset.is_valid()
            delegate_formset.save()
            return HttpResponseRedirect(institute.get_absolute_url())
    else:
        form = InstituteForm(instance=institute)
        delegate_formset = DelegateFormSet(instance=institute)

    return render_to_response(
        'institutes/institute_form.html',
        {'institute': institute, 'form': form, 'delegate_formset': delegate_formset},
        context_instance=RequestContext(request))
Пример #2
0
def add_edit_institute(request, institute_id=None):

    if institute_id:
        institute = get_object_or_404(Institute, pk=institute_id)
    else:
        institute = None

    delegate_formset_class = inlineformset_factory(
        Institute, InstituteDelegate, form=DelegateForm, extra=3)

    if request.method == 'POST':
        form = InstituteForm(request.POST, instance=institute)

        if form.is_valid():
            institute = form.save()

            delegate_formset = delegate_formset_class(
                request.POST, instance=institute)

            if delegate_formset.is_valid():
                delegate_formset.save()
                return HttpResponseRedirect(institute.get_absolute_url())
    else:
        form = InstituteForm(instance=institute)
        delegate_formset = delegate_formset_class(instance=institute)

    media = form.media
    for dform in delegate_formset.forms:
        media = media + dform.media

    return render_to_response(
        'karaage/institutes/institute_form.html',
        {'institute': institute, 'form': form,
            'media': media, 'delegate_formset': delegate_formset},
        context_instance=RequestContext(request))
Пример #3
0
def add_edit_institute(request, institute_id=None):

    if institute_id:
        institute = get_object_or_404(Institute, pk=institute_id)
    else:
        institute = None

    delegate_formset_class = inlineformset_factory(
        Institute, InstituteDelegate, form=DelegateForm, extra=3, min_num=1, validate_min=True
    )

    if request.method == "POST":
        form = InstituteForm(request.POST, instance=institute)

        delegate_formset = delegate_formset_class(request.POST, instance=institute)

        if form.is_valid():
            institute = form.save()

            if delegate_formset.is_valid():
                delegate_formset.save()
                return HttpResponseRedirect(institute.get_absolute_url())
    else:
        form = InstituteForm(instance=institute)
        delegate_formset = delegate_formset_class(instance=institute)

    media = form.media
    for dform in delegate_formset.forms:
        media = media + dform.media

    return render(
        template_name="karaage/institutes/institute_form.html",
        context={"institute": institute, "form": form, "media": media, "delegate_formset": delegate_formset},
        request=request,
    )
Пример #4
0
def add_edit_institute(request, institute_id=None):

    if institute_id:
        institute = get_object_or_404(Institute, pk=institute_id)
        flag = 2
    else:
        institute = None
        flag = 1

    delegate_formset = inlineformset_factory(Institute, InstituteDelegate, form=DelegateForm, extra=3)
    if request.method == 'POST':
        form = InstituteForm(request.POST, instance=institute)
# JH: Fix bugs: (1) delegate_formset replaced by delegateformset on 13 October 2015; (2) Fixed to add delegates 
        delegateformset = delegate_formset(request.POST, instance=institute)

        if form.is_valid():
            if flag == 1:
                institute = form.save(commit = False)
                delegateformset = delegate_formset(
                    request.POST, instance=institute)
                if delegateformset.is_valid():
                    institute.save()
                    delegateformset.save()
                    
            elif flag == 2:
                delegateformset = delegate_formset(request.POST, instance=institute)
                if delegateformset.is_valid():
                    delegateformset.save()
                    
            return HttpResponseRedirect(institute.get_absolute_url())
    else:
        form = InstituteForm(instance=institute)
        delegateformset = delegate_formset(instance=institute)

    media = form.media
    for dform in delegateformset.forms:
        media = media + dform.media

    return render_to_response(
        'karaage/institutes/institute_form.html',
        {'institute': institute, 'form': form,
            'media': media, 'delegate_formset': delegateformset},
        context_instance=RequestContext(request))
Пример #5
0
def add_edit_institute(request, institute_id=None):

    if institute_id:
        institute = get_object_or_404(Institute, pk=institute_id)
        flag = 2
    else:
        institute = None
        flag = 1

    delegate_formset = inlineformset_factory(Institute,
                                             InstituteDelegate,
                                             form=DelegateForm,
                                             extra=3)
    if request.method == 'POST':
        form = InstituteForm(request.POST, instance=institute)
        # JH: Fix bugs: (1) delegate_formset replaced by delegateformset on 13 October 2015; (2) Fixed to add delegates
        delegateformset = delegate_formset(request.POST, instance=institute)

        if form.is_valid():
            if flag == 1:
                institute = form.save(commit=False)
                delegateformset = delegate_formset(request.POST,
                                                   instance=institute)
                if delegateformset.is_valid():
                    institute.save()
                    delegateformset.save()

            elif flag == 2:
                delegateformset = delegate_formset(request.POST,
                                                   instance=institute)
                if delegateformset.is_valid():
                    delegateformset.save()

            return HttpResponseRedirect(institute.get_absolute_url())
    else:
        form = InstituteForm(instance=institute)
        delegateformset = delegate_formset(instance=institute)

    media = form.media
    for dform in delegateformset.forms:
        media = media + dform.media

    return render_to_response('karaage/institutes/institute_form.html', {
        'institute': institute,
        'form': form,
        'media': media,
        'delegate_formset': delegateformset
    },
                              context_instance=RequestContext(request))
Пример #6
0
def add_edit_institute(request, institute_id=None):

    if institute_id:
        institute = get_object_or_404(Institute, pk=institute_id)
    else:
        institute = None

    delegate_formset_class = inlineformset_factory(Institute,
                                                   InstituteDelegate,
                                                   form=DelegateForm,
                                                   extra=3,
                                                   min_num=1,
                                                   validate_min=True)

    if request.method == 'POST':
        form = InstituteForm(request.POST, instance=institute)

        delegate_formset = delegate_formset_class(request.POST,
                                                  instance=institute)

        if form.is_valid():
            institute = form.save()

            if delegate_formset.is_valid():
                delegate_formset.save()
                return HttpResponseRedirect(institute.get_absolute_url())
    else:
        form = InstituteForm(instance=institute)
        delegate_formset = delegate_formset_class(instance=institute)

    media = form.media
    for dform in delegate_formset.forms:
        media = media + dform.media

    return render(template_name='karaage/institutes/institute_form.html',
                  context={
                      'institute': institute,
                      'form': form,
                      'media': media,
                      'delegate_formset': delegate_formset
                  },
                  request=request)