OpenSlides/openslides/poll/models.py

203 lines
5.6 KiB
Python
Raw Normal View History

2011-07-31 10:46:29 +02:00
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
openslides.poll.models
~~~~~~~~~~~~~~~~~~~~~~
Models for the poll app.
2012-04-25 22:29:19 +02:00
:copyright: 2011, 2012 by OpenSlides team, see AUTHORS.
2011-07-31 10:46:29 +02:00
:license: GNU GPL, see LICENSE for more details.
"""
from django.db import models
from openslides.projector.api import register_slidemodel
from openslides.projector.models import SlideMixin
from openslides.utils.translation_ext import ugettext as _ # TODO
from openslides.utils.modelfields import MinMaxIntegerField
2012-07-03 00:05:48 +02:00
2012-02-14 16:31:21 +01:00
class BaseOption(models.Model):
poll = models.ForeignKey('BasePoll')
2011-07-31 10:46:29 +02:00
def get_votes(self):
return Vote.objects.filter(option=self)
2011-07-31 10:46:29 +02:00
2012-02-14 16:31:21 +01:00
class TextOption(BaseOption):
text = models.CharField(max_length=255)
2011-07-31 10:46:29 +02:00
def __unicode__(self):
2012-02-14 16:31:21 +01:00
return self.text
2011-07-31 10:46:29 +02:00
2012-02-14 16:31:21 +01:00
class Vote(models.Model):
option = models.ForeignKey(BaseOption)
#profile = models.ForeignKey(Profile) # TODO: we need a person+ here
weight = models.IntegerField(default=1, null=True) # Use MinMaxIntegerField
2012-02-14 16:31:21 +01:00
value = models.CharField(max_length=255, null=True)
2011-07-31 10:46:29 +02:00
def get_weight(self, raw=False):
if raw:
return self.weight
2012-02-19 19:27:00 +01:00
return print_value(self.weight)
2012-04-17 17:35:50 +02:00
def get_value(self):
return unicode(_(self.value))
2012-04-17 17:35:50 +02:00
def __unicode__(self):
return self.get_weight()
2011-07-31 10:46:29 +02:00
class CountVotesCast(models.Model):
votescast = MinMaxIntegerField(null=True, blank=True, min_value=-2, verbose_name=_("Votes cast"))
def append_pollform_fields(self, fields):
fields.append('votescast')
2012-02-19 19:27:00 +01:00
def print_votescast(self):
return print_value(self.votescast)
class Meta:
abstract = True
class CountInvalid(models.Model):
votesinvalid = MinMaxIntegerField(null=True, blank=True, min_value=-2, verbose_name=_("Votes invalid"))
def append_pollform_fields(self, fields):
fields.append('votesinvalid')
2012-02-19 19:27:00 +01:00
def print_votesinvalid(self):
return print_value(self.votesinvalid)
class Meta:
abstract = True
2012-03-03 11:16:10 +01:00
class PublishPollMixin(models.Model):
published = models.BooleanField(default=False)
def set_published(self, published):
self.published = published
self.save()
class Meta:
abstract = True
2012-03-03 09:11:56 +01:00
class BasePoll(models.Model, SlideMixin):
#TODO: It would be nice if this class wouldn't be a subclass from models.Model. But it is needet aslong
# BaseOption has a foreignKey on BasePoll
2012-02-15 12:04:11 +01:00
prefix = 'BasePoll'
description = models.TextField(null=True, blank=True, verbose_name=_("Description")) #TODO: Use this field or delete it.
2011-07-31 10:46:29 +02:00
2012-02-14 16:31:21 +01:00
option_class = TextOption
2012-04-17 17:35:50 +02:00
vote_values = [_('votes', fixstr=True)]
2011-07-31 10:46:29 +02:00
def has_votes(self):
2012-04-17 17:35:50 +02:00
"""
Return True, the there are votes in the poll.
"""
if self.get_options().filter(vote__isnull=False):
return True
return False
def set_options(self, options_data=[]):
2012-04-17 17:35:50 +02:00
"""
Add new Option pbjects to the poll.
option_data: A List of arguments for the Option.
"""
2012-02-14 16:31:21 +01:00
for option_data in options_data:
2012-04-17 17:35:50 +02:00
option = self.get_option_class()(**option_data)
2012-02-14 16:31:21 +01:00
option.poll = self
option.save()
def get_options(self):
2012-04-17 17:35:50 +02:00
"""
Return the option objects for the poll.
"""
2012-02-14 16:31:21 +01:00
return self.get_option_class().objects.filter(poll=self)
def get_option_class(self):
2012-04-17 17:35:50 +02:00
"""
Return the option class for the poll. Default is self.option_class.
"""
2012-02-14 16:31:21 +01:00
return self.option_class
def get_vote_values(self):
2012-04-17 17:35:50 +02:00
"""
Return the possible values for the poll as list.
"""
2012-02-14 16:31:21 +01:00
return self.vote_values
def set_form_values(self, option, data):
2012-04-17 17:35:50 +02:00
# TODO: recall this function. It has nothing to do with a form
"""
Create or update the vote objects for the poll.
"""
2012-02-14 16:31:21 +01:00
for value in self.get_vote_values():
try:
vote = Vote.objects.filter(option=option).get(value=value)
except Vote.DoesNotExist:
vote = Vote(option=option, value=value)
vote.weight = data[value]
vote.save()
def get_form_values(self, option_id):
2012-04-17 17:35:50 +02:00
# TODO: recall this function. It has nothing to do with a form
"""
Return a the values and the weight of the values as a list with two elements.
"""
2012-02-14 16:31:21 +01:00
values = []
for value in self.get_vote_values():
try:
vote = Vote.objects.filter(option=option_id).get(value=value)
2012-04-17 17:35:50 +02:00
values.append(vote)
2012-02-14 16:31:21 +01:00
except Vote.DoesNotExist:
values.append(Vote(value=value, weight=''))
2012-02-14 16:31:21 +01:00
return values
def get_vote_form(self, **kwargs):
2012-04-17 17:35:50 +02:00
"""
Return the form for one option of the poll.
"""
from poll.forms import OptionForm
2012-02-14 16:31:21 +01:00
return OptionForm(extra=self.get_form_values(kwargs['formid']), **kwargs)
def get_vote_forms(self, **kwargs):
2012-04-17 17:35:50 +02:00
"""
Return a list of forms for the poll
"""
2012-02-14 16:31:21 +01:00
forms = []
for option in self.get_options():
form = self.get_vote_form(formid=option.id, **kwargs)
form.option = option
forms.append(form)
return forms
2012-02-15 12:36:50 +01:00
def slide(self):
2012-04-17 17:35:50 +02:00
"""
show a Slide for the Poll.
"""
2012-02-15 12:36:50 +01:00
data = super(BasePoll, self).slide()
# data['template'] = 'projector/TODO.html'
return data
2012-04-12 16:21:30 +02:00
def get_absolute_url(self):
return ''
2012-02-14 16:31:21 +01:00
2012-02-19 19:27:00 +01:00
def print_value(value):
if value == -1:
value = _('majority')
2012-02-19 19:27:00 +01:00
elif value == -2:
value = _('undocumented')
2012-07-03 00:05:48 +02:00
elif value is None:
value = ''
2012-02-19 19:27:00 +01:00
return unicode(value)