Skip to content

Commit

Permalink
hearings created_by query param added (#28)
Browse files Browse the repository at this point in the history
Co-authored-by: Hannes Honkasaari <[email protected]>
  • Loading branch information
hienous and Hannes Honkasaari authored Apr 7, 2021
1 parent fa6ae42 commit 4a3f47b
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 1 deletion.
96 changes: 96 additions & 0 deletions democracy/tests/test_hearing.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,27 @@ def create_hearings(n, organization=None):
)
return hearings

def create_hearings_created_by(n, organization=None, user=None):
'''
Existing hearings are not deleted as this function is used multiple
times in a specific test with multiple users/organzations.
'''
hearings = []

# Depending on the database backend, created_at dates (which are used for ordering)
# may be truncated to the closest second, so we purposefully backdate these
# to ensure ordering on all platforms.
for i in range(n):
hearings.append(
Hearing.objects.create(
title='Test purpose created hearing title %s' % (i + 1),
created_at=now() - datetime.timedelta(seconds=1 + (n - i)),
organization=organization,
created_by_id=user.id
)
)
return hearings


@pytest.mark.django_db
def test_list_all_hearings_no_objects(api_client):
Expand Down Expand Up @@ -324,6 +345,81 @@ def test_filter_hearings_by_title(api_client):
assert len(data['results']) == 1
assert data['results'][0]['title'][default_lang_code] == hearings[0].title

@pytest.mark.django_db
def test_filter_hearings_created_by_me(api_client, john_smith_api_client, jane_doe_api_client, stark_doe_api_client):
# Retrieves hearings that are created by the user
main_organization = Organization.objects.create(name='main organization')

second_organization = Organization.objects.create(name='second organization')

# John is a member of the main organization
john_smith_api_client.user.admin_organizations.add(main_organization)
# Jane is a member of the main organization
jane_doe_api_client.user.admin_organizations.add(main_organization)
# Stark is a member of the second organization
stark_doe_api_client.user.admin_organizations.add(second_organization)

'''Create hearings'''
# Jane creates 6 hearings
jane_hearings = create_hearings_created_by(6,main_organization, jane_doe_api_client.user)
# John creates 3 hearings
john_hearings = create_hearings_created_by(3,main_organization, john_smith_api_client.user)
# Stark creates 1 hearing
stark_hearings = create_hearings_created_by(1,second_organization, stark_doe_api_client.user)


'''Filtering with me'''
# Jane should get 6 results when filtering with 'me'
jane_response = jane_doe_api_client.get(list_endpoint, data={"created_by": "me"})
jane_data = get_data_from_response(jane_response)
assert len(jane_data['results']) == 6

# John should get 3 results when filtering with 'me'
john_response = john_smith_api_client.get(list_endpoint, data={"created_by": "me"})
john_data = get_data_from_response(john_response)
assert len(john_data['results']) == 3

# Stark should get 1 result when filtering with 'me'
stark_response = stark_doe_api_client.get(list_endpoint, data={"created_by": "me"})
stark_data = get_data_from_response(stark_response)
assert len(stark_data['results']) == 1


'''Filtering with main_organization.name'''
# Jane should get 9 results when filtering with main_organization id
jane_response = jane_doe_api_client.get(list_endpoint, data={"created_by": main_organization.name})
jane_data = get_data_from_response(jane_response)
assert len(jane_data['results']) == 9

# John should get 9 results when filtering with main_organization id
john_response = john_smith_api_client.get(list_endpoint, data={"created_by": main_organization.name})
john_data = get_data_from_response(john_response)
assert len(john_data['results']) == 9

# Stark should get 9 results when filtering with main_organization id
stark_response = stark_doe_api_client.get(list_endpoint, data={"created_by": main_organization.name})
stark_data = get_data_from_response(stark_response)
assert len(stark_data['results']) == 9


'''Filtering with second_organization.name'''
# Jane should get 1 result when filtering with second_organization id
jane_response = jane_doe_api_client.get(list_endpoint, data={"created_by": second_organization.name})
jane_data = get_data_from_response(jane_response)
assert len(jane_data['results']) == 1

# John should get 1 result when filtering with second_organization id
john_response = john_smith_api_client.get(list_endpoint, data={"created_by": second_organization.name})
john_data = get_data_from_response(john_response)
assert len(john_data['results']) == 1

# Stark should get 1 result when filtering with second_organization id
stark_response = stark_doe_api_client.get(list_endpoint, data={"created_by": second_organization.name})
stark_data = get_data_from_response(stark_response)
assert len(stark_data['results']) == 1




@pytest.mark.parametrize('plugin_fullscreen', [
True,
Expand Down
18 changes: 17 additions & 1 deletion democracy/views/hearing.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from rest_framework.settings import api_settings

from democracy.enums import InitialSectionType
from democracy.models import ContactPerson, Hearing, Label, Section, SectionImage, Project
from democracy.models import ContactPerson, Hearing, Label, Section, SectionImage, Project, Organization
from democracy.pagination import DefaultLimitPagination
from democracy.renderers import GeoJSONRenderer
from democracy.views.base import AdminsSeeUnpublishedMixin
Expand Down Expand Up @@ -165,6 +165,7 @@ def create(self, validated_data):
sections_data = validated_data.pop('sections')
project_data = validated_data.pop('project', None)
validated_data['organization'] = self.context['request'].user.get_default_organization()
validated_data['created_by_id'] = self.context['request'].user.id
hearing = super().create(validated_data)
self._create_or_update_sections(hearing, sections_data, force_create=True)
self._create_or_update_project(hearing, project_data)
Expand All @@ -188,6 +189,7 @@ def update(self, instance, validated_data):

sections_data = validated_data.pop('sections')
project_data = validated_data.pop('project', None)
validated_data['modified_by_id'] = self.context['request'].user.id
hearing = super().update(instance, validated_data)
sections = self._create_or_update_sections(hearing, sections_data)
self._create_or_update_project(hearing, project_data)
Expand Down Expand Up @@ -410,6 +412,20 @@ def get_serializer_class(self, *args, **kwargs):
def filter_queryset(self, queryset):
next_closing = self.request.query_params.get('next_closing', None)
open = self.request.query_params.get('open', None)
created_by = self.request.query_params.get('created_by', None)

if created_by is not None and self.request.user:
if created_by.lower() == 'me':
queryset = queryset.filter(created_by_id=self.request.user.id)
else:
try:
organizationObject = Organization.objects.get(name=created_by)
except Organization.DoesNotExist:
organizationObject = None

if organizationObject is not None:
queryset = queryset.filter(organization=organizationObject.id)

if next_closing is not None:
# sliced querysets cannot be filtered or ordered further
return queryset.filter(close_at__gt=next_closing).order_by('close_at')[:1]
Expand Down

0 comments on commit 4a3f47b

Please sign in to comment.