Commits

Bertrand Bordage committed d803c9d Draft

Stays on the same page after sending a vote + Some CSS.

Comments (0)

Files changed (7)

cmsplugin_poll/cms_plugins.py

     render_template = "cmsplugin_poll/detail.html"
 
     def render(self, context, instance, placeholder):
-        context.update({
-                "poll": instance.poll,
-                })
+        context['poll'] = instance.poll
         return context
 
 plugin_pool.register_plugin(CMSPollPlugin)

cmsplugin_poll/locale/fr/LC_MESSAGES/django.po

 msgstr ""
 "Project-Id-Version: cmsplugin_poll 0.2\n"
 "Report-Msgid-Bugs-To: \n"
-"POT-Creation-Date: 2012-07-15 22:58+0200\n"
-"PO-Revision-Date: 2012-07-15 22:58+0200\n"
+"POT-Creation-Date: 2012-07-16 22:58+0200\n"
+"PO-Revision-Date: 2012-07-15 00:59+0200\n"
 "Last-Translator: Bertrand Bordage <bordage.bertrand@gmail.com>\n"
 "Language: fr\n"
 "MIME-Version: 1.0\n"
 "Content-Type: text/plain; charset=UTF-8\n"
 "Content-Transfer-Encoding: 8bit\n"
 
-#: admin.py:8
+#: admin.py:9
 msgid "Close selected polls"
 msgstr "Clore les sondages sélectionnés"
 
-#: admin.py:17
+#: admin.py:20
 msgid "Date information"
 msgstr "Informations de date"
 
-#: cms_plugins.py:8
+#: cms_plugins.py:9
 msgid "Simple poll"
 msgstr "Sondage simple"
 
-#: models.py:6
+#: models.py:7
 msgid "question"
 msgstr "question"
 
-#: models.py:7
+#: models.py:8
 msgid "date published"
 msgstr "date de publication"
 
-#: models.py:8
+#: models.py:9
 msgid "date closed"
 msgstr "date de clôture"
 
-#: models.py:11
-#: templates/cmsplugin_poll/detail.html:2
+#: models.py:12
+#: templates/cmsplugin_poll/detail.html:40
 msgid "Poll"
 msgstr "Sondage"
 
-#: models.py:12
+#: models.py:13
 msgid "Polls"
 msgstr "Sondages"
 
 msgid "Choices"
 msgstr "Choix"
 
-#: models.py:46
+#: models.py:47
 msgid "Poll to display"
 msgstr "Sondage à afficher"
 
-#: models.py:49
+#: models.py:50
 msgid "Poll plugin"
 msgstr "Bloc de sondage"
 
-#: models.py:50
+#: models.py:51
 msgid "Poll plugins"
 msgstr "Blocs de sondage"
 
-#: views.py:23
+#: views.py:26
 msgid "This poll is closed"
 msgstr "Ce sondage est clos"
 
-#: views.py:25
+#: views.py:28
 msgid "You already vote for this poll"
 msgstr "Vous avez déjà voté dans ce sondage"
 
-#: views.py:30
+#: views.py:33
 msgid "You didn't select a choice"
 msgstr "Vous n'avez pas sélectionné de choix"
 
-#: views.py:34
+#: views.py:37
 msgid "Thank you for your vote"
 msgstr "Merci d'avoir voté"
 
-#: templates/cmsplugin_poll/detail.html:11
+#: templates/cmsplugin_poll/detail.html:63
 msgid "Vote"
 msgstr "Voter"
 
-#: templates/cmsplugin_poll/latest_polls.html:2
+#: templates/cmsplugin_poll/detail.html:66
+msgid "Please vote to see the results."
+msgstr "Veuillez voter pour voir les résultats."
+
+#: templates/cmsplugin_poll/latest_polls.html:3
 msgid "Previous polls"
 msgstr "Sondages précédents"
 
-#: templates/cmsplugin_poll/latest_polls.html:10
+#: templates/cmsplugin_poll/latest_polls.html:15
 msgid "No polls are available."
 msgstr "Aucun sondage disponible."
 
-#: templates/cmsplugin_poll/results.html:2
+#: templates/cmsplugin_poll/results.html:3
 msgid "Poll results"
 msgstr "Résultat du sondage"

cmsplugin_poll/models.py

     def __repr__(self):
         return unicode(self)
 
+    @models.permalink
+    def get_absolute_url(self):
+        return ('cmsplugin_poll.views.detail', (self.id,))
+
     @property
     def votes(self):
         return self.choice_set.aggregate(models.Sum('votes'))['votes__sum']

cmsplugin_poll/templates/cmsplugin_poll/detail.html

-{% load i18n %}
+{% load i18n poll_tags %}
 
-<h3>{% trans "Poll" %}</h3>
-<p>
-  <a href="{% url cmsplugin_poll.views.results poll.id %}">
-    {{ poll.question }}
-  </a>
-</p>
+<style>
+  .cmsplugin_poll
+  {
+    padding: 5px;
+    border: 1px solid grey;
+    border-radius: 5px;
+    background-color: white;
+    position: relative;
+  }
 
-{% if error_message %}<p><strong>{{ error_message }}</strong></p>{% endif %}
+  .cmsplugin_poll .messages .error
+  {
+    color: red;
+  }
 
