From 114a68b12037cf59b0445dd610ed958648e384f6 Mon Sep 17 00:00:00 2001 From: Oskar Hahn Date: Wed, 15 Feb 2012 13:44:55 +0100 Subject: [PATCH] some additions in poll and application-poll --- openslides/application/models.py | 25 +++++++++++++------ .../templates/application/view.html | 14 +++++------ openslides/application/views.py | 6 ++--- openslides/poll/models.py | 16 ++++++------ openslides/utils/utils.py | 6 +++-- 5 files changed, 39 insertions(+), 28 deletions(-) diff --git a/openslides/application/models.py b/openslides/application/models.py index 1264b259e..496c3dff1 100644 --- a/openslides/application/models.py +++ b/openslides/application/models.py @@ -412,16 +412,15 @@ class Application(models.Model, Slide): """ Generates a poll object for the application """ - poll = ApplicationPoll() + poll = ApplicationPoll(application=self) poll.save() - poll.set_options([{'application': self}]) + poll.set_options() self.writelog(_("Poll created"), user) return poll @property def polls(self): - #todo: return an query_set - return [option.poll for option in self.applicationoption_set.all()] + return self.applicationpoll_set.all() @property def results(self): @@ -493,14 +492,24 @@ register_slidemodel(Application) class ApplicationOption(BaseOption): - application = models.ForeignKey(Application) - def __unicode__(self): - return unicode(self.application) + def __getattr__(self, name): + if name in [_('yes'), _('no'), _('contained')]: + print self.poll.get_vote_values() + try: + return self.get_votes().get(value=name) + except Vote.DoesNotExist: + pass + raise AttributeError(name) class ApplicationPoll(BasePoll): option_class = ApplicationOption + application = models.ForeignKey(Application) + def get_application(self): - return self.get_options()[0].application + return self.application + + def set_options(self): + self.get_option_class()(poll=self).save() diff --git a/openslides/application/templates/application/view.html b/openslides/application/templates/application/view.html index e3cc2ad5f..eb121c571 100644 --- a/openslides/application/templates/application/view.html +++ b/openslides/application/templates/application/view.html @@ -74,16 +74,16 @@ {% endif %} {% trans 'project' %}
- {% if poll.has_vote %} - {% for option in poll.options %} - {{ option.yes }}
- {{ option.no }}
- {{ option.undesided }}
+ {% if poll.has_votes %} + {% with poll.get_options.0 as option %} + {{ option.yes.weight }}
+ {{ option.no.weight }}
+ {{ option.contained.weight }}
{{ poll.votesinvalidf }}
- {{ poll.votescastf }} + {{ poll.votescastf }}
- {% endfor %} + {% endwith %} {% if perms.application.can_manage_application %} {% if forloop.last %} {% if "genpoll" in actions %} diff --git a/openslides/application/views.py b/openslides/application/views.py index e829499c1..f0c3dbf35 100644 --- a/openslides/application/views.py +++ b/openslides/application/views.py @@ -362,14 +362,14 @@ def delete_poll(request, poll_id): """ delete a poll from this application """ - poll = Poll.objects.get(pk=poll_id) + poll = ApplicationPoll.objects.get(pk=poll_id) application = poll.application - count = application.poll_set.filter(id__lte=poll_id).count() + count = application.polls.filter(id__lte=poll_id).count() if request.method == 'POST': poll.delete() messages.success(request, _('Poll was successfully deleted.')) else: - del_confirm_form(request, poll, name=_("the %s. poll") % count) + del_confirm_form(request, poll, name=_("the %s. poll") % count, delete_link=reverse('application_poll_delete', args=[poll_id])) return redirect(reverse('application_view', args=[application.id])) diff --git a/openslides/poll/models.py b/openslides/poll/models.py index 60e3d7d56..113e367d3 100644 --- a/openslides/poll/models.py +++ b/openslides/poll/models.py @@ -22,12 +22,8 @@ from poll.forms import OptionForm class BaseOption(models.Model): poll = models.ForeignKey('BasePoll') - @property - def votes(self): - count = 0 - for vote in Vote.objects.filter(option=self): - count += vote.weight - return weight + def get_votes(self): + return Vote.objects.filter(option=self) class TextOption(BaseOption): @@ -54,7 +50,12 @@ class BasePoll(models.Model, Slide): option_class = TextOption vote_values = [_('votes')] - def set_options(self, options_data): + def has_votes(self): + if self.get_options().filter(vote__isnull=False): + return True + return False + + def set_options(self, options_data=[]): for option_data in options_data: option = self.option_class(**option_data) option.poll = self @@ -78,7 +79,6 @@ class BasePoll(models.Model, Slide): vote.weight = data[value] vote.save() - def get_form_values(self, option_id): values = [] for value in self.get_vote_values(): diff --git a/openslides/utils/utils.py b/openslides/utils/utils.py index de420c935..7c31715fe 100644 --- a/openslides/utils/utils.py +++ b/openslides/utils/utils.py @@ -35,10 +35,12 @@ def gen_confirm_form(request, message, url): messages.warning(request, '%s
' % (message, url, csrf(request)['csrf_token'], _("Yes"), _("No"))) -def del_confirm_form(request, object, name=None): +def del_confirm_form(request, object, name=None, delete_link=None): if name is None: name = object - gen_confirm_form(request, _('Do you really want to delete %s?') % name, object.get_absolute_url('delete')) + if delete_link is None: + delete_link = object.get_absolute_url('delete') + gen_confirm_form(request, _('Do you really want to delete %s?') % name, delete_link) def render_response(req, *args, **kwargs):