This repository has been archived on 2023-02-21. You can view files and clone it, but cannot push or open issues or pull requests.
tabellio.contest/tabellio/contest/contest.py

102 lines
3.2 KiB
Python

import datetime
import random
from five import grok
from zope import schema
from Products.Five.browser import BrowserView
from Products.CMFCore.utils import getToolByName
from plone.directives import form, dexterity
from plone.dexterity.utils import createContentInContainer
from tabellio.contest.interfaces import MessageFactory as _
class IContest(form.Schema):
title = schema.TextLine(title=_(u'Title'))
intro = schema.Text(title=_(u'Introduction'))
question1 = schema.TextLine(title=_(u'First question'))
question1answers = schema.Text(title=_(u'Answers to first question'),
description=_(u'One answer per line'))
question1correctanswer = schema.TextLine(
title=_(u'Correct answer to first question'))
question2 = schema.TextLine(title=_(u'Second question'))
question2answers = schema.Text(title=_(u'Answers to second question'),
description=_(u'One answer per line'))
question2correctanswer = schema.TextLine(
title=_(u'Correct answer to second question'))
active = schema.Bool(title=_(u'Active'), default=False)
class View(grok.View):
grok.context(IContest)
grok.require('zope2.View')
def question1answerslist(self):
return self.context.question1answers.split('\n')
def question2answerslist(self):
return self.context.question2answers.split('\n')
class Thanks(grok.View):
grok.context(IContest)
grok.require('zope2.View')
class Participants(grok.View):
grok.context(IContest)
grok.require('zope2.View') # XXX
def has_winners(self):
for object in self.context.objectValues():
if object.winner and not object.removed:
return True
return False
def winners(self):
winners = []
for object in self.context.objectValues():
if object.winner and not object.removed:
winners.append(object)
return winners
class Participate(BrowserView):
def __call__(self):
plone_tool = getToolByName(self.context, 'plone_utils')
new_id = plone_tool.normalizeString(self.request.form.get('name'))
if new_id in self.context:
counter = 1
while True:
if '%s-%s' % (new_id, counter) in self.context:
counter += 1
continue
new_id = '%s-%s' % (new_id, counter)
break
self.context.invokeFactory('tabellio.contest.participant', new_id)
object = self.context[new_id]
for attr in ('name', 'address', 'zipcode', 'locality', 'phone',
'email', 'answer1', 'answer2'):
setattr(object, attr, self.request.form.get(attr))
object.datetime = datetime.datetime.now()
object.ipaddress = self.request._client_addr
self.request.response.redirect('./thanks')
class PickWinner(BrowserView):
def __call__(self):
potential_winners = [x for x in self.context.objectValues() if
x.is_correct() and not x.winner and not x.removed]
if potential_winners:
winner = random.SystemRandom().choice(potential_winners)
winner.winner = True
self.request.response.redirect('./participants')