From ebec3575c4935e3a2799540cfa32cbf82a06939f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89lie=20Bouttier?= Date: Sun, 19 Nov 2017 21:41:52 +0100 Subject: [PATCH] new proposal with logged user: use form initial --- cfp/views.py | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/cfp/views.py b/cfp/views.py index 5f355ef..28d2495 100644 --- a/cfp/views.py +++ b/cfp/views.py @@ -168,20 +168,19 @@ def proposal_home(request): categories = request.conference.opened_categories if not categories.exists(): return render(request, 'cfp/closed.html') + initial = {} + fields = ['name', 'email', 'biography'] if request.user.is_authenticated(): if Participant.objects.filter(site=request.conference.site, email=request.user.email).exists(): return redirect(reverse('proposal-dashboard')) - else: - NewSpeakerForm = modelform_factory(Participant, form=ParticipantForm, fields=['name', 'biography']) - if request.POST: - data = request.POST - else: - # TODO: import biography from User profile - data = dict(name=request.user.get_full_name()) - else: - NewSpeakerForm = modelform_factory(Participant, form=ParticipantForm, fields=['name', 'email', 'biography']) - data = request.POST or None - speaker_form = NewSpeakerForm(data, conference=request.conference) + elif not request.POST: + # TODO: import biography from User profile + initial.update({ + 'name': request.user.get_full_name(), + }) + fields.remove('email') + 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) if request.method == 'POST' and all(map(lambda f: f.is_valid(), [speaker_form, talk_form])): speaker = speaker_form.save(commit=False)