|
| 1 | +"""Browser views for the Poll content type.""" |
| 2 | + |
| 3 | +from experimental.doodle import _ |
| 4 | +from experimental.doodle.interfaces import IVoteStorage |
| 5 | +from plone.protect.interfaces import IDisableCSRFProtection |
| 6 | +from Products.Five.browser import BrowserView |
| 7 | +from Products.Five.browser.pagetemplatefile import ViewPageTemplateFile |
| 8 | +from zope.interface import alsoProvides |
| 9 | + |
| 10 | + |
| 11 | +def _format_slot(dt) -> str: |
| 12 | + """Return a human-readable label for a datetime option.""" |
| 13 | + return dt.strftime("%a %d %b %Y, %H:%M UTC") |
| 14 | + |
| 15 | + |
| 16 | +class PollView(BrowserView): |
| 17 | + """Default view for a Poll: shows the vote form. |
| 18 | +
|
| 19 | + Handles GET (render) and POST (record vote, then redirect). |
| 20 | + """ |
| 21 | + |
| 22 | + index = ViewPageTemplateFile("templates/poll_view.pt") |
| 23 | + |
| 24 | + def __call__(self): |
| 25 | + if self.request.method == "POST": |
| 26 | + error = self._handle_post() |
| 27 | + if error is None: |
| 28 | + # Post/Redirect/Get: prevent duplicate submissions on refresh. |
| 29 | + url = self.context.absolute_url() + "/@@poll_view" |
| 30 | + self.request.response.redirect(url) |
| 31 | + return "" |
| 32 | + self._error = error |
| 33 | + else: |
| 34 | + self._error = None |
| 35 | + return self.index() |
| 36 | + |
| 37 | + # ------------------------------------------------------------------ |
| 38 | + # Template helpers |
| 39 | + # ------------------------------------------------------------------ |
| 40 | + |
| 41 | + @property |
| 42 | + def options(self): |
| 43 | + """Return ``[(index, label), ...]`` for every proposed slot.""" |
| 44 | + return [ |
| 45 | + (i, _format_slot(dt)) |
| 46 | + for i, dt in enumerate(self.context.options or []) |
| 47 | + ] |
| 48 | + |
| 49 | + @property |
| 50 | + def error(self): |
| 51 | + return self._error |
| 52 | + |
| 53 | + @property |
| 54 | + def participant_count(self): |
| 55 | + return len(IVoteStorage(self.context).participants()) |
| 56 | + |
| 57 | + # ------------------------------------------------------------------ |
| 58 | + # POST handler |
| 59 | + # ------------------------------------------------------------------ |
| 60 | + |
| 61 | + def _handle_post(self): |
| 62 | + """Validate the submitted form and cast the vote. |
| 63 | +
|
| 64 | + Returns ``None`` on success, or a translated error string to |
| 65 | + display on the form. |
| 66 | +
|
| 67 | + Voting is open to anonymous users, so there is no valid CSRF |
| 68 | + authenticator token for them. We explicitly opt out of CSRF |
| 69 | + protection here — the write is intentional and the form is |
| 70 | + publicly accessible by design. |
| 71 | + """ |
| 72 | + alsoProvides(self.request, IDisableCSRFProtection) |
| 73 | + form = self.request.form |
| 74 | + name = (form.get("name") or "").strip() |
| 75 | + if not name: |
| 76 | + return _("Please enter your name.") |
| 77 | + |
| 78 | + options = self.context.options or [] |
| 79 | + votes = [] |
| 80 | + for i in range(len(options)): |
| 81 | + raw = form.get(f"votes_{i}", "false") |
| 82 | + votes.append(raw == "true") |
| 83 | + |
| 84 | + try: |
| 85 | + IVoteStorage(self.context).cast_vote(name, votes) |
| 86 | + except ValueError as exc: |
| 87 | + return str(exc) |
| 88 | + |
| 89 | + return None |
| 90 | + |
| 91 | + |
| 92 | +class PollResultsView(BrowserView): |
| 93 | + """Results view for a Poll: shows the tally.""" |
| 94 | + |
| 95 | + index = ViewPageTemplateFile("templates/results.pt") |
| 96 | + |
| 97 | + def __call__(self): |
| 98 | + return self.index() |
| 99 | + |
| 100 | + # ------------------------------------------------------------------ |
| 101 | + # Template helpers |
| 102 | + # ------------------------------------------------------------------ |
| 103 | + |
| 104 | + @property |
| 105 | + def rows(self): |
| 106 | + """Return ``[(label, yes_count, max_count), ...]`` for each slot.""" |
| 107 | + storage = IVoteStorage(self.context) |
| 108 | + counts = storage.tally() |
| 109 | + max_count = max(counts) if counts else 0 |
| 110 | + return [ |
| 111 | + (_format_slot(dt), count, max_count) |
| 112 | + for dt, count in zip(self.context.options or [], counts) |
| 113 | + ] |
| 114 | + |
| 115 | + @property |
| 116 | + def participants(self): |
| 117 | + return IVoteStorage(self.context).participants() |
0 commit comments