Exemplo n.º 1
0
 def test_choice_form(self):
     """Testing whether the choice form is valid."""
     form_data = {
         'text': "A great python framework",
         'point': 0,
         'question': self.question.id
     }
     form = ChoiceForm(data=form_data)
     self.assertTrue(form.is_valid())
Exemplo n.º 2
0
def create_choice(request, question_id):
    choice_form = ChoiceForm(request.POST or None)
    if choice_form.is_valid(): #and choice_form.is_valid():
        choice = choice_form.save(commit=False)
        choice.question = get_object_or_404(Question, pk=question_id)
        choice.save()
        return redirect(reverse("polls:detail", kwargs={'pk':question_id}))

    return render(request, 'polls/create_choice.html', {'choice_form':choice_form, 'question_id':question_id})
Exemplo n.º 3
0
def edit_choice(request, question_id, choice_id):
    a_choice = get_object_or_404(Choice, pk=choice_id)
    choice_form = ChoiceForm (request.POST or None, instance = a_choice)
    if choice_form.is_valid():
        choice = choice_form.save(commit=False)
        choice.question = get_object_or_404(Question, pk=question_id)
        choice.save()
        return redirect(reverse("polls:detail", kwargs={'pk':question_id,}))

    return render (request, "polls/edit_choice.html", {'choice_form': choice_form, 'choice_id':choice_id, 'question_id':question_id})
Exemplo n.º 4
0
def create_choice(request, poll_id):
    poll = get_object_or_404(Poll, id=poll_id)
    choice = Choice(poll=poll)
    if request.method == 'POST':
        form = ChoiceForm(request.POST, instance=choice)
        if form.is_valid():
            form.save()
            return HttpResponseRedirect(reverse('polls:administration:view', args=[poll.id]))
    else:
        form = ChoiceForm(initial={'poll':poll})
    return render(request, 'polls/administration/create_choice.html',
            {'form': form,
             'poll': poll,
             'active_module': 'polls'})
Exemplo n.º 5
0
def create_choice(request, poll_id):
    poll = get_object_or_404(Poll, id=poll_id)
    choice = Choice(poll=poll)
    if request.method == 'POST':
        form = ChoiceForm(request.POST, request.FILES, instance=choice)
        if form.is_valid():
            form.save()
            return HttpResponseRedirect(reverse('polls:administration:view', args=[poll.id]))
    else:
        form = ChoiceForm(initial={'poll':poll})
    return render(request, 'polls/administration/create_choice.html',
            {'form': form,
             'poll': poll,
             'active_module': 'polls'})
Exemplo n.º 6
0
def suggest_choice(request, poll_id):
    p = get_object_or_404(Poll, pk=poll_id)

    if request.method == 'POST':
        form = ChoiceForm(request.POST)
        # cand we loop validation (and creation) of the forms here
        if form.is_valid():
            # TODO: default votes to 0 in model
            p.choice_set.create(choice_text=form.cleaned_data['choice_text'], votes=0)
            # can we loop saving of the forms here?
            return HttpResponseRedirect(reverse('poll_detail', args=(p.id,)))
    else:
        # cand we loop creation of the forms here
        form = ChoiceForm()
    return render_to_response('polls/suggest_choice.html', {'form': form, 'poll': p}, context_instance=RequestContext(request))
Exemplo n.º 7
0
def edit_choice(request, choice_id):
    choice = get_object_or_404(Choice, id=choice_id)
    if request.method == 'POST':
        form = ChoiceForm(request.POST, instance=choice)
        if form.is_valid():
            form.save()
            #            choice.choice = form.cleaned_data['choice']
            #            choice.votes = form.cleaned_data['votes']
            #            choice.save()
            return HttpResponseRedirect(reverse('polls:administration:view', args=[choice.poll.id]))
    else:

        form = ChoiceForm(model_to_dict(choice))
    return render(request, 'polls/administration/edit_choice.html',
            {'form': form,
             'choice': choice,
             'active_module': 'polls'})
Exemplo n.º 8
0
def edit_choice(request, choice_id):
    choice = get_object_or_404(Choice, id=choice_id)
    if request.method == 'POST':
        form = ChoiceForm(request.POST, request.FILES, instance=choice)
        if form.is_valid():
            form.save()
            #            choice.choice = form.cleaned_data['choice']
            #            choice.votes = form.cleaned_data['votes']
            #            choice.save()
            return HttpResponseRedirect(reverse('polls:administration:view', args=[choice.poll.id]))
    else:

        form = ChoiceForm(instance=choice)
    return render(request, 'polls/administration/edit_choice.html',
            {'form': form,
             'choice': choice,
             'active_module': 'polls'})
