Skip to content

Commit

Permalink
only allow updated rapid responses to cancel other rapid responses
Browse files Browse the repository at this point in the history
  • Loading branch information
Jon committed Aug 30, 2019
1 parent 7667e84 commit 64b909a
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 4 deletions.
6 changes: 4 additions & 2 deletions observation_portal/observations/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -348,9 +348,11 @@ def update(self, instance, validated_data):
start__lte=instance.end,
start__gte=old_end_time,
state='PENDING'
).exclude(
request__request_group__observation_type=RequestGroup.RAPID_RESPONSE
)
if instance.request.request_group.observation_type != RequestGroup.RAPID_RESPONSE:
observations = observations.exclude(
request__request_group__observation_type=RequestGroup.RAPID_RESPONSE
)
num_canceled = Observation.cancel(observations)
logger.info(
"updated end time for observation {instance.id} to {instance.end}. Canceled {num_canceled} overlapping observations.")
Expand Down
31 changes: 29 additions & 2 deletions observation_portal/observations/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -977,7 +977,7 @@ def test_update_observation_end_time_succeeds(self):
observation.refresh_from_db()
self.assertEqual(observation.end, new_end)

def test_update_observation_end_time_cancels_overlapping_observations(self):
def test_update_observation_end_time_cancels_proper_overlapping_observations(self):
original_end = datetime(2016, 9, 2, 23, 35, 40).replace(tzinfo=timezone.utc)
self.window.start = datetime(2016, 9, 1, tzinfo=timezone.utc)
self.window.save()
Expand All @@ -989,8 +989,14 @@ def test_update_observation_end_time_cancels_overlapping_observations(self):
self._create_observation(observation)
observation = Observation.objects.first()
cancel_obs_1 = self._create_clone_observation(observation, datetime(2016, 9, 2, 23, 35, 41), datetime(2016, 9, 2, 23, 39, 59))
cancel_obs_2 = self._create_clone_observation(observation, datetime(2016, 9, 2, 23, 40, 0), datetime(2016, 9, 2, 13, 55, 34))
cancel_obs_2 = self._create_clone_observation(observation, datetime(2016, 9, 2, 23, 42, 0), datetime(2016, 9, 2, 23, 55, 34))
extra_obs_1 = self._create_clone_observation(observation, datetime(2016, 9, 2, 23, 55, 35), datetime(2016, 9, 3, 0, 14, 21))
rr_obs_1 = self._create_clone_observation(observation, datetime(2016, 9, 2, 23, 40, 0), datetime(2016, 9, 2, 23, 41, 59))
rr_requestgroup = create_simple_requestgroup(self.user, self.proposal, window=self.window, location=self.location)
rr_requestgroup.observation_type = RequestGroup.RAPID_RESPONSE
rr_requestgroup.save()
rr_obs_1.request = rr_requestgroup.requests.first()
rr_obs_1.save()

new_end = datetime(2016, 9, 2, 23, 47, 22).replace(tzinfo=timezone.utc)
update_data = {"end": datetime.strftime(new_end, '%Y-%m-%dT%H:%M:%SZ')}
Expand All @@ -1003,6 +1009,27 @@ def test_update_observation_end_time_cancels_overlapping_observations(self):
self.assertEqual(cancel_obs_2.state, 'CANCELED')
extra_obs_1.refresh_from_db()
self.assertEqual(extra_obs_1.state, 'PENDING')
rr_obs_1.refresh_from_db()
self.assertEqual(rr_obs_1.state, 'PENDING')

def test_update_observation_end_time_rr_cancels_overlapping_rr(self):
self.window.start = datetime(2016, 9, 1, tzinfo=timezone.utc)
self.window.save()
self.requestgroup.observation_type = RequestGroup.RAPID_RESPONSE
self.requestgroup.save()
observation = self._generate_observation_data(
self.requestgroup.requests.first().id, [self.requestgroup.requests.first().configurations.first().id],
start="2016-09-02T22:35:39Z",
end="2016-09-02T23:35:40Z"
)
self._create_observation(observation)
observation = Observation.objects.first()
cancel_obs_1 = self._create_clone_observation(observation, datetime(2016, 9, 2, 23, 35, 41), datetime(2016, 9, 2, 23, 39, 59))
new_end = datetime(2016, 9, 2, 23, 47, 22).replace(tzinfo=timezone.utc)
update_data = {"end": datetime.strftime(new_end, '%Y-%m-%dT%H:%M:%SZ')}
self.client.patch(reverse('api:observations-detail', args=(observation.id,)), update_data)
cancel_obs_1.refresh_from_db()
self.assertEqual(cancel_obs_1.state, 'CANCELED')

def test_update_observation_end_before_start_does_nothing(self):
original_end = datetime(2016, 9, 5, 23, 35, 40).replace(tzinfo=timezone.utc)
Expand Down

0 comments on commit 64b909a

Please sign in to comment.