2011-07-31 10:46:29 +02:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
"""
|
|
|
|
openslides.assignment.views
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
Views for the assignment app.
|
|
|
|
|
|
|
|
:copyright: 2011 by the OpenSlides team, see AUTHORS.
|
|
|
|
:license: GNU GPL, see LICENSE for more details.
|
|
|
|
"""
|
|
|
|
|
|
|
|
from django.shortcuts import redirect
|
|
|
|
from django.core.urlresolvers import reverse
|
|
|
|
from django.contrib import messages
|
|
|
|
from django.contrib.auth.decorators import login_required
|
|
|
|
from django.utils.translation import ugettext as _
|
|
|
|
|
2011-11-01 14:45:24 +01:00
|
|
|
from openslides.agenda.models import Item
|
2011-07-31 10:46:29 +02:00
|
|
|
from poll.models import Poll, Option
|
2011-09-03 13:53:37 +02:00
|
|
|
from poll.forms import OptionResultForm, PollForm
|
2011-07-31 10:46:29 +02:00
|
|
|
from assignment.models import Assignment
|
2011-09-03 10:52:29 +02:00
|
|
|
from assignment.forms import AssignmentForm, AssignmentRunForm
|
2011-09-03 11:42:44 +02:00
|
|
|
from utils.utils import template, permission_required, gen_confirm_form, del_confirm_form, ajax_request
|
2011-09-07 07:52:44 +02:00
|
|
|
from utils.pdf import print_assignment, print_assignment_poll
|
2011-07-31 10:46:29 +02:00
|
|
|
from participant.models import Profile
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
votes = []
|
2011-09-03 11:56:35 +02:00
|
|
|
for candidate in assignment.candidates:
|
|
|
|
tmplist = [[candidate, assignment.is_elected(candidate)], []]
|
2011-07-31 10:46:29 +02:00
|
|
|
for poll in assignment.poll_set.all():
|
2011-09-09 16:34:01 +02:00
|
|
|
if (poll.published and not request.user.has_perm('assignment.can_manage_assignment')) or request.user.has_perm('assignment.can_manage_assignment'):
|
|
|
|
if candidate in poll.options_values:
|
|
|
|
option = Option.objects.filter(poll=poll).filter(user=candidate)[0]
|
|
|
|
if poll.optiondecision:
|
|
|
|
tmplist[1].append([option.yes, option.no, option.undesided])
|
|
|
|
else:
|
|
|
|
tmplist[1].append(option.yes)
|
2011-07-31 10:46:29 +02:00
|
|
|
else:
|
2011-09-09 16:34:01 +02:00
|
|
|
tmplist[1].append("-")
|
2011-07-31 10:46:29 +02:00
|
|
|
votes.append(tmplist)
|
|
|
|
|
2011-09-03 13:53:37 +02:00
|
|
|
polls = []
|
|
|
|
for poll in assignment.poll_set.filter(assignment=assignment):
|
|
|
|
polls.append(poll)
|
2011-09-06 10:41:06 +02:00
|
|
|
|
2011-07-31 10:46:29 +02:00
|
|
|
return {'assignment': assignment,
|
|
|
|
'form': form,
|
2011-09-03 13:53:37 +02:00
|
|
|
'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)
|
2011-07-31 10:46:29 +02:00
|
|
|
return {
|
|
|
|
'form': form,
|
|
|
|
'assignment': assignment,
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@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."))
|
|
|
|
return redirect(reverse('assignment_view', args=assignment_id))
|
|
|
|
|
|
|
|
|
|
|
|
@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.") )
|
|
|
|
return redirect(reverse('assignment_view', args=assignment_id))
|
|
|
|
|
|
|
|
|
|
|
|
@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]))
|
|
|
|
return redirect(reverse('assignment_view', args=assignment_id))
|
|
|
|
|
|
|
|
|
2011-11-01 14:45:24 +01:00
|
|
|
@permission_required('assignment.can_manage_application')
|
|
|
|
def set_active(request, assignment_id):
|
2012-03-28 15:17:50 +02:00
|
|
|
item = Item.objects.get(itemassignment__assignment__id=assignment_id)
|
|
|
|
item.set_active(False)
|
2011-11-01 14:45:24 +01:00
|
|
|
return redirect(reverse('assignment_view', args=[assignment_id]))
|
|
|
|
|
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):
|
2011-07-31 10:46:29 +02:00
|
|
|
try:
|
|
|
|
poll = Assignment.objects.get(pk=assignment_id).gen_poll()
|
|
|
|
messages.success(request, _("New ballot was successfully created.") )
|
|
|
|
except Assignment.DoesNotExist:
|
|
|
|
pass
|
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
|
|
|
|
|
|
|
|
2011-09-03 16:20:10 +02:00
|
|
|
@permission_required('assignment.can_manage_assignment')
|
2011-07-31 10:46:29 +02:00
|
|
|
@template('assignment/poll_view.html')
|
2011-09-03 10:52:29 +02:00
|
|
|
def poll_view(request, poll_id):
|
2011-07-31 10:46:29 +02:00
|
|
|
poll = Poll.objects.get(pk=poll_id)
|
2011-09-03 10:52:29 +02:00
|
|
|
ballotnumber = poll.ballot
|
2011-07-31 10:46:29 +02:00
|
|
|
options = poll.options.order_by('user__user__first_name')
|
|
|
|
assignment = poll.assignment
|
|
|
|
if request.user.has_perm('assignment.can_manage_assignment'):
|
|
|
|
if request.method == 'POST':
|
2011-09-03 13:53:37 +02:00
|
|
|
form = PollForm(request.POST, prefix="poll")
|
2011-07-31 10:46:29 +02:00
|
|
|
if form.is_valid():
|
2011-09-03 00:43:09 +02:00
|
|
|
poll.votesinvalid = form.cleaned_data['invalid'] or 0
|
2011-09-03 13:53:37 +02:00
|
|
|
poll.votescast = form.cleaned_data['votescast'] or 0
|
2011-07-31 10:46:29 +02:00
|
|
|
poll.save()
|
|
|
|
|
|
|
|
success = 0
|
|
|
|
for option in options:
|
|
|
|
option.form = OptionResultForm(request.POST, prefix="o%d" % option.id)
|
|
|
|
if option.form.is_valid():
|
|
|
|
option.voteyes = option.form.cleaned_data['yes']
|
|
|
|
option.voteno = option.form.cleaned_data['no'] or 0
|
|
|
|
option.voteundesided = option.form.cleaned_data['undesided'] or 0
|
|
|
|
option.save()
|
|
|
|
success = success + 1
|
|
|
|
if success == options.count():
|
|
|
|
messages.success(request, _("Votes are successfully saved.") )
|
2011-09-03 16:20:10 +02:00
|
|
|
if not 'apply' in request.POST:
|
|
|
|
return redirect(reverse('assignment_view', args=[assignment.id]))
|
2011-07-31 10:46:29 +02:00
|
|
|
else:
|
2011-09-03 13:53:37 +02:00
|
|
|
form = PollForm(initial={'invalid': poll.votesinvalid, 'votescast': poll.votescast}, prefix="poll")
|
2011-07-31 10:46:29 +02:00
|
|
|
for option in options:
|
|
|
|
option.form = OptionResultForm(initial={
|
|
|
|
'yes': option.voteyes,
|
|
|
|
'no': option.voteno,
|
|
|
|
'undesided': option.voteundesided,
|
|
|
|
}, prefix="o%d" % option.id)
|
|
|
|
return {
|
|
|
|
'poll': poll,
|
|
|
|
'form': form,
|
|
|
|
'options': options,
|
|
|
|
'ballotnumber': ballotnumber,
|
|
|
|
}
|
|
|
|
|
2011-09-08 12:30:34 +02:00
|
|
|
@permission_required('assignment.can_manage_assignment')
|
|
|
|
def set_published(request, poll_id, published=True):
|
|
|
|
try:
|
|
|
|
poll = Poll.objects.get(pk=poll_id)
|
|
|
|
poll.set_published(published)
|
|
|
|
if poll.published:
|
|
|
|
messages.success(request, _("Poll successfully set to published.") )
|
|
|
|
else:
|
|
|
|
messages.success(request, _("Poll successfully set to unpublished.") )
|
|
|
|
except Poll.DoesNotExist:
|
|
|
|
messages.error(request, _('Poll ID %d does not exist.') % int(poll_id))
|
2011-09-09 16:18:11 +02:00
|
|
|
return redirect(reverse('assignment_view', args=[poll.assignment.id]))
|
2011-07-31 10:46:29 +02:00
|
|
|
|
|
|
|
@permission_required('assignment.can_manage_assignment')
|
|
|
|
def delete_poll(request, poll_id):
|
|
|
|
poll = Poll.objects.get(pk=poll_id)
|
|
|
|
assignment = poll.assignment
|
|
|
|
ballot = assignment.poll_set.filter(id__lte=poll_id).count()
|
|
|
|
if request.method == 'POST':
|
|
|
|
poll.delete()
|
|
|
|
messages.success(request, _('The %s. ballot was successfully deleted.') % ballot)
|
|
|
|
else:
|
|
|
|
del_confirm_form(request, poll, name=_("the %s. ballot") % ballot)
|
|
|
|
return redirect(reverse('assignment_view', args=[assignment.id]))
|
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]))
|