Browse Source

non-js placeholders for polls

Rafał Pitoń 8 years ago
parent
commit
50a808b3c7

+ 7 - 0
misago/templates/misago/poll/index.html

@@ -0,0 +1,7 @@
+<div class="panel panel-default panel-poll">
+  {% if user.is_anonymous or not poll.acl.can_vote or poll.has_selected_choices %}
+    {% include "misago/poll/results.html" %}
+  {% else %}
+    {% include "misago/poll/voting.html" %}
+  {% endif %}
+</div>

+ 40 - 0
misago/templates/misago/poll/info.html

@@ -0,0 +1,40 @@
+{% load i18n misago_capture %}
+<ul class="list-unstyled list-inline poll-details">
+  <li class="poll-info-creation">
+    {% capture trimmed as poster %}
+      {% if poll.poster_id %}
+        <a href="{% url 'misago:user' pk=poll.poster_id slug=poll.poster_slug %}" class="item-title">{{ poll.poster_name }}</a>
+      {% else %}
+        <span class="item-title">{{ poll.poster_name }}</span>
+      {% endif %}
+    {% endcapture %}
+    {% capture trimmed as posted_on %}
+      <abbr title="{{ poll.posted_on }}">{{ poll.posted_on|date }}</abbr>
+    {% endcapture %}
+    {% blocktrans trimmed with poster=poster|safe posted_on=posted_on|safe %}
+      Posted by {{ poster }} on {{ posted_on }}.
+    {% endblocktrans %}
+  </li>
+  {% if poll.length %}
+    <li class="poll-info-ends-on">
+      {% capture trimmed as ends_on %}
+        <abbr title="{{ poll.ends_on }}">{{ poll.ends_on|date }}</abbr>
+      {% endcapture %}
+      {% blocktrans trimmed with ends_on=ends_on|safe %}
+        Voting ends on {{ ends_on }}.
+      {% endblocktrans %}
+    </li>
+  {% endif %}
+  <li class="poll-info-votes">
+    {% blocktrans trimmed count votes=poll.votes %}
+      {{ votes }} vote
+    {% plural %}
+      {{ votes }} votes
+    {% endblocktrans %}
+  </li>
+  {% if poll.is_public %}
+    <li class="poll-info-public">
+      {% trans "Votes are public." %}
+    </li>
+  {% endif %}
+</ul>

+ 82 - 0
misago/templates/misago/poll/results.html

@@ -0,0 +1,82 @@
+{% load i18n %}
+<div class="panel-body">
+  <h2>{{ poll.question }}</h2>
+  {% include "misago/poll/info.html" %}
+
+  <ul class="list-unstyled poll-choices-bars">
+    {% for choice in poll.view_choices %}
+      <li>
+        <ul class="list-unstyled list-inline poll-chart">
+          <li class="poll-char-label">
+            <strong>{{ choice.label }}</strong>
+          </li>
+          <li class="poll-chart-votes">
+            {% blocktrans trimmed count votes=choice.votes with proc=choice.proc %}
+              {{ votes }} vote, {{ proc }}% of total.
+            {% plural %}
+              {{ votes }} votes, {{ proc }}% of total.
+            {% endblocktrans %}
+          </li>
+          {% if choice.selected %}
+            <li class="poll-chart-selected">
+              <span class="material-icon">
+                check_circle
+              </span> {% trans "Your choice." %}
+            </li>
+          {% endif %}
+        </ul>
+        <div class="progress">
+          <div
+            class="progress-bar"
+            role="progressbar"
+            aria-valuenow={proc}
+            aria-valuemin="0"
+            aria-valuemax="100"
+            style="width: {{ choice.proc }}%""
+          >
+            <span class="sr-only">
+              {% blocktrans trimmed count votes=choice.votes with proc=choice.proc %}
+                {{ votes }} vote, {{ proc }}% of total.
+              {% plural %}
+                {{ votes }} votes, {{ proc }}% of total.
+              {% endblocktrans %}
+            </span>
+          </div>
+        </div>
+      </li>
+    {% endfor %}
+  </ul>
+
+  {% if poll.acl.can_vote or poll.acl.can_see_votes or poll.acl.can_edit or poll.acl.can_delete %}
+    <ul class="list-unstyled list-inline poll-options">
+      {% if poll.acl.can_vote %}
+        <li>
+          <button class="btn btn-default" disabled>
+            {% trans "Vote" %}
+          </button>
+        </li>
+      {% endif %}
+      {% if poll.acl.can_see_votes %}
+        <li>
+          <button class="btn btn-default" disabled>
+            {% trans "See votes" %}
+          </button>
+        </li>
+      {% endif %}
+      {% if poll.acl.can_see_votes %}
+        <li>
+          <button class="btn btn-default" disabled>
+            {% trans "Edit" %}
+          </button>
+        </li>
+      {% endif %}
+      {% if poll.acl.can_see_votes %}
+        <li>
+          <button class="btn btn-default" disabled>
+            {% trans "Delete" %}
+          </button>
+        </li>
+      {% endif %}
+    </ul>
+  {% endif %}
+</div>

