Ejemplo n.º 1
0
def seed_polls(num_entries=10, choice_min=2, choice_max=5, overwrite = False):
    """
    Seeds num_entries poll with random users as owners
    Each poll will be seeded with # choices from choice_min to choice_max
    """

    if overwrite:
        print("over writing poll")
        Poll.objects.all().delete()
    users = list(User.objects.all())
    count = 0
    for _ in range(num_entries):
        p = Poll(
            owner = random.choice(users),
            text=fake.paragraph(),
            pub_date=datetime.datetime.now()
        )
        p.save()
        num_choices = random.randrange(choice_min,choice_max+1)
        for _ in range(num_choices):
            c = Choice(
                poll=p,
                choice_text=fake.sentence()
            )
            c.save()
            count += 1
            perecent_complete = count / num_entries * 100
            print(
                "Addind {} new poll: {:.2f}%".format(
                    num_entries, perecent_complete
                ),
                end='\r',
                flush=True
            )
        print()
Ejemplo n.º 2
0
 def get(self,request,id=None):
     if id:
         pass
     else:
         poll_form = PollForm(instance=Questions())
         choice_forms = [ChoiceForm(prefix=str(
             x), instance=Choice()) for x in range(3)]
         context = {'poll_form': poll_form, 'choice_forms': choice_forms}
         return render(request, 'poll/new_poll.html', context)
Ejemplo n.º 3
0
    def test_get_number_of_votes(self, app):
        with app.app_context():
            now = datetime.now()
            p = Poll(id=None, name="Nom", date=now)
            p.save()

            assert p.number_of_votes() == 0

            c1 = Choice(id=None, choice="Premier choix", poll=p)
            c1.save()

            c2 = Choice(id=None, choice="Deuxième choix", poll=p)
            c2.save()

            assert p.number_of_votes() == 0

            c1.cast_vote()
            c1.cast_vote()
            c2.cast_vote()

            assert p.number_of_votes() == 3
Ejemplo n.º 4
0
def add_poll(request):
    if request.method == "POST":
        form = PollForm(request.POST)
        if form.is_valid():
            new_poll = form.save(commit=False)
            new_poll.owner = request.user
            new_poll.save()
            new_choice1 = Choice(
                poll=new_poll,
                choice_text=form.cleaned_data['choice1']).save()
            new_choice2 = Choice(
                poll=new_poll,
                choice_text=form.cleaned_data['choice2']).save()
            messages.success(
                request,
                "Poll and Choices added successfully",
                extra_tags='alert alert-success alert-dismissible fade show')
            return redirect('poll:polls')
    else:
        form = PollForm()

    context = {'form': form}
    return render(request, 'poll/add_poll.html', context)
Ejemplo n.º 5
0
    def get(self, request, id=None):
        if id:
            question = get_object_or_404(Qustions, id = id)
            poll_form = PollForm(instance=question)
            choices = question.choice_set.all()
            choice_forms = [ChoiceForm(prefix=str(
                choice.id), instance=choice) for choice in choice in choices           

            ]
            template = 'polls/edit_poll.html'
        else:
            poll_form = PollForm(instance=Questions())
            choice_forms = [ChoiceForm(prefix=str(
                x), instance=Choice()) for x in range(3)]
            template = 'polls/new_poll.html'
        context = {'poll_form': poll_form, 'choice_forms': choice_forms}
        return render(request, template, context)
Ejemplo n.º 6
0
 def post(self, request, id=None):
     context = {}
     if id:
         return self.put(request, id)
     poll_form = PollForm(request.POST, instance=Questions())
     choice_forms = [ChoiceForm(request.POST, prefix=str(
         x), instance=Choice()) for x in range(0, 3)]
     if poll_form.is_valid() and all([cf.is_valid() for cf in choice_forms]):
         new_poll = poll_form.save(commit=False)
         new_poll.created_by = request.user
         new_poll.save()
         for cf in choice_forms:
             new_choice = cf.save(commit=False)
             new_choice.question = new_poll
             new_choice.save()
         return HttpResponseRedirect('/poll/')
     context = {'poll_form': poll_form, 'choice_forms': choice_forms}
     return render(request, 'polls/new_poll.html', context)
Ejemplo n.º 7
0
    def convert_poll_choices(self):
        cursor = connection.cursor()
        cursor.execute("SELECT * FROM smf_poll_choices")
        rows = cursor.fetchall()

        for row in rows:
            try:
                poll = Poll.objects.get(old_poll_id=row[0])
                try:
                    choice = Choice()
                    choice.poll = poll
                    choice.old_choice_id = row[1]
                    choice.label = row[2]
                    choice.votes = row[3]
                    choice.save()

                except Exception, e:
                    print str(e)
            except Poll.DoesNotExist, e:
                print str(e)
Ejemplo n.º 8
0
    def create_new_choice_for_poll_from_post_data(cls, poll, post_data):
        choice = Choice(id=None, choice=post_data['choice'], poll=poll)
        choice.save()

        return choice