Added links to set and reset the state of a motion

This commit is contained in:
Oskar Hahn 2013-02-01 17:20:11 +01:00
parent ac41a02ac5
commit 1a2f0a1b5a
6 changed files with 72 additions and 9 deletions

View File

@ -2,7 +2,7 @@
# -*- coding: utf-8 -*-
"""
openslides.motion.models
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~
Models for the motion app.
@ -88,7 +88,7 @@ class Motion(SlideMixin, models.Model):
Saves the motion. Create or update a motion_version object
"""
if not self.state_id:
self.state = 'default'
self.reset_state()
super(Motion, self).save(*args, **kwargs)
for attr in ['title', 'text', 'reason']:
@ -296,18 +296,27 @@ class Motion(SlideMixin, models.Model):
"""
return get_state(self.state_id)
def set_state(self, state):
def set_state(self, next_state):
"""
Set the state of this motion.
state has to be a valid state id or State object.
next_state has to be a valid state id or State object.
"""
if type(state) is not State:
state = get_state(state)
self.state_id = state.id
if type(next_state) is not State:
next_state = get_state(next_state)
if next_state in self.state.next_states:
self.state_id = next_state.id
else:
raise WorkflowError('%s is not a valid next_state' % next_state)
state = property(get_state, set_state)
def reset_state(self):
"""
Set the state to the default state.
"""
self.state_id = get_state('default').id
class MotionVersion(models.Model):
title = models.CharField(max_length=255, verbose_name=ugettext_lazy("Title"))

View File

@ -13,7 +13,16 @@
<p>Submitter: {% for submitter in motion.submitter.all %}{{ submitter.person }} {% endfor %}</p>
<p>Supporter: {% for supporter in motion.supporter.all %}{{ supporter.person }} {% endfor %}</p>
<p>State: {{ motion.state }}</p>
<h4>possible stats:</h4>
<ul>
{% for state in motion.state.next_states %}
<li><a href="{% url 'motion_set_state' motion.pk state.id %}">{{ state }}</a></li>
{% endfor %}
<li><a href="{% url 'motion_reset_state' motion.pk %}">Reset State</a></li>
</ul>
<h4>Versions</h4>
<ol>
{% for motion_version in motion.versions.all %}
{% if motion_version.id == motion.version.id %}

View File

@ -62,4 +62,14 @@ urlpatterns = patterns('openslides.motion.views',
'poll_delete',
name='motion_poll_delete',
),
url(r'^(?P<pk>\d+)/set_state/(?P<state>[a-z]{3})/$',
'set_state',
name='motion_set_state',
),
url(r'^(?P<pk>\d+)/reset_state/$',
'reset_state',
name='motion_reset_state',
),
)

View File

@ -32,6 +32,7 @@ from openslides.config.models import config
from .models import Motion, MotionSubmitter, MotionSupporter, MotionPoll
from .forms import (BaseMotionForm, MotionSubmitterMixin, MotionSupporterMixin,
MotionCreateNewVersionMixin, ConfigForm)
from .workflow import WorkflowError
class MotionListView(ListView):
"""
@ -233,6 +234,34 @@ class PollDeleteView(PollMixin, DeleteView):
poll_delete = PollDeleteView.as_view()
class MotionSetStateView(SingleObjectMixin, RedirectView):
permission_required = 'motion.can_manage_motion'
url_name = 'motion_detail'
model = Motion
reset = False
def pre_redirect(self, request, *args, **kwargs):
self.object = self.get_object()
try:
if self.reset:
self.object.reset_state()
else:
self.object.state = kwargs['state']
except WorkflowError:
messages.error(request, _('Can not set the state to: %s.')
% html_strong(kwargs['state']))
else:
self.object.save()
messages.success(request, _('Motion status was set to: %s.'
% html_strong(self.object.state)))
def get_url_name_args(self):
return [self.object.pk]
set_state = MotionSetStateView.as_view()
reset_state = MotionSetStateView.as_view(reset=True)
class Config(FormView):
permission_required = 'config.can_manage_config'
form_class = ConfigForm

View File

@ -30,7 +30,10 @@ def motion_workflow_choices():
def get_state(state='default'):
global _workflow
if _workflow is not None:
return _workflow[state]
try:
return _workflow[state]
except KeyError:
raise WorkflowError('Unknown state: %s' % state)
_workflow = {}
for workflow in settings.MOTION_WORKFLOW:
if workflow[0] == config['motion_workflow']:

View File

@ -240,10 +240,13 @@ class RedirectView(PermissionMixin, AjaxMixin, _RedirectView):
def get_redirect_url(self, **kwargs):
if self.url_name is not None:
return reverse(self.url_name)
return reverse(self.url_name, args=self.get_url_name_args())
else:
return super(RedirectView, self).get_redirect_url(**kwargs)
def get_url_name_args(self):
return []
class FormView(PermissionMixin, ExtraContextMixin, UrlMixin, _FormView):
def form_invalid(self, form):