From f8a1c58fff2a39e60ad75610a03ddc09012ac4f0 Mon Sep 17 00:00:00 2001 From: Oskar Hahn Date: Sat, 7 Jul 2012 11:14:04 +0200 Subject: [PATCH] #277 throw an error, in the invalid-field or the votecast-field is lower -2 --- openslides/poll/models.py | 13 +++++++------ openslides/utils/modelfields.py | 23 +++++++++++++++++++++++ 2 files changed, 30 insertions(+), 6 deletions(-) create mode 100644 openslides/utils/modelfields.py diff --git a/openslides/poll/models.py b/openslides/poll/models.py index 706d8c503..bfdb29fce 100644 --- a/openslides/poll/models.py +++ b/openslides/poll/models.py @@ -12,9 +12,10 @@ from django.db import models -from projector.api import register_slidemodel -from projector.models import SlideMixin -from utils.translation_ext import ugettext as _ +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 class BaseOption(models.Model): @@ -34,7 +35,7 @@ class TextOption(BaseOption): 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) + weight = models.IntegerField(default=1, null=True) # Use MinMaxIntegerField value = models.CharField(max_length=255, null=True) def get_weight(self, raw=False): @@ -50,7 +51,7 @@ class Vote(models.Model): class CountVotesCast(models.Model): - votescast = models.IntegerField(null=True, blank=True, verbose_name=_("Votes cast")) + votescast = MinMaxIntegerField(null=True, blank=True, min_value=-2, verbose_name=_("Votes cast")) def append_pollform_fields(self, fields): fields.append('votescast') @@ -63,7 +64,7 @@ class CountVotesCast(models.Model): class CountInvalid(models.Model): - votesinvalid = models.IntegerField(null=True, blank=True, verbose_name=_("Votes invalid")) + votesinvalid = MinMaxIntegerField(null=True, blank=True, min_value=-2, verbose_name=_("Votes invalid")) def append_pollform_fields(self, fields): fields.append('votesinvalid') diff --git a/openslides/utils/modelfields.py b/openslides/utils/modelfields.py new file mode 100644 index 000000000..72f63b51d --- /dev/null +++ b/openslides/utils/modelfields.py @@ -0,0 +1,23 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +""" + openslides.utils.modelfields + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + + Modelfields for OpenSlides + + :copyright: 2011, 2012 by OpenSlides team, see AUTHORS. + :license: GNU GPL, see LICENSE for more details. +""" + +from django.db import models + +class MinMaxIntegerField(models.IntegerField): + def __init__(self, min_value=None, max_value=None, *args, **kwargs): + self.min_value, self.max_value = min_value, max_value + super(MinMaxIntegerField, self).__init__(*args, **kwargs) + + def formfield(self, **kwargs): + defaults = {'min_value': self.min_value, 'max_value' : self.max_value} + defaults.update(kwargs) + return super(MinMaxIntegerField, self).formfield(**defaults)