Skip to content

Commit

Permalink
prevent inherited changes for joined pickups (#346)
Browse files Browse the repository at this point in the history
* prevent inherited date changes for joined pickups

Closes #596

* prevent inherited changes if users joined a pickup
  • Loading branch information
tiltec committed Aug 14, 2017
1 parent 7c10c24 commit b306e4c
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 13 deletions.
37 changes: 24 additions & 13 deletions foodsaving/stores/models.py
Expand Up @@ -74,32 +74,43 @@ def get_dates_for_rule(self, start_date):
return [tz.localize(d) for d in dates]

def update_pickup_dates(self, start=timezone.now):
# shifting period start time into the future avoids pickup dates which are only valid for a short time
"""
synchronizes the pickup dates with the series
changes to the series fields are also made to the pickup dates, except for
- the field on the pickup date has been modified
- users have joined the pickup date
"""

# shift start time slightly into future to avoid pickup dates which are only valid for very short time
start_date = start() + relativedelta(minutes=5)

for pickup, new_date in zip_longest(
self.pickup_dates.filter(date__gte=start_date),
self.get_dates_for_rule(start_date=start_date)
):
if not pickup:
pickup = PickupDate.objects.create(
# does not yet exist
PickupDate.objects.create(
date=new_date,
max_collectors=self.max_collectors,
series=self,
store=self.store,
description=self.description
)
if not new_date:
# only delete pickup dates when they are empty
if pickup.collectors.count() <= 0:
elif pickup.collectors.count() < 1:
# only modify pickups when nobody has joined
if not new_date:
# series changed and now this pickup should not exist anymore
pickup.delete()
continue
if new_date and not pickup.is_date_changed:
pickup.date = new_date
if not pickup.is_max_collectors_changed:
pickup.max_collectors = self.max_collectors
if not pickup.is_description_changed:
pickup.description = self.description
pickup.save()
else:
if not pickup.is_date_changed:
pickup.date = new_date
if not pickup.is_max_collectors_changed:
pickup.max_collectors = self.max_collectors
if not pickup.is_description_changed:
pickup.description = self.description
pickup.save()

def __str__(self):
return '{} - {}'.format(self.date, self.store)
Expand Down
25 changes: 25 additions & 0 deletions foodsaving/stores/tests/test_pickupdates_api.py
Expand Up @@ -383,6 +383,31 @@ def test_dont_mark_as_changed_if_data_is_equal(self):
self.assertFalse(pickup_under_test.is_max_collectors_changed)
self.assertFalse(pickup_under_test.is_date_changed)

def test_keep_date_if_pickup_has_collectors(self):
"""
https://github.com/yunity/foodsaving-frontend/issues/596
It's unexpected if the date changes automatically when people have joined
"""
self.client.force_login(user=self.member)
pickup_under_test = self.series.pickup_dates.first()
url = '/api/pickup-dates/{}/add/'.format(pickup_under_test.id)

# join pickup
response = self.client.post(url)
self.assertEqual(response.status_code, status.HTTP_200_OK, response.data)
original_date = pickup_under_test.date

# change series date
url = '/api/pickup-date-series/{}/'.format(self.series.id)
response = self.client.patch(url, {'start_date': self.series.start_date + relativedelta(hours=1)})
self.assertEqual(response.status_code, status.HTTP_200_OK, response.data)

# check if time of pickup is the same
url = '/api/pickup-dates/{}/'.format(pickup_under_test.id)
response = self.client.get(url)
self.assertEqual(response.status_code, status.HTTP_200_OK, response.data)
self.assertEqual(parse(response.data['date']), original_date, "time shouldn't change!")


class TestPickupDateSeriesAPIAuth(APITestCase):
""" Testing actions that are forbidden """
Expand Down

0 comments on commit b306e4c

Please sign in to comment.