2015-01-17 23:05:52 +01:00
|
|
|
import collections
|
|
|
|
|
2012-07-01 15:35:05 +02:00
|
|
|
from django import forms
|
2013-09-25 10:01:01 +02:00
|
|
|
from django.utils.translation import ugettext_lazy
|
2011-07-31 10:46:29 +02:00
|
|
|
|
2015-03-26 05:36:10 +01:00
|
|
|
from openslides.mediafiles.models import Mediafile
|
2013-09-25 10:01:01 +02:00
|
|
|
from openslides.utils.forms import (CleanHtmlFormMixin, CssClassMixin,
|
2014-03-27 20:30:15 +01:00
|
|
|
CSVImportForm, LocalizedModelChoiceField)
|
2014-10-11 14:34:49 +02:00
|
|
|
from openslides.users.models import User
|
2013-09-25 10:01:01 +02:00
|
|
|
|
2014-05-11 19:10:08 +02:00
|
|
|
from ckeditor.widgets import CKEditorWidget
|
|
|
|
|
2014-12-26 13:45:13 +01:00
|
|
|
from .models import Category, Motion, Workflow, Tag
|
2012-02-20 18:44:02 +01:00
|
|
|
|
2011-07-31 10:46:29 +02:00
|
|
|
|
2013-04-16 10:04:28 +02:00
|
|
|
class BaseMotionForm(CleanHtmlFormMixin, CssClassMixin, forms.ModelForm):
|
|
|
|
"""
|
|
|
|
Base FormClass for a Motion.
|
2013-02-05 18:46:46 +01:00
|
|
|
|
2013-02-06 23:56:21 +01:00
|
|
|
For it's own, it append the version data to the fields.
|
2013-02-05 18:46:46 +01:00
|
|
|
|
2013-02-06 23:56:21 +01:00
|
|
|
The class can be mixed with the following mixins to add fields for the
|
2013-02-05 18:46:46 +01:00
|
|
|
submitter, supporters etc.
|
2013-01-26 15:25:54 +01:00
|
|
|
"""
|
2013-04-16 10:04:28 +02:00
|
|
|
clean_html_fields = ('text', 'reason')
|
2013-02-05 18:46:46 +01:00
|
|
|
|
2013-04-22 19:59:05 +02:00
|
|
|
title = forms.CharField(widget=forms.TextInput(), label=ugettext_lazy("Title"))
|
2013-04-16 10:04:28 +02:00
|
|
|
"""
|
|
|
|
Title of the motion. Will be saved in a MotionVersion object.
|
|
|
|
"""
|
2013-02-05 18:46:46 +01:00
|
|
|
|
2014-05-11 19:10:08 +02:00
|
|
|
text = forms.CharField(widget=CKEditorWidget(), label=ugettext_lazy("Text"))
|
2013-04-16 10:04:28 +02:00
|
|
|
"""
|
|
|
|
Text of the motion. Will be saved in a MotionVersion object.
|
|
|
|
"""
|
2013-02-05 18:46:46 +01:00
|
|
|
|
|
|
|
reason = forms.CharField(
|
2014-05-11 19:10:08 +02:00
|
|
|
widget=CKEditorWidget(), required=False, label=ugettext_lazy("Reason"))
|
2013-04-16 10:04:28 +02:00
|
|
|
"""
|
|
|
|
Reason of the motion. will be saved in a MotionVersion object.
|
|
|
|
"""
|
2013-02-05 18:46:46 +01:00
|
|
|
|
2013-10-16 23:57:29 +02:00
|
|
|
attachments = forms.ModelMultipleChoiceField(
|
|
|
|
queryset=Mediafile.objects.all(),
|
|
|
|
required=False,
|
|
|
|
label=ugettext_lazy('Attachments'))
|
|
|
|
"""
|
|
|
|
Attachments of the motion.
|
|
|
|
"""
|
|
|
|
|
2014-12-26 13:45:13 +01:00
|
|
|
tags = forms.ModelMultipleChoiceField(
|
|
|
|
queryset=Tag.objects.all(),
|
|
|
|
required=False,
|
|
|
|
label=ugettext_lazy('Tags'))
|
|
|
|
|
2015-01-17 23:05:52 +01:00
|
|
|
key_order = ('identifier',
|
|
|
|
'title',
|
|
|
|
'text',
|
|
|
|
'reason',
|
|
|
|
'submitter',
|
|
|
|
'supporter',
|
|
|
|
'category',
|
|
|
|
'tags',
|
|
|
|
'attachments',
|
|
|
|
'workflow',
|
|
|
|
'disable_versioning',)
|
|
|
|
"""
|
|
|
|
Order of fields, including optional fields from mixins (for example MotionSupporterMixin)
|
|
|
|
"""
|
|
|
|
|
2013-01-06 12:07:37 +01:00
|
|
|
class Meta:
|
|
|
|
model = Motion
|
|
|
|
fields = ()
|
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
2013-06-11 11:14:36 +02:00
|
|
|
"""
|
2013-10-16 23:57:29 +02:00
|
|
|
Fill the FormFields related to the version data with initial data.
|
2014-12-26 13:45:13 +01:00
|
|
|
Fill also the initial data for attachments and tags.
|
2013-06-11 11:14:36 +02:00
|
|
|
"""
|
2013-01-26 16:05:30 +01:00
|
|
|
self.motion = kwargs.get('instance', None)
|
|
|
|
self.initial = kwargs.setdefault('initial', {})
|
|
|
|
if self.motion is not None:
|
2013-06-11 11:14:36 +02:00
|
|
|
last_version = self.motion.get_last_version()
|
|
|
|
self.initial['title'] = last_version.title
|
|
|
|
self.initial['text'] = last_version.text
|
|
|
|
self.initial['reason'] = last_version.reason
|
2013-10-16 23:57:29 +02:00
|
|
|
self.initial['attachments'] = self.motion.attachments.all()
|
2014-12-26 13:45:13 +01:00
|
|
|
self.initial['tags'] = self.motion.tags.all()
|
2013-01-06 12:07:37 +01:00
|
|
|
super(BaseMotionForm, self).__init__(*args, **kwargs)
|
|
|
|
|
2015-01-17 23:05:52 +01:00
|
|
|
keys = self.fields.keys()
|
|
|
|
keys_order = [key for key in self.key_order if key in keys]
|
|
|
|
keys_order.extend(set(keys) - set(keys_order))
|
|
|
|
self.fields = collections.OrderedDict([(key, self.fields[key]) for key in keys_order])
|
|
|
|
|
2012-06-30 11:31:35 +02:00
|
|
|
|
2013-01-26 15:25:54 +01:00
|
|
|
class MotionSubmitterMixin(forms.ModelForm):
|
2013-02-05 18:46:46 +01:00
|
|
|
"""Mixin to append the submitter field to a MotionForm."""
|
|
|
|
|
2014-10-11 14:34:49 +02:00
|
|
|
submitter = forms.ModelMultipleChoiceField(
|
|
|
|
User.objects, label=ugettext_lazy("Submitter"), required=False)
|
2013-02-06 23:56:21 +01:00
|
|
|
"""Submitter of the motion. Can be one or more persons."""
|
2011-11-09 22:53:10 +01:00
|
|
|
|
2013-01-26 16:05:30 +01:00
|
|
|
def __init__(self, *args, **kwargs):
|
2013-02-05 18:46:46 +01:00
|
|
|
"""Fill in the submitter of the motion as default value."""
|
2013-01-26 16:05:30 +01:00
|
|
|
if self.motion is not None:
|
2015-04-30 19:13:28 +02:00
|
|
|
submitter = self.motion.submitters.all()
|
2013-01-26 16:05:30 +01:00
|
|
|
self.initial['submitter'] = submitter
|
|
|
|
super(MotionSubmitterMixin, self).__init__(*args, **kwargs)
|
|
|
|
|
2012-03-16 14:31:59 +01:00
|
|
|
|
2013-01-26 15:25:54 +01:00
|
|
|
class MotionSupporterMixin(forms.ModelForm):
|
2013-02-05 18:46:46 +01:00
|
|
|
"""Mixin to append the supporter field to a Motionform."""
|
|
|
|
|
2014-10-11 14:34:49 +02:00
|
|
|
supporter = forms.ModelMultipleChoiceField(
|
|
|
|
User.objects, required=False, label=ugettext_lazy("Supporters"))
|
2013-02-06 23:56:21 +01:00
|
|
|
"""Supporter of the motion. Can be one or more persons."""
|
2013-01-26 15:25:54 +01:00
|
|
|
|
2013-01-26 16:42:23 +01:00
|
|
|
def __init__(self, *args, **kwargs):
|
2013-02-05 18:46:46 +01:00
|
|
|
"""Fill in the supporter of the motions as default value."""
|
2013-01-26 16:42:23 +01:00
|
|
|
if self.motion is not None:
|
2015-04-30 19:13:28 +02:00
|
|
|
supporter = self.motion.supporters.all()
|
2013-01-26 16:42:23 +01:00
|
|
|
self.initial['supporter'] = supporter
|
|
|
|
super(MotionSupporterMixin, self).__init__(*args, **kwargs)
|
|
|
|
|
2013-01-26 15:25:54 +01:00
|
|
|
|
2013-02-06 23:56:21 +01:00
|
|
|
class MotionDisableVersioningMixin(forms.ModelForm):
|
|
|
|
"""Mixin to add the option to the form to choose to disable versioning."""
|
2013-02-05 18:46:46 +01:00
|
|
|
|
2013-02-06 23:56:21 +01:00
|
|
|
disable_versioning = forms.BooleanField(
|
2013-04-22 19:59:05 +02:00
|
|
|
required=False, label=ugettext_lazy("Don't create a new version"),
|
|
|
|
help_text=ugettext_lazy("Don't create a new version. Useful e.g. for trivial changes."))
|
2013-02-05 18:46:46 +01:00
|
|
|
"""BooleanField to decide, if a new version will be created, or the
|
|
|
|
last_version will be used."""
|
2012-03-16 14:31:59 +01:00
|
|
|
|
2012-04-17 17:35:50 +02:00
|
|
|
|
2013-05-09 23:51:58 +02:00
|
|
|
# TODO: Add category and identifier to the form as normal fields (the django way),
|
|
|
|
# not as 'new' field from 'new' forms.
|
|
|
|
|
2013-03-11 20:41:02 +01:00
|
|
|
class MotionCategoryMixin(forms.ModelForm):
|
2013-05-09 23:51:58 +02:00
|
|
|
"""
|
|
|
|
Mixin to let the user choose the category for the motion.
|
|
|
|
"""
|
2013-03-11 20:41:02 +01:00
|
|
|
|
2013-04-22 19:59:05 +02:00
|
|
|
category = forms.ModelChoiceField(queryset=Category.objects.all(), required=False, label=ugettext_lazy("Category"))
|
2013-05-09 23:51:58 +02:00
|
|
|
"""
|
|
|
|
Category of the motion.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
"""
|
|
|
|
Fill in the category of the motion as default value.
|
|
|
|
"""
|
|
|
|
if self.motion is not None:
|
|
|
|
category = self.motion.category
|
|
|
|
self.initial['category'] = category
|
|
|
|
super(MotionCategoryMixin, self).__init__(*args, **kwargs)
|
2013-03-11 20:41:02 +01:00
|
|
|
|
|
|
|
|
2013-03-12 23:35:08 +01:00
|
|
|
class MotionIdentifierMixin(forms.ModelForm):
|
2013-04-28 10:02:19 +02:00
|
|
|
"""
|
|
|
|
Mixin to let the user choose the identifier for the motion.
|
|
|
|
"""
|
2013-03-12 23:35:08 +01:00
|
|
|
|
2013-04-28 10:02:19 +02:00
|
|
|
identifier = forms.CharField(required=False, label=ugettext_lazy('Identifier'))
|
|
|
|
|
2013-05-09 23:51:58 +02:00
|
|
|
class Meta:
|
|
|
|
model = Motion
|
|
|
|
fields = ('identifier',)
|
2013-05-08 18:07:09 +02:00
|
|
|
|
|
|
|
|
2013-06-14 09:56:16 +02:00
|
|
|
class MotionWorkflowMixin(forms.ModelForm):
|
2013-05-13 23:08:55 +02:00
|
|
|
"""
|
2013-06-14 09:56:16 +02:00
|
|
|
Mixin to let the user change the workflow of the motion.
|
2013-05-13 23:08:55 +02:00
|
|
|
"""
|
|
|
|
|
2013-06-14 09:56:16 +02:00
|
|
|
workflow = LocalizedModelChoiceField(
|
2013-05-13 23:08:55 +02:00
|
|
|
queryset=Workflow.objects.all(),
|
2013-06-14 09:56:16 +02:00
|
|
|
empty_label=None,
|
2013-05-13 23:08:55 +02:00
|
|
|
label=ugettext_lazy('Workflow'),
|
2013-06-14 09:56:16 +02:00
|
|
|
help_text=ugettext_lazy('Set a specific workflow to switch to it. '
|
|
|
|
'If you do so, the state of the motion will be reset.'))
|
2013-05-13 23:08:55 +02:00
|
|
|
|
|
|
|
|
2014-03-27 20:30:15 +01:00
|
|
|
class MotionCSVImportForm(CSVImportForm):
|
2013-05-12 00:47:49 +02:00
|
|
|
"""
|
|
|
|
Form for motion import via csv file.
|
|
|
|
"""
|
|
|
|
override = forms.BooleanField(
|
|
|
|
required=False,
|
|
|
|
label=ugettext_lazy('Override existing motions with the same identifier'),
|
|
|
|
help_text=ugettext_lazy('If this is active, every motion with the same identifier as in your csv file will be overridden.'))
|
|
|
|
"""
|
|
|
|
Flag to decide whether existing motions (according to the identifier)
|
|
|
|
should be overridden.
|
|
|
|
"""
|
|
|
|
|
2014-10-11 14:34:49 +02:00
|
|
|
default_submitter = forms.ModelChoiceField(
|
|
|
|
User.objects.all(),
|
2013-05-12 00:47:49 +02:00
|
|
|
required=True,
|
|
|
|
label=ugettext_lazy('Default submitter'),
|
|
|
|
help_text=ugettext_lazy('This person is used as submitter for any line of your csv file which does not contain valid submitter data.'))
|
|
|
|
"""
|
|
|
|
Person which is used as submitter, if the line of the csv file does
|
|
|
|
not contain valid submitter data.
|
|
|
|
"""
|