-<form action="{% url cmsplugin_poll.views.vote poll.id %}" method="post">
-  {% csrf_token %}
-  {% for choice in poll.choice_set.iterator %}
-    <input type="radio" name="choice"
-           id="choice{{ forloop.counter }}" value="{{ choice.id }}" />
-    <label for="choice{{ forloop.counter }}">
-      {{ choice.choice }}
-    </label><br />
-  {% endfor %}
-  <input type="submit" value='{% trans "Vote" %}' />
-</form>
+  .cmsplugin_poll .info
+  {
+    font-size: 75%;
+    color: grey;
+  }
+
+  .cmsplugin_poll ul
+  {
+    list-style-position: inside;
+    padding-left: 0.5em;
+  }
+</style>
+
+<div class="cmsplugin_poll">
+
+  {% show_results request poll as show_results %}
+  {% if show_results %}
+
+    {% include 'cmsplugin_poll/results.html' %}
+
+  {% else %}
+
+    <h4>{% trans "Poll" %}</h4>
+    <h3>{{ poll.question }}</h3>
+
+    {% if messages %}
+      <div class="messages">
+        {% for message in messages %}
+          <span{% if message.tags %} class="{{ message.tags }}"{% endif %}>
+            {{ message }}.
+          </span>
+        {% endfor %}
+      </div>
+    {% endif %}
+
+    <form action="{% url cmsplugin_poll.views.vote poll.id %}" method="post">
+      {% csrf_token %}
+      {% for choice in poll.choice_set.iterator %}
+        <input type="radio" name="choice"
+               id="choice{{ forloop.counter }}" value="{{ choice.id }}" />
+        <label for="choice{{ forloop.counter }}">
+          {{ choice.choice }}
+        </label><br />
+      {% endfor %}
+      <input type="hidden" name="next" value="{{ request.path }}">
+      <input type="submit" value='{% trans "Vote" %}' />
+    </form>
+    <span class="info">
+      {% trans 'Please vote to see the results.' %}
+    </span>
+  {% endif %}
+</div>

cmsplugin_poll/templates/cmsplugin_poll/results.html

 {% load i18n poll_tags %}
 
-<h2>{% trans "Poll results" %}</h2>
+<h4>{% trans "Poll results" %}</h4>
+
 {% if messages %}
   <div class="messages">
     {% for message in messages %}

cmsplugin_poll/templatetags/poll_tags.py

 
 register = Library()
 
+
 @register.simple_tag
 def get_latest_polls(count=5):
     "FIXME: I'm useless!"
     polls = Poll.objects.all()[:5]
     return render_to_string("cmsplugin_poll/latest_polls.html", {
-            "polls" : polls
+            "polls": polls
             })
 
+
 @register.simple_tag
 def get_choice_rate(poll, choice):
     return "%d%%" % poll.getrate(choice)
+
+
+@register.assignment_tag
+def show_results(request, poll):
+    poll_is_closed = poll.close_date is not None
+    session_has_voted = request.session.get("poll_%d" % poll.id, False)
+    return poll_is_closed or session_has_voted

cmsplugin_poll/views.py

 
 def index(request):
     polls = Poll.objects.iterator()
-    return render_to_response("cmsplugin_poll/latest_polls.html", {
-            "polls": polls
-            })
+    c = RequestContext(request, {'polls': polls})
+    return render_to_response('cmsplugin_poll/latest_polls.html', c)
 
 
 def detail(request, poll_id):
-    p = get_object_or_404(Poll, pk=poll_id)
-    return render_to_response("cmsplugin_poll/detail.html", {"poll": p},
-                              context_instance=RequestContext(request))
+    poll = get_object_or_404(Poll, pk=poll_id)
+    c = RequestContext(request, {'poll': poll})
+    return render_to_response('cmsplugin_poll/detail.html', c)
 
 
 def vote(request, poll_id):
-    p = get_object_or_404(Poll, pk=poll_id)
-    if p.close_date is not None:
+    poll = get_object_or_404(Poll, pk=poll_id)
+    if poll.close_date is not None:
         messages.error(request, _("This poll is closed"))
-    elif request.session.get("poll_%d" % p.id, False):
+    elif request.session.get("poll_%d" % poll.id, False):
         messages.error(request, _("You already vote for this poll"))
     else:
         try:
-            selected_choice = p.choice_set.get(pk=request.POST['choice'])
+            selected_choice = poll.choice_set.get(pk=request.POST['choice'])
         except (KeyError, Choice.DoesNotExist):
             messages.error(request, _("You didn't select a choice"))
         else:
             selected_choice.votes += 1
             selected_choice.save()
             messages.info(request, _("Thank you for your vote"))
-            request.session["poll_%d" % p.id] = True
-    return HttpResponseRedirect(reverse('cmsplugin_poll.views.results',
-                                        args=(p.id,)))
+            request.session["poll_%d" % poll.id] = True
+    url = request.POST.get('next', poll.get_absolute_url())
+    return HttpResponseRedirect(url)
 
 
 def results(request, poll_id):
-    p = get_object_or_404(Poll, pk=poll_id)
-    return render_to_response('cmsplugin_poll/results.html', {'poll': p},
-                              context_instance=RequestContext(request))
+    poll = get_object_or_404(Poll, pk=poll_id)
+    c = RequestContext(request, {'poll': poll})
+    return render_to_response('cmsplugin_poll/results.html', c)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.