2011-07-31 10:46:29 +02:00
|
|
|
|
#!/usr/bin/env python
|
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
"""
|
|
|
|
|
openslides.assignment.views
|
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
|
|
Views for the assignment 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.
|
|
|
|
|
"""
|
|
|
|
|
|
2012-04-20 14:33:40 +02:00
|
|
|
|
import os
|
2012-06-11 13:43:48 +02:00
|
|
|
|
|
|
|
|
|
from reportlab.lib import colors
|
|
|
|
|
from reportlab.lib.units import cm
|
|
|
|
|
from reportlab.platypus import SimpleDocTemplate, PageBreak, Paragraph, Spacer, Table, TableStyle
|
|
|
|
|
|
2011-07-31 10:46:29 +02:00
|
|
|
|
from django.shortcuts import redirect
|
|
|
|
|
from django.core.urlresolvers import reverse
|
|
|
|
|
from django.contrib import messages
|
|
|
|
|
from django.contrib.auth.decorators import login_required
|
2012-04-20 14:33:40 +02:00
|
|
|
|
from django.contrib.auth.models import User
|
2012-05-14 21:34:46 +02:00
|
|
|
|
from django.utils.translation import ungettext, ugettext as _
|
2011-07-31 10:46:29 +02:00
|
|
|
|
|
2012-04-14 12:52:56 +02:00
|
|
|
|
from config.models import config
|
2012-06-11 13:43:48 +02:00
|
|
|
|
from settings import SITE_ROOT
|
2012-02-19 19:27:00 +01:00
|
|
|
|
|
2012-04-14 14:24:13 +02:00
|
|
|
|
from utils.utils import template, permission_required, gen_confirm_form, del_confirm_form, ajax_request
|
|
|
|
|
from utils.pdf import stylesheet
|
2012-06-23 10:27:58 +02:00
|
|
|
|
from utils.views import FormView, DeleteView, PDFView, RedirectView
|
2012-04-14 14:24:13 +02:00
|
|
|
|
from utils.template import Tab
|
2012-06-24 21:55:46 +02:00
|
|
|
|
from utils.translation_ext import ugettext
|
2012-04-14 14:24:13 +02:00
|
|
|
|
|
2012-06-23 10:43:01 +02:00
|
|
|
|
from projector.projector import Widget
|
2012-06-11 13:43:48 +02:00
|
|
|
|
|
2012-02-19 19:27:00 +01:00
|
|
|
|
from poll.views import PollFormView
|
|
|
|
|
|
2012-06-23 10:27:58 +02:00
|
|
|
|
from agenda.models import Item
|
|
|
|
|
|
2012-02-19 19:27:00 +01:00
|
|
|
|
from assignment.models import Assignment, AssignmentPoll, AssignmentOption
|
2012-03-16 14:31:59 +01:00
|
|
|
|
from assignment.forms import AssignmentForm, AssignmentRunForm, ConfigForm
|
2012-02-19 19:27:00 +01:00
|
|
|
|
|
2011-07-31 10:46:29 +02:00
|
|
|
|
from participant.models import Profile
|
|
|
|
|
|
2012-05-23 14:30:06 +02:00
|
|
|
|
|
2011-09-04 12:21:58 +02:00
|
|
|
|
@permission_required('assignment.can_see_assignment')
|
2011-07-31 10:46:29 +02:00
|
|
|
|
@template('assignment/overview.html')
|
|
|
|
|
def get_overview(request):
|
|
|
|
|
query = Assignment.objects
|
|
|
|
|
if 'status' in request.GET and '---' not in request.GET['status']:
|
|
|
|
|
query = query.filter(status__iexact=request.GET['status'])
|
|
|
|
|
try:
|
|
|
|
|
sort = request.GET['sort']
|
|
|
|
|
if sort in ['name','status']:
|
|
|
|
|
query = query.order_by(sort)
|
|
|
|
|
except KeyError:
|
2011-10-05 21:35:20 +02:00
|
|
|
|
query = query.order_by('name')
|
2011-07-31 10:46:29 +02:00
|
|
|
|
if 'reverse' in request.GET:
|
|
|
|
|
query = query.reverse()
|
|
|
|
|
|
|
|
|
|
assignments = query.all()
|
|
|
|
|
return {
|
|
|
|
|
'assignments': assignments,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2011-09-04 12:21:58 +02:00
|
|
|
|
@permission_required('assignment.can_see_assignment')
|
2011-07-31 10:46:29 +02:00
|
|
|
|
@template('assignment/view.html')
|
|
|
|
|
def view(request, assignment_id=None):
|
|
|
|
|
form = None
|
|
|
|
|
assignment = Assignment.objects.get(pk=assignment_id)
|
|
|
|
|
if request.method == 'POST':
|
|
|
|
|
if request.user.has_perm('assignment.can_nominate_other'):
|
2011-09-03 10:52:29 +02:00
|
|
|
|
form = AssignmentRunForm(request.POST)
|
2011-07-31 10:46:29 +02:00
|
|
|
|
if form.is_valid():
|
|
|
|
|
user = form.cleaned_data['candidate']
|
|
|
|
|
try:
|
|
|
|
|
assignment.run(user)
|
|
|
|
|
messages.success(request, _("Candidate <b>%s</b> was nominated successfully.") % (user))
|
|
|
|
|
except NameError, e:
|
|
|
|
|
messages.error(request, e)
|
|
|
|
|
else:
|
|
|
|
|
if request.user.has_perm('assignment.can_nominate_other'):
|
2011-09-03 10:52:29 +02:00
|
|
|
|
form = AssignmentRunForm()
|
2011-07-31 10:46:29 +02:00
|
|
|
|
|
2012-06-24 23:31:06 +02:00
|
|
|
|
# why not use assignment.vote_results?
|
2012-04-15 14:58:05 +02:00
|
|
|
|
polls = assignment.poll_set.all()
|
2011-07-31 10:46:29 +02:00
|
|
|
|
votes = []
|
2012-06-24 23:31:06 +02:00
|
|
|
|
options = []
|
|
|
|
|
for poll in polls:
|
|
|
|
|
options += poll.get_options()
|
|
|
|
|
|
|
|
|
|
for candidate in set([option.candidate for option in options]):
|
2012-04-15 14:58:05 +02:00
|
|
|
|
tmplist = ((candidate, assignment.is_elected(candidate)), [])
|
|
|
|
|
for poll in polls:
|
2012-04-14 14:24:13 +02:00
|
|
|
|
if (poll.published and not request.user.has_perm('assignment.can_manage_assignment')) or request.user.has_perm('assignment.can_manage_assignment'):
|
2012-04-15 14:58:05 +02:00
|
|
|
|
# candidate exists in poll
|
|
|
|
|
if poll.get_options().filter(candidate=candidate).exists():
|
2012-04-14 14:24:13 +02:00
|
|
|
|
option = AssignmentOption.objects.filter(poll=poll).get(candidate=candidate)
|
|
|
|
|
try:
|
2012-06-18 09:48:27 +02:00
|
|
|
|
tmplist[1].append(poll.get_form_values(option.id))
|
2012-04-14 14:24:13 +02:00
|
|
|
|
except IndexError:
|
|
|
|
|
tmplist[1].append('–')
|
|
|
|
|
else:
|
|
|
|
|
tmplist[1].append("-")
|
2012-04-15 14:58:05 +02:00
|
|
|
|
votes.append(tmplist)
|
2012-02-19 19:27:00 +01:00
|
|
|
|
return {
|
|
|
|
|
'assignment': assignment,
|
|
|
|
|
'form': form,
|
|
|
|
|
'votes': votes,
|
|
|
|
|
'polls': polls,
|
|
|
|
|
}
|
2011-07-31 10:46:29 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@permission_required('assignment.can_manage_assignment')
|
|
|
|
|
@template('assignment/edit.html')
|
|
|
|
|
def edit(request, assignment_id=None):
|
|
|
|
|
"""
|
|
|
|
|
View zum editieren und neuanlegen von Wahlen
|
|
|
|
|
"""
|
|
|
|
|
if assignment_id is not None:
|
|
|
|
|
assignment = Assignment.objects.get(id=assignment_id)
|
|
|
|
|
else:
|
|
|
|
|
assignment = None
|
|
|
|
|
|
|
|
|
|
if request.method == 'POST':
|
2011-09-03 10:52:29 +02:00
|
|
|
|
form = AssignmentForm(request.POST, instance=assignment)
|
2011-07-31 10:46:29 +02:00
|
|
|
|
if form.is_valid():
|
2011-09-03 19:25:05 +02:00
|
|
|
|
assignment = form.save()
|
2011-07-31 10:46:29 +02:00
|
|
|
|
if assignment_id is None:
|
|
|
|
|
messages.success(request, _('New election was successfully created.'))
|
|
|
|
|
else:
|
|
|
|
|
messages.success(request, _('Election was successfully modified.'))
|
2011-09-03 19:25:05 +02:00
|
|
|
|
if not 'apply' in request.POST:
|
|
|
|
|
return redirect(reverse("assignment_overview"))
|
|
|
|
|
if assignment_id is None:
|
|
|
|
|
return redirect(reverse('assignment_edit', args=[assignment.id]))
|
2011-11-24 19:42:32 +01:00
|
|
|
|
else:
|
|
|
|
|
messages.error(request, _('Please check the form for errors.'))
|
2011-07-31 10:46:29 +02:00
|
|
|
|
else:
|
2011-09-03 10:52:29 +02:00
|
|
|
|
form = AssignmentForm(instance=assignment)
|
2012-04-28 23:10:19 +02:00
|
|
|
|
if assignment:
|
|
|
|
|
polls = assignment.poll_set.filter(assignment=assignment)
|
|
|
|
|
else:
|
|
|
|
|
polls = None
|
2011-07-31 10:46:29 +02:00
|
|
|
|
return {
|
|
|
|
|
'form': form,
|
|
|
|
|
'assignment': assignment,
|
2012-04-28 23:10:19 +02:00
|
|
|
|
'polls': polls,
|
2011-07-31 10:46:29 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@permission_required('assignment.can_manage_assignment')
|
|
|
|
|
def delete(request, assignment_id):
|
|
|
|
|
assignment = Assignment.objects.get(pk=assignment_id)
|
|
|
|
|
if request.method == 'POST':
|
|
|
|
|
assignment.delete()
|
|
|
|
|
messages.success(request, _('Election <b>%s</b> was successfully deleted.') % assignment)
|
|
|
|
|
else:
|
|
|
|
|
del_confirm_form(request, assignment)
|
|
|
|
|
return redirect(reverse('assignment_overview'))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@permission_required('assignment.can_manage_assignment')
|
|
|
|
|
@template('assignment/view.html')
|
|
|
|
|
def set_status(request, assignment_id=None, status=None):
|
|
|
|
|
try:
|
|
|
|
|
if status is not None:
|
|
|
|
|
assignment = Assignment.objects.get(pk=assignment_id)
|
|
|
|
|
assignment.set_status(status)
|
|
|
|
|
messages.success(request, _('Election status was set to: <b>%s</b>.') % assignment.get_status_display())
|
|
|
|
|
except Assignment.DoesNotExist:
|
|
|
|
|
pass
|
|
|
|
|
return redirect(reverse('assignment_view', args=[assignment_id]))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@permission_required('assignment.can_nominate_self')
|
|
|
|
|
def run(request, assignment_id):
|
|
|
|
|
assignment = Assignment.objects.get(pk=assignment_id)
|
|
|
|
|
try:
|
|
|
|
|
assignment.run(request.user.profile)
|
|
|
|
|
messages.success(request, _('You have set your candidature successfully.') )
|
|
|
|
|
except NameError, e:
|
|
|
|
|
messages.error(request, e)
|
|
|
|
|
except Profile.DoesNotExist:
|
|
|
|
|
messages.error(request,
|
|
|
|
|
_("You can't candidate. Your user account is only for administration."))
|
2012-06-28 20:10:48 +02:00
|
|
|
|
return redirect(reverse('assignment_view', args=[assignment_id]))
|
2011-07-31 10:46:29 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@login_required
|
|
|
|
|
def delrun(request, assignment_id):
|
|
|
|
|
assignment = Assignment.objects.get(pk=assignment_id)
|
|
|
|
|
assignment.delrun(request.user.profile)
|
|
|
|
|
messages.success(request, _("You have withdrawn your candidature successfully.") )
|
2012-06-28 20:10:48 +02:00
|
|
|
|
return redirect(reverse('assignment_view', args=[assignment_id]))
|
2011-07-31 10:46:29 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@permission_required('assignment.can_manage_assignment')
|
|
|
|
|
def delother(request, assignment_id, profile_id):
|
|
|
|
|
assignment = Assignment.objects.get(pk=assignment_id)
|
|
|
|
|
profile = Profile.objects.get(pk=profile_id)
|
|
|
|
|
|
|
|
|
|
if request.method == 'POST':
|
|
|
|
|
assignment.delrun(profile)
|
|
|
|
|
messages.success(request, _("Candidate <b>%s</b> was withdrawn successfully.") % (profile))
|
|
|
|
|
else:
|
|
|
|
|
gen_confirm_form(request,
|
|
|
|
|
_("Do you really want to withdraw <b>%s</b> from the election?") \
|
|
|
|
|
% profile, reverse('assignment_delother', args=[assignment_id, profile_id]))
|
2012-06-28 20:10:48 +02:00
|
|
|
|
return redirect(reverse('assignment_view', args=[assignment_id]))
|
2011-07-31 10:46:29 +02:00
|
|
|
|
|
|
|
|
|
|
2012-04-21 22:27:11 +02:00
|
|
|
|
@permission_required('assignment.can_manage_assignment')
|
2011-11-01 14:45:24 +01:00
|
|
|
|
def set_active(request, assignment_id):
|
2012-02-03 23:12:28 +01:00
|
|
|
|
assignment = Assignment.objects.get(pk=assignment_id)
|
|
|
|
|
assignment.set_active()
|
2011-11-01 14:45:24 +01:00
|
|
|
|
return redirect(reverse('assignment_view', args=[assignment_id]))
|
|
|
|
|
|
2012-03-03 11:16:10 +01:00
|
|
|
|
|
2011-07-31 10:46:29 +02:00
|
|
|
|
@permission_required('assignment.can_manage_assignment')
|
2011-09-03 10:52:29 +02:00
|
|
|
|
def gen_poll(request, assignment_id):
|
2012-02-19 19:27:00 +01:00
|
|
|
|
poll = Assignment.objects.get(pk=assignment_id).gen_poll()
|
|
|
|
|
messages.success(request, _("New ballot was successfully created.") )
|
2011-09-03 10:52:29 +02:00
|
|
|
|
return redirect(reverse('assignment_poll_view', args=[poll.id]))
|
2011-07-31 10:46:29 +02:00
|
|
|
|
|
|
|
|
|
|
2012-02-19 19:27:00 +01:00
|
|
|
|
class ViewPoll(PollFormView):
|
|
|
|
|
poll_class = AssignmentPoll
|
|
|
|
|
template_name = 'assignment/poll_view.html'
|
|
|
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
|
context = super(ViewPoll, self).get_context_data(**kwargs)
|
|
|
|
|
self.assignment = self.poll.get_assignment()
|
2012-04-21 22:27:11 +02:00
|
|
|
|
context['assignment'] = self.assignment
|
|
|
|
|
context['poll'] = self.poll
|
2012-04-28 23:10:19 +02:00
|
|
|
|
context['polls'] = self.assignment.poll_set.filter(assignment=self.assignment)
|
2012-05-14 21:34:46 +02:00
|
|
|
|
context['ballotnumber'] = self.poll.get_ballot()
|
2012-02-19 19:27:00 +01:00
|
|
|
|
return context
|
|
|
|
|
|
|
|
|
|
def get_success_url(self):
|
|
|
|
|
if not 'apply' in self.request.POST:
|
|
|
|
|
return reverse('assignment_view', args=[self.poll.assignment.id])
|
|
|
|
|
return ''
|
|
|
|
|
|
2011-07-31 10:46:29 +02:00
|
|
|
|
|
2011-09-08 12:30:34 +02:00
|
|
|
|
@permission_required('assignment.can_manage_assignment')
|
2012-05-20 20:02:36 +02:00
|
|
|
|
def set_publish_status(request, poll_id):
|
2011-09-08 12:30:34 +02:00
|
|
|
|
try:
|
2012-03-03 11:16:10 +01:00
|
|
|
|
poll = AssignmentPoll.objects.get(pk=poll_id)
|
2011-09-08 12:30:34 +02:00
|
|
|
|
if poll.published:
|
2012-05-20 20:02:36 +02:00
|
|
|
|
poll.set_published(False)
|
2011-09-08 12:30:34 +02:00
|
|
|
|
else:
|
2012-05-20 20:02:36 +02:00
|
|
|
|
poll.set_published(True)
|
2012-03-03 11:16:10 +01:00
|
|
|
|
except AssignmentPoll.DoesNotExist:
|
2012-05-20 20:02:36 +02:00
|
|
|
|
messages.error(request, _('Ballot ID %d does not exist.') % int(poll_id))
|
|
|
|
|
return redirect(reverse('assignment_view', args=[poll.assignment.id]))
|
2012-05-19 16:25:55 +02:00
|
|
|
|
|
|
|
|
|
if request.is_ajax():
|
2012-05-20 20:02:36 +02:00
|
|
|
|
return ajax_request({'published': poll.published})
|
2011-07-31 10:46:29 +02:00
|
|
|
|
|
2012-05-20 20:02:36 +02:00
|
|
|
|
if poll.published:
|
|
|
|
|
messages.success(request, _("Ballot successfully published.") )
|
|
|
|
|
else:
|
|
|
|
|
messages.success(request, _("Ballot successfully unpublished.") )
|
|
|
|
|
return redirect(reverse('assignment_view', args=[poll.assignment.id]))
|
2012-03-03 11:16:10 +01:00
|
|
|
|
|
|
|
|
|
|
2011-09-03 11:56:35 +02:00
|
|
|
|
@permission_required('assignment.can_manage_assignment')
|
|
|
|
|
def set_elected(request, assignment_id, profile_id, elected=True):
|
|
|
|
|
assignment = Assignment.objects.get(pk=assignment_id)
|
|
|
|
|
profile = Profile.objects.get(pk=profile_id)
|
|
|
|
|
assignment.set_elected(profile, elected)
|
|
|
|
|
|
|
|
|
|
if request.is_ajax():
|
|
|
|
|
if elected:
|
|
|
|
|
link = reverse('assignment_user_not_elected', args=[assignment.id, profile.id])
|
|
|
|
|
text = _('not elected')
|
|
|
|
|
else:
|
|
|
|
|
link = reverse('assignment_user_elected', args=[assignment.id, profile.id])
|
|
|
|
|
text = _('elected')
|
|
|
|
|
return ajax_request({'elected': elected,
|
|
|
|
|
'link': link,
|
|
|
|
|
'text': text})
|
|
|
|
|
|
|
|
|
|
return redirect(reverse('assignment_view', args=[assignment_id]))
|
2012-03-16 14:31:59 +01:00
|
|
|
|
|
|
|
|
|
|
2012-04-14 14:24:13 +02:00
|
|
|
|
class AssignmentPollDelete(DeleteView):
|
|
|
|
|
"""
|
|
|
|
|
Delete an assignment poll object.
|
|
|
|
|
"""
|
|
|
|
|
permission_required = 'assignment.can_manage_assignment'
|
|
|
|
|
model = AssignmentPoll
|
|
|
|
|
|
|
|
|
|
def pre_redirect(self, request, *args, **kwargs):
|
|
|
|
|
self.set_assignment()
|
|
|
|
|
super(AssignmentPollDelete, self).pre_redirect(request, *args, **kwargs)
|
|
|
|
|
|
|
|
|
|
def pre_post_redirect(self, request, *args, **kwargs):
|
|
|
|
|
self.set_assignment()
|
|
|
|
|
super(AssignmentPollDelete, self).pre_post_redirect(request, *args, **kwargs)
|
|
|
|
|
|
|
|
|
|
def set_assignment(self):
|
2012-04-14 19:14:51 +02:00
|
|
|
|
self.assignment = self.object.assignment
|
2012-04-14 14:24:13 +02:00
|
|
|
|
|
|
|
|
|
def get_redirect_url(self, **kwargs):
|
2012-04-14 19:14:51 +02:00
|
|
|
|
return reverse('assignment_view', args=[self.assignment.id])
|
|
|
|
|
|
2012-04-14 14:24:13 +02:00
|
|
|
|
|
|
|
|
|
class AssignmentPDF(PDFView):
|
2012-05-14 22:02:45 +02:00
|
|
|
|
permission_required = 'assignment.can_see_assignment'
|
2012-04-14 14:24:13 +02:00
|
|
|
|
top_space = 0
|
|
|
|
|
|
2012-04-29 18:54:42 +02:00
|
|
|
|
def get_filename(self):
|
|
|
|
|
try:
|
|
|
|
|
assignment_id = self.kwargs['assignment_id']
|
|
|
|
|
assignment = Assignment.objects.get(id=assignment_id)
|
|
|
|
|
filename = u'%s-%s' % (_("Assignment"), assignment.name.replace(' ','_'))
|
|
|
|
|
except:
|
|
|
|
|
filename = _("Elections")
|
|
|
|
|
return filename
|
|
|
|
|
|
2012-04-14 14:24:13 +02:00
|
|
|
|
def append_to_pdf(self, story):
|
|
|
|
|
try:
|
|
|
|
|
assignment_id = self.kwargs['assignment_id']
|
|
|
|
|
except KeyError:
|
|
|
|
|
assignment_id = None
|
2012-04-21 22:27:11 +02:00
|
|
|
|
if assignment_id is None: #print all assignments
|
2012-04-14 14:24:13 +02:00
|
|
|
|
title = config["assignment_pdf_title"]
|
|
|
|
|
story.append(Paragraph(title, stylesheet['Heading1']))
|
|
|
|
|
preamble = config["assignment_pdf_preamble"]
|
|
|
|
|
if preamble:
|
|
|
|
|
story.append(Paragraph("%s" % preamble.replace('\r\n','<br/>'), stylesheet['Paragraph']))
|
|
|
|
|
story.append(Spacer(0,0.75*cm))
|
|
|
|
|
# List of assignments
|
|
|
|
|
for assignment in Assignment.objects.order_by('name'):
|
|
|
|
|
story.append(Paragraph(assignment.name, stylesheet['Heading3']))
|
|
|
|
|
# Assignment details (each assignment on single page)
|
|
|
|
|
for assignment in Assignment.objects.order_by('name'):
|
|
|
|
|
story.append(PageBreak())
|
|
|
|
|
story = self.get_assignment(assignment, story)
|
|
|
|
|
else: # print selected assignment
|
|
|
|
|
assignment = Assignment.objects.get(id=assignment_id)
|
|
|
|
|
story = self.get_assignment(assignment, story)
|
|
|
|
|
|
|
|
|
|
def get_assignment(self, assignment, story):
|
|
|
|
|
# title
|
|
|
|
|
story.append(Paragraph(_("Election")+": %s" % assignment.name, stylesheet['Heading1']))
|
|
|
|
|
story.append(Spacer(0,0.5*cm))
|
|
|
|
|
# posts
|
|
|
|
|
cell1a = []
|
|
|
|
|
cell1a.append(Paragraph("<font name='Ubuntu-Bold'>%s:</font>" % _("Number of available posts"), stylesheet['Bold']))
|
|
|
|
|
cell1b = []
|
|
|
|
|
cell1b.append(Paragraph(str(assignment.posts), stylesheet['Paragraph']))
|
|
|
|
|
# candidates
|
|
|
|
|
cell2a = []
|
|
|
|
|
cell2a.append(Paragraph("<font name='Ubuntu-Bold'>%s:</font><seqreset id='counter'>" % _("Candidates"), stylesheet['Heading4']))
|
|
|
|
|
cell2b = []
|
|
|
|
|
for c in assignment.profile.all():
|
|
|
|
|
cell2b.append(Paragraph("<seq id='counter'/>. %s" % unicode(c), stylesheet['Signaturefield']))
|
|
|
|
|
if assignment.status == "sea":
|
|
|
|
|
for x in range(0,2*assignment.posts):
|
|
|
|
|
cell2b.append(Paragraph("<seq id='counter'/>. __________________________________________",stylesheet['Signaturefield']))
|
|
|
|
|
cell2b.append(Spacer(0,0.2*cm))
|
|
|
|
|
# vote results
|
|
|
|
|
table_votes = []
|
|
|
|
|
results = self.get_assignment_votes(assignment)
|
|
|
|
|
cell3a = []
|
|
|
|
|
cell3a.append(Paragraph("%s:" % (_("Vote results")), stylesheet['Heading4']))
|
|
|
|
|
if len(results) > 0:
|
|
|
|
|
if len(results[0]) >= 1:
|
|
|
|
|
cell3a.append(Paragraph("%s %s" % (len(results[0][1]), _("ballots")), stylesheet['Normal']))
|
|
|
|
|
if len(results[0][1]) > 0:
|
|
|
|
|
data_votes = []
|
|
|
|
|
# add table head row
|
|
|
|
|
headrow = []
|
|
|
|
|
headrow.append(_("Candidates"))
|
|
|
|
|
for i in range (0,len(results[0][1])):
|
|
|
|
|
headrow.append("%s." %(str(i+1)))
|
|
|
|
|
data_votes.append(headrow)
|
|
|
|
|
# add result rows
|
|
|
|
|
for candidate in results:
|
|
|
|
|
row = []
|
|
|
|
|
if candidate[0][1]:
|
|
|
|
|
elected = "* "
|
|
|
|
|
else:
|
|
|
|
|
elected = ""
|
|
|
|
|
c = str(candidate[0][0]).split("(",1)
|
|
|
|
|
if len(c) > 1:
|
|
|
|
|
row.append(elected+c[0]+"\n"+"("+c[1])
|
|
|
|
|
else:
|
|
|
|
|
row.append(elected+str(candidate[0][0]))
|
|
|
|
|
for votes in candidate[1]:
|
|
|
|
|
if type(votes) == type(list()):
|
|
|
|
|
tmp = _("Y")+": "+str(votes[0])+"\n"
|
|
|
|
|
tmp += _("N")+": "+str(votes[1])+"\n"
|
|
|
|
|
tmp += _("A")+": "+str(votes[2])
|
|
|
|
|
row.append(tmp)
|
|
|
|
|
else:
|
|
|
|
|
row.append(str(votes))
|
|
|
|
|
|
|
|
|
|
data_votes.append(row)
|
|
|
|
|
polls = []
|
|
|
|
|
for poll in assignment.poll_set.filter(assignment=assignment):
|
|
|
|
|
polls.append(poll)
|
|
|
|
|
# votes invalid
|
|
|
|
|
row = []
|
|
|
|
|
row.append(_("Invalid votes"))
|
|
|
|
|
for p in polls:
|
|
|
|
|
if p.published:
|
2012-04-17 21:16:55 +02:00
|
|
|
|
row.append(p.print_votesinvalid())
|
2012-04-14 14:24:13 +02:00
|
|
|
|
data_votes.append(row)
|
|
|
|
|
|
|
|
|
|
# votes cast
|
|
|
|
|
row = []
|
|
|
|
|
row.append(_("Votes cast"))
|
|
|
|
|
for p in polls:
|
|
|
|
|
if p.published:
|
2012-04-17 21:16:55 +02:00
|
|
|
|
row.append(p.print_votescast())
|
2012-04-14 14:24:13 +02:00
|
|
|
|
data_votes.append(row)
|
|
|
|
|
|
|
|
|
|
table_votes=Table(data_votes)
|
|
|
|
|
table_votes.setStyle( TableStyle([
|
|
|
|
|
('GRID', (0,0), (-1,-1), 0.5, colors.grey),
|
|
|
|
|
('VALIGN',(0,0),(-1,-1), 'TOP'),
|
|
|
|
|
('LINEABOVE',(0,0),(-1,0),2,colors.black),
|
|
|
|
|
('LINEABOVE',(0,1),(-1,1),1,colors.black),
|
|
|
|
|
('LINEBELOW',(0,-1),(-1,-1),2,colors.black),
|
|
|
|
|
('ROWBACKGROUNDS', (0, 1), (-1, -1), (colors.white, (.9, .9, .9))),
|
|
|
|
|
]))
|
|
|
|
|
|
|
|
|
|
# table
|
|
|
|
|
data = []
|
|
|
|
|
data.append([cell1a,cell1b])
|
|
|
|
|
if table_votes:
|
|
|
|
|
data.append([cell3a,table_votes])
|
|
|
|
|
data.append(['','* = '+_('elected')])
|
|
|
|
|
else:
|
|
|
|
|
data.append([cell2a,cell2b])
|
|
|
|
|
data.append([Spacer(0,0.2*cm),''])
|
|
|
|
|
t=Table(data)
|
|
|
|
|
t._argW[0]=4.5*cm
|
|
|
|
|
t._argW[1]=11*cm
|
|
|
|
|
t.setStyle(TableStyle([ ('BOX', (0,0), (-1,-1), 1, colors.black),
|
|
|
|
|
('VALIGN', (0,0), (-1,-1), 'TOP'),
|
|
|
|
|
]))
|
|
|
|
|
story.append(t)
|
|
|
|
|
story.append(Spacer(0,1*cm))
|
|
|
|
|
# text
|
|
|
|
|
story.append(Paragraph("%s" % assignment.description.replace('\r\n','<br/>'), stylesheet['Paragraph']))
|
|
|
|
|
return story
|
|
|
|
|
|
|
|
|
|
def get_assignment_votes(self, assignment):
|
|
|
|
|
votes = []
|
|
|
|
|
for candidate in assignment.candidates:
|
2012-04-15 14:58:05 +02:00
|
|
|
|
tmplist = ((candidate, assignment.is_elected(candidate)), [])
|
2012-04-14 14:24:13 +02:00
|
|
|
|
for poll in assignment.poll_set.all():
|
|
|
|
|
if poll.published:
|
2012-04-15 14:58:05 +02:00
|
|
|
|
if poll.get_options().filter(candidate=candidate).exists():
|
2012-04-14 14:24:13 +02:00
|
|
|
|
option = AssignmentOption.objects.filter(poll=poll).get(candidate=candidate)
|
|
|
|
|
try:
|
|
|
|
|
tmplist[1].append(option.get_votes()[0])
|
|
|
|
|
except IndexError:
|
|
|
|
|
tmplist[1].append('–')
|
|
|
|
|
else:
|
|
|
|
|
tmplist[1].append("-")
|
2012-04-15 14:58:05 +02:00
|
|
|
|
votes.append(tmplist)
|
2012-04-14 14:24:13 +02:00
|
|
|
|
return votes
|
|
|
|
|
|
|
|
|
|
|
2012-06-23 10:27:58 +02:00
|
|
|
|
class CreateAgendaItem(RedirectView):
|
|
|
|
|
permission_required = 'agenda.can_manage_agenda'
|
|
|
|
|
|
|
|
|
|
def pre_redirect(self, request, *args, **kwargs):
|
|
|
|
|
self.assignment = Assignment.objects.get(pk=kwargs['assignment_id'])
|
|
|
|
|
self.item = Item(releated_sid=self.assignment.sid)
|
|
|
|
|
self.item.save()
|
|
|
|
|
|
|
|
|
|
def get_redirect_url(self, **kwargs):
|
|
|
|
|
return reverse('item_overview')
|
|
|
|
|
|
|
|
|
|
|
2012-04-20 14:33:40 +02:00
|
|
|
|
class AssignmentPollPDF(PDFView):
|
|
|
|
|
permission_required = 'assignment.can_manage_assignment'
|
|
|
|
|
top_space = 0
|
|
|
|
|
|
|
|
|
|
def get(self, request, *args, **kwargs):
|
|
|
|
|
self.poll = AssignmentPoll.objects.get(id=self.kwargs['poll_id'])
|
|
|
|
|
return super(AssignmentPollPDF, self).get(request, *args, **kwargs)
|
|
|
|
|
|
|
|
|
|
def get_filename(self):
|
|
|
|
|
filename = u'%s-%s-#%s' % (_("Election"), self.poll.assignment.name.replace(' ','_'), 1)#self.poll.get_ballot())
|
|
|
|
|
return filename
|
|
|
|
|
|
2012-04-29 18:54:42 +02:00
|
|
|
|
def get_template(self, buffer):
|
|
|
|
|
return SimpleDocTemplate(buffer, topMargin=-6, bottomMargin=-6, leftMargin=0, rightMargin=0, showBoundary=False)
|
2012-04-20 14:33:40 +02:00
|
|
|
|
|
2012-04-29 18:54:42 +02:00
|
|
|
|
def build_document(self, pdf_document, story):
|
|
|
|
|
pdf_document.build(story)
|
|
|
|
|
|
|
|
|
|
def append_to_pdf(self, story):
|
2012-04-20 14:33:40 +02:00
|
|
|
|
imgpath = os.path.join(SITE_ROOT, 'static/images/circle.png')
|
|
|
|
|
circle = "<img src='%s' width='15' height='15'/> " % imgpath
|
|
|
|
|
cell = []
|
|
|
|
|
cell.append(Spacer(0,0.8*cm))
|
|
|
|
|
cell.append(Paragraph(_("Election") + ": " + self.poll.assignment.name, stylesheet['Ballot_title']))
|
2012-05-14 21:34:46 +02:00
|
|
|
|
cell.append(Paragraph(self.poll.assignment.polldescription, stylesheet['Ballot_subtitle']))
|
2012-04-20 14:33:40 +02:00
|
|
|
|
options = self.poll.get_options().order_by('candidate')
|
2012-05-14 21:34:46 +02:00
|
|
|
|
cell.append(Paragraph(str(self.poll.get_ballot())+". "+_("ballot")+", "+str(len(options))+" "+ ungettext("candidate", "candidates", len(options))+", "+str(self.poll.assignment.posts)+" "+_("available posts"), stylesheet['Ballot_description']))
|
2012-04-20 14:33:40 +02:00
|
|
|
|
cell.append(Spacer(0,0.4*cm))
|
|
|
|
|
|
|
|
|
|
data= []
|
|
|
|
|
# get ballot papers config values
|
|
|
|
|
ballot_papers_selection = config["assignment_pdf_ballot_papers_selection"]
|
|
|
|
|
ballot_papers_number = config["assignment_pdf_ballot_papers_number"]
|
2012-06-29 00:19:06 +02:00
|
|
|
|
|
|
|
|
|
# set number of ballot papers
|
|
|
|
|
if ballot_papers_selection == "NUMBER_OF_DELEGATES":
|
|
|
|
|
number = User.objects.filter(profile__type__iexact="delegate").count()
|
|
|
|
|
elif ballot_papers_selection == "NUMBER_OF_ALL_PARTICIPANTS":
|
|
|
|
|
number = int(Profile.objects.count())
|
|
|
|
|
else: # ballot_papers_selection == "CUSTOM_NUMBER"
|
|
|
|
|
number = int(ballot_papers_number)
|
|
|
|
|
number = max(1, number)
|
|
|
|
|
|
2012-06-28 18:45:00 +02:00
|
|
|
|
# Choose kind of ballot paper
|
2012-06-28 22:49:02 +02:00
|
|
|
|
if self.poll.yesnoabstain:
|
2012-06-28 18:45:00 +02:00
|
|
|
|
for option in options:
|
2012-06-29 00:19:06 +02:00
|
|
|
|
candidate = option.candidate
|
|
|
|
|
cell.append(Paragraph(candidate.user.get_full_name(), stylesheet['Ballot_option_name']))
|
|
|
|
|
if candidate.group:
|
|
|
|
|
cell.append(Paragraph("(%s)" % candidate.group, stylesheet['Ballot_option_group']))
|
2012-06-28 18:45:00 +02:00
|
|
|
|
else:
|
|
|
|
|
cell.append(Paragraph(" ", stylesheet['Ballot_option_group']))
|
|
|
|
|
cell.append(Paragraph(circle+_("Yes")+" "+circle+_("No")+" "+circle+_("Abstention"), stylesheet['Ballot_option_YNA']))
|
|
|
|
|
# print ballot papers
|
2012-06-29 00:19:06 +02:00
|
|
|
|
for user in xrange(number / 2):
|
|
|
|
|
data.append([cell, cell])
|
2012-04-20 14:33:40 +02:00
|
|
|
|
rest = number % 2
|
|
|
|
|
if rest:
|
2012-06-29 00:19:06 +02:00
|
|
|
|
data.append([cell, ''])
|
2012-06-28 18:45:00 +02:00
|
|
|
|
if len(options) <= 2:
|
2012-06-29 00:19:06 +02:00
|
|
|
|
t = Table(data, 10.5*cm, 7.42*cm)
|
2012-06-28 18:45:00 +02:00
|
|
|
|
elif len(options) <= 5:
|
2012-06-29 00:19:06 +02:00
|
|
|
|
t = Table(data, 10.5*cm, 14.84*cm)
|
2012-06-28 18:45:00 +02:00
|
|
|
|
else:
|
2012-06-29 00:19:06 +02:00
|
|
|
|
t = Table(data, 10.5*cm, 29.7*cm)
|
2012-04-20 14:33:40 +02:00
|
|
|
|
else:
|
2012-06-28 18:45:00 +02:00
|
|
|
|
for option in options:
|
2012-06-29 00:19:06 +02:00
|
|
|
|
candidate = option.candidate
|
|
|
|
|
cell.append(Paragraph(circle + candidate.user.get_full_name(), stylesheet['Ballot_option_name']))
|
|
|
|
|
if candidate.group:
|
|
|
|
|
cell.append(Paragraph("(%s)" % candidate.group, stylesheet['Ballot_option_group_right']))
|
2012-06-28 18:45:00 +02:00
|
|
|
|
else:
|
|
|
|
|
cell.append(Paragraph(" ", stylesheet['Ballot_option_group_right']))
|
|
|
|
|
# print ballot papers
|
2012-06-29 00:19:06 +02:00
|
|
|
|
for user in xrange(number / 2):
|
|
|
|
|
data.append([cell, cell])
|
|
|
|
|
rest = number % 2
|
|
|
|
|
if rest:
|
|
|
|
|
data.append([cell, ''])
|
2012-06-28 18:45:00 +02:00
|
|
|
|
if len(options) <= 4:
|
2012-06-29 00:19:06 +02:00
|
|
|
|
t = Table(data, 10.5*cm, 7.42*cm)
|
2012-06-28 18:45:00 +02:00
|
|
|
|
elif len(options) <= 8:
|
2012-06-29 00:19:06 +02:00
|
|
|
|
t = Table(data, 10.5*cm, 14.84*cm)
|
2012-06-28 18:45:00 +02:00
|
|
|
|
else:
|
2012-06-29 00:19:06 +02:00
|
|
|
|
t = Table(data, 10.5*cm, 29.7*cm)
|
2012-06-28 22:49:02 +02:00
|
|
|
|
|
2012-04-20 14:33:40 +02:00
|
|
|
|
t.setStyle(TableStyle([ ('GRID', (0,0), (-1,-1), 0.25, colors.grey),
|
|
|
|
|
('VALIGN', (0,0), (-1,-1), 'TOP'),
|
|
|
|
|
]))
|
|
|
|
|
story.append(t)
|
|
|
|
|
|
|
|
|
|
|
2012-03-16 14:31:59 +01:00
|
|
|
|
class Config(FormView):
|
2012-04-14 12:52:56 +02:00
|
|
|
|
permission_required = 'config.can_manage_config'
|
2012-03-16 14:31:59 +01:00
|
|
|
|
form_class = ConfigForm
|
|
|
|
|
template_name = 'assignment/config.html'
|
|
|
|
|
|
|
|
|
|
def get_initial(self):
|
|
|
|
|
return {
|
|
|
|
|
'assignment_publish_winner_results_only': config['assignment_publish_winner_results_only'],
|
|
|
|
|
'assignment_pdf_ballot_papers_selection': config['assignment_pdf_ballot_papers_selection'],
|
|
|
|
|
'assignment_pdf_ballot_papers_number': config['assignment_pdf_ballot_papers_number'],
|
|
|
|
|
'assignment_pdf_title': config['assignment_pdf_title'],
|
|
|
|
|
'assignment_pdf_preamble': config['assignment_pdf_preamble'],
|
2012-06-18 09:48:27 +02:00
|
|
|
|
'assignment_poll_vote_values': config['assignment_poll_vote_values'],
|
2012-03-16 14:31:59 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
def form_valid(self, form):
|
|
|
|
|
if form.cleaned_data['assignment_publish_winner_results_only']:
|
|
|
|
|
config['assignment_publish_winner_results_only'] = True
|
|
|
|
|
else:
|
|
|
|
|
config['assignment_publish_winner_results_only'] = False
|
|
|
|
|
config['assignment_pdf_ballot_papers_selection'] = form.cleaned_data['assignment_pdf_ballot_papers_selection']
|
|
|
|
|
config['assignment_pdf_ballot_papers_number'] = form.cleaned_data['assignment_pdf_ballot_papers_number']
|
|
|
|
|
config['assignment_pdf_title'] = form.cleaned_data['assignment_pdf_title']
|
|
|
|
|
config['assignment_pdf_preamble'] = form.cleaned_data['assignment_pdf_preamble']
|
2012-06-18 09:48:27 +02:00
|
|
|
|
config['assignment_poll_vote_values'] = form.cleaned_data['assignment_poll_vote_values']
|
2012-03-16 14:31:59 +01:00
|
|
|
|
messages.success(self.request, _('Election settings successfully saved.'))
|
|
|
|
|
return super(Config, self).form_valid(form)
|
2012-03-18 17:11:58 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def register_tab(request):
|
|
|
|
|
selected = True if request.path.startswith('/assignment/') else False
|
|
|
|
|
return Tab(
|
|
|
|
|
title=_('Elections'),
|
|
|
|
|
url=reverse('assignment_overview'),
|
|
|
|
|
permission=request.user.has_perm('assignment.can_see_assignment') or request.user.has_perm('assignment.can_nominate_other') or request.user.has_perm('assignment.can_nominate_self') or request.user.has_perm('assignment.can_manage_assignment'),
|
|
|
|
|
selected=selected,
|
|
|
|
|
)
|
2012-06-11 13:43:48 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_widgets(request):
|
2012-06-23 10:43:01 +02:00
|
|
|
|
return [
|
2012-06-24 21:55:46 +02:00
|
|
|
|
Widget(name=ugettext('Assignments'), template='assignment/widget.html',
|
2012-06-23 10:43:01 +02:00
|
|
|
|
context={'assignments': Assignment.objects.all()})
|
|
|
|
|
]
|