Ejemplo n.º 1
0
def production_add(request, play=None, place=None, company=None):

    initial = {}
    if play: initial['play'] = play.id

    production_form = ProductionForm(data=request.POST or None, initial=initial)

    # Inline even though empty, because model validation means it can't assign null
    # to Place.production when calling construct_instance()
    ProductionPlaceFormSet = inlineformset_factory( Production, ProductionPlace, extra=1, form=PlaceForm )
    place_formset = ProductionPlaceFormSet(
        data = request.POST or None,
        prefix = 'place',
        queryset = ProductionPlace.objects.none()
    )

    ProductionCompanyFormSet = inlineformset_factory( Production, Production_Companies, extra=1, form=CompanyInlineForm )
    companies_formset = ProductionCompanyFormSet(
        data = request.POST or None,
        prefix = 'company',
        queryset = Production_Companies.objects.none()
    )

    # Yucky, but no way to pass initial to a model formset XXX
    if place:
        place_formset.forms[0].initial['place'] = place.id
    if company:
        companies_formset.forms[0].initial['productioncompany'] = company.id

    if request.method == 'POST':
        if request.POST.get('disregard'):
            messages.success(request, u"All right, we\u2019ve ignored what you had done.")
            if play: return HttpResponseRedirect(play.get_absolute_url())
            if company: return HttpResponseRedirect(company.get_absolute_url())
            if place: return HttpResponseRedirect(place.get_absolute_url())
        if production_form.is_valid() and place_formset.is_valid() and companies_formset.is_valid():
            # Nasty things to set up the parent/child inline relations as it expects them to be.
            production = production_form.save()
            place_formset.instance = production
            for form in place_formset.forms:
                form.instance.production = production
            companies_formset.instance = production
            for form in companies_formset.forms:
                form.instance.production = production
            place_formset.save()
            companies_formset.save()
            messages.success(request, "Your addition has been stored; thank you. If you know members of the cast or crew, please feel free to add them now.")
            url = production.get_edit_cast_url()
            if request.POST.get('initial_person'):
                url += '?person=' + request.POST.get('initial_person')
            return HttpResponseRedirect(url)

    return render(request, 'productions/add.html', {
        'place': place,
        'place_formset': place_formset,
        'companies_formset': companies_formset,
        'play': play,
        'company': company,
        'form': production_form,
    })
Ejemplo n.º 2
0
        prefix = 'place',
        instance = production,
    )

    ProductionCompanyFormSet = inlineformset_factory( Production, Production_Companies, extra=1, form=CompanyInlineForm )
    companies_formset = ProductionCompanyFormSet(
        data = request.POST or None,
        prefix = 'company',
        instance = production,
    )

    if request.method == 'POST':
        if request.POST.get('disregard'):
            messages.success(request, u"All right, we\u2019ve ignored any changes you made.")
            return HttpResponseRedirect(production.get_absolute_url())
        if production_form.is_valid() and place_formset.is_valid() and companies_formset.is_valid():
            production_form.save()
            place_formset.save()
            companies_formset.save()
            messages.success(request, "Your changes have been stored; thank you.")
            return HttpResponseRedirect(production.get_absolute_url())

    return render(request, 'productions/edit.html', {
        'form': production_form,
        'place_formset': place_formset,
        'companies_formset': companies_formset,
        'production': production,
        'places': production.place_set.order_by('start_date', 'press_date'),
    })

@login_required