+ 50 - 0
misago/templates/misago/poll/voting.html

@@ -0,0 +1,50 @@
+{% load i18n %}
+<form onSubmit="false">
+  <div class="panel-body">
+    <h2>{{ poll.question }}</h2>
+    {% include "misago/poll/info.html" %}
+
+    <ul class="list-unstyled poll-select-choices">
+      {% for choice in poll.choices %}
+        <li class="poll-select-choice">
+          <button class="btn" disabled>
+            <span class="material-icon">
+              check
+            </span>
+            <strong>
+              {{ choice.label }}
+            </strong>
+          </button>
+        </li>
+      {% endfor %}
+    </ul>
+
+    <ul class="list-unstyled list-inline poll-help">
+      <li class="poll-help-choices-left">
+        {% blocktrans trimmed count choices=poll.allowed_choices %}
+          You can select {{ choices }} choice.
+        {% plural %}
+          You can select {{ choices }} choices.
+        {% endblocktrans %}
+      </li>
+      {% if poll.allow_revotes %}
+        <li class="poll-help-allow-revotes">
+          {% trans "You can change your vote later." %}
+        </li>
+      {% else %}
+        <li class="poll-help-no-revotes">
+          {% trans "Votes are final." %}
+        </li>
+      {% endif %}
+    </ul>
+
+  </div>
+  <div class="panel-footer">
+    <button class="btn btn-primary" disabled>
+      {% trans "Save your vote" %}
+    </button>
+    <button class="btn btn-default" disabled>
+      {% trans "See results" %}
+    </button>
+  </div>
+</form>

+ 5 - 0
misago/templates/misago/thread/thread.html

@@ -25,6 +25,11 @@
   <div class="container">
 
     {% include "misago/thread/toolbar-top.html" %}
+
+    {% if thread.poll %}
+      {% include "misago/poll/index.html" with poll=thread.poll %}
+    {% endif %}
+
     {% include "misago/thread/posts/index.html" %}
     {% include "misago/thread/paginator.html" %}
 

+ 26 - 2
misago/threads/models/poll.py

@@ -1,4 +1,5 @@
 from datetime import timedelta
+from math import ceil
 
 from django.conf import settings
 from django.contrib.postgres.fields import JSONField
@@ -31,10 +32,15 @@ class Poll(models.Model):
     is_public = models.BooleanField(default=False)
 
     @property
+    def ends_on(self):
+        if self.length:
+            return self.posted_on + timedelta(days=self.length)
+        return None
+
+    @property
     def is_over(self):
         if self.length:
-            poll_cutoff = self.posted_on + timedelta(days=self.length)
-            return timezone.now() > poll_cutoff
+            return timezone.now() > self.ends_on
         return False
 
     @property
@@ -65,3 +71,21 @@ class Poll(models.Model):
             if choice.get('selected'):
                 return True
         return False
+
+    @property
+    def view_choices(self):
+        view_choices = []
+        for choice in self.choices:
+            if choice['votes'] and self.votes:
+                proc = int(ceil(choice['votes'] * 100 / self.votes))
+            else:
+                proc = 0
+
+            view_choices.append({
+                'label': choice['label'],
+                'votes': choice['votes'],
+                'selected': choice['selected'],
+                'proc': proc
+            })
+        return view_choices
+