From 82c804e2d631dc29fe7cbea8aee3d8a1fa7aed89 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Norman=20J=C3=A4ckel?= Date: Wed, 3 Apr 2013 00:40:56 +0200 Subject: [PATCH] Inserted a new config variable 'Stop submitting of new motions' Normal delegates can not submit a new motion when this is set to true. Inserted also a new template filter to relate to config variables in if-clauses. --- openslides/motion/signals.py | 9 ++++- .../motion/templates/motion/motion_list.html | 4 +- openslides/motion/views.py | 13 ++++++- openslides/utils/templatetags/tags.py | 5 +++ tests/motion/test_models.py | 7 +++- tests/motion/test_views.py | 31 ++++++++++++++- tests/utils/__init__.py | 0 tests/utils/test_template_tags_filters.py | 39 +++++++++++++++++++ 8 files changed, 103 insertions(+), 5 deletions(-) create mode 100644 tests/utils/__init__.py create mode 100644 tests/utils/test_template_tags_filters.py diff --git a/openslides/motion/signals.py b/openslides/motion/signals.py index 8946201fd..7c65d07f3 100644 --- a/openslides/motion/signals.py +++ b/openslides/motion/signals.py @@ -26,6 +26,12 @@ def setup_motion_config_page(sender, **kwargs): """ Motion config variables. """ + motion_stop_submitting = ConfigVariable( + name='motion_stop_submitting', + default_value=False, + form_field=forms.BooleanField( + label=_('Stop submitting new motions by non-staff users'), + required=False)) motion_min_supporters = ConfigVariable( name='motion_min_supporters', default_value=0, @@ -106,7 +112,8 @@ def setup_motion_config_page(sender, **kwargs): url='motion', required_permission='config.can_manage', weight=30, - variables=(motion_min_supporters, + variables=(motion_stop_submitting, + motion_min_supporters, motion_preamble, motion_pdf_ballot_papers_selection, motion_pdf_ballot_papers_number, diff --git a/openslides/motion/templates/motion/motion_list.html b/openslides/motion/templates/motion/motion_list.html index bc5d630c9..ef4409645 100644 --- a/openslides/motion/templates/motion/motion_list.html +++ b/openslides/motion/templates/motion/motion_list.html @@ -10,7 +10,9 @@ {% trans "Motions" %} {% if perms.motion.can_create_motion %} - {% trans 'New' %} + {% if not 'motion_stop_submitting'|get_config or perms.motion.can_manage_motion %} + {% trans 'New' %} + {% endif %} {% endif %} {% if perms.motion.can_manage_motion %} {% trans 'Category' %} diff --git a/openslides/motion/views.py b/openslides/motion/views.py index 608d8f5d3..9176e822c 100644 --- a/openslides/motion/views.py +++ b/openslides/motion/views.py @@ -152,9 +152,20 @@ class MotionMixin(object): class MotionCreateView(MotionMixin, CreateView): """View to create a motion.""" - permission_required = 'motion.can_create_motion' model = Motion + def has_permission(self, request, *args, **kwargs): + """ + Checks whether the requesting user can submit a new motion. He needs + at least the permission 'motion.can_create_motion'. If the submitting + of new motions by non-staff users is stopped via config variable + 'motion_stop_submitting', the requesting user needs also to have + 'motion.can_manage_motion'. + """ + if request.user.has_perm('motion.can_create_motion'): + return not config['motion_stop_submitting'] or request.user.has_perm('motion.can_manage_motion') + return False + def form_valid(self, form): """Write a log message, if the form is valid.""" value = super(MotionCreateView, self).form_valid(form) diff --git a/openslides/utils/templatetags/tags.py b/openslides/utils/templatetags/tags.py index 1119b9cb7..7563ebfb2 100644 --- a/openslides/utils/templatetags/tags.py +++ b/openslides/utils/templatetags/tags.py @@ -22,6 +22,11 @@ def get_config(key): return config[key] +@register.filter +def get_config(key): + return config[key] + + @register.simple_tag def active(request, pattern): if request.path.startswith(pattern): diff --git a/tests/motion/test_models.py b/tests/motion/test_models.py index 475afd7dc..aeb79809f 100644 --- a/tests/motion/test_models.py +++ b/tests/motion/test_models.py @@ -4,7 +4,7 @@ Tests for openslides.motion.models ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - :copyright: 2011, 2012 by OpenSlides team, see AUTHORS. + :copyright: 2011–2013 by OpenSlides team, see AUTHORS. :license: GNU GPL, see LICENSE for more details. """ @@ -136,3 +136,8 @@ class ModelTest(TestCase): with self.assertRaises(WorkflowError): state_1.next_states.add(state_2) state_1.save() + + +class ConfigTest(TestCase): + def test_stop_submitting(self): + self.assertFalse(config['motion_stop_submitting']) diff --git a/tests/motion/test_views.py b/tests/motion/test_views.py index 15cc01c79..90519ca71 100644 --- a/tests/motion/test_views.py +++ b/tests/motion/test_views.py @@ -4,12 +4,13 @@ Tests for openslides.motion.models ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - :copyright: 2011, 2012 by OpenSlides team, see AUTHORS. + :copyright: 2011–2013 by OpenSlides team, see AUTHORS. :license: GNU GPL, see LICENSE for more details. """ from django.test.client import Client +from openslides.config.api import config from openslides.utils.test import TestCase from openslides.participant.models import User, Group from openslides.motion.models import Motion @@ -22,6 +23,14 @@ class MotionViewTestCase(TestCase): self.admin_client = Client() self.admin_client.login(username='admin', password='admin') + # Staff + self.staff = User.objects.create_user('staff', 'staff@user.user', 'staff') + staff_group = Group.objects.get(name='Staff') + self.staff.groups.add(staff_group) + self.staff.save() + self.staff_client = Client() + self.staff_client.login(username='staff', password='staff') + # Delegate self.delegate = User.objects.create_user('delegate', 'delegate@user.user', 'delegate') delegate_group = Group.objects.get(name='Delegates') @@ -90,6 +99,26 @@ class TestMotionCreateView(MotionViewTestCase): self.assertEqual(response.status_code, 403) self.assertFalse(Motion.objects.filter(versions__title='registered motion').exists()) + def test_delegate_after_stop_submitting_new_motions(self): + config['motion_stop_submitting'] = True + response = self.delegate_client.get(self.url) + self.assertEqual(response.status_code, 403) + + def test_delegate_after_stop_submitting_new_motions_overview(self): + config['motion_stop_submitting'] = True + response = self.delegate_client.get('/motion/') + self.assertNotContains(response, 'href="/motion/new/"', status_code=200) + + def test_staff_after_stop_submitting_new_motions(self): + config['motion_stop_submitting'] = True + response = self.staff_client.get(self.url) + self.assertEqual(response.status_code, 200) + + def test_staff_after_stop_submitting_new_motions_overview(self): + config['motion_stop_submitting'] = True + response = self.staff_client.get('/motion/') + self.assertContains(response, 'href="/motion/new/"', status_code=200) + class TestMotionUpdateView(MotionViewTestCase): url = '/motion/1/edit/' diff --git a/tests/utils/__init__.py b/tests/utils/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/tests/utils/test_template_tags_filters.py b/tests/utils/test_template_tags_filters.py new file mode 100644 index 000000000..f84fd7b18 --- /dev/null +++ b/tests/utils/test_template_tags_filters.py @@ -0,0 +1,39 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +""" + Tests for openslides.utils.templatetags.tags + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + + :copyright: 2011–2013 by OpenSlides team, see AUTHORS. + :license: GNU GPL, see LICENSE for more details. +""" + +from django.template import Template, Context + +from openslides.utils.test import TestCase +from openslides.config.api import config + + +class ConfigTagAndFilter(TestCase): + def test_config_tag(self): + config['taiNg3reQuooGha4'] = 'iWoor0caThieK7yi' + template = Template("{% load tags %} The config var is {% get_config 'taiNg3reQuooGha4' %}.") + self.assertTrue('The config var is iWoor0caThieK7yi.' in template.render(Context({}))) + + def test_config_filter(self): + config['fkjTze56ncuejWqs'] = 'REG56Hnmfk9TdfsD' + template = Template("{% load tags %} The config var is {{ 'fkjTze56ncuejWqs'|get_config }}.") + self.assertTrue('The config var is REG56Hnmfk9TdfsD.' in template.render(Context({}))) + + def test_both_in_one(self): + config['jfhsnezfh452w6Fg'] = True + config['sdmvldkfgj4534gk'] = 'FdgfkR04jtg9f8bq' + template_code = """{% load tags %} + {% if 'jfhsnezfh452w6Fg'|get_config %} + {% get_config 'sdmvldkfgj4534gk' %} + {% else %} + bad_e0fvkfHFD + {% endif %}""" + template = Template(template_code) + self.assertTrue('FdgfkR04jtg9f8bq' in template.render(Context({}))) + self.assertFalse('bad_e0fvkfHFD' in template.render(Context({})))