Added view to create related assignment and motion items. Updated serializers.

This commit is contained in:
Norman Jäckel 2015-06-21 23:23:40 +02:00
parent 4c9683a1b3
commit e0188a149d
4 changed files with 72 additions and 3 deletions

View File

@ -1,12 +1,15 @@
from django.contrib.contenttypes.models import ContentType
from django.db import transaction from django.db import transaction
from django.utils.translation import ugettext as _ from django.utils.translation import ugettext as _
from openslides.agenda.models import Item
from openslides.utils.rest_api import ( from openslides.utils.rest_api import (
DictField, DictField,
IntegerField, IntegerField,
ListField, ListField,
ListSerializer, ListSerializer,
ModelSerializer, ModelSerializer,
SerializerMethodField,
ValidationError, ValidationError,
) )
@ -156,6 +159,7 @@ class AssignmentFullSerializer(ModelSerializer):
""" """
Serializer for assignment.models.Assignment objects. With all polls. Serializer for assignment.models.Assignment objects. With all polls.
""" """
agenda_items = SerializerMethodField()
assignment_related_users = AssignmentRelatedUserSerializer(many=True, read_only=True) assignment_related_users = AssignmentRelatedUserSerializer(many=True, read_only=True)
polls = AssignmentAllPollSerializer(many=True, read_only=True) polls = AssignmentAllPollSerializer(many=True, read_only=True)
@ -170,14 +174,22 @@ class AssignmentFullSerializer(ModelSerializer):
'assignment_related_users', 'assignment_related_users',
'poll_description_default', 'poll_description_default',
'polls', 'polls',
'tags',) 'tags',
'agenda_items',)
def get_agenda_items(self, obj):
"""
Returns a list of ids of all agenda items that are related to this
assignment.
"""
assignment_content_type = ContentType.objects.get_for_model(obj)
return (item.pk for item in Item.objects.filter(content_type=assignment_content_type, object_id=obj.pk))
class AssignmentShortSerializer(AssignmentFullSerializer): class AssignmentShortSerializer(AssignmentFullSerializer):
""" """
Serializer for assignment.models.Assignment objects. Without unpublished poll. Serializer for assignment.models.Assignment objects. Without unpublished poll.
""" """
assignment_related_users = AssignmentRelatedUserSerializer(many=True, read_only=True)
polls = AssignmentShortPollSerializer(many=True, read_only=True) polls = AssignmentShortPollSerializer(many=True, read_only=True)
class Meta: class Meta:
@ -191,4 +203,5 @@ class AssignmentShortSerializer(AssignmentFullSerializer):
'assignment_related_users', 'assignment_related_users',
'poll_description_default', 'poll_description_default',
'polls', 'polls',
'tags',) 'tags',
'agenda_items',)

View File

@ -15,6 +15,7 @@ from reportlab.platypus import (
TableStyle, TableStyle,
) )
from openslides.agenda.models import Item
from openslides.config.api import config from openslides.config.api import config
from openslides.users.models import Group, User # TODO: remove this from openslides.users.models import Group, User # TODO: remove this
from openslides.utils.pdf import stylesheet from openslides.utils.pdf import stylesheet
@ -213,6 +214,25 @@ class AssignmentViewSet(ModelViewSet):
assignment.create_poll() assignment.create_poll()
return Response({'detail': _(' Poll created successfully.')}) return Response({'detail': _(' Poll created successfully.')})
@detail_route(methods=['post'])
def create_agenda_item(self, request, pk=None):
"""
Speacial view endpoint to create an agenda item that is related to
this assignment.
"""
# Check permission.
if not request.user.has_perm('assignments.can_manage'):
self.permission_denied(request)
# Retrieve motion.
assignment = self.get_object()
# Create agenda item.
Item.objects.create(content_object=assignment)
# Initiate response.
return Response({'detail': _('Agenda item successfully created.')})
class AssignmentPollViewSet(UpdateModelMixin, DestroyModelMixin, GenericViewSet): class AssignmentPollViewSet(UpdateModelMixin, DestroyModelMixin, GenericViewSet):
""" """

View File

@ -1,12 +1,15 @@
from django.contrib.contenttypes.models import ContentType
from django.db import transaction from django.db import transaction
from django.utils.translation import ugettext as _ from django.utils.translation import ugettext as _
from openslides.agenda.models import Item
from openslides.config.api import config from openslides.config.api import config
from openslides.utils.rest_api import ( from openslides.utils.rest_api import (
CharField, CharField,
IntegerField, IntegerField,
ModelSerializer, ModelSerializer,
PrimaryKeyRelatedField, PrimaryKeyRelatedField,
SerializerMethodField,
ValidationError, ValidationError,
) )
@ -138,6 +141,7 @@ class MotionSerializer(ModelSerializer):
Serializer for motion.models.Motion objects. Serializer for motion.models.Motion objects.
""" """
active_version = PrimaryKeyRelatedField(read_only=True) active_version = PrimaryKeyRelatedField(read_only=True)
agenda_items = SerializerMethodField()
log_messages = MotionLogSerializer(many=True, read_only=True) log_messages = MotionLogSerializer(many=True, read_only=True)
polls = MotionPollSerializer(many=True, read_only=True) polls = MotionPollSerializer(many=True, read_only=True)
reason = CharField(allow_blank=True, required=False, write_only=True) reason = CharField(allow_blank=True, required=False, write_only=True)
@ -165,6 +169,7 @@ class MotionSerializer(ModelSerializer):
'workflow', 'workflow',
'tags', 'tags',
'attachments', 'attachments',
'agenda_items',
'polls', 'polls',
'log_messages',) 'log_messages',)
read_only_fields = ('parent',) # Some other fields are also read_only. See definitions above. read_only_fields = ('parent',) # Some other fields are also read_only. See definitions above.
@ -228,3 +233,11 @@ class MotionSerializer(ModelSerializer):
attr.add(*validated_data[key]) attr.add(*validated_data[key])
return motion return motion
def get_agenda_items(self, obj):
"""
Returns a list of ids of all agenda items that are related to this
motion.
"""
motion_content_type = ContentType.objects.get_for_model(obj)
return (item.pk for item in Item.objects.filter(content_type=motion_content_type, object_id=obj.pk))

View File

@ -6,6 +6,7 @@ from django.utils.translation import ugettext_noop
from reportlab.platypus import SimpleDocTemplate from reportlab.platypus import SimpleDocTemplate
from rest_framework import status from rest_framework import status
from openslides.agenda.models import Item
from openslides.config.api import config from openslides.config.api import config
from openslides.utils.rest_api import ( from openslides.utils.rest_api import (
ModelViewSet, ModelViewSet,
@ -244,6 +245,28 @@ class MotionViewSet(ModelViewSet):
person=request.user) person=request.user)
return Response({'detail': message}) return Response({'detail': message})
@detail_route(methods=['post'])
def create_agenda_item(self, request, pk=None):
"""
Speacial view endpoint to create an agenda item that is related to
this motion.
"""
# Check permission.
if not request.user.has_perm('motions.can_manage'):
self.permission_denied(request)
# Retrieve motion.
motion = self.get_object()
# Create agenda item.
Item.objects.create(content_object=motion)
# Write the log message and initiate response.
motion.write_log(
message_list=[ugettext_noop('Agenda item created')],
person=request.user)
return Response({'detail': _('Agenda item successfully created.')})
class PollPDFView(PDFView): class PollPDFView(PDFView):
""" """