some additions in poll and application-poll

This commit is contained in:
Oskar Hahn 2012-02-15 13:44:55 +01:00
parent c4874ae6a0
commit 114a68b120
5 changed files with 39 additions and 28 deletions

View File

@ -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()

View File

@ -74,16 +74,16 @@
{% endif %}
<a href="{% url application_poll_activate poll.id %}">{% trans 'project' %}</a>
<br>
{% if poll.has_vote %}
{% for option in poll.options %}
<img src="/static/images/icons/voting-yes.png" title="{% trans 'Yes' %}"> {{ option.yes }}<br>
<img src="/static/images/icons/voting-no.png" title="{% trans 'No' %}"> {{ option.no }}<br>
<img src="/static/images/icons/voting-abstention.png" title="{% trans 'Abstention' %}"> {{ option.undesided }}<br>
{% if poll.has_votes %}
{% with poll.get_options.0 as option %}
<img src="/static/images/icons/voting-yes.png" title="{% trans 'Yes' %}"> {{ option.yes.weight }}<br>
<img src="/static/images/icons/voting-no.png" title="{% trans 'No' %}"> {{ option.no.weight }}<br>
<img src="/static/images/icons/voting-abstention.png" title="{% trans 'Abstention' %}"> {{ option.contained.weight }}<br>
<img src="/static/images/icons/voting-invalid.png" title="{% trans 'Invalid' %}"> {{ poll.votesinvalidf }}<br>
<div style="border-top: 1px solid; padding-top: 5px; margin: 5px 0; width: 10em;">
<img src="/static/images/icons/voting-total.png" title="{% trans 'Votes cast' %}"> {{ poll.votescastf }}
</div>
{% endfor %}
{% endwith %}
{% if perms.application.can_manage_application %}
{% if forloop.last %}
{% if "genpoll" in actions %}

View File

@ -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]))

View File

@ -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():

View File

@ -35,10 +35,12 @@ def gen_confirm_form(request, message, url):
messages.warning(request, '%s<form action="%s" method="post"><input type="hidden" value="%s" name="csrfmiddlewaretoken"><input type="submit" value="%s" /> <input type="button" value="%s"></form>' % (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 <b>%s</b>?') % 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 <b>%s</b>?') % name, delete_link)
def render_response(req, *args, **kwargs):