def add_credit(request, releaser_id): releaser = get_object_or_404(Releaser, id=releaser_id) if request.method == 'POST': form = ReleaserCreditForm(releaser, request.POST) credit_formset = CreditFormSet(request.POST, queryset=Credit.objects.none(), prefix="credit") if form.is_valid() and credit_formset.is_valid(): production = Production.objects.get(id=form.cleaned_data['production_id']) credits = credit_formset.save(commit=False) if credits: nick = form.cleaned_data['nick'] for credit in credits: credit.nick = nick credit.production = production credit.save() production.updated_at = datetime.datetime.now() production.has_bonafide_edits = True production.save() releaser.updated_at = datetime.datetime.now() releaser.save() credits_description = ', '.join([credit.description for credit in credits]) description = (u"Added credit for %s on %s: %s" % (nick, production, credits_description)) Edit.objects.create(action_type='add_credit', focus=production, focus2=nick.releaser, description=description, user=request.user) return HttpResponseRedirect(releaser.get_absolute_edit_url()) else: form = ReleaserCreditForm(releaser) credit_formset = CreditFormSet(queryset=Credit.objects.none(), prefix="credit") return render(request, 'releasers/add_credit.html', { 'releaser': releaser, 'form': form, 'credit_formset': credit_formset, })
def edit_credit(request, releaser_id, nick_id, production_id): releaser = get_object_or_404(Releaser, id=releaser_id) nick = get_object_or_404(Nick, releaser=releaser, id=nick_id) production = get_object_or_404(Production, id=production_id) credits = Credit.objects.filter(nick=nick, production=production) if request.method == 'POST': form = ReleaserCreditForm(releaser, request.POST, initial={ 'nick': nick.id, 'production_id': production.id, 'production_name': production.title, }) credit_formset = CreditFormSet(request.POST, queryset=credits, prefix="credit") if form.is_valid() and credit_formset.is_valid(): production = Production.objects.get(id=form.cleaned_data['production_id']) updated_credits = credit_formset.save(commit=False) # make sure that each credit has production and nick populated for credit in updated_credits: credit.nick = nick credit.production = production credit.save() new_nick = form.cleaned_data['nick'] if form.changed_data: # need to update the production / nick field of all credits in the set # (not just the ones that have been updated by credit_formset.save) credits.update(nick=new_nick, production=production) releaser.updated_at = datetime.datetime.now() releaser.save() production.updated_at = datetime.datetime.now() production.has_bonafide_edits = True production.save() new_credits = Credit.objects.filter(nick=new_nick, production=production) credits_description = ', '.join([credit.description for credit in new_credits]) Edit.objects.create(action_type='edit_credit', focus=production, focus2=releaser, description=(u"Updated %s's credit on %s: %s" % (new_nick, production, credits_description)), user=request.user) return HttpResponseRedirect(releaser.get_absolute_edit_url()) else: form = ReleaserCreditForm(releaser, { 'nick': nick.id, 'production_id': production.id, 'production_name': production.title, }) credit_formset = CreditFormSet(queryset=credits, prefix="credit") return render(request, 'releasers/edit_credit.html', { 'releaser': releaser, 'nick': nick, 'production': production, 'form': form, 'credit_formset': credit_formset, })