Skip to main content

A polling plugin for the Wagtail CMS.

Project description

A plugin for Wagtail that provides polling functionality Documentation on ReadTheDocs

Installing

Install using pip:

pip install wagtailpolls

It works with Wagtail 1.0b2 and upwards.

Using

Add wagtailpolls to your INSTALLED_APPS, add the line from wagtailpolls.views import vote to your urls.py and include the URL url(r'^vote/(?P<poll_pk>.*)/$', vote.vote, name='wagtailpolls_vote').

Define a foreign key referring to wagtailpolls.Poll and use the PollChooserPanel:

from django.db import models
from wagtailpolls.edit_handlers import PollChooserPanel
from wagtail.wagtailadmin.edit_handlers import FieldPanel

class Content(Page):
    body = models.TextField()
    poll = models.ForeignKey(
        'wagtailpolls.Poll',
        null=True,
        blank=True,
        on_delete=models.SET_NULL
    )

    content_panels = [
        FieldPanel('body', classname="full"),
        PollChooserPanel('poll'),
    ]

Then, in your editor, ensure that you have added some polls in the polls section in wagtail admin. You will be able to select a poll from there accessable in the template as you would expect.

Templating & Display

There are many ways in which you may want to display your poll. wagtailpolls comes with a template tag to assist with this, as well as certain attributes accesible via templating to render each question as a form. Here is an example using all of the tools provided:

{% extends "layouts/page.html" %}
{% load wagtailpolls_tags %}
{% block content %}
<h1>{{ self.title }}</h1>
<br>
<form class='poll' method='POST' action='{% vote self.poll %}'>
{% csrf_token %}
{{self.poll.form}}
<br><br>
    <input type="submit" value="Vote">
</form>
{% endblock %}

As shown, the {% vote %} template tag will need to be passed a poll instance to function correctly. You will also need to {% load wagtailpolls_tags %} at the top of the file where this template tag is used. The poll can be rendered with all questions using .form at the end. .form_as_ul and all other form types will also work.

Voting

When a vote has been submitted, the server will return a JsonResponse something like:

{"total_votes": 11, "total_questions": 3, "poll": "Test Poll", "votes": {"Nah": 10, "Yeah": 1, "Maybe": 0}}

With javascript, this data can be used to create a frontend for your poll to your own liking.

The voting form also performs some validation. If the voting form is unable to obtain your IP it will return something like:

{"poll": "Test Poll", "total_questions": 3, "total_votes": 11, "votes": {"Yeah": 1, "Maybe": 0, "Nah": 10}, "form_error": {"__all__": ["Sorry, we were not able to obtain your ip address"]}}

There is also a POLL_COOLDOWN which is set in your settings. This will only allow users on the same IP to vote at an interval of your choosing. If this is caught, the error will be present in the JsonResponse much like the error above.

Additionally, information will be added to the django session (basically cookies will be set) that will help make sure devices are not able to vote twice. When a vote is rejected due to this reason, the vote simply won’t register with no error being returned in the JsonResponse.

Settings

The following settings need to be set in your settings.py file.

VOTE_COOLDOWN This is to be an integer representing minutes

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

wagtailpolls-0.1.2.tar.gz (11.8 kB view details)

Uploaded Source

File details

Details for the file wagtailpolls-0.1.2.tar.gz.

File metadata

File hashes

Hashes for wagtailpolls-0.1.2.tar.gz
Algorithm Hash digest
SHA256 e8d861bb2a82c03ec1ec39c821ce883901f4ba216b9baba3304a42ba4fcbd58b
MD5 be77259243f61d7c241eaa521ed29f08
BLAKE2b-256 cd1252f927cac023ecca67801c202cafc211ed2aa7d60c5252332b7dfdc94d38

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page