Skip to content

Commit

Permalink
Merge pull request #368 from City-of-Helsinki/excel
Browse files Browse the repository at this point in the history
Hearing report fixes
  • Loading branch information
ranta authored Jan 3, 2022
2 parents 2be7975 + d774816 commit cd71b6f
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 5 deletions.
21 changes: 18 additions & 3 deletions democracy/tests/test_comment.py
Original file line number Diff line number Diff line change
Expand Up @@ -728,15 +728,30 @@ def test_56_get_hearing_with_section_check_n_comments_property(api_client, get_c
assert data['sections'][0]['n_comments'] == 1

@pytest.mark.django_db
def test_get_section_comment_creator_name_property_without_authorization(john_doe_api_client, default_hearing):
def test_get_section_comment_creator_name_without_auth_not_public(john_doe_api_client, default_hearing, settings):
settings.HEARING_REPORT_PUBLIC_AUTHOR_NAMES = False

section = default_hearing.sections.first()
url = get_hearing_detail_url(default_hearing.id, 'sections/%s/comments' % section.id)
response = john_doe_api_client.get(url)
data = get_data_from_response(response, 200)
# check no section comment has creator_name when not authorized
# check no section comment has creator_name when not authorized and the setting is disabled
for comment in data:
assert not "creator_name" in comment



@pytest.mark.django_db
def test_get_section_comment_creator_name_without_auth_public(john_doe_api_client, default_hearing, settings):
settings.HEARING_REPORT_PUBLIC_AUTHOR_NAMES = True

section = default_hearing.sections.first()
url = get_hearing_detail_url(default_hearing.id, 'sections/%s/comments' % section.id)
response = john_doe_api_client.get(url)
data = get_data_from_response(response, 200)
# check section comment has creator_name when not authorized and the setting is enabled
for comment in data:
assert "creator_name" in comment


@pytest.mark.django_db
def test_get_section_comment_creator_name_property_with_authorization(admin_api_client, default_hearing):
Expand Down
2 changes: 1 addition & 1 deletion democracy/views/hearing_report.py
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ def get_response(self):
content_type='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
)
# remove special characters from filename to avoid potential file naming issues
response['Content-Disposition'] = 'attachment; filename={filename}.xlsx'.format(
response['Content-Disposition'] = 'attachment; filename="{filename}.xlsx"'.format(
filename=re.sub(r"\W+|_", " ", self._get_default_translation(self.json['title'])))
return response

Expand Down
7 changes: 6 additions & 1 deletion democracy/views/section_comment.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import django_filters
from django.conf import settings
from django.core.exceptions import ValidationError as DjangoValidationError
from django.db.models import F
from django.db.transaction import atomic
Expand Down Expand Up @@ -173,7 +174,11 @@ def get_creator_name(self, obj):

def to_representation(self, instance):
data = super(SectionCommentSerializer, self).to_representation(instance)
if not self.context['request'].user.is_staff and not self.context['request'].user.is_superuser:
if (
not settings.HEARING_REPORT_PUBLIC_AUTHOR_NAMES
and not self.context['request'].user.is_staff
and not self.context['request'].user.is_superuser
):
del data['creator_name']

return data
Expand Down

0 comments on commit cd71b6f

Please sign in to comment.