From 87cd31f6a5bdf1955bb43ed8063875aee7fb6c2d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Norman=20J=C3=A4ckel?= Date: Thu, 12 Feb 2015 18:48:14 +0100 Subject: [PATCH] Updated ViewSets to Django REST Framework 3.0.5. Refactored imports from openslides/utils/rest_api.py for better overriding them later. Fixed #1450. Updated requirements. --- openslides/agenda/serializers.py | 14 ++++++------- openslides/agenda/views.py | 8 ++++---- openslides/assignment/serializers.py | 14 ++++++------- openslides/assignment/views.py | 5 ++--- openslides/config/views.py | 8 ++++---- openslides/core/serializers.py | 6 +++--- openslides/core/views.py | 8 +++----- openslides/mediafile/serializers.py | 6 +++--- openslides/mediafile/views.py | 5 ++--- openslides/motion/serializers.py | 30 ++++++++++++++-------------- openslides/motion/views.py | 11 ++++------ openslides/users/serializers.py | 10 +++++----- openslides/users/views.py | 8 +++----- openslides/utils/rest_api.py | 13 ++++++++++-- requirements_production.txt | 2 +- 15 files changed, 74 insertions(+), 74 deletions(-) diff --git a/openslides/agenda/serializers.py b/openslides/agenda/serializers.py index 88f3b63ce..0ced4060a 100644 --- a/openslides/agenda/serializers.py +++ b/openslides/agenda/serializers.py @@ -1,11 +1,11 @@ from django.core.urlresolvers import reverse -from openslides.utils.rest_api import get_collection_and_id_from_url, serializers +from openslides.utils.rest_api import CharField, ModelSerializer, RelatedField, get_collection_and_id_from_url from .models import Item, Speaker -class SpeakerSerializer(serializers.ModelSerializer): +class SpeakerSerializer(ModelSerializer): """ Serializer for agenda.models.Speaker objects. """ @@ -19,7 +19,7 @@ class SpeakerSerializer(serializers.ModelSerializer): 'weight') -class RelatedItemRelatedField(serializers.RelatedField): +class RelatedItemRelatedField(RelatedField): """ A custom field to use for the content_object generic relationship. """ @@ -34,14 +34,14 @@ class RelatedItemRelatedField(serializers.RelatedField): return {'collection': collection, 'id': obj_id} -class ItemSerializer(serializers.ModelSerializer): +class ItemSerializer(ModelSerializer): """ Serializer for agenda.models.Item objects. """ - get_title = serializers.CharField(read_only=True) - get_title_supplement = serializers.CharField(read_only=True) + get_title = CharField(read_only=True) + get_title_supplement = CharField(read_only=True) content_object = RelatedItemRelatedField(read_only=True) - item_no = serializers.CharField(read_only=True) + item_no = CharField(read_only=True) speaker_set = SpeakerSerializer(many=True, read_only=True) class Meta: diff --git a/openslides/agenda/views.py b/openslides/agenda/views.py index 1c39cd09e..715b808d6 100644 --- a/openslides/agenda/views.py +++ b/openslides/agenda/views.py @@ -25,7 +25,7 @@ from openslides.projector.api import ( get_overlays) from openslides.utils.exceptions import OpenSlidesError from openslides.utils.pdf import stylesheet -from openslides.utils.rest_api import viewsets +from openslides.utils.rest_api import ModelViewSet from openslides.utils.utils import html_strong from openslides.utils.views import ( AjaxMixin, @@ -775,11 +775,11 @@ class ItemCSVImportView(CSVImportView): template_name = 'agenda/item_form_csv_import.html' -class ItemViewSet(viewsets.ModelViewSet): +class ItemViewSet(ModelViewSet): """ API endpoint to list, retrieve, create, update and destroy agenda items. """ - model = Item + queryset = Item.objects.all() serializer_class = ItemSerializer def check_permissions(self, request): @@ -807,7 +807,7 @@ class ItemViewSet(viewsets.ModelViewSet): """ Filters organizational items if the user has no permission to see them. """ - queryset = Item.objects.all() + queryset = super().get_queryset() if not self.request.user.has_perm('agenda.can_see_orga_items'): queryset = queryset.exclude(type__exact=Item.ORGANIZATIONAL_ITEM) return queryset diff --git a/openslides/assignment/serializers.py b/openslides/assignment/serializers.py index 61d836131..b3cbfb4ea 100644 --- a/openslides/assignment/serializers.py +++ b/openslides/assignment/serializers.py @@ -1,4 +1,4 @@ -from openslides.utils.rest_api import serializers +from openslides.utils.rest_api import ListSerializer, ModelSerializer from .models import ( models, @@ -9,7 +9,7 @@ from .models import ( AssignmentVote) -class AssignmentRelatedUserSerializer(serializers.ModelSerializer): +class AssignmentRelatedUserSerializer(ModelSerializer): """ Serializer for assignment.models.AssignmentRelatedUser objects. """ @@ -21,7 +21,7 @@ class AssignmentRelatedUserSerializer(serializers.ModelSerializer): 'status') -class AssignmentVoteSerializer(serializers.ModelSerializer): +class AssignmentVoteSerializer(ModelSerializer): """ Serializer for assignment.models.AssignmentVote objects. """ @@ -30,7 +30,7 @@ class AssignmentVoteSerializer(serializers.ModelSerializer): fields = ('weight', 'value',) -class AssignmentOptionSerializer(serializers.ModelSerializer): +class AssignmentOptionSerializer(ModelSerializer): """ Serializer for assignment.models.AssignmentOption objects. """ @@ -41,7 +41,7 @@ class AssignmentOptionSerializer(serializers.ModelSerializer): fields = ('candidate', 'assignmentvote_set',) -class FilterPollListSerializer(serializers.ListSerializer): +class FilterPollListSerializer(ListSerializer): """ Customized serializer to filter polls (exclude unpublished). """ @@ -57,7 +57,7 @@ class FilterPollListSerializer(serializers.ListSerializer): return [self.child.to_representation(item) for item in iterable] -class AssignmentAllPollSerializer(serializers.ModelSerializer): +class AssignmentAllPollSerializer(ModelSerializer): """ Serializer for assignment.models.AssignmentPoll objects. @@ -98,7 +98,7 @@ class AssignmentShortPollSerializer(AssignmentAllPollSerializer): 'votescast',) -class AssignmentFullSerializer(serializers.ModelSerializer): +class AssignmentFullSerializer(ModelSerializer): """ Serializer for assignment.models.Assignment objects. With all polls. """ diff --git a/openslides/assignment/views.py b/openslides/assignment/views.py index ed3e98480..3c4e9aae8 100644 --- a/openslides/assignment/views.py +++ b/openslides/assignment/views.py @@ -14,7 +14,7 @@ from openslides.config.api import config from openslides.users.models import Group, User # TODO: remove this from openslides.poll.views import PollFormView from openslides.utils.pdf import stylesheet -from openslides.utils.rest_api import viewsets +from openslides.utils.rest_api import ModelViewSet from openslides.utils.utils import html_strong from openslides.utils.views import (CreateView, DeleteView, DetailView, ListView, PDFView, @@ -215,11 +215,10 @@ class AssignmentDeleteCandidateshipOtherView(SingleObjectMixin, QuestionView): return _("Candidate %s was withdrawn successfully.") % html_strong(self.user) -class AssignmentViewSet(viewsets.ModelViewSet): +class AssignmentViewSet(ModelViewSet): """ API endpoint to list, retrieve, create, update and destroy assignments. """ - model = Assignment queryset = Assignment.objects.all() def check_permissions(self, request): diff --git a/openslides/config/views.py b/openslides/config/views.py index 104de6f14..5fd7c8859 100644 --- a/openslides/config/views.py +++ b/openslides/config/views.py @@ -4,7 +4,7 @@ from django.core.urlresolvers import reverse from django.http import Http404 from django.utils.translation import ugettext as _ -from openslides.utils.rest_api import response, viewsets +from openslides.utils.rest_api import Response, ViewSet from openslides.utils.views import FormView from .api import config @@ -105,7 +105,7 @@ class ConfigView(FormView): return super(ConfigView, self).form_valid(form) -class ConfigViewSet(viewsets.ViewSet): +class ConfigViewSet(ViewSet): """ API endpoint to list, retrieve and update the config. """ @@ -115,7 +115,7 @@ class ConfigViewSet(viewsets.ViewSet): """ # TODO: Check if we need permission check here. data = ({'key': key, 'value': value} for key, value in config.items()) - return response.Response(data) + return Response(data) def retrieve(self, request, *args, **kwargs): """ @@ -127,7 +127,7 @@ class ConfigViewSet(viewsets.ViewSet): data = {'key': key, 'value': config[key]} except ConfigNotFound: raise Http404 - return response.Response(data) + return Response(data) def update(self, request, pk=None): """ diff --git a/openslides/core/serializers.py b/openslides/core/serializers.py index 0af4676da..30e0ae83d 100644 --- a/openslides/core/serializers.py +++ b/openslides/core/serializers.py @@ -1,9 +1,9 @@ -from openslides.utils.rest_api import serializers +from openslides.utils.rest_api import ModelSerializer from .models import CustomSlide, Tag -class CustomSlideSerializer(serializers.ModelSerializer): +class CustomSlideSerializer(ModelSerializer): """ Serializer for core.models.CustomSlide objects. """ @@ -12,7 +12,7 @@ class CustomSlideSerializer(serializers.ModelSerializer): fields = ('id', 'title', 'text', 'weight',) -class TagSerializer(serializers.ModelSerializer): +class TagSerializer(ModelSerializer): """ Serializer for core.models.Tag objects. """ diff --git a/openslides/core/views.py b/openslides/core/views.py index 45fd3f86b..36e7fea7c 100644 --- a/openslides/core/views.py +++ b/openslides/core/views.py @@ -16,7 +16,7 @@ from openslides import get_git_commit_id, RELEASE from openslides.config.api import config from openslides.utils import views as utils_views from openslides.utils.plugins import get_plugin_description, get_plugin_verbose_name, get_plugin_version -from openslides.utils.rest_api import viewsets +from openslides.utils.rest_api import ModelViewSet from openslides.utils.signals import template_manipulation from openslides.utils.widgets import Widget @@ -235,11 +235,10 @@ class CustomSlideDeleteView(CustomSlideViewMixin, utils_views.DeleteView): pass -class CustomSlideViewSet(viewsets.ModelViewSet): +class CustomSlideViewSet(ModelViewSet): """ API endpoint to list, retrieve, create, update and destroy custom slides. """ - model = CustomSlide queryset = CustomSlide.objects.all() serializer_class = CustomSlideSerializer @@ -330,11 +329,10 @@ class TagListView(utils_views.AjaxMixin, utils_views.ListView): **context) -class TagViewSet(viewsets.ModelViewSet): +class TagViewSet(ModelViewSet): """ API endpoint to list, retrieve, create, update and destroy tags. """ - model = Tag queryset = Tag.objects.all() serializer_class = TagSerializer diff --git a/openslides/mediafile/serializers.py b/openslides/mediafile/serializers.py index 0189127f3..db2c9a074 100644 --- a/openslides/mediafile/serializers.py +++ b/openslides/mediafile/serializers.py @@ -1,13 +1,13 @@ -from openslides.utils.rest_api import serializers +from openslides.utils.rest_api import ModelSerializer, SerializerMethodField from .models import Mediafile -class MediafileSerializer(serializers.ModelSerializer): +class MediafileSerializer(ModelSerializer): """ Serializer for mediafile.models.Mediafile objects. """ - filesize = serializers.SerializerMethodField() + filesize = SerializerMethodField() class Meta: model = Mediafile diff --git a/openslides/mediafile/views.py b/openslides/mediafile/views.py index 1491180f1..6e6c87404 100644 --- a/openslides/mediafile/views.py +++ b/openslides/mediafile/views.py @@ -2,7 +2,7 @@ from django.http import HttpResponse from openslides.config.api import config from openslides.projector.api import get_active_slide -from openslides.utils.rest_api import viewsets +from openslides.utils.rest_api import ModelViewSet from openslides.utils.views import (AjaxView, CreateView, DeleteView, RedirectView, ListView, UpdateView) @@ -202,12 +202,11 @@ class PdfToggleFullscreenView(RedirectView): return {'fullscreen': config['pdf_fullscreen']} -class MediafileViewSet(viewsets.ModelViewSet): +class MediafileViewSet(ModelViewSet): """ API endpoint to list, retrieve, create, update and destroy mediafile objects. """ - model = Mediafile queryset = Mediafile.objects.all() serializer_class = MediafileSerializer diff --git a/openslides/motion/serializers.py b/openslides/motion/serializers.py index 5829f7683..d8566aedd 100644 --- a/openslides/motion/serializers.py +++ b/openslides/motion/serializers.py @@ -1,4 +1,4 @@ -from openslides.utils.rest_api import serializers +from openslides.utils.rest_api import ModelSerializer, PrimaryKeyRelatedField, SerializerMethodField from .models import ( Category, @@ -14,7 +14,7 @@ from .models import ( Workflow,) -class CategorySerializer(serializers.ModelSerializer): +class CategorySerializer(ModelSerializer): """ Serializer for motion.models.Category objects. """ @@ -23,7 +23,7 @@ class CategorySerializer(serializers.ModelSerializer): fields = ('id', 'name', 'prefix',) -class StateSerializer(serializers.ModelSerializer): +class StateSerializer(ModelSerializer): """ Serializer for motion.models.State objects. """ @@ -44,19 +44,19 @@ class StateSerializer(serializers.ModelSerializer): 'next_states',) -class WorkflowSerializer(serializers.ModelSerializer): +class WorkflowSerializer(ModelSerializer): """ Serializer for motion.models.Workflow objects. """ state_set = StateSerializer(many=True, read_only=True) - first_state = serializers.PrimaryKeyRelatedField(read_only=True) + first_state = PrimaryKeyRelatedField(read_only=True) class Meta: model = Workflow fields = ('id', 'name', 'state_set', 'first_state',) -class MotionSubmitterSerializer(serializers.ModelSerializer): +class MotionSubmitterSerializer(ModelSerializer): """ Serializer for motion.models.MotionSubmitter objects. """ @@ -65,7 +65,7 @@ class MotionSubmitterSerializer(serializers.ModelSerializer): fields = ('person',) # TODO: Rename this to 'user', see #1348 -class MotionSupporterSerializer(serializers.ModelSerializer): +class MotionSupporterSerializer(ModelSerializer): """ Serializer for motion.models.MotionSupporter objects. """ @@ -74,7 +74,7 @@ class MotionSupporterSerializer(serializers.ModelSerializer): fields = ('person',) # TODO: Rename this to 'user', see #1348 -class MotionLogSerializer(serializers.ModelSerializer): +class MotionLogSerializer(ModelSerializer): """ Serializer for motion.models.MotionLog objects. """ @@ -83,7 +83,7 @@ class MotionLogSerializer(serializers.ModelSerializer): fields = ('message_list', 'person', 'time',) -class MotionVoteSerializer(serializers.ModelSerializer): +class MotionVoteSerializer(ModelSerializer): """ Serializer for motion.models.MotionVote objects. """ @@ -92,7 +92,7 @@ class MotionVoteSerializer(serializers.ModelSerializer): fields = ('value', 'weight',) -class MotionOptionSerializer(serializers.ModelSerializer): +class MotionOptionSerializer(ModelSerializer): """ Serializer for motion.models.MotionOption objects. """ @@ -103,7 +103,7 @@ class MotionOptionSerializer(serializers.ModelSerializer): fields = ('motionvote_set',) -class MotionPollSerializer(serializers.ModelSerializer): +class MotionPollSerializer(ModelSerializer): """ Serializer for motion.models.MotionPoll objects. """ @@ -119,7 +119,7 @@ class MotionPollSerializer(serializers.ModelSerializer): 'votescast',) -class MotionVersionSerializer(serializers.ModelSerializer): +class MotionVersionSerializer(ModelSerializer): """ Serializer for motion.models.MotionVersion objects. """ @@ -134,16 +134,16 @@ class MotionVersionSerializer(serializers.ModelSerializer): 'reason',) -class MotionSerializer(serializers.ModelSerializer): +class MotionSerializer(ModelSerializer): """ Serializer for motion.models.Motion objects. """ versions = MotionVersionSerializer(many=True, read_only=True) - active_version = serializers.PrimaryKeyRelatedField(read_only=True) + active_version = PrimaryKeyRelatedField(read_only=True) submitter = MotionSubmitterSerializer(many=True, read_only=True) supporter = MotionSupporterSerializer(many=True, read_only=True) state = StateSerializer(read_only=True) - workflow = serializers.SerializerMethodField() + workflow = SerializerMethodField() polls = MotionPollSerializer(many=True, read_only=True) log_messages = MotionLogSerializer(many=True, read_only=True) diff --git a/openslides/motion/views.py b/openslides/motion/views.py index 489c56b92..e7f215f19 100644 --- a/openslides/motion/views.py +++ b/openslides/motion/views.py @@ -10,7 +10,7 @@ from django.shortcuts import get_object_or_404 from openslides.agenda.views import CreateRelatedAgendaItemView as _CreateRelatedAgendaItemView from openslides.config.api import config from openslides.poll.views import PollFormView -from openslides.utils.rest_api import viewsets +from openslides.utils.rest_api import ModelViewSet from openslides.utils.utils import html_strong, htmldiff from openslides.utils.views import (CreateView, CSVImportView, DeleteView, DetailView, ListView, PDFView, QuestionView, @@ -539,11 +539,10 @@ class SupportView(SingleObjectMixin, QuestionView): return _("You have unsupported this motion successfully.") -class MotionViewSet(viewsets.ModelViewSet): +class MotionViewSet(ModelViewSet): """ API endpoint to list, retrieve, create, update and destroy motions. """ - model = Motion queryset = Motion.objects.all() serializer_class = MotionSerializer @@ -840,11 +839,10 @@ class CategoryDeleteView(DeleteView): success_url_name = 'motion_category_list' -class CategoryViewSet(viewsets.ModelViewSet): +class CategoryViewSet(ModelViewSet): """ API endpoint to list, retrieve, create, update and destroy categories. """ - model = Category queryset = Category.objects.all() serializer_class = CategorySerializer @@ -886,11 +884,10 @@ class MotionCSVImportView(CSVImportView): return super(CSVImportView, self).form_valid(form) -class WorkflowViewSet(viewsets.ModelViewSet): +class WorkflowViewSet(ModelViewSet): """ API endpoint to list, retrieve, create, update and destroy workflows. """ - model = Workflow queryset = Workflow.objects.all() serializer_class = WorkflowSerializer diff --git a/openslides/users/serializers.py b/openslides/users/serializers.py index 7e1729b9d..0e8e12983 100644 --- a/openslides/users/serializers.py +++ b/openslides/users/serializers.py @@ -1,9 +1,9 @@ -from openslides.utils.rest_api import serializers +from openslides.utils.rest_api import ModelSerializer, RelatedField from .models import Group, User # TODO: Don't import Group from models but from core.models. -class UserShortSerializer(serializers.ModelSerializer): +class UserShortSerializer(ModelSerializer): """ Serializer for users.models.User objects. @@ -21,7 +21,7 @@ class UserShortSerializer(serializers.ModelSerializer): 'groups',) -class UserFullSerializer(serializers.ModelSerializer): +class UserFullSerializer(ModelSerializer): """ Serializer for users.models.User objects. @@ -45,7 +45,7 @@ class UserFullSerializer(serializers.ModelSerializer): 'is_active',) -class PermissionRelatedField(serializers.RelatedField): +class PermissionRelatedField(RelatedField): """ A custom field to use for the permission relationship. """ @@ -56,7 +56,7 @@ class PermissionRelatedField(serializers.RelatedField): return '.'.join((value.content_type.app_label, value.codename,)) -class GroupSerializer(serializers.ModelSerializer): +class GroupSerializer(ModelSerializer): """ Serializer for django.contrib.auth.models.Group objects. """ diff --git a/openslides/users/views.py b/openslides/users/views.py index 4f3ecdfcc..b20090a32 100644 --- a/openslides/users/views.py +++ b/openslides/users/views.py @@ -6,7 +6,7 @@ from django.core.urlresolvers import reverse from django.utils.translation import ugettext as _, ugettext_lazy, activate from openslides.config.api import config -from openslides.utils.rest_api import viewsets +from openslides.utils.rest_api import ModelViewSet from openslides.utils.utils import delete_default_permissions, html_strong from openslides.utils.views import ( CreateView, CSVImportView, DeleteView, DetailView, FormView, ListView, @@ -261,11 +261,10 @@ class ResetPasswordView(SingleObjectMixin, QuestionView): return _('The Password for %s was successfully reset.') % html_strong(self.get_object()) -class UserViewSet(viewsets.ModelViewSet): +class UserViewSet(ModelViewSet): """ API endpoint to list, retrieve, create, update and delete users. """ - model = User queryset = User.objects.all() def check_permissions(self, request): @@ -291,11 +290,10 @@ class UserViewSet(viewsets.ModelViewSet): return serializer_class -class GroupViewSet(viewsets.ModelViewSet): +class GroupViewSet(ModelViewSet): """ API endpoint to list, retrieve, create, update and delete groups. """ - model = Group queryset = Group.objects.all() serializer_class = GroupSerializer diff --git a/openslides/utils/rest_api.py b/openslides/utils/rest_api.py index 262774042..a01657a4d 100644 --- a/openslides/utils/rest_api.py +++ b/openslides/utils/rest_api.py @@ -3,11 +3,20 @@ import re from urllib.parse import urlparse from django.core.urlresolvers import reverse -from rest_framework import response, routers, serializers, viewsets # noqa +from rest_framework.serializers import ( # noqa + CharField, + ListSerializer, + ModelSerializer, + PrimaryKeyRelatedField, + RelatedField, + SerializerMethodField) +from rest_framework.response import Response # noqa +from rest_framework.routers import DefaultRouter +from rest_framework.viewsets import ModelViewSet, ViewSet # noqa from .exceptions import OpenSlidesError -router = routers.DefaultRouter() +router = DefaultRouter() class RESTModelMixin: diff --git a/requirements_production.txt b/requirements_production.txt index 19c9fc499..8f12db506 100644 --- a/requirements_production.txt +++ b/requirements_production.txt @@ -5,7 +5,7 @@ bleach>=1.4,<1.5 django-ckeditor-updated>=4.2.3,<4.4 django-haystack>=2.1,<2.4 django-mptt>=0.6,<0.7 -djangorestframework>=3.0.1,<3.0.5 +djangorestframework>=3.0.5,<3.1.0 jsonfield>=0.9.19,<1.1 natsort>=3.2,<3.6 reportlab>=3.0,<3.2