Exemplo n.º 1
0
 def test_hour_conversion(self):
     self.assertEqual(1, timedelta_to_hours(timezone.timedelta(hours=1)))
     self.assertEqual(1.25,
                      timedelta_to_hours(timezone.timedelta(minutes=75)))
     self.assertEqual(0.05,
                      timedelta_to_hours(timezone.timedelta(seconds=180)))
     self.assertEqual(
         28.25,
         timedelta_to_hours(
             timezone.timedelta(days=1, hours=3, minutes=60, seconds=900)))
Exemplo n.º 2
0
    def valid_time(start_time, end_time, machine_type):
        # Normal non rule ignoring reservations will not be longer than 1 week
        if timedelta_to_hours(end_time - start_time) > 168:
            return False
        rules = [
            rule for rule in ReservationRule.objects.filter(
                machine_type=machine_type)
            if rule.hours_inside(start_time, end_time)
        ]
        if timedelta_to_hours(end_time - start_time) > max(rule.max_hours
                                                           for rule in rules):
            return False

        return all(
            rule.valid_time_in_rule(start_time, end_time,
                                    len(rules) > 1) for rule in rules)
Exemplo n.º 3
0
    def valid_time(start_time, end_time, machine_type):
        # Normal non rule ignoring reservations will not be longer than 1 week
        if timedelta_to_hours(end_time - start_time) > (7 * 24):
            return False
        rules = [
            rule for rule in machine_type.reservation_rules.all()
            if rule.hours_inside(start_time, end_time)
        ]
        if timedelta_to_hours(end_time - start_time) > max(rule.max_hours
                                                           for rule in rules):
            return False

        if timedelta_to_hours(end_time - start_time) <= min(rule.max_hours
                                                            for rule in rules):
            return True

        return all(
            rule.valid_time_in_rule(start_time, end_time,
                                    len(rules) > 1) for rule in rules)
Exemplo n.º 4
0
 def format_period(machine, start_time, end_time):
     """
     Formats a time period for the context
     """
     return {
         "machine": machine,
         "start_time": start_time,
         "end_time": end_time,
         "duration": ceil(timedelta_to_hours(end_time - start_time)),
     }
Exemplo n.º 5
0
    def get_periods(self, machine, required_time):
        """
        Finds all future periods for the given machine with a minimum length

        :param machine: The machine to get periods for
        :param required_time: The minimum required time for the period
        :return: A list of periods
        """
        periods = []
        reservations = list(
            Reservation.objects.filter(
                end_time__gte=timezone.now(),
                machine__pk=machine.pk).order_by("start_time"))

        # Find all periods between reservations
        for period_start, period_end in zip(reservations, reservations[1:]):
            duration = timedelta_to_hours(
                period_end.start_time - period_start.end_time)
            if duration >= required_time:
                periods.append(
                    self.format_period(
                        machine,
                        period_start.end_time,
                        period_end.start_time))

        # Add remaining time after last reservation
        if reservations:
            periods.append(self.format_period(
                machine, reservations[-1].end_time,
                timezone.now() + timezone.timedelta(days=Reservation.reservation_future_limit_days)))
        # If the machine is not reserved anytime in the future, we include the
        # whole allowed period
        else:
            periods.append(
                self.format_period(
                    machine,
                    timezone.now(),
                    timezone.now() +
                    timezone.timedelta(
                        days=Reservation.reservation_future_limit_days)))
        return periods
Exemplo n.º 6
0
 def valid_time_in_rule(self, start_time, end_time, border_cross):
     if border_cross:
         return self.hours_inside(
             start_time, end_time) <= self.max_inside_border_crossed
     return timedelta_to_hours(end_time - start_time) <= self.max_hours