Esempio n. 1
0
 def test_data_is_extracted_successfully(self):
     answer_mock = Mock(spec=EntryAnswer)()
     answer_mock.question_id = 4
     answer_mock.answer = 'This will help society.'
     entry_mock = Mock(spec=Entry)()
     entry_mock.status = Entry.DRAFT
     entry_mock.entryanswer_set.all.return_value = [answer_mock]
     initial_data = forms.get_challenge_initial_data(entry_mock)
     eq_(initial_data, {
         'status': Entry.DRAFT,
         'question_4': 'This will help society.',
     })
Esempio n. 2
0
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)