def test_get_challenge_form(self): question_mock = Mock(spec=Question)() question_mock.id = 3 question_mock.question = 'Question 3' question_mock.is_required = False challenge_mock = Mock(spec=Challenge)() challenge_mock.question_set.all.return_value = [question_mock] FormClass = forms.get_challenge_form(challenge_mock) form = FormClass() eq_(sorted(form.fields.keys()), ['question_3', 'status']) challenge_mock.question_set.all.assert_called_once()
def challenge_entry(request, challenge_slug, app_slug): """Entry form for an ``Application`` for a given ``Challenge``. The ``Challenge`` has a list of ``Questions`` that will be translated into a ``ChallengeForm``.""" # Application must be owned by the user: application = get_object_or_404( Application, slug__exact=app_slug, owner=request.user, status=Application.PUBLISHED) # Challenge must be open: challenge = get_object_or_404( Challenge, slug__exact=challenge_slug, status=Challenge.PUBLISHED) if not challenge.is_open(): return redirect('entry_detail', challenge_slug, app_slug) # Determine if an entry already exists: entry = Entry.objects.get_entry_or_none(challenge, application) ChallengeForm = forms.get_challenge_form(challenge) if request.method == 'POST': entry, is_new = Entry.objects.get_or_create( challenge=challenge, application=application) form = ChallengeForm(request.POST) if form.is_valid(): status = form.cleaned_data.pop('status') entry.status = status entry.save() entry.save_answers(form.cleaned_data) messages.success(request, 'Entry saved.') return redirect( 'challenge_entry', challenge_slug=challenge.slug, app_slug=application.slug) else: args = [forms.get_challenge_initial_data(entry)] if entry else [] form = ChallengeForm(*args) context = { 'form': form, 'challenge': challenge, 'application': application, 'entry': entry, } return TemplateResponse(request, 'challenges/object_entry.html', context)