new proposal with logged user: use form initial

This commit is contained in:
Élie Bouttier 2017-11-19 21:41:52 +01:00
parent c7c41cfb3d
commit ebec3575c4

View File

@ -168,20 +168,19 @@ def proposal_home(request):
categories = request.conference.opened_categories categories = request.conference.opened_categories
if not categories.exists(): if not categories.exists():
return render(request, 'cfp/closed.html') return render(request, 'cfp/closed.html')
initial = {}
fields = ['name', 'email', 'biography']
if request.user.is_authenticated(): if request.user.is_authenticated():
if Participant.objects.filter(site=request.conference.site, email=request.user.email).exists(): if Participant.objects.filter(site=request.conference.site, email=request.user.email).exists():
return redirect(reverse('proposal-dashboard')) return redirect(reverse('proposal-dashboard'))
else: elif not request.POST:
NewSpeakerForm = modelform_factory(Participant, form=ParticipantForm, fields=['name', 'biography'])
if request.POST:
data = request.POST
else:
# TODO: import biography from User profile # TODO: import biography from User profile
data = dict(name=request.user.get_full_name()) initial.update({
else: 'name': request.user.get_full_name(),
NewSpeakerForm = modelform_factory(Participant, form=ParticipantForm, fields=['name', 'email', 'biography']) })
data = request.POST or None fields.remove('email')
speaker_form = NewSpeakerForm(data, conference=request.conference) NewSpeakerForm = modelform_factory(Participant, form=ParticipantForm, fields=fields)
speaker_form = NewSpeakerForm(request.POST or None, initial=initial, conference=request.conference)
talk_form = TalkForm(request.POST or None, categories=categories) talk_form = TalkForm(request.POST or None, categories=categories)
if request.method == 'POST' and all(map(lambda f: f.is_valid(), [speaker_form, talk_form])): if request.method == 'POST' and all(map(lambda f: f.is_valid(), [speaker_form, talk_form])):
speaker = speaker_form.save(commit=False) speaker = speaker_form.save(commit=False)