Skip to content

Commit

Permalink
Added project phase is_active in update responses (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
SanttuA authored Aug 19, 2020
1 parent 257e73b commit 1cce7b7
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
40 changes: 40 additions & 0 deletions democracy/tests/test_hearing.py
Original file line number Diff line number Diff line change
Expand Up @@ -1206,6 +1206,25 @@ def test_PUT_hearing_success(valid_hearing_json, john_smith_api_client):
assert updated_data['created_at'] == created_at
assert_hearing_equals(data, updated_data, john_smith_api_client.user, create=False)

# Test that updating hearing with project returns a response with phases' is_active value
@pytest.mark.django_db
def test_PUT_hearing_with_project_phase_is_active(valid_hearing_json_with_project, john_smith_api_client):
response = john_smith_api_client.post(endpoint, data=valid_hearing_json_with_project, format='json')
data = get_data_from_response(response, status_code=201)
_update_hearing_data(data)

# add is_active which is not included in POST response
phases = valid_hearing_json_with_project['project']['phases']
for index, phase in enumerate(phases):
data['project']['phases'][index]['is_active'] = phase['is_active']

response = john_smith_api_client.put('%s%s/' % (endpoint, data['id']), data=data, format='json')
updated_data = get_data_from_response(response, status_code=200)

for index, phase in enumerate(phases):
assert updated_data['project']['phases'][index]['is_active'] == phase['is_active']

assert_hearing_equals(data, updated_data, john_smith_api_client.user, create=False)

# Test that a user cannot PUT a hearing without the translation
@pytest.mark.django_db
Expand Down Expand Up @@ -1453,6 +1472,27 @@ def test_PATCH_hearing(valid_hearing_json, john_smith_api_client):
data = get_data_from_response(response, status_code=200)
assert data['closed'] == True

# Test that updating hearing with project returns a response with phases' is_active value
@pytest.mark.django_db
def test_PATCH_hearing_with_project_phase_is_active(valid_hearing_json_with_project, john_smith_api_client):
valid_hearing_json_with_project['close_at'] = datetime.datetime.now() + datetime.timedelta(days=1)
response = john_smith_api_client.post(endpoint, data=valid_hearing_json_with_project, format='json')
data = get_data_from_response(response, status_code=201)

# add is_active which is not included in POST response
phases = valid_hearing_json_with_project['project']['phases']
for index, phase in enumerate(phases):
data['project']['phases'][index]['is_active'] = phase['is_active']

assert data['closed'] == False
before = datetime.datetime.now() - datetime.timedelta(days=1)
response = john_smith_api_client.patch('%s%s/' % (endpoint, data['id']), data={'close_at': before}, format='json')
data = get_data_from_response(response, status_code=200)

assert data['closed'] == True
for index, phase in enumerate(phases):
assert data['project']['phases'][index]['is_active'] == phase['is_active']


# Test that a user cannot PATCH a hearing without the translation
@pytest.mark.django_db
Expand Down
5 changes: 5 additions & 0 deletions democracy/views/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ def to_representation(self, instance):
# do not return ordering explicitly
self.fields.pop('ordering')
data = super().to_representation(instance)

# include is_active when updating a hearing with a project
if self.context['request'].method in ['PUT', 'PATCH']:
data['is_active'] = self.context['view'].get_object().project_phase_id == instance.pk

if 'hearing' in self.context:
data['is_active'] = self.context['hearing'].project_phase_id == instance.pk
return data
Expand Down

0 comments on commit 1cce7b7

Please sign in to comment.