<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
    openslides.poll.models
    ~~~~~~~~~~~~~~~~~~~~~~

    Models for the poll app.

    :copyright: 2011, 2012 by OpenSlides team, see AUTHORS.
    :license: GNU GPL, see LICENSE for more details.
"""

from django.core.exceptions import ObjectDoesNotExist
from django.db import models
from django.utils.translation import ugettext_lazy as _, ugettext_noop

from openslides.utils.modelfields import MinMaxIntegerField


class BaseOption(models.Model):
    """
    Base option class for a Poll.

    Subclasses have to define a poll-field, which are a subclass of BasePoll.
    """

    def get_votes(self):
        return self.get_vote_class().objects.filter(option=self)

    def __getitem__(self, name):
        try:
            return self.get_votes().get(value=name)
        except self.get_vote_class().DoesNotExist:
            return None

    def get_vote_class(self):
        return self.vote_class

    class Meta:
        abstract = True


class BaseVote(models.Model):
    """
    Base Vote class for an option.

    Subclasses have to define a option-field, which are a subclass of
    BaseOption.
    """
    weight = models.IntegerField(default=1, null=True) # Use MinMaxIntegerField
    value = models.CharField(max_length=255, null=True)

    def print_weight(self, raw=False):
        if raw:
            return self.weight
        try:
            percent_base = self.option.poll.percent_base()
        except AttributeError:
            # The poll class is no child of CountVotesCast
            percent_base = 0

        return print_value(self.weight, percent_base)

    def get_value(self):
        return _(self.value)

    def __unicode__(self):
        return self.print_weight()

    class Meta:
        abstract = True


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')

    def print_votescast(self):
        return print_value(self.votescast)

    def percent_base(self):
        return 100 / float(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')

    def print_votesinvalid(self):
        try:
            percent_base = self.percent_base()
        except AttributeError:
            # The poll class is no child of CountVotesCast
            percent_base = 0

        return print_value(self.votesinvalid, percent_base)<commit_msg>#288 fix type error if a poll has no votecast<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
    openslides.poll.models
    ~~~~~~~~~~~~~~~~~~~~~~

    Models for the poll app.

    :copyright: 2011, 2012 by OpenSlides team, see AUTHORS.
    :license: GNU GPL, see LICENSE for more details.
"""

from django.core.exceptions import ObjectDoesNotExist
from django.db import models
from django.utils.translation import ugettext_lazy as _, ugettext_noop

from openslides.utils.modelfields import MinMaxIntegerField


class BaseOption(models.Model):
    """
    Base option class for a Poll.

    Subclasses have to define a poll-field, which are a subclass of BasePoll.
    """

    def get_votes(self):
        return self.get_vote_class().objects.filter(option=self)

    def __getitem__(self, name):
        try:
            return self.get_votes().get(value=name)
        except self.get_vote_class().DoesNotExist:
            return None

    def get_vote_class(self):
        return self.vote_class

    class Meta:
        abstract = True


class BaseVote(models.Model):
    """
    Base Vote class for an option.

    Subclasses have to define a option-field, which are a subclass of
    BaseOption.
    """
    weight = models.IntegerField(default=1, null=True) # Use MinMaxIntegerField
    value = models.CharField(max_length=255, null=True)

    def print_weight(self, raw=False):
        if raw:
            return self.weight
        try:
            percent_base = self.option.poll.percent_base()
        except AttributeError:
            # The poll class is no child of CountVotesCast
            percent_base = 0

        return print_value(self.weight, percent_base)

    def get_value(self):
        return _(self.value)

    def __unicode__(self):
        return self.print_weight()

    class Meta:
        abstract = True


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')

    def print_votescast(self):
        return print_value(self.votescast)

    def percent_base(self):
        try:
            return 100 / float(self.votescast)
        except TypeError:
            return 0

    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')

    def print_votesinvalid(self):
        try:
            percent_base = self.percent_base()
        except AttributeError:
            # The poll class is no child of CountVotesCast
            percent_base = 0

        return print_value(self.votesinvalid, percent_base)
{'fraction_non_alphanumeric': 0.0944305605141021, 'fraction_numerical': 0.00785433773652267, 'mean_word_length': 2.7130550033134524, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3363326', 'n_tokens_mistral': 1702, 'n_tokens_neox': 1605, 'n_words': 429}