Esempio n. 1
0
 def create(self, validated_data):
     master = self.context['request'].user.master
     # Schedule.objects.get(master=master, )
     schedule, created = Schedule.objects.get_or_create(
         master=master,
         date=validated_data['date'],
     )
     time_tuples = time_slot_utils.parse_time_slots(
         validated_data['time_slots'], include_last=True)
     # TODO that's not fool proof
     # timeslots may not be overwritten
     logging.info(f'Creating schedule on {schedule.date} '
                  f'for master {master.first_name}. '
                  f'time_slots={time_tuples}')
     for time_tuple in time_tuples:
         try:
             target_time = datetime.time(hour=time_tuple.hour,
                                         minute=time_tuple.minute)
             TimeSlot.objects.get(schedule=schedule,
                                  time__value=target_time)
         except TimeSlot.DoesNotExist:
             TimeSlot.objects.create(time=Time.objects.create(
                 hour=time_tuple.hour, minute=time_tuple.minute),
                                     schedule=schedule,
                                     taken=False)
         else:
             raise ApplicationError(f'Trying to overwrite time slot'
                                    f' at {target_time}')
     return schedule
Esempio n. 2
0
    def patch(self, request, *args, **kwargs):
        """
        Deletes time slots of schedule at `date`

        ```
        [{
          'date': '2017-10-10',
          'time_slots': '10:00-13:30,15:00,17:00-19:00'
        }]
        ```
        Response:

        200 OK
        """
        serializer = self.get_serializer(data=request.data)
        serializer.is_valid(raise_exception=True)
        date = serializer.validated_data['date']
        slot_string = serializer.validated_data['time_slots']

        time_tuples = time_slot_utils.parse_time_slots(slot_string,
                                                       include_last=True)

        try:
            schedule = request.user.master.schedule.get(date=date)
        except Schedule.DoesNotExist:
            raise NotFound(detail=f'Schedule at {date} was not found')
        else:
            for time_tuple in time_tuples:
                schedule.delete_slot(
                    datetime.time(hour=time_tuple.hour,
                                  minute=time_tuple.minute))
            if len(schedule.time_slots.all()) == 0:
                schedule.delete()

        return Response(status=status.HTTP_204_NO_CONTENT)
    def test_enumeration(self):
        slots = sorted(time_slot_utils.parse_time_slots('10:00, 10:30'))
        self.assertEqual(len(slots), 2)
        self.assertEqual(slots[0].hour, 10)
        self.assertEqual(slots[0].minute, 0)

        self.assertEqual(slots[1].hour, 10)
        self.assertEqual(slots[1].minute, 30)
    def test_range(self):
        slots = sorted(time_slot_utils.parse_time_slots('10:00-11:30'))
        self.assertEqual(len(slots), 3)
        self.assertEqual(slots[0].hour, 10)
        self.assertEqual(slots[0].minute, 00)

        self.assertEqual(slots[1].hour, 10)
        self.assertEqual(slots[1].minute, 30)

        self.assertEqual(slots[2].hour, 11)
        self.assertEqual(slots[2].minute, 00)
    def test_combo(self):
        slots = sorted(
            time_slot_utils.parse_time_slots('10:00, 10:30, 12:00-13:00'))
        self.assertEqual(len(slots), 4)
        self.assertEqual(slots[0].hour, 10)
        self.assertEqual(slots[0].minute, 0)

        self.assertEqual(slots[1].hour, 10)
        self.assertEqual(slots[1].minute, 30)

        self.assertEqual(slots[2].hour, 12)
        self.assertEqual(slots[2].minute, 0)

        self.assertEqual(slots[3].hour, 12)
        self.assertEqual(slots[3].minute, 30)
 def test_single(self):
     slots = sorted(time_slot_utils.parse_time_slots('10:00'))
     self.assertEqual(len(slots), 1)
     self.assertEqual(slots[0].hour, 10)
     self.assertEqual(slots[0].minute, 0)