diff --git a/democracy/tests/test_comment.py b/democracy/tests/test_comment.py index f377bddb..347e9bcc 100644 --- a/democracy/tests/test_comment.py +++ b/democracy/tests/test_comment.py @@ -39,6 +39,24 @@ def test_55_add_comment_to_hearing(john_doe, john_doe_api_client, default_hearin assert data['n_votes'] == 0 +@pytest.mark.django_db +def test_add_auth_code_to_comment(api_client, default_hearing): + comment_data = get_comment_data(authorization_code="foo6") + response = api_client.post(get_hearing_detail_url(default_hearing.id, 'comments'), comment_data) + response_data = get_data_from_response(response, status_code=201) + assert comment_data["authorization_code"] ==\ + Hearing.objects.get(pk=default_hearing.pk).comments.get(id=response_data["id"]).authorization_code + + +@pytest.mark.django_db +def test_list_comments_with_auth_code(api_client, default_hearing): + comment_data = get_comment_data(authorization_code="foo6") + url = get_hearing_detail_url(default_hearing.id, 'comments') + api_client.post(url, comment_data) + data = get_data_from_response(api_client.get(url, {"authorization_code": "foo6"})) + assert len(data) == 1 + + @pytest.mark.django_db def test_54_list_all_comments_added_to_hearing_check_amount(api_client, default_hearing): # list all comments @@ -354,3 +372,47 @@ def test_add_plugin_data_to_comment(api_client, default_hearing, case): assert "no plugin data is allowed" in data["plugin_data"][0] else: raise NotImplementedError("...") + + +@pytest.mark.django_db +def test_do_not_get_plugin_data_for_comment(api_client, default_hearing): + with override_settings( + DEMOCRACY_PLUGINS={ + "test_plugin": "democracy.tests.plug.TestPlugin" + } + ): + section = default_hearing.sections.first() + section.plugin_identifier = "test_plugin" + section.save() + url = get_hearing_detail_url(default_hearing.id, 'sections/%s/comments' % section.id) + comment_data = get_comment_data( + content="", + plugin_data="foo6" + ) + response = api_client.post(url, data=comment_data) + response_data = get_data_from_response(response, status_code=201) + comment_list = get_data_from_response(api_client.get(url)) + created_comment = [c for c in comment_list if c["id"] == response_data["id"]][0] + assert "plugin_data" not in created_comment + + +@pytest.mark.django_db +def test_get_plugin_data_for_comment(api_client, default_hearing): + with override_settings( + DEMOCRACY_PLUGINS={ + "test_plugin": "democracy.tests.plug.TestPlugin" + } + ): + section = default_hearing.sections.first() + section.plugin_identifier = "test_plugin" + section.save() + url = get_hearing_detail_url(default_hearing.id, 'sections/%s/comments' % section.id) + comment_data = get_comment_data( + content="", + plugin_data="foo6" + ) + response = api_client.post(url, data=comment_data) + response_data = get_data_from_response(response, status_code=201) + comment_list = get_data_from_response(api_client.get(url, {"include": "plugin_data"})) + created_comment = [c for c in comment_list if c["id"] == response_data["id"]][0] + assert created_comment["plugin_data"] == comment_data["plugin_data"][::-1] # The TestPlugin reverses data diff --git a/democracy/views/comment.py b/democracy/views/comment.py index 71c88405..23e9f1ac 100644 --- a/democracy/views/comment.py +++ b/democracy/views/comment.py @@ -1,8 +1,9 @@ # -*- coding: utf-8 -*- +import django_filters from django.core.exceptions import ValidationError from django.db import transaction from django.utils.encoding import force_text -from rest_framework import permissions, response, serializers, status, viewsets +from rest_framework import filters, permissions, response, serializers, status, viewsets from rest_framework.decorators import detail_route from reversion import revisions @@ -15,11 +16,27 @@ class BaseCommentSerializer(AbstractSerializerMixin, CreatedBySerializer, serializers.ModelSerializer): + def to_representation(self, instance): + r = super().to_representation(instance) + request = self.context.get('request', None) + if request: + if request.GET.get('include', None) == 'plugin_data': + r['plugin_data'] = instance.plugin_data + return r + class Meta: model = BaseComment fields = COMMENT_FIELDS +class BaseCommentFilter(django_filters.FilterSet): + authorization_code = django_filters.CharFilter() + + class Meta: + model = BaseComment + fields = ['authorization_code', ] + + class BaseCommentViewSet(AdminsSeeUnpublishedMixin, viewsets.ModelViewSet): """ Base viewset for comments. @@ -27,6 +44,8 @@ class BaseCommentViewSet(AdminsSeeUnpublishedMixin, viewsets.ModelViewSet): permission_classes = (permissions.AllowAny,) serializer_class = None create_serializer_class = None + filter_backends = (filters.DjangoFilterBackend,) + filter_class = BaseCommentFilter def get_serializer(self, *args, **kwargs): serializer_class = kwargs.pop("serializer_class", None) or self.get_serializer_class() diff --git a/democracy/views/section_comment.py b/democracy/views/section_comment.py index cea0ec40..1de39878 100644 --- a/democracy/views/section_comment.py +++ b/democracy/views/section_comment.py @@ -6,9 +6,7 @@ from democracy.models import SectionComment from democracy.models.section import Section -from democracy.views.comment import COMMENT_FIELDS, BaseCommentViewSet - -from .base import CreatedBySerializer +from democracy.views.comment import COMMENT_FIELDS, BaseCommentViewSet, BaseCommentSerializer class SectionCommentCreateSerializer(serializers.ModelSerializer): @@ -44,7 +42,7 @@ def validate(self, attrs): return attrs -class SectionCommentSerializer(CreatedBySerializer, serializers.ModelSerializer): +class SectionCommentSerializer(BaseCommentSerializer): """ Serializer for comment added to section. """