2015-04-30 19:13:28 +02:00
|
|
|
from django.db import transaction
|
|
|
|
from django.utils.translation import ugettext as _
|
|
|
|
|
2015-06-29 12:08:15 +02:00
|
|
|
from openslides.core.config import config
|
2015-04-30 19:13:28 +02:00
|
|
|
from openslides.utils.rest_api import (
|
|
|
|
CharField,
|
2015-07-22 15:23:57 +02:00
|
|
|
DictField,
|
2015-04-30 19:13:28 +02:00
|
|
|
IntegerField,
|
|
|
|
ModelSerializer,
|
|
|
|
PrimaryKeyRelatedField,
|
2015-10-21 21:13:45 +02:00
|
|
|
SerializerMethodField,
|
2015-06-16 10:37:23 +02:00
|
|
|
ValidationError,
|
|
|
|
)
|
2015-01-24 16:35:50 +01:00
|
|
|
|
|
|
|
from .models import (
|
|
|
|
Category,
|
|
|
|
Motion,
|
|
|
|
MotionLog,
|
|
|
|
MotionPoll,
|
|
|
|
MotionVersion,
|
|
|
|
State,
|
2015-06-16 10:37:23 +02:00
|
|
|
Workflow,
|
|
|
|
)
|
2015-01-24 16:35:50 +01:00
|
|
|
|
|
|
|
|
2015-04-30 19:13:28 +02:00
|
|
|
def validate_workflow_field(value):
|
|
|
|
"""
|
|
|
|
Validator to ensure that the workflow with the given id exists.
|
|
|
|
"""
|
|
|
|
if not Workflow.objects.filter(pk=value).exists():
|
|
|
|
raise ValidationError(_('Workflow %(pk)d does not exist.') % {'pk': value})
|
|
|
|
|
|
|
|
|
2015-02-12 18:48:14 +01:00
|
|
|
class CategorySerializer(ModelSerializer):
|
2015-01-24 16:35:50 +01:00
|
|
|
"""
|
|
|
|
Serializer for motion.models.Category objects.
|
|
|
|
"""
|
|
|
|
class Meta:
|
|
|
|
model = Category
|
2015-02-04 00:08:38 +01:00
|
|
|
fields = ('id', 'name', 'prefix',)
|
2015-01-24 16:35:50 +01:00
|
|
|
|
|
|
|
|
2015-02-12 18:48:14 +01:00
|
|
|
class StateSerializer(ModelSerializer):
|
2015-01-24 16:35:50 +01:00
|
|
|
"""
|
|
|
|
Serializer for motion.models.State objects.
|
|
|
|
"""
|
|
|
|
class Meta:
|
|
|
|
model = State
|
|
|
|
fields = (
|
|
|
|
'id',
|
|
|
|
'name',
|
|
|
|
'action_word',
|
|
|
|
'icon',
|
|
|
|
'required_permission_to_see',
|
|
|
|
'allow_support',
|
|
|
|
'allow_create_poll',
|
|
|
|
'allow_submitter_edit',
|
|
|
|
'versioning',
|
|
|
|
'leave_old_version_active',
|
|
|
|
'dont_set_identifier',
|
|
|
|
'next_states',)
|
|
|
|
|
|
|
|
|
2015-02-12 18:48:14 +01:00
|
|
|
class WorkflowSerializer(ModelSerializer):
|
2015-01-24 16:35:50 +01:00
|
|
|
"""
|
|
|
|
Serializer for motion.models.Workflow objects.
|
|
|
|
"""
|
|
|
|
state_set = StateSerializer(many=True, read_only=True)
|
2015-02-12 18:48:14 +01:00
|
|
|
first_state = PrimaryKeyRelatedField(read_only=True)
|
2015-01-24 16:35:50 +01:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = Workflow
|
2015-02-04 00:08:38 +01:00
|
|
|
fields = ('id', 'name', 'state_set', 'first_state',)
|
2015-01-24 16:35:50 +01:00
|
|
|
|
|
|
|
|
2015-02-12 18:48:14 +01:00
|
|
|
class MotionLogSerializer(ModelSerializer):
|
2015-01-24 16:35:50 +01:00
|
|
|
"""
|
|
|
|
Serializer for motion.models.MotionLog objects.
|
|
|
|
"""
|
|
|
|
class Meta:
|
|
|
|
model = MotionLog
|
|
|
|
fields = ('message_list', 'person', 'time',)
|
|
|
|
|
|
|
|
|
2015-02-12 18:48:14 +01:00
|
|
|
class MotionPollSerializer(ModelSerializer):
|
2015-01-24 16:35:50 +01:00
|
|
|
"""
|
|
|
|
Serializer for motion.models.MotionPoll objects.
|
|
|
|
"""
|
2015-10-21 21:13:45 +02:00
|
|
|
yes = SerializerMethodField()
|
|
|
|
no = SerializerMethodField()
|
|
|
|
abstain = SerializerMethodField()
|
2015-07-22 15:23:57 +02:00
|
|
|
votes = DictField(
|
|
|
|
child=IntegerField(min_value=-2),
|
|
|
|
write_only=True)
|
2015-01-24 16:35:50 +01:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = MotionPoll
|
|
|
|
fields = (
|
2015-07-22 15:23:57 +02:00
|
|
|
'id',
|
2015-10-21 21:13:45 +02:00
|
|
|
'yes',
|
|
|
|
'no',
|
|
|
|
'abstain',
|
2015-01-24 16:35:50 +01:00
|
|
|
'votesvalid',
|
|
|
|
'votesinvalid',
|
2015-07-22 15:23:57 +02:00
|
|
|
'votescast',
|
|
|
|
'votes',)
|
2015-10-21 21:13:45 +02:00
|
|
|
|
|
|
|
def get_yes(self, obj):
|
2015-10-21 22:24:11 +02:00
|
|
|
try:
|
|
|
|
result = obj.get_votes().get(value='Yes').weight
|
|
|
|
except obj.get_vote_class().DoesNotExist:
|
|
|
|
result = None
|
|
|
|
return result
|
2015-10-21 21:13:45 +02:00
|
|
|
|
|
|
|
def get_no(self, obj):
|
2015-10-21 22:24:11 +02:00
|
|
|
try:
|
|
|
|
result = obj.get_votes().get(value='No').weight
|
|
|
|
except obj.get_vote_class().DoesNotExist:
|
|
|
|
result = None
|
|
|
|
return result
|
2015-10-21 21:13:45 +02:00
|
|
|
|
|
|
|
def get_abstain(self, obj):
|
2015-10-21 22:24:11 +02:00
|
|
|
try:
|
|
|
|
result = obj.get_votes().get(value='Abstain').weight
|
|
|
|
except obj.get_vote_class().DoesNotExist:
|
|
|
|
result = None
|
|
|
|
return result
|
2015-07-22 15:23:57 +02:00
|
|
|
|
|
|
|
@transaction.atomic
|
|
|
|
def update(self, instance, validated_data):
|
|
|
|
"""
|
|
|
|
Customized update method for polls. To update votes use the write
|
|
|
|
only field 'votes'.
|
|
|
|
|
|
|
|
Example data:
|
|
|
|
|
|
|
|
"votes": {"Yes": 10, "No": 4, "Abstain": -2}
|
|
|
|
"""
|
|
|
|
# Update votes.
|
|
|
|
votes = validated_data.get('votes')
|
|
|
|
if votes:
|
|
|
|
if len(votes) != len(instance.get_vote_values()):
|
|
|
|
raise ValidationError({
|
|
|
|
'detail': _('You have to submit data for %d vote values.') % len(instance.get_vote_values())})
|
|
|
|
for vote_value, vote_weight in votes.items():
|
|
|
|
if vote_value not in instance.get_vote_values():
|
|
|
|
raise ValidationError({
|
|
|
|
'detail': _('Vote value %s is invalid.') % vote_value})
|
|
|
|
instance.set_vote_objects_with_values(instance.get_options().get(), votes)
|
|
|
|
|
|
|
|
# Update remaining writeable fields.
|
|
|
|
instance.votesvalid = validated_data.get('votesvalid', instance.votesvalid)
|
|
|
|
instance.votesinvalid = validated_data.get('votesinvalid', instance.votesinvalid)
|
|
|
|
instance.votescast = validated_data.get('votescast', instance.votescast)
|
|
|
|
instance.save()
|
|
|
|
return instance
|
2015-01-24 16:35:50 +01:00
|
|
|
|
|
|
|
|
2015-02-12 18:48:14 +01:00
|
|
|
class MotionVersionSerializer(ModelSerializer):
|
2015-01-24 16:35:50 +01:00
|
|
|
"""
|
|
|
|
Serializer for motion.models.MotionVersion objects.
|
|
|
|
"""
|
|
|
|
class Meta:
|
|
|
|
model = MotionVersion
|
|
|
|
fields = (
|
|
|
|
'id',
|
|
|
|
'version_number',
|
|
|
|
'creation_time',
|
|
|
|
'title',
|
|
|
|
'text',
|
|
|
|
'reason',)
|
|
|
|
|
|
|
|
|
2015-02-12 18:48:14 +01:00
|
|
|
class MotionSerializer(ModelSerializer):
|
2015-01-24 16:35:50 +01:00
|
|
|
"""
|
|
|
|
Serializer for motion.models.Motion objects.
|
|
|
|
"""
|
2015-02-12 18:48:14 +01:00
|
|
|
active_version = PrimaryKeyRelatedField(read_only=True)
|
2015-01-24 16:35:50 +01:00
|
|
|
log_messages = MotionLogSerializer(many=True, read_only=True)
|
2015-04-30 19:13:28 +02:00
|
|
|
polls = MotionPollSerializer(many=True, read_only=True)
|
|
|
|
reason = CharField(allow_blank=True, required=False, write_only=True)
|
|
|
|
state = StateSerializer(read_only=True)
|
|
|
|
text = CharField(write_only=True)
|
|
|
|
title = CharField(max_length=255, write_only=True)
|
|
|
|
versions = MotionVersionSerializer(many=True, read_only=True)
|
|
|
|
workflow = IntegerField(min_value=1, required=False, validators=[validate_workflow_field])
|
2015-01-24 16:35:50 +01:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = Motion
|
|
|
|
fields = (
|
2015-02-04 00:08:38 +01:00
|
|
|
'id',
|
2015-01-24 16:35:50 +01:00
|
|
|
'identifier',
|
2015-04-30 19:13:28 +02:00
|
|
|
'title',
|
|
|
|
'text',
|
|
|
|
'reason',
|
2015-01-24 16:35:50 +01:00
|
|
|
'versions',
|
|
|
|
'active_version',
|
2015-04-30 19:13:28 +02:00
|
|
|
'parent',
|
|
|
|
'category',
|
|
|
|
'submitters',
|
|
|
|
'supporters',
|
2015-01-24 16:35:50 +01:00
|
|
|
'state',
|
|
|
|
'workflow',
|
2015-04-30 19:13:28 +02:00
|
|
|
'tags',
|
2015-01-24 16:35:50 +01:00
|
|
|
'attachments',
|
|
|
|
'polls',
|
2015-10-24 19:02:43 +02:00
|
|
|
'agenda_item_id',
|
2015-01-24 16:35:50 +01:00
|
|
|
'log_messages',)
|
2015-04-30 19:13:28 +02:00
|
|
|
read_only_fields = ('parent',) # Some other fields are also read_only. See definitions above.
|
2015-01-24 16:35:50 +01:00
|
|
|
|
2015-04-30 19:13:28 +02:00
|
|
|
@transaction.atomic
|
|
|
|
def create(self, validated_data):
|
|
|
|
"""
|
|
|
|
Customized method to create a new motion from some data.
|
|
|
|
"""
|
|
|
|
motion = Motion()
|
|
|
|
motion.title = validated_data['title']
|
|
|
|
motion.text = validated_data['text']
|
|
|
|
motion.reason = validated_data.get('reason', '')
|
|
|
|
motion.identifier = validated_data.get('identifier')
|
|
|
|
motion.category = validated_data.get('category')
|
2015-06-16 18:12:59 +02:00
|
|
|
motion.reset_state(validated_data.get('workflow', int(config['motions_workflow'])))
|
2015-04-30 19:13:28 +02:00
|
|
|
motion.save()
|
2015-07-06 09:19:42 +02:00
|
|
|
if validated_data.get('submitters'):
|
2015-04-30 19:13:28 +02:00
|
|
|
motion.submitters.add(*validated_data['submitters'])
|
|
|
|
else:
|
|
|
|
motion.submitters.add(validated_data['request_user'])
|
2015-07-06 09:19:42 +02:00
|
|
|
motion.supporters.add(*validated_data.get('supporters', []))
|
|
|
|
motion.attachments.add(*validated_data.get('attachments', []))
|
|
|
|
motion.tags.add(*validated_data.get('tags', []))
|
2015-04-30 19:13:28 +02:00
|
|
|
return motion
|
|
|
|
|
|
|
|
@transaction.atomic
|
|
|
|
def update(self, motion, validated_data):
|
2015-01-24 16:35:50 +01:00
|
|
|
"""
|
2015-04-30 19:13:28 +02:00
|
|
|
Customized method to update a motion.
|
2015-01-24 16:35:50 +01:00
|
|
|
"""
|
2015-04-30 19:13:28 +02:00
|
|
|
# Identifier and category.
|
|
|
|
for key in ('identifier', 'category'):
|
|
|
|
if key in validated_data.keys():
|
|
|
|
setattr(motion, key, validated_data[key])
|
|
|
|
|
|
|
|
# Workflow.
|
|
|
|
workflow = validated_data.get('workflow')
|
|
|
|
if workflow is not None and workflow != motion.workflow:
|
|
|
|
motion.reset_state(workflow)
|
|
|
|
|
|
|
|
# Decide if a new version is saved to the database.
|
|
|
|
if (motion.state.versioning and
|
|
|
|
not validated_data.get('disable_versioning', False)): # TODO
|
|
|
|
version = motion.get_new_version()
|
|
|
|
else:
|
|
|
|
version = motion.get_last_version()
|
|
|
|
|
|
|
|
# Title, text, reason.
|
|
|
|
for key in ('title', 'text', 'reason'):
|
|
|
|
if key in validated_data.keys():
|
|
|
|
setattr(version, key, validated_data[key])
|
|
|
|
|
|
|
|
motion.save(use_version=version)
|
|
|
|
|
|
|
|
# Submitters, supporters, attachments and tags
|
|
|
|
for key in ('submitters', 'supporters', 'attachments', 'tags'):
|
|
|
|
if key in validated_data.keys():
|
|
|
|
attr = getattr(motion, key)
|
|
|
|
attr.clear()
|
|
|
|
attr.add(*validated_data[key])
|
|
|
|
|
|
|
|
return motion
|