Exemplo n.º 9
0
def add_choice(request, quiz_id, question_id):
    """Add the new choice that superuser created to the database."""
    if (request.user.is_superuser):
        if request.method == 'POST':
            form = ChoiceForm(request.POST)
            if form.is_valid():
                new_choice = form.save()
                new_choice.save()
                return HttpResponseRedirect(
                    reverse('create_choice',
                            kwargs={
                                'question_id': question_id,
                                'quiz_id': quiz_id
                            }))
            else:
                return render(request, 'invalidAttempt.html',
                              {'message': 'Invalid input!'})
    else:
        return render(request, 'invalidAttempt.html',
                      {'message': 'You are not a super user!'})
Exemplo n.º 10
0
def sign(request):
  form = ChoiceForm(request.POST)
  if form.is_valid():
   if telephoneExpression.match(form.clean_data['phone']):
     fre = {'fname': form.clean_data['first_name'], 'lname': form.clean_data['last_name'], 
     'email': form.clean_data ['email'], 'phone': form.clean_data['phone'], 
     'pref_book': form.clean_data['pref_book'], 
     'osys': form.clean_data['osys']}
    
     post = Post(first_name=form.clean_data['first_name'], last_name=form.clean_data['last_name'],
     email=form.clean_data['email'], phone=form.clean_data['phone'],
     pref_book=form.clean_data['pref_book'], osys=form.clean_data['osys'])
     post.put()

     return render_to_response('thanks.html', fre
                            )
   else:
     return render_to_response('err.html')
  else:
     return render_to_response('err.html')  
Exemplo n.º 11
0
def edit_poll(request, slug):
    poll = get_object_or_404(Poll, slug=slug)

    if request.user != poll.user:
        raise PermissionDenied("You must own a poll in order to edit it.")

    choices = Choice.objects.get_choices_and_votes_for_poll(poll.id)

    poll_form = PollForm(request, instance=poll, prefix='poll')
    choice_form = ChoiceForm(request, poll, prefix='choice')

    if request.method == 'POST':
        if 'poll' in request.POST:
            poll_form = PollForm(request,
                                 request.POST,
                                 instance=poll,
                                 prefix='poll')
            if poll_form.is_valid():
                p = poll_form.save()
                return HttpResponseRedirect(reverse('molnet-polls-edit-poll',
                                                    kwargs={'slug': p.slug}))

        elif 'choice' in request.POST:
            choice_form = ChoiceForm(request,
                                     poll,
                                     request.POST,
                                     prefix='choice')
            if choice_form.is_valid():
                choice, created = Choice.objects \
                    .get_or_create(poll=poll,
                                   choice=choice_form.cleaned_data['choice'],
                                   defaults={'user': request.user})
                return HttpResponseRedirect(reverse('molnet-polls-edit-poll',
                                                    kwargs={'slug':
                                                            poll.slug}))
        elif 'delete-choice' in request.POST and 'choice-id' in request.POST:
            try:
                choice = Choice.objects.get(id=request.POST['choice-id'],
                                            poll=poll) \
                                       .delete()
                return HttpResponseRedirect(reverse('molnet-polls-edit-poll',
                                            kwargs={'slug': poll.slug}))
            except Choice.DoesNotExist:
                raise Http404
        elif 'delete' in request.POST:
            poll.delete()
            return HttpResponseRedirect(reverse('molnet-polls-startpage'))
        elif 'close' in request.POST:
            poll.status="CLOSED"
            poll.save()
            return HttpResponseRedirect(reverse('molnet-polls-edit-poll',
                                                kwargs={'slug': poll.slug}))
        elif 're-open' in request.POST:
            poll.status="PUBLISHED"
            poll.save()
            return HttpResponseRedirect(reverse('molnet-polls-edit-poll',
                                                kwargs={'slug': poll.slug}))
        elif 'unpublish' in request.POST:
            poll.status="DRAFT"
            poll.save()
            return HttpResponseRedirect(reverse('molnet-polls-edit-poll',
                                                kwargs={'slug': poll.slug}))
        elif 'publish' in request.POST:
            poll.status="PUBLISHED"
            poll.published_at = datetime.datetime.now()
            poll.save()
            return HttpResponseRedirect(reverse('molnet-polls-edit-poll',
                                                kwargs={'slug': poll.slug}))
        else:
            raise Http404

    related_polls = None
    sidebar_polls = get_sidebar_polls(request.user)

    t = loader.get_template('polls-edit-poll.html')
    c = RequestContext(request,
                       {'poll': poll,
                        'choices': choices,
                        'choice_form': choice_form,
                        'poll_form': poll_form,
                        'related_polls': related_polls,
                        'sidebar_polls': sidebar_polls})
    return HttpResponse(t.render(c))