OpenSlides/openslides/assignment/views.py

281 lines
11 KiB
Python
Raw Normal View History

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 _
from poll.models import Poll, Option
from poll.forms import OptionResultForm, PollForm
2011-07-31 10:46:29 +02:00
from assignment.models import Assignment
from assignment.forms import AssignmentForm, AssignmentRunForm
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:
pass
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'):
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'):
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():
if candidate in poll.options_values:
option = Option.objects.filter(poll=poll).filter(user=candidate)[0]
if poll.optiondecision:
2011-09-03 11:56:35 +02:00
tmplist[1].append([option.yes, option.no, option.undesided])
2011-07-31 10:46:29 +02:00
else:
2011-09-03 11:56:35 +02:00
tmplist[1].append(option.yes)
2011-07-31 10:46:29 +02:00
else:
2011-09-03 11:56:35 +02:00
tmplist[1].append("-")
2011-07-31 10:46:29 +02:00
votes.append(tmplist)
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,
'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':
form = AssignmentForm(request.POST, instance=assignment)
2011-07-31 10:46:29 +02:00
if form.is_valid():
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.'))
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-07-31 10:46:29 +02:00
else:
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))
@permission_required('assignment.can_manage_assignment')
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
return redirect(reverse('assignment_poll_view', args=[poll.id]))
2011-07-31 10:46:29 +02:00
@permission_required('assignment.can_manage_assignment')
2011-07-31 10:46:29 +02:00
@template('assignment/poll_view.html')
def poll_view(request, poll_id):
2011-07-31 10:46:29 +02:00
poll = Poll.objects.get(pk=poll_id)
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':
form = PollForm(request.POST, prefix="poll")
2011-07-31 10:46:29 +02:00
if form.is_valid():
poll.votesinvalid = form.cleaned_data['invalid'] or 0
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.") )
if not 'apply' in request.POST:
return redirect(reverse('assignment_view', args=[assignment.id]))
2011-07-31 10:46:29 +02:00
else:
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,
}
@permission_required('assignment.can_manage_assignment')
def set_published(request, poll_id, published=True):
try:
poll = Poll.objects.get(pk=poll_id)
print poll.published
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))
return redirect(reverse('assignment_view', args=[poll.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]))