Skip to content

Commit

Permalink
Merge pull request #155 from akx/disembodied-comments
Browse files Browse the repository at this point in the history
Allow plugin data to be posted as a comment without other content
  • Loading branch information
juyrjola committed Mar 18, 2016
2 parents 0e4787c + ee3b8d4 commit 8a6271e
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 12 deletions.
25 changes: 25 additions & 0 deletions democracy/migrations/0011_contentless_comments.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.9.2 on 2016-03-17 13:46
from __future__ import unicode_literals

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('democracy', '0010_auto_20160315_1026'),
]

operations = [
migrations.AlterField(
model_name='hearingcomment',
name='content',
field=models.TextField(blank=True, verbose_name='content'),
),
migrations.AlterField(
model_name='sectioncomment',
name='content',
field=models.TextField(blank=True, verbose_name='content'),
),
]
4 changes: 3 additions & 1 deletion democracy/models/comment.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class BaseComment(BaseModel):
parent_field = None # Required for factories and API
parent_model = None # Required for factories and API
title = models.CharField(verbose_name=_('title'), blank=True, max_length=255)
content = models.TextField(verbose_name=_('content'))
content = models.TextField(verbose_name=_('content'), blank=True)
authorization_code = models.CharField(verbose_name=_('authorization code'), max_length=32, blank=True)
author_name = models.CharField(verbose_name=_('author name'), max_length=255, blank=True, null=True, editable=False)
plugin_identifier = models.CharField(verbose_name=_('plugin identifier'), blank=True, max_length=255)
Expand Down Expand Up @@ -54,6 +54,8 @@ def parent_id(self):
return getattr(self, "%s_id" % self.parent_field, None)

def save(self, *args, **kwargs):
if not (self.plugin_data or self.content):
raise ValueError("Comments must have either plugin data or textual content")
if not self.author_name:
self.author_name = (str(self.created_by) if self.created_by_id else None)
return super(BaseComment, self).save(*args, **kwargs)
Expand Down
10 changes: 1 addition & 9 deletions democracy/tests/test_comment.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,15 +146,6 @@ def test_56_add_comment_to_section_without_data(api_client, default_hearing):
assert response.status_code == 400


@pytest.mark.django_db
def test_56_add_comment_to_section_invalid_key(api_client, default_hearing):
section = default_hearing.sections.first()
url = get_hearing_detail_url(default_hearing.id, 'sections/%s/comments' % section.id)
response = api_client.post(url, data={'invalidKey': 'Yes it is'})
# expect bad request, we have invalid key in payload
assert response.status_code == 400


@pytest.mark.django_db
def test_56_add_comment_to_section(john_doe_api_client, default_hearing):
section = default_hearing.sections.first()
Expand Down Expand Up @@ -333,6 +324,7 @@ def test_add_plugin_data_to_comment(api_client, default_hearing, case):
section.save()
url = get_hearing_detail_url(default_hearing.id, 'sections/%s/comments' % section.id)
comment_data = get_comment_data(
content="",
plugin_data=("foo6" if case == "plug-valid" else "invalid555")
)
response = api_client.post(url, data=comment_data)
Expand Down
12 changes: 10 additions & 2 deletions democracy/views/hearing_comment.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,28 @@
from rest_framework.exceptions import ValidationError

from democracy.models import HearingComment
from democracy.views.comment import COMMENT_FIELDS, BaseCommentSerializer, BaseCommentViewSet


class HearingCommentSerializer(BaseCommentSerializer):

class Meta:
model = HearingComment
fields = ['hearing'] + COMMENT_FIELDS


class HearingCommentCreateSerializer(BaseCommentSerializer):

class Meta:
model = HearingComment
fields = ['content', 'hearing', 'authorization_code']

def validate(self, attrs):
attrs = super().validate(attrs)

if not attrs.get("content"):
raise ValidationError("Content must be supplied.")

return attrs


class HearingCommentViewSet(BaseCommentViewSet):
model = HearingComment
Expand Down
2 changes: 2 additions & 0 deletions democracy/views/section_comment.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ def validate(self, attrs):
detail.setdefault("plugin_data", []).extend(detail.pop(api_settings.NON_FIELD_ERRORS_KEY, ()))
raise ValidationError(detail=detail)
attrs["plugin_identifier"] = section.plugin_identifier
if not (attrs.get("plugin_data") or attrs.get("content")):
raise ValidationError("Either content or plugin data must be supplied.")
return attrs


Expand Down

0 comments on commit 8a6271e

Please sign in to comment.