2011-07-31 10:46:29 +02:00
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
2012-10-24 11:04:23 +02:00
openslides . motion . views
2011-07-31 10:46:29 +02:00
~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
2012-10-24 11:04:23 +02:00
Views for the motion app .
2011-07-31 10:46:29 +02:00
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-07-08 09:21:39 +02:00
# for python 2.5 support
2011-11-14 16:37:12 +01:00
from __future__ import with_statement
2011-07-31 10:46:29 +02:00
2011-11-09 22:53:10 +01:00
import csv
2012-04-13 23:52:34 +02:00
import os
2011-11-24 19:38:25 +01:00
2012-04-18 19:57:39 +02:00
try :
from urlparse import parse_qs
except ImportError : # python <= 2.5
from cgi import parse_qs
2012-06-11 13:43:48 +02:00
from reportlab . lib import colors
from reportlab . lib . units import cm
2012-07-08 09:21:39 +02:00
from reportlab . platypus import ( SimpleDocTemplate , PageBreak , Paragraph , Spacer ,
Table , TableStyle )
2012-06-11 13:43:48 +02:00
2012-07-08 09:21:39 +02:00
from django . conf import settings
2011-07-31 10:46:29 +02:00
from django . contrib import messages
from django . contrib . auth . decorators import login_required
2012-07-10 11:27:06 +02:00
from django . contrib . auth . models import User
2012-04-27 21:22:44 +02:00
from django . core . context_processors import csrf
2012-07-08 09:21:39 +02:00
from django . core . urlresolvers import reverse
2011-11-09 22:53:10 +01:00
from django . db import transaction
2012-07-08 09:21:39 +02:00
from django . shortcuts import redirect
2012-07-10 12:11:07 +02:00
from django . utils . translation import ugettext as _ , ungettext
2012-06-11 13:43:48 +02:00
2012-07-08 09:21:39 +02:00
from openslides . utils import csv_ext
2012-07-01 15:35:05 +02:00
from openslides . utils . pdf import stylesheet
2012-07-08 09:21:39 +02:00
from openslides . utils . template import Tab
from openslides . utils . utils import ( template , permission_required ,
del_confirm_form , gen_confirm_form )
2012-09-19 03:10:15 +02:00
from openslides . utils . views import ( PDFView , RedirectView , DeleteView ,
FormView , SingleObjectMixin , QuestionMixin )
2012-08-07 22:43:57 +02:00
from openslides . utils . person import get_person
2012-06-11 13:43:48 +02:00
2012-07-08 09:21:39 +02:00
from openslides . config . models import config
2012-07-01 15:35:05 +02:00
from openslides . projector . projector import Widget
2012-06-11 13:43:48 +02:00
2012-07-01 15:35:05 +02:00
from openslides . poll . views import PollFormView
2012-06-11 13:43:48 +02:00
2012-08-07 22:43:57 +02:00
from openslides . participant . api import gen_username , gen_password
2012-11-21 23:38:25 +01:00
from openslides . participant . models import User , Group
2012-02-15 12:04:11 +01:00
2012-07-01 15:35:05 +02:00
from openslides . agenda . models import Item
2012-02-15 12:04:11 +01:00
2012-10-24 11:04:23 +02:00
from openslides . motion . models import Motion , AVersion , MotionPoll
from openslides . motion . forms import ( MotionForm ,
MotionFormTrivialChanges , MotionManagerForm ,
MotionManagerFormSupporter , MotionImportForm , ConfigForm )
2012-02-15 12:04:11 +01:00
2012-04-15 13:10:24 +02:00
2012-10-24 11:04:23 +02:00
@permission_required ( ' motion.can_see_motion ' )
@template ( ' motion/overview.html ' )
2011-07-31 10:46:29 +02:00
def overview ( request ) :
"""
2012-10-24 11:04:23 +02:00
View all motions
2011-07-31 10:46:29 +02:00
"""
try :
2012-04-18 19:57:39 +02:00
sortfilter = parse_qs ( request . COOKIES [ ' votecollector_sortfilter ' ] )
for value in sortfilter :
sortfilter [ value ] = sortfilter [ value ] [ 0 ]
2011-07-31 10:46:29 +02:00
except KeyError :
2012-04-18 19:57:39 +02:00
sortfilter = { }
for value in [ u ' sort ' , u ' reverse ' , u ' number ' , u ' status ' , u ' needsup ' , u ' statusvalue ' ] :
if value in request . REQUEST :
if request . REQUEST [ value ] == ' 0 ' :
try :
del sortfilter [ value ]
except KeyError :
pass
else :
sortfilter [ value ] = request . REQUEST [ value ]
2012-10-24 11:04:23 +02:00
query = Motion . objects . all ( )
2012-04-18 19:57:39 +02:00
if ' number ' in sortfilter :
query = query . filter ( number = None )
if ' status ' in sortfilter :
if ' statusvalue ' in sortfilter and ' on ' in sortfilter [ ' status ' ] :
query = query . filter ( status__iexact = sortfilter [ ' statusvalue ' ] )
if ' sort ' in sortfilter :
if sortfilter [ ' sort ' ] == ' title ' :
sort = ' aversion__title '
elif sortfilter [ ' sort ' ] == ' time ' :
sort = ' aversion__time '
else :
sort = sortfilter [ ' sort ' ]
query = query . order_by ( sort )
2012-04-28 08:45:11 +02:00
if sort . startswith ( ' aversion_ ' ) :
2012-10-24 11:04:23 +02:00
# limit result to last version of an motion
query = query . filter ( aversion__id__in = [ x . last_version . id for x in Motion . objects . all ( ) ] )
2012-04-28 08:45:11 +02:00
2012-04-18 19:57:39 +02:00
if ' reverse ' in sortfilter :
2011-07-31 10:46:29 +02:00
query = query . reverse ( )
2012-04-18 19:57:39 +02:00
# todo: rewrite this with a .filter()
if ' needsup ' in sortfilter :
2012-10-24 11:04:23 +02:00
motions = [ ]
for motion in query . all ( ) :
if not motion . enough_supporters :
motions . append ( motion )
2011-07-31 10:46:29 +02:00
else :
2012-10-24 11:04:23 +02:00
motions = query
2012-04-18 19:57:39 +02:00
2012-10-24 11:04:23 +02:00
if type ( motions ) is not list :
motions = list ( query . all ( ) )
2012-04-27 22:46:27 +02:00
# not the most efficient way to do this but 'get_allowed_actions'
# is not callable from within djangos templates..
2012-10-24 11:04:23 +02:00
for ( i , motion ) in enumerate ( motions ) :
2012-04-27 22:46:27 +02:00
try :
2012-10-24 11:04:23 +02:00
motions [ i ] = {
' actions ' : motion . get_allowed_actions ( request . user ) ,
' motion ' : motion
2012-04-27 22:46:27 +02:00
}
except :
# todo: except what?
2012-10-24 11:04:23 +02:00
motions [ i ] = {
2012-04-27 22:46:27 +02:00
' actions ' : [ ] ,
2012-10-24 11:04:23 +02:00
' motion ' : motion
2012-04-27 22:46:27 +02:00
}
2011-07-31 10:46:29 +02:00
return {
2012-10-24 11:04:23 +02:00
' motions ' : motions ,
' min_supporters ' : int ( config [ ' motion_min_supporters ' ] ) ,
2011-07-31 10:46:29 +02:00
}
2012-10-24 11:04:23 +02:00
@permission_required ( ' motion.can_see_motion ' )
@template ( ' motion/view.html ' )
def view ( request , motion_id , newest = False ) :
2011-07-31 10:46:29 +02:00
"""
2012-10-24 11:04:23 +02:00
View one motion .
2011-07-31 10:46:29 +02:00
"""
2012-10-24 11:04:23 +02:00
motion = Motion . objects . get ( pk = motion_id )
2011-09-06 10:41:06 +02:00
if newest :
2012-10-24 11:04:23 +02:00
version = motion . last_version
2011-09-06 10:41:06 +02:00
else :
2012-10-24 11:04:23 +02:00
version = motion . public_version
revisions = motion . versions
actions = motion . get_allowed_actions ( user = request . user )
2011-11-01 06:47:10 +01:00
2011-07-31 10:46:29 +02:00
return {
2012-10-24 11:04:23 +02:00
' motion ' : motion ,
2011-07-31 10:46:29 +02:00
' revisions ' : revisions ,
' actions ' : actions ,
2012-10-24 11:04:23 +02:00
' min_supporters ' : int ( config [ ' motion_min_supporters ' ] ) ,
2011-09-06 22:22:29 +02:00
' version ' : version ,
2012-10-24 11:04:23 +02:00
#'results': motion.results
2011-07-31 10:46:29 +02:00
}
@login_required
2012-10-24 11:04:23 +02:00
@template ( ' motion/edit.html ' )
def edit ( request , motion_id = None ) :
2011-07-31 10:46:29 +02:00
"""
2012-10-24 11:04:23 +02:00
View a form to edit or create a motion .
2011-07-31 10:46:29 +02:00
"""
2012-10-24 11:04:23 +02:00
if request . user . has_perm ( ' motion.can_manage_motion ' ) :
2011-07-31 10:46:29 +02:00
is_manager = True
else :
is_manager = False
if not is_manager \
2012-10-24 11:04:23 +02:00
and not request . user . has_perm ( ' motion.can_create_motion ' ) :
2012-08-05 22:31:20 +02:00
messages . error ( request , _ ( " You have not the necessary rights to create or edit motions. " ) )
2012-10-24 11:04:23 +02:00
return redirect ( reverse ( ' motion_overview ' ) )
if motion_id is not None :
motion = Motion . objects . get ( id = motion_id )
if not ' edit ' in motion . get_allowed_actions ( request . user ) :
2012-09-13 12:09:17 +02:00
messages . error ( request , _ ( " You can not edit this motion. " ) )
2012-10-24 11:04:23 +02:00
return redirect ( reverse ( ' motion_view ' , args = [ motion . id ] ) )
actions = motion . get_allowed_actions ( user = request . user )
2011-07-31 10:46:29 +02:00
else :
2012-10-24 11:04:23 +02:00
motion = None
2012-04-20 22:13:47 +02:00
actions = None
2011-07-31 10:46:29 +02:00
2012-10-24 11:04:23 +02:00
formclass = MotionFormTrivialChanges \
if config [ ' motion_allow_trivial_change ' ] and motion_id \
else MotionForm
2012-06-30 11:31:35 +02:00
2012-10-24 11:04:23 +02:00
managerformclass = MotionManagerFormSupporter \
if config [ ' motion_min_supporters ' ] \
else MotionManagerForm
2012-06-30 11:50:46 +02:00
2011-07-31 10:46:29 +02:00
if request . method == ' POST ' :
2012-06-30 11:31:35 +02:00
dataform = formclass ( request . POST , prefix = " data " )
2011-07-31 10:46:29 +02:00
valid = dataform . is_valid ( )
if is_manager :
2012-06-30 11:50:46 +02:00
managerform = managerformclass ( request . POST ,
2012-10-24 11:04:23 +02:00
instance = motion ,
2011-07-31 10:46:29 +02:00
prefix = " manager " )
valid = valid and managerform . is_valid ( )
else :
managerform = None
if valid :
del_supporters = True
if is_manager :
2012-10-24 11:04:23 +02:00
if motion : # Edit motion
original_supporters = list ( motion . supporters )
2012-08-04 12:19:31 +02:00
else :
original_supporters = [ ]
2012-10-24 11:04:23 +02:00
motion = managerform . save ( commit = False )
elif motion_id is None :
motion = Motion ( submitter = request . user )
motion . title = dataform . cleaned_data [ ' title ' ]
motion . text = dataform . cleaned_data [ ' text ' ]
motion . reason = dataform . cleaned_data [ ' reason ' ]
2012-06-30 14:50:14 +02:00
try :
2012-10-24 11:04:23 +02:00
trivial_change = config [ ' motion_allow_trivial_change ' ] \
2012-06-30 14:50:14 +02:00
and dataform . cleaned_data [ ' trivial_change ' ]
except KeyError :
trivial_change = False
2012-10-24 11:04:23 +02:00
motion . save ( request . user , trivial_change = trivial_change )
2011-09-08 17:39:57 +02:00
if is_manager :
2012-08-04 12:46:32 +02:00
try :
new_supporters = set ( managerform . cleaned_data [ ' supporter ' ] )
except KeyError :
# The managerform has no field for the supporters
pass
else :
2012-10-24 11:04:23 +02:00
old_supporters = set ( motion . supporters )
2012-08-04 12:46:32 +02:00
# add new supporters
for supporter in new_supporters . difference ( old_supporters ) :
2012-10-24 11:04:23 +02:00
motion . support ( supporter )
2012-08-04 12:46:32 +02:00
# remove old supporters
for supporter in old_supporters . difference ( new_supporters ) :
2012-10-24 11:04:23 +02:00
motion . unsupport ( supporter )
2012-08-05 11:48:03 +02:00
2012-10-24 11:04:23 +02:00
if motion_id is None :
2012-08-05 22:31:20 +02:00
messages . success ( request , _ ( ' New motion was successfully created. ' ) )
2011-07-31 10:46:29 +02:00
else :
2012-08-05 22:31:20 +02:00
messages . success ( request , _ ( ' Motion was successfully modified. ' ) )
2011-09-04 10:02:54 +02:00
2011-09-03 19:25:05 +02:00
if not ' apply ' in request . POST :
2012-10-24 11:04:23 +02:00
return redirect ( reverse ( ' motion_view ' , args = [ motion . id ] ) )
if motion_id is None :
return redirect ( reverse ( ' motion_edit ' , args = [ motion . 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 :
2012-10-24 11:04:23 +02:00
if motion_id is None :
initial = { ' text ' : config [ ' motion_preamble ' ] }
2011-07-31 10:46:29 +02:00
else :
2012-10-24 11:04:23 +02:00
if motion . status == " pub " and motion . supporters :
if request . user . has_perm ( ' motion.can_manage_motion ' ) :
2012-08-05 22:31:20 +02:00
messages . warning ( request , _ ( " Attention: Do you really want to edit this motion? The supporters will <b>not</b> be removed automatically because you can manage motions. Please check if the supports are valid after your changing! " ) )
2011-07-31 10:46:29 +02:00
else :
2012-10-24 11:04:23 +02:00
messages . warning ( request , _ ( " Attention: Do you really want to edit this motion? All <b> %s </b> supporters will be removed! Try to convince the supporters again. " ) % motion . count_supporters ( ) )
initial = { ' title ' : motion . title ,
' text ' : motion . text ,
' reason ' : motion . reason }
2011-07-31 10:46:29 +02:00
2012-06-30 11:31:35 +02:00
dataform = formclass ( initial = initial , prefix = " data " )
2011-07-31 10:46:29 +02:00
if is_manager :
2012-10-24 11:04:23 +02:00
if motion_id is None :
2012-08-12 12:52:38 +02:00
initial = { ' submitter ' : request . user . person_id }
2011-07-31 10:46:29 +02:00
else :
2012-10-24 11:04:23 +02:00
initial = { ' submitter ' : motion . submitter . person_id ,
' supporter ' : [ supporter . person_id for supporter in motion . supporters ] }
2012-08-03 00:11:53 +02:00
managerform = managerformclass ( initial = initial ,
2012-10-24 11:04:23 +02:00
instance = motion , prefix = " manager " )
2011-07-31 10:46:29 +02:00
else :
managerform = None
return {
' form ' : dataform ,
' managerform ' : managerform ,
2012-10-24 11:04:23 +02:00
' motion ' : motion ,
2012-04-20 22:13:47 +02:00
' actions ' : actions ,
2011-07-31 10:46:29 +02:00
}
2012-06-30 14:50:14 +02:00
2012-10-24 11:04:23 +02:00
@permission_required ( ' motion.can_manage_motion ' )
@template ( ' motion/view.html ' )
def set_number ( request , motion_id ) :
2011-07-31 10:46:29 +02:00
"""
2012-10-24 11:04:23 +02:00
set a number for an motion .
2011-07-31 10:46:29 +02:00
"""
try :
2012-10-24 11:04:23 +02:00
Motion . objects . get ( pk = motion_id ) . set_number ( user = request . user )
2012-08-05 22:31:20 +02:00
messages . success ( request , _ ( " Motion number was successfully set. " ) )
2012-10-24 11:04:23 +02:00
except Motion . DoesNotExist :
2011-07-31 10:46:29 +02:00
pass
except NameError :
pass
2012-10-24 11:04:23 +02:00
return redirect ( reverse ( ' motion_view ' , args = [ motion_id ] ) )
2011-07-31 10:46:29 +02:00
2012-10-24 11:04:23 +02:00
@permission_required ( ' motion.can_manage_motion ' )
@template ( ' motion/view.html ' )
def permit ( request , motion_id ) :
2011-07-31 10:46:29 +02:00
"""
2012-10-24 11:04:23 +02:00
permit an motion .
2011-07-31 10:46:29 +02:00
"""
try :
2012-10-24 11:04:23 +02:00
Motion . objects . get ( pk = motion_id ) . permit ( user = request . user )
2012-08-05 22:31:20 +02:00
messages . success ( request , _ ( " Motion was successfully authorized. " ) )
2012-10-24 11:04:23 +02:00
except Motion . DoesNotExist :
2011-07-31 10:46:29 +02:00
pass
2012-07-24 07:32:20 +02:00
except NameError , e :
messages . error ( request , e )
2012-10-24 11:04:23 +02:00
return redirect ( reverse ( ' motion_view ' , args = [ motion_id ] ) )
2011-07-31 10:46:29 +02:00
2012-10-24 11:04:23 +02:00
@permission_required ( ' motion.can_manage_motion ' )
@template ( ' motion/view.html ' )
def notpermit ( request , motion_id ) :
2011-07-31 10:46:29 +02:00
"""
2012-10-24 11:04:23 +02:00
reject ( not permit ) an motion .
2011-07-31 10:46:29 +02:00
"""
try :
2012-10-24 11:04:23 +02:00
Motion . objects . get ( pk = motion_id ) . notpermit ( user = request . user )
2012-08-05 22:31:20 +02:00
messages . success ( request , _ ( " Motion was successfully rejected. " ) )
2012-10-24 11:04:23 +02:00
except Motion . DoesNotExist :
2011-07-31 10:46:29 +02:00
pass
2012-07-24 07:32:20 +02:00
except NameError , e :
messages . error ( request , e )
2012-10-24 11:04:23 +02:00
return redirect ( reverse ( ' motion_view ' , args = [ motion_id ] ) )
2011-07-31 10:46:29 +02:00
2012-10-24 11:04:23 +02:00
@template ( ' motion/view.html ' )
def set_status ( request , motion_id = None , status = None ) :
2011-07-31 10:46:29 +02:00
"""
2012-10-24 11:04:23 +02:00
set a status of an motion .
2011-07-31 10:46:29 +02:00
"""
try :
if status is not None :
2012-10-24 11:04:23 +02:00
motion = Motion . objects . get ( pk = motion_id )
motion . set_status ( user = request . user , status = status )
messages . success ( request , _ ( " Motion status was set to: <b> %s </b>. " ) % motion . get_status_display ( ) )
except Motion . DoesNotExist :
2011-07-31 10:46:29 +02:00
pass
2012-07-23 23:17:03 +02:00
except NameError , e :
messages . error ( request , e )
2012-10-24 11:04:23 +02:00
return redirect ( reverse ( ' motion_view ' , args = [ motion_id ] ) )
2011-07-31 10:46:29 +02:00
2012-10-24 11:04:23 +02:00
@permission_required ( ' motion.can_manage_motion ' )
@template ( ' motion/view.html ' )
def reset ( request , motion_id ) :
2011-07-31 10:46:29 +02:00
"""
2012-10-24 11:04:23 +02:00
reset an motion .
2011-07-31 10:46:29 +02:00
"""
try :
2012-10-24 11:04:23 +02:00
Motion . objects . get ( pk = motion_id ) . reset ( user = request . user )
2012-08-05 22:31:20 +02:00
messages . success ( request , _ ( " Motion status was reset. " ) )
2012-10-24 11:04:23 +02:00
except Motion . DoesNotExist :
2011-07-31 10:46:29 +02:00
pass
2012-10-24 11:04:23 +02:00
return redirect ( reverse ( ' motion_view ' , args = [ motion_id ] ) )
2011-07-31 10:46:29 +02:00
2012-10-23 11:54:18 +02:00
class SupportView ( SingleObjectMixin , QuestionMixin , RedirectView ) :
2011-07-31 10:46:29 +02:00
"""
2012-10-28 02:09:47 +02:00
Classed based view to support or unsupport a motion . Use
support = True or support = False in urls . py
2011-07-31 10:46:29 +02:00
"""
2012-10-25 09:23:24 +02:00
permission_required = ' motion.can_support_motion '
model = Motion
pk_url_kwarg = ' motion_id '
2012-10-25 10:25:59 +02:00
support = True
2012-09-19 03:10:15 +02:00
2012-10-28 19:59:41 +01:00
def get ( self , request , * args , * * kwargs ) :
self . object = self . get_object ( )
return super ( SupportView , self ) . get ( request , * args , * * kwargs )
2012-09-19 03:10:15 +02:00
2012-10-28 17:44:15 +01:00
def check_allowed_actions ( self , request ) :
"""
Checks whether request . user can support or unsupport the motion .
Returns True or False .
"""
2012-10-28 19:59:41 +01:00
allowed_actions = self . object . get_allowed_actions ( request . user )
2012-10-28 02:09:47 +02:00
if self . support and not ' support ' in allowed_actions :
messages . error ( request , _ ( ' You can not support this motion. ' ) )
2012-10-28 17:44:15 +01:00
return False
2012-10-28 02:09:47 +02:00
elif not self . support and not ' unsupport ' in allowed_actions :
messages . error ( request , _ ( ' You can not unsupport this motion. ' ) )
2012-10-28 17:44:15 +01:00
return False
2012-10-28 02:09:47 +02:00
else :
2012-10-28 17:44:15 +01:00
return True
def pre_redirect ( self , request , * args , * * kwargs ) :
if self . check_allowed_actions ( request ) :
2012-10-28 02:09:47 +02:00
super ( SupportView , self ) . pre_redirect ( request , * args , * * kwargs )
2012-10-28 19:59:41 +01:00
def get_question ( self ) :
if self . support :
return _ ( ' Do you really want to support this motion? ' )
else :
return _ ( ' Do you really want to unsupport this motion? ' )
def case_yes ( self ) :
if self . check_allowed_actions ( self . request ) :
2012-10-28 02:09:47 +02:00
if self . support :
2012-10-28 19:59:41 +01:00
self . object . support ( person = self . request . user )
2012-10-25 10:15:41 +02:00
else :
2012-10-28 19:59:41 +01:00
self . object . unsupport ( person = self . request . user )
2011-07-31 10:46:29 +02:00
2012-10-28 19:59:41 +01:00
def get_success_message ( self ) :
2012-10-25 10:25:59 +02:00
if self . support :
2012-10-28 19:59:41 +01:00
return _ ( " You have supported this motion successfully. " )
2012-10-25 10:25:59 +02:00
else :
2012-10-28 19:59:41 +01:00
return _ ( " You have unsupported this motion successfully. " )
def get_redirect_url ( self , * * kwargs ) :
return reverse ( ' motion_view ' , args = [ kwargs [ self . pk_url_kwarg ] ] )
2012-10-25 10:25:59 +02:00
2012-10-24 11:04:23 +02:00
@permission_required ( ' motion.can_manage_motion ' )
@template ( ' motion/view.html ' )
def gen_poll ( request , motion_id ) :
2011-07-31 10:46:29 +02:00
"""
2012-10-24 11:04:23 +02:00
gen a poll for this motion .
2011-07-31 10:46:29 +02:00
"""
try :
2012-10-24 11:04:23 +02:00
poll = Motion . objects . get ( pk = motion_id ) . gen_poll ( user = request . user )
2011-09-03 16:20:10 +02:00
messages . success ( request , _ ( " New vote was successfully created. " ) )
2012-10-24 11:04:23 +02:00
except Motion . DoesNotExist :
2012-02-14 16:31:21 +01:00
pass # TODO: do not call poll after this excaption
2012-10-24 11:04:23 +02:00
return redirect ( reverse ( ' motion_poll_view ' , args = [ poll . id ] ) )
2011-07-31 10:46:29 +02:00
2012-10-24 11:04:23 +02:00
@permission_required ( ' motion.can_manage_motion ' )
2011-07-31 10:46:29 +02:00
def delete_poll ( request , poll_id ) :
"""
2012-10-24 11:04:23 +02:00
delete a poll from this motion
2011-07-31 10:46:29 +02:00
"""
2012-10-24 11:04:23 +02:00
poll = MotionPoll . objects . get ( pk = poll_id )
motion = poll . motion
count = motion . polls . filter ( id__lte = poll_id ) . count ( )
2011-07-31 10:46:29 +02:00
if request . method == ' POST ' :
poll . delete ( )
2012-10-24 11:04:23 +02:00
motion . writelog ( _ ( " Poll deleted " ) , request . user )
2011-07-31 10:46:29 +02:00
messages . success ( request , _ ( ' Poll was successfully deleted. ' ) )
else :
2012-10-24 11:04:23 +02:00
del_confirm_form ( request , poll , name = _ ( " the %s . poll " ) % count , delete_link = reverse ( ' motion_poll_delete ' , args = [ poll_id ] ) )
return redirect ( reverse ( ' motion_view ' , args = [ motion . id ] ) )
2011-07-31 10:46:29 +02:00
2012-06-09 19:39:52 +02:00
2012-10-24 11:04:23 +02:00
class MotionDelete ( DeleteView ) :
2012-04-27 21:22:44 +02:00
"""
2012-10-24 11:04:23 +02:00
Delete one or more Motions .
2012-04-27 21:22:44 +02:00
"""
2012-10-24 11:04:23 +02:00
model = Motion
url = ' motion_overview '
2012-04-27 21:22:44 +02:00
2012-10-24 12:45:40 +02:00
def has_permission ( self , request , * args , * * kwargs ) :
self . kwargs = kwargs
return self . get_object ( ) . get_allowed_actions ( request . user )
2012-04-27 21:22:44 +02:00
def get_object ( self ) :
2012-10-24 11:04:23 +02:00
self . motions = [ ]
2012-04-27 21:22:44 +02:00
2012-10-24 11:04:23 +02:00
if self . kwargs . get ( ' motion_id ' , None ) :
2012-04-27 21:22:44 +02:00
try :
2012-10-24 11:04:23 +02:00
return Motion . objects . get ( id = int ( self . kwargs [ ' motion_id ' ] ) )
except Motion . DoesNotExist :
2012-04-27 21:22:44 +02:00
return None
2012-10-24 11:04:23 +02:00
if self . kwargs . get ( ' motion_ids ' , [ ] ) :
for appid in self . kwargs [ ' motion_ids ' ] :
2012-04-27 21:22:44 +02:00
try :
2012-10-24 11:04:23 +02:00
self . motions . append ( Motion . objects . get ( id = int ( appid ) ) )
except Motion . DoesNotExist :
2012-04-27 21:22:44 +02:00
pass
2012-10-24 11:04:23 +02:00
if self . motions :
return self . motions [ 0 ]
2012-04-27 21:22:44 +02:00
return None
def pre_post_redirect ( self , request , * args , * * kwargs ) :
self . object = self . get_object ( )
2012-10-24 11:04:23 +02:00
if len ( self . motions ) :
for motion in self . motions :
if not ' delete ' in motion . get_allowed_actions ( user = request . user ) :
messages . error ( request , _ ( " You can not delete motion <b> %s </b>. " ) % motion )
2012-04-27 21:22:44 +02:00
continue
2012-10-24 11:04:23 +02:00
title = motion . title
motion . delete ( force = True )
2012-08-05 22:31:20 +02:00
messages . success ( request , _ ( " Motion <b> %s </b> was successfully deleted. " ) % title )
2012-04-27 21:22:44 +02:00
elif self . object :
2012-08-12 12:52:38 +02:00
if not ' delete ' in self . object . get_allowed_actions ( user = request . user ) :
2012-08-13 19:16:42 +02:00
messages . error ( request , _ ( " You can not delete motion <b> %s </b>. " ) % self . object )
2012-09-19 17:00:40 +02:00
elif self . get_answer ( ) == ' yes ' :
2012-08-04 12:19:31 +02:00
title = self . object . title
self . object . delete ( force = True )
2012-08-13 19:16:42 +02:00
messages . success ( request , _ ( " Motion <b> %s </b> was successfully deleted. " ) % title )
2012-04-27 21:22:44 +02:00
else :
messages . error ( request , _ ( " Invalid request " ) )
2012-07-13 11:16:06 +02:00
2012-02-14 16:31:21 +01:00
class ViewPoll ( PollFormView ) :
2012-10-24 11:04:23 +02:00
permission_required = ' motion.can_manage_motion '
poll_class = MotionPoll
template_name = ' motion/poll_view.html '
2012-02-14 16:31:21 +01:00
def get_context_data ( self , * * kwargs ) :
2012-02-19 17:31:17 +01:00
context = super ( ViewPoll , self ) . get_context_data ( * * kwargs )
2012-10-24 11:04:23 +02:00
self . motion = self . poll . get_motion ( )
context [ ' motion ' ] = self . motion
2012-04-18 19:30:32 +02:00
context [ ' ballot ' ] = self . poll . get_ballot ( )
2012-10-24 11:04:23 +02:00
context [ ' actions ' ] = self . motion . get_allowed_actions ( user = self . request . user )
2012-02-14 16:31:21 +01:00
return context
2012-05-20 23:12:50 +02:00
def get_modelform_class ( self ) :
cls = super ( ViewPoll , self ) . get_modelform_class ( )
2012-08-12 12:52:38 +02:00
user = self . request . user
2012-05-20 23:12:50 +02:00
class ViewPollFormClass ( cls ) :
def save ( self , commit = True ) :
instance = super ( ViewPollFormClass , self ) . save ( commit )
2012-10-24 11:04:23 +02:00
motion = instance . motion
motion . writelog ( _ ( " Poll was updated " ) , user )
2012-05-20 23:12:50 +02:00
return instance
return ViewPollFormClass
2012-02-14 16:31:21 +01:00
def get_success_url ( self ) :
if not ' apply ' in self . request . POST :
2012-10-24 11:04:23 +02:00
return reverse ( ' motion_view ' , args = [ self . poll . motion . id ] )
2012-02-14 16:31:21 +01:00
return ' '
2012-10-24 11:04:23 +02:00
@permission_required ( ' motion.can_manage_motion ' )
2011-09-03 18:11:42 +02:00
def permit_version ( request , aversion_id ) :
aversion = AVersion . objects . get ( pk = aversion_id )
2012-10-24 11:04:23 +02:00
motion = aversion . motion
2011-09-04 10:43:25 +02:00
if request . method == ' POST ' :
2012-10-24 11:04:23 +02:00
motion . accept_version ( aversion , user = request . user )
2011-09-04 10:43:25 +02:00
messages . success ( request , _ ( " Version <b> %s </b> accepted. " ) % ( aversion . aid ) )
else :
2012-10-24 11:04:23 +02:00
gen_confirm_form ( request , _ ( ' Do you really want to authorize version <b> %s </b>? ' ) % aversion . aid , reverse ( ' motion_version_permit ' , args = [ aversion . id ] ) )
return redirect ( reverse ( ' motion_view ' , args = [ motion . id ] ) )
2011-09-03 18:11:42 +02:00
2012-10-24 11:04:23 +02:00
@permission_required ( ' motion.can_manage_motion ' )
2011-09-03 18:11:42 +02:00
def reject_version ( request , aversion_id ) :
aversion = AVersion . objects . get ( pk = aversion_id )
2012-10-24 11:04:23 +02:00
motion = aversion . motion
2011-09-04 19:07:45 +02:00
if request . method == ' POST ' :
2012-10-24 11:04:23 +02:00
if motion . reject_version ( aversion , user = request . user ) :
2011-09-04 19:07:45 +02:00
messages . success ( request , _ ( " Version <b> %s </b> rejected. " ) % ( aversion . aid ) )
else :
messages . error ( request , _ ( " ERROR by rejecting the version. " ) )
2011-09-03 18:11:42 +02:00
else :
2012-10-24 11:04:23 +02:00
gen_confirm_form ( request , _ ( ' Do you really want to reject version <b> %s </b>? ' ) % aversion . aid , reverse ( ' motion_version_reject ' , args = [ aversion . id ] ) )
return redirect ( reverse ( ' motion_view ' , args = [ motion . id ] ) )
2011-09-03 18:11:42 +02:00
2012-02-15 12:04:11 +01:00
2012-10-24 11:04:23 +02:00
@permission_required ( ' motion.can_manage_motion ' )
@template ( ' motion/import.html ' )
def motion_import ( request ) :
2011-11-09 22:53:10 +01:00
if request . method == ' POST ' :
2012-10-24 11:04:23 +02:00
form = MotionImportForm ( request . POST , request . FILES )
2011-11-09 22:53:10 +01:00
if form . is_valid ( ) :
2012-04-13 21:55:13 +02:00
import_permitted = form . cleaned_data [ ' import_permitted ' ]
2011-11-09 22:53:10 +01:00
try :
2012-04-13 22:33:47 +02:00
# check for valid encoding (will raise UnicodeDecodeError if not)
request . FILES [ ' csvfile ' ] . read ( ) . decode ( ' utf-8 ' )
request . FILES [ ' csvfile ' ] . seek ( 0 )
2012-04-14 12:52:56 +02:00
2011-11-09 22:53:10 +01:00
users_generated = 0
2012-10-24 11:04:23 +02:00
motions_generated = 0
motions_modified = 0
2012-11-21 23:38:25 +01:00
groups_assigned = 0
2012-11-22 10:26:44 +01:00
groups_generated = 0
2011-11-09 22:53:10 +01:00
with transaction . commit_on_success ( ) :
2011-11-22 18:49:36 +01:00
dialect = csv . Sniffer ( ) . sniff ( request . FILES [ ' csvfile ' ] . readline ( ) )
2012-07-23 08:55:05 +02:00
dialect = csv_ext . patchup ( dialect )
2011-11-22 18:49:36 +01:00
request . FILES [ ' csvfile ' ] . seek ( 0 )
for ( lno , line ) in enumerate ( csv . reader ( request . FILES [ ' csvfile ' ] , dialect = dialect ) ) :
2011-11-09 22:53:10 +01:00
# basic input verification
if lno < 1 :
continue
try :
2012-11-21 23:38:25 +01:00
( number , title , text , reason , first_name , last_name , is_group ) = line [ : 7 ]
if is_group . strip ( ) . lower ( ) in [ ' y ' , ' j ' , ' t ' , ' yes ' , ' ja ' , ' true ' , ' 1 ' , 1 ] :
is_group = True
else :
is_group = False
2011-11-09 22:53:10 +01:00
except ValueError :
messages . error ( request , _ ( ' Ignoring malformed line %d in import file. ' ) % ( lno + 1 ) )
continue
2012-10-24 11:04:23 +02:00
form = MotionForm ( { ' title ' : title , ' text ' : text , ' reason ' : reason } )
2011-11-09 22:53:10 +01:00
if not form . is_valid ( ) :
messages . error ( request , _ ( ' Ignoring malformed line %d in import file. ' ) % ( lno + 1 ) )
continue
if number :
try :
number = abs ( long ( number ) )
if number < 1 :
messages . error ( request , _ ( ' Ignoring malformed line %d in import file. ' ) % ( lno + 1 ) )
continue
except ValueError :
messages . error ( request , _ ( ' Ignoring malformed line %d in import file. ' ) % ( lno + 1 ) )
continue
2012-11-21 23:38:25 +01:00
if is_group :
# fetch existing groups or issue an error message
try :
user = Group . objects . get ( name = last_name )
if user . group_as_person == False :
messages . error ( request , _ ( ' Ignoring line %d because the assigned group may not act as a person. ' ) % ( lno + 1 ) )
continue
else :
user = get_person ( user . person_id )
groups_assigned + = 1
except Group . DoesNotExist :
2012-11-22 10:26:44 +01:00
group = Group ( )
group . group_as_person = True
group . description = _ ( ' Created by motion import. ' )
group . name = last_name
group . save ( )
groups_generated + = 1
user = get_person ( group . person_id )
2012-11-21 23:38:25 +01:00
else :
# fetch existing users or create new users as needed
try :
user = User . objects . get ( first_name = first_name , last_name = last_name )
except User . DoesNotExist :
user = None
if user is None :
if not first_name or not last_name :
messages . error ( request , _ ( ' Ignoring line %d because it contains an incomplete first / last name pair. ' ) % ( lno + 1 ) )
continue
user = User ( )
user . last_name = last_name
user . first_name = first_name
user . username = gen_username ( first_name , last_name )
user . structure_level = ' '
user . committee = ' '
user . gender = ' '
user . type = ' '
user . default_password = gen_password ( )
user . save ( )
user . reset_password ( )
users_generated + = 1
2012-10-24 11:04:23 +02:00
# create / modify the motion
motion = None
2011-11-09 22:53:10 +01:00
if number :
try :
2012-10-24 11:04:23 +02:00
motion = Motion . objects . get ( number = number )
motions_modified + = 1
except Motion . DoesNotExist :
motion = None
if motion is None :
motion = Motion ( submitter = user )
2011-11-09 22:53:10 +01:00
if number :
2012-10-24 11:04:23 +02:00
motion . number = number
motions_generated + = 1
2011-11-09 22:53:10 +01:00
2012-10-24 11:04:23 +02:00
motion . title = form . cleaned_data [ ' title ' ]
motion . text = form . cleaned_data [ ' text ' ]
motion . reason = form . cleaned_data [ ' reason ' ]
2012-04-13 21:55:13 +02:00
if import_permitted :
2012-10-24 11:04:23 +02:00
motion . status = ' per '
2012-04-13 21:55:13 +02:00
2012-10-24 11:04:23 +02:00
motion . save ( user , trivial_change = True )
2011-11-09 22:53:10 +01:00
2012-10-24 11:04:23 +02:00
if motions_generated :
2012-08-05 22:31:20 +02:00
messages . success ( request , ungettext ( ' %d motion was successfully imported. ' ,
2012-10-24 11:04:23 +02:00
' %d motions were successfully imported. ' , motions_generated ) % motions_generated )
if motions_modified :
2012-08-05 22:31:20 +02:00
messages . success ( request , ungettext ( ' %d motion was successfully modified. ' ,
2012-10-24 11:04:23 +02:00
' %d motions were successfully modified. ' , motions_modified ) % motions_modified )
2011-11-09 22:53:10 +01:00
if users_generated :
messages . success ( request , ungettext ( ' %d new user was added. ' , ' %d new users were added. ' , users_generated ) % users_generated )
2012-11-22 10:26:44 +01:00
if groups_generated :
messages . success ( request , ungettext ( ' %d new group was added. ' , ' %d new groups were added. ' , groups_generated ) % groups_generated )
2012-11-21 23:38:25 +01:00
if groups_assigned :
messages . success ( request , ungettext ( ' %d group assigned to motions. ' , ' %d groups assigned to motions. ' , groups_assigned ) % groups_assigned )
2012-10-24 11:04:23 +02:00
return redirect ( reverse ( ' motion_overview ' ) )
2011-11-09 22:53:10 +01:00
except csv . Error :
message . error ( request , _ ( ' Import aborted because of severe errors in the input file. ' ) )
2012-04-13 22:33:47 +02:00
except UnicodeDecodeError :
messages . error ( request , _ ( ' Import file has wrong character encoding, only UTF-8 is supported! ' ) )
2011-11-09 22:53:10 +01:00
else :
messages . error ( request , _ ( ' Please check the form for errors. ' ) )
else :
2012-08-05 22:31:20 +02:00
messages . warning ( request , _ ( " Attention: Existing motions will be modified if you import new motions with the same number. " ) )
messages . warning ( request , _ ( " Attention: Importing an motions without a number multiple times will create duplicates. " ) )
2012-10-24 11:04:23 +02:00
form = MotionImportForm ( )
2011-11-09 22:53:10 +01:00
return {
' form ' : form ,
}
2012-04-19 12:46:04 +02:00
class CreateAgendaItem ( RedirectView ) :
permission_required = ' agenda.can_manage_agenda '
def pre_redirect ( self , request , * args , * * kwargs ) :
2012-10-24 11:04:23 +02:00
self . motion = Motion . objects . get ( pk = kwargs [ ' motion_id ' ] )
self . item = Item ( related_sid = self . motion . sid )
2012-04-19 12:46:04 +02:00
self . item . save ( )
def get_redirect_url ( self , * * kwargs ) :
2012-06-03 10:19:08 +02:00
return reverse ( ' item_overview ' )
2012-04-19 12:46:04 +02:00
2012-10-24 11:04:23 +02:00
class MotionPDF ( PDFView ) :
permission_required = ' motion.can_see_motion '
2012-04-13 23:52:34 +02:00
top_space = 0
2012-04-18 18:54:48 +02:00
def get_filename ( self ) :
2012-10-24 11:04:23 +02:00
motion_id = self . kwargs [ ' motion_id ' ]
if motion_id is None :
filename = _ ( " Motions " )
2012-04-18 18:54:48 +02:00
else :
2012-10-24 11:04:23 +02:00
motion = Motion . objects . get ( id = motion_id )
if motion . number :
number = motion . number
2012-04-18 18:54:48 +02:00
else :
number = " "
2012-10-24 11:04:23 +02:00
filename = u ' %s %s ' % ( _ ( " Motion " ) , str ( number ) )
2012-04-18 18:54:48 +02:00
return filename
2012-04-15 12:11:03 +02:00
2012-04-18 18:54:48 +02:00
def append_to_pdf ( self , story ) :
2012-10-24 11:04:23 +02:00
motion_id = self . kwargs [ ' motion_id ' ]
if motion_id is None : #print all motions
title = config [ " motion_pdf_title " ]
2012-04-13 23:52:34 +02:00
story . append ( Paragraph ( title , stylesheet [ ' Heading1 ' ] ) )
2012-10-24 11:04:23 +02:00
preamble = config [ " motion_pdf_preamble " ]
2012-04-13 23:52:34 +02:00
if preamble :
story . append ( Paragraph ( " %s " % preamble . replace ( ' \r \n ' , ' <br/> ' ) , stylesheet [ ' Paragraph ' ] ) )
story . append ( Spacer ( 0 , 0.75 * cm ) )
2012-10-24 11:04:23 +02:00
motions = Motion . objects . all ( )
if not motions : # No motions existing
2012-08-05 22:31:20 +02:00
story . append ( Paragraph ( _ ( " No motions available. " ) , stylesheet [ ' Heading3 ' ] ) )
2012-10-24 11:04:23 +02:00
else : # Print all Motions
# List of motions
for motion in motions :
if motion . number :
story . append ( Paragraph ( _ ( " Motion No. " ) + " %s : %s " % ( motion . number , motion . title ) , stylesheet [ ' Heading3 ' ] ) )
2012-06-29 11:42:28 +02:00
else :
2012-10-24 11:04:23 +02:00
story . append ( Paragraph ( _ ( " Motion No. " ) + " : %s " % ( motion . title ) , stylesheet [ ' Heading3 ' ] ) )
# Motions details (each motion on single page)
for motion in motions :
2012-06-29 11:42:28 +02:00
story . append ( PageBreak ( ) )
2012-10-24 11:04:23 +02:00
story = self . get_motion ( motion , story )
else : # print selected motion
motion = Motion . objects . get ( id = motion_id )
story = self . get_motion ( motion , story )
2012-04-13 23:52:34 +02:00
2012-10-24 11:04:23 +02:00
def get_motion ( self , motion , story ) :
2012-09-13 21:33:57 +02:00
# Preparing Table
data = [ ]
2012-10-24 11:04:23 +02:00
# motion number
if motion . number :
story . append ( Paragraph ( _ ( " Motion No. " ) + " %s " % motion . number , stylesheet [ ' Heading1 ' ] ) )
2012-04-13 23:52:34 +02:00
else :
2012-08-05 22:31:20 +02:00
story . append ( Paragraph ( _ ( " Motion No. " ) , stylesheet [ ' Heading1 ' ] ) )
2012-04-13 23:52:34 +02:00
# submitter
cell1a = [ ]
2012-09-13 21:33:57 +02:00
cell1a . append ( Spacer ( 0 , 0.2 * cm ) )
2012-04-13 23:52:34 +02:00
cell1a . append ( Paragraph ( " <font name= ' Ubuntu-Bold ' > %s :</font> " % _ ( " Submitter " ) , stylesheet [ ' Heading4 ' ] ) )
cell1b = [ ]
2012-09-13 21:33:57 +02:00
cell1b . append ( Spacer ( 0 , 0.2 * cm ) )
2012-10-24 11:04:23 +02:00
cell1b . append ( Paragraph ( " %s " % motion . submitter , stylesheet [ ' Normal ' ] ) )
2012-09-13 21:33:57 +02:00
data . append ( [ cell1a , cell1b ] )
2012-10-24 11:04:23 +02:00
if motion . status == " pub " :
2012-09-13 21:33:57 +02:00
# Cell for the signature
cell2a = [ ]
cell2b = [ ]
cell2a . append ( Paragraph ( " <font name= ' Ubuntu-Bold ' > %s :</font> " % _ ( " Signature " ) , stylesheet [ ' Heading4 ' ] ) )
cell2b . append ( Paragraph ( " __________________________________________ " , stylesheet [ ' Signaturefield ' ] ) )
cell2b . append ( Spacer ( 0 , 0.1 * cm ) )
cell2b . append ( Spacer ( 0 , 0.2 * cm ) )
data . append ( [ cell2a , cell2b ] )
2012-04-13 23:52:34 +02:00
# supporters
2012-10-24 11:04:23 +02:00
if config [ ' motion_min_supporters ' ] :
2012-09-13 21:33:57 +02:00
cell3a = [ ]
cell3b = [ ]
cell3a . append ( Paragraph ( " <font name= ' Ubuntu-Bold ' > %s :</font><seqreset id= ' counter ' > " % _ ( " Supporters " ) , stylesheet [ ' Heading4 ' ] ) )
2012-10-24 11:04:23 +02:00
for supporter in motion . supporters :
2012-09-13 21:33:57 +02:00
cell3b . append ( Paragraph ( " <seq id= ' counter ' />. %s " % supporter , stylesheet [ ' Signaturefield ' ] ) )
2012-10-24 11:04:23 +02:00
if motion . status == " pub " :
for x in range ( motion . missing_supporters ) :
2012-09-13 21:33:57 +02:00
cell3b . append ( Paragraph ( " <seq id= ' counter ' />. __________________________________________ " , stylesheet [ ' Signaturefield ' ] ) )
cell3b . append ( Spacer ( 0 , 0.2 * cm ) )
data . append ( [ cell3a , cell3b ] )
2012-04-13 23:52:34 +02:00
# status
2012-09-13 21:33:57 +02:00
cell4a = [ ]
cell4b = [ ]
2012-10-24 11:04:23 +02:00
note = " " . join ( motion . notes )
2012-09-13 21:33:57 +02:00
cell4a . append ( Paragraph ( " <font name= ' Ubuntu-Bold ' > %s :</font> " % _ ( " Status " ) , stylesheet [ ' Heading4 ' ] ) )
2012-04-13 23:52:34 +02:00
if note != " " :
2012-10-24 11:04:23 +02:00
if motion . status == " pub " :
2012-09-13 21:33:57 +02:00
cell4b . append ( Paragraph ( note , stylesheet [ ' Normal ' ] ) )
2012-04-13 23:52:34 +02:00
else :
2012-10-24 11:04:23 +02:00
cell4b . append ( Paragraph ( " %s | %s " % ( motion . get_status_display ( ) , note ) , stylesheet [ ' Normal ' ] ) )
2012-04-13 23:52:34 +02:00
else :
2012-10-24 11:04:23 +02:00
cell4b . append ( Paragraph ( " %s " % motion . get_status_display ( ) , stylesheet [ ' Normal ' ] ) )
2012-09-13 21:33:57 +02:00
data . append ( [ cell4a , cell4b ] )
2012-04-13 23:52:34 +02:00
2012-06-29 11:22:47 +02:00
# Version number (aid)
2012-10-24 11:04:23 +02:00
if motion . public_version . aid > 1 :
2012-09-13 21:33:57 +02:00
cell5a = [ ]
cell5b = [ ]
cell5a . append ( Paragraph ( " <font name= ' Ubuntu-Bold ' > %s :</font> " % _ ( " Version " ) , stylesheet [ ' Heading4 ' ] ) )
2012-10-24 11:04:23 +02:00
cell5b . append ( Paragraph ( " %s " % motion . public_version . aid , stylesheet [ ' Normal ' ] ) )
2012-09-13 21:33:57 +02:00
data . append ( [ cell5a , cell5b ] )
2012-06-29 11:22:47 +02:00
2012-04-13 23:52:34 +02:00
# voting results
2012-10-24 11:04:23 +02:00
poll_results = motion . get_poll_results ( )
2012-04-15 12:11:03 +02:00
if poll_results :
2012-09-13 21:33:57 +02:00
cell6a = [ ]
cell6a . append ( Paragraph ( " <font name= ' Ubuntu-Bold ' > %s :</font> " % _ ( " Vote results " ) , stylesheet [ ' Heading4 ' ] ) )
cell6b = [ ]
2012-04-13 23:52:34 +02:00
ballotcounter = 0
2012-04-15 12:11:03 +02:00
for result in poll_results :
2012-04-13 23:52:34 +02:00
ballotcounter + = 1
2012-04-15 12:11:03 +02:00
if len ( poll_results ) > 1 :
2012-09-13 21:33:57 +02:00
cell6b . append ( Paragraph ( " %s . %s " % ( ballotcounter , _ ( " Vote " ) ) , stylesheet [ ' Bold ' ] ) )
cell6b . append ( Paragraph ( " %s : %s <br/> %s : %s <br/> %s : %s <br/> %s : %s <br/> %s : %s " % ( _ ( " Yes " ) , result [ 0 ] , _ ( " No " ) , result [ 1 ] , _ ( " Abstention " ) , result [ 2 ] , _ ( " Invalid " ) , result [ 3 ] , _ ( " Votes cast " ) , result [ 4 ] ) , stylesheet [ ' Normal ' ] ) )
cell6b . append ( Spacer ( 0 , 0.2 * cm ) )
data . append ( [ cell6a , cell6b ] )
# Creating Table
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 ' ) ] ) )
2012-04-13 23:52:34 +02:00
story . append ( t )
2012-09-13 21:33:57 +02:00
story . append ( Spacer ( 0 , 1 * cm ) )
2012-04-13 23:52:34 +02:00
# title
2012-10-24 11:04:23 +02:00
story . append ( Paragraph ( motion . public_version . title , stylesheet [ ' Heading3 ' ] ) )
2012-04-13 23:52:34 +02:00
# text
2012-10-24 11:04:23 +02:00
story . append ( Paragraph ( " %s " % motion . public_version . text . replace ( ' \r \n ' , ' <br/> ' ) , stylesheet [ ' Paragraph ' ] ) )
2012-04-13 23:52:34 +02:00
# reason
2012-10-24 11:04:23 +02:00
if motion . public_version . reason :
2012-07-06 15:30:14 +02:00
story . append ( Paragraph ( _ ( " Reason " ) + " : " , stylesheet [ ' Heading3 ' ] ) )
2012-10-24 11:04:23 +02:00
story . append ( Paragraph ( " %s " % motion . public_version . reason . replace ( ' \r \n ' , ' <br/> ' ) , stylesheet [ ' Paragraph ' ] ) )
2012-04-13 23:52:34 +02:00
return story
2012-10-24 11:04:23 +02:00
class MotionPollPDF ( PDFView ) :
permission_required = ' motion.can_manage_motion '
2012-04-13 23:52:34 +02:00
top_space = 0
def get ( self , request , * args , * * kwargs ) :
2012-10-24 11:04:23 +02:00
self . poll = MotionPoll . objects . get ( id = self . kwargs [ ' poll_id ' ] )
return super ( MotionPollPDF , self ) . get ( request , * args , * * kwargs )
2012-04-13 23:52:34 +02:00
def get_filename ( self ) :
2012-10-24 11:04:23 +02:00
filename = u ' %s %s _ %s ' % ( _ ( " Motion " ) , str ( self . poll . motion . number ) , _ ( " Poll " ) )
2012-04-13 23:52:34 +02:00
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 )
def build_document ( self , pdf_document , story ) :
pdf_document . build ( story )
2012-04-13 23:52:34 +02:00
def append_to_pdf ( self , story ) :
2012-07-01 15:35:05 +02:00
imgpath = os . path . join ( settings . SITE_ROOT , ' static/images/circle.png ' )
2012-04-13 23:52:34 +02:00
circle = " <img src= ' %s ' width= ' 15 ' height= ' 15 ' /> " % imgpath
cell = [ ]
cell . append ( Spacer ( 0 , 0.8 * cm ) )
2012-10-24 11:04:23 +02:00
cell . append ( Paragraph ( _ ( " Motion No. %s " ) % self . poll . motion . number , stylesheet [ ' Ballot_title ' ] ) )
cell . append ( Paragraph ( self . poll . motion . title , stylesheet [ ' Ballot_subtitle ' ] ) )
2012-07-10 12:11:07 +02:00
cell . append ( Paragraph ( _ ( " %d . Vote " ) % self . poll . get_ballot ( ) , stylesheet [ ' Ballot_description ' ] ) )
2012-04-13 23:52:34 +02:00
cell . append ( Spacer ( 0 , 0.5 * cm ) )
2012-07-10 12:11:07 +02:00
cell . append ( Paragraph ( circle + unicode ( _ ( " Yes " ) ) , stylesheet [ ' Ballot_option ' ] ) )
cell . append ( Paragraph ( circle + unicode ( _ ( " No " ) ) , stylesheet [ ' Ballot_option ' ] ) )
cell . append ( Paragraph ( circle + unicode ( _ ( " Abstention " ) ) , stylesheet [ ' Ballot_option ' ] ) )
2012-04-13 23:52:34 +02:00
data = [ ]
# get ballot papers config values
2012-10-24 11:04:23 +02:00
ballot_papers_selection = config [ " motion_pdf_ballot_papers_selection " ]
ballot_papers_number = config [ " motion_pdf_ballot_papers_number " ]
2012-06-29 00:19:06 +02:00
2012-04-13 23:52:34 +02:00
# set number of ballot papers
2012-06-29 00:19:06 +02:00
if ballot_papers_selection == " NUMBER_OF_DELEGATES " :
2012-09-18 22:47:12 +02:00
number = User . objects . filter ( type__iexact = " delegate " ) . count ( )
2012-06-29 00:19:06 +02:00
elif ballot_papers_selection == " NUMBER_OF_ALL_PARTICIPANTS " :
2012-09-18 22:47:12 +02:00
number = int ( User . objects . count ( ) )
2012-06-29 00:19:06 +02:00
else : # ballot_papers_selection == "CUSTOM_NUMBER"
2012-04-13 23:52:34 +02:00
number = int ( ballot_papers_number )
2012-06-29 00:19:06 +02:00
number = max ( 1 , number )
2012-04-13 23:52:34 +02:00
# print ballot papers
2012-04-18 19:30:32 +02:00
if number > 0 :
2012-07-10 12:11:07 +02:00
for user in xrange ( number / 2 ) :
data . append ( [ cell , cell ] )
2012-04-18 19:30:32 +02:00
rest = number % 2
if rest :
2012-07-10 12:11:07 +02:00
data . append ( [ cell , ' ' ] )
t = Table ( data , 10.5 * cm , 7.42 * cm )
t . setStyle ( TableStyle ( [ ( ' GRID ' , ( 0 , 0 ) , ( - 1 , - 1 ) , 0.25 , colors . grey ) ,
( ' VALIGN ' , ( 0 , 0 ) , ( - 1 , - 1 ) , ' TOP ' ) ,
] ) )
2012-04-18 19:30:32 +02:00
story . append ( t )
2012-04-13 23:52:34 +02:00
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
2012-10-24 11:04:23 +02:00
template_name = ' motion/config.html '
2012-03-16 14:31:59 +01:00
def get_initial ( self ) :
return {
2012-10-24 11:04:23 +02:00
' motion_min_supporters ' : config [ ' motion_min_supporters ' ] ,
' motion_preamble ' : config [ ' motion_preamble ' ] ,
' motion_pdf_ballot_papers_selection ' : config [ ' motion_pdf_ballot_papers_selection ' ] ,
' motion_pdf_ballot_papers_number ' : config [ ' motion_pdf_ballot_papers_number ' ] ,
' motion_pdf_title ' : config [ ' motion_pdf_title ' ] ,
' motion_pdf_preamble ' : config [ ' motion_pdf_preamble ' ] ,
' motion_allow_trivial_change ' : config [ ' motion_allow_trivial_change ' ] ,
2012-03-16 14:31:59 +01:00
}
def form_valid ( self , form ) :
2012-10-24 11:04:23 +02:00
config [ ' motion_min_supporters ' ] = form . cleaned_data [ ' motion_min_supporters ' ]
config [ ' motion_preamble ' ] = form . cleaned_data [ ' motion_preamble ' ]
config [ ' motion_pdf_ballot_papers_selection ' ] = form . cleaned_data [ ' motion_pdf_ballot_papers_selection ' ]
config [ ' motion_pdf_ballot_papers_number ' ] = form . cleaned_data [ ' motion_pdf_ballot_papers_number ' ]
config [ ' motion_pdf_title ' ] = form . cleaned_data [ ' motion_pdf_title ' ]
config [ ' motion_pdf_preamble ' ] = form . cleaned_data [ ' motion_pdf_preamble ' ]
config [ ' motion_allow_trivial_change ' ] = form . cleaned_data [ ' motion_allow_trivial_change ' ]
2012-08-05 22:31:20 +02:00
messages . success ( self . request , _ ( ' Motion settings successfully saved. ' ) )
2012-03-16 14:31:59 +01:00
return super ( Config , self ) . form_valid ( form )
2012-03-18 17:11:58 +01:00
def register_tab ( request ) :
2012-10-24 11:04:23 +02:00
selected = True if request . path . startswith ( ' /motion/ ' ) else False
2012-03-18 17:11:58 +01:00
return Tab (
2012-10-24 11:04:23 +02:00
title = _ ( ' Motions ' ) ,
url = reverse ( ' motion_overview ' ) ,
permission = request . user . has_perm ( ' motion.can_see_motion ' ) or request . user . has_perm ( ' motion.can_support_motion ' ) or request . user . has_perm ( ' motion.can_support_motion ' ) or request . user . has_perm ( ' motion.can_manage_motion ' ) ,
2012-03-18 17:11:58 +01:00
selected = selected ,
)
2012-06-11 13:43:48 +02:00
def get_widgets ( request ) :
2012-06-11 14:22:09 +02:00
return [
2012-08-15 11:56:43 +02:00
Widget (
2012-10-24 11:04:23 +02:00
name = ' motions ' ,
2012-11-08 09:43:04 +01:00
display_name = _ ( ' Motions ' ) ,
2012-10-24 11:04:23 +02:00
template = ' motion/widget.html ' ,
context = { ' motions ' : Motion . objects . all ( ) } ,
2012-11-21 15:14:38 +01:00
permission_required = ' projector.can_manage_projector ' ) ]