Skip to content

Commit

Permalink
Merge pull request #168 from City-of-Helsinki/include-plugin-data
Browse files Browse the repository at this point in the history
Include plugin data in comment serialization, to obtain data for hearing visualization
  • Loading branch information
akx committed Apr 15, 2016
2 parents dc066ec + 06ef800 commit a5a2584
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 5 deletions.
62 changes: 62 additions & 0 deletions democracy/tests/test_comment.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
21 changes: 20 additions & 1 deletion democracy/views/comment.py
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -15,18 +16,36 @@

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.
"""
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()
Expand Down
6 changes: 2 additions & 4 deletions democracy/views/section_comment.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -44,7 +42,7 @@ def validate(self, attrs):
return attrs


class SectionCommentSerializer(CreatedBySerializer, serializers.ModelSerializer):
class SectionCommentSerializer(BaseCommentSerializer):
"""
Serializer for comment added to section.
"""
Expand Down

0 comments on commit a5a2584

Please sign in to comment.