def save(self, *args, **kwargs): old = type(self).objects.get(pk=self.pk) if self.pk else None super().save(*args, **kwargs) if not old or old.start_date != self.start_date or old.rule != self.rule: self.update_pickups() if old: description_changed = old.description != self.description max_collectors_changed = old.max_collectors != self.max_collectors duration_changed = old.duration != self.duration if description_changed or max_collectors_changed or duration_changed: for pickup in self.pickup_dates.upcoming(): if description_changed and old.description == pickup.description: pickup.description = self.description if max_collectors_changed and old.max_collectors == pickup.max_collectors: pickup.max_collectors = self.max_collectors if duration_changed: if self.duration: pickup.has_duration = True pickup.date = CustomDateTimeTZRange( pickup.date.start, pickup.date.start + self.duration) else: pickup.has_duration = False pickup.date = CustomDateTimeTZRange( pickup.date.start, pickup.date.start + default_duration) pickup.save()
def save(self, *args, **kwargs): old = type(self).objects.get(pk=self.pk) if self.pk else None super().save(*args, **kwargs) if not old or old.start_date != self.start_date or old.rule != self.rule: self.update_activities() if old: description_changed = old.description != self.description max_participants_changed = old.max_participants != self.max_participants duration_changed = old.duration != self.duration if description_changed or max_participants_changed or duration_changed: for activity in self.activities.upcoming(): if description_changed and old.description == activity.description: activity.description = self.description if max_participants_changed and old.max_participants == activity.max_participants: activity.max_participants = self.max_participants if duration_changed: if self.duration: activity.has_duration = True activity.date = CustomDateTimeTZRange( activity.date.start, activity.date.start + self.duration) else: activity.has_duration = False activity.date = CustomDateTimeTZRange( activity.date.start, activity.date.start + default_duration) activity.save()
def save(self, *args, **kwargs): if not self.has_duration: # reset duration to default if activity has no explicit duration start = self.date.start self.date = CustomDateTimeTZRange(start, start + default_duration) super().save(*args, **kwargs)
def create_pickup(self, date): return self.pickup_dates.create( date=CustomDateTimeTZRange(date, date + (self.duration or default_duration)), has_duration=self.duration is not None, max_collectors=self.max_collectors, series=self, place=self.place, description=self.description, last_changed_by=self.last_changed_by, )
def test_remove_duration_resets_end_date(self): self.client.force_login(user=self.member) start = timezone.now() + timedelta(hours=1) end = timezone.now() + timedelta(hours=2) response = self.client.patch(self.activity_url, {'date': [start, end], 'has_duration': True}, format='json') self.assertEqual(response.status_code, status.HTTP_200_OK, response.data) response = self.client.patch(self.activity_url, {'has_duration': False}, format='json') self.assertEqual(response.status_code, status.HTTP_200_OK, response.data) self.activity.refresh_from_db() self.assertEqual(self.activity.date, CustomDateTimeTZRange(start, start + timedelta(minutes=30)))
def create_activity(self, date): return self.activities.create( activity_type=self.activity_type, date=CustomDateTimeTZRange( date, date + (self.duration or default_duration)), has_duration=self.duration is not None, max_participants=self.max_participants, series=self, place=self.place, description=self.description, last_changed_by=self.last_changed_by, )
def test_patch_date(self): self.client.force_login(user=self.member) start = timezone.now() + timedelta(hours=1) end = timezone.now() + timedelta(hours=2) response = self.client.patch(self.pickup_url, { 'date': [start, end], }, format='json') self.assertEqual(response.status_code, status.HTTP_200_OK, response.data) self.pickup.refresh_from_db() self.assertEqual(self.pickup.date, CustomDateTimeTZRange(start, end))
def to_internal_value(self, data): if not isinstance(data, list): self.fail('list') if not 0 < len(data) <= 2: self.fail('length') lower = data[0] upper = data[1] if len(data) > 1 else None lower = self.child.to_internal_value(lower) if lower else None upper = self.child.to_internal_value(upper) if upper else None if not lower: self.fail('required') upper = lower + timedelta(minutes=30) if not upper else upper return CustomDateTimeTZRange(lower, upper)
def to_range(date, **kwargs): duration = timedelta(**kwargs) if kwargs else default_duration return CustomDateTimeTZRange(date, date + duration)
def default_pickup_date_range(): return CustomDateTimeTZRange(timezone.now(), timezone.now() + default_duration)
def filter(self, qs, value): if value: value = CustomDateTimeTZRange(value.start, value.stop) return super().filter(qs, value)