88 lines
3.1 KiB
Python
88 lines
3.1 KiB
Python
from quixote import get_publisher
|
|
|
|
from wcs.fields import WidgetField, register_field_class
|
|
from qommon.form import *
|
|
|
|
class VoteAnonymityWidget(CheckboxWidget):
|
|
vote_anonymity = 'anonymous'
|
|
|
|
def __init__(self, name, value = None, elements = None, **kwargs):
|
|
if kwargs and kwargs.get('vote_anonymity'):
|
|
self.vote_anonymity = kwargs.get('vote_anonymity')
|
|
CheckboxWidget.__init__(self, name, value = value, elements = elements, **kwargs)
|
|
|
|
def render(self):
|
|
if self.vote_anonymity != 'choice':
|
|
# XXX: include info for user ?
|
|
return ''
|
|
return CheckboxWidget.render(self)
|
|
|
|
def render_title(self, title):
|
|
return CheckboxWidget.render_title(self, _('Anonymous Voting'))
|
|
|
|
def render_content(self):
|
|
value = True
|
|
if self.value:
|
|
if self.value[0] == 'anonymous':
|
|
value = True
|
|
else:
|
|
value = False
|
|
return htmltag('input', xml_end=True,
|
|
type='checkbox',
|
|
name=self.name,
|
|
value='yes',
|
|
checked=value and 'checked' or None,
|
|
**self.attrs)
|
|
|
|
def _parse(self, request):
|
|
if self.vote_anonymity == 'anonymous':
|
|
self.value = ('anonymous', None)
|
|
elif self.vote_anonymity == 'public':
|
|
self.value = ('public', request.user.id)
|
|
else:
|
|
if type(request.form.get(self.name)) is tuple:
|
|
self.value = request.form.get(self.name)
|
|
else:
|
|
CheckboxWidget._parse(self, request)
|
|
if self.value is True:
|
|
self.value = ('anonymous', None)
|
|
else:
|
|
self.value = ('public', request.user.id)
|
|
|
|
|
|
class VoteAnonymityField(WidgetField):
|
|
key = 'vote-anonymity'
|
|
description = N_('Vote Anonymity')
|
|
|
|
widget_class = VoteAnonymityWidget
|
|
vote_anonymity = 'anonymous'
|
|
# possible values:
|
|
# 'anonymous': anonymous vote
|
|
# 'public': public vote
|
|
# 'choice': anonymous or public, choice of voter
|
|
|
|
def fill_admin_form(self, form):
|
|
form.add(StringWidget, 'label', title = _('Label'), value = self.label,
|
|
required = True, size = 50)
|
|
form.add(SingleSelectWidget, 'vote_anonymity', title = _('Vote Anonymity'),
|
|
value = self.vote_anonymity,
|
|
options = [('anonymous', _('Anonymous Vote')),
|
|
('public', _('Public Vote')),
|
|
('choice', _('Choice of Voter'))])
|
|
|
|
def get_admin_attributes(self):
|
|
return ['label', 'vote_anonymity']
|
|
|
|
def perform_more_widget_changes(self, form, kwargs, edit = True):
|
|
kwargs['vote_anonymity'] = self.vote_anonymity
|
|
|
|
def get_view_value(self, value):
|
|
if self.vote_anonymity == 'anonymous':
|
|
return _('Anonymous') # XXX: include token?
|
|
elif self.vote_anonymity == 'public':
|
|
return get_publisher().user_class.get(value[1]).get_display_name()
|
|
else:
|
|
raise
|
|
|
|
register_field_class(VoteAnonymityField)
|