コード例 #1
0
    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()
コード例 #2
0
    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()
コード例 #3
0
    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)
コード例 #4
0
 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,
     )
コード例 #5
0
    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)))
コード例 #6
0
 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,
     )
コード例 #7
0
 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))
コード例 #8
0
 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)
コード例 #9
0
def to_range(date, **kwargs):
    duration = timedelta(**kwargs) if kwargs else default_duration
    return CustomDateTimeTZRange(date, date + duration)
コード例 #10
0
def default_pickup_date_range():
    return CustomDateTimeTZRange(timezone.now(),
                                 timezone.now() + default_duration)
コード例 #11
0
 def filter(self, qs, value):
     if value:
         value = CustomDateTimeTZRange(value.start, value.stop)
     return super().filter(qs, value)