Esempio n. 1
0
    def test_add_and_delete_events(self):
        self.client.login(username='******', password='******')
        date = make_aware(datetime.datetime(2015, 6, 10))
        resp = self.client.get(reverse('admin:monthly_schedule', kwargs={'year': date.year, 'month': date.month}))
        data = {
            'form-TOTAL_FORMS': 2,
            'form-INITIAL_FORMS': 0,
            'form-0-day_{}'.format(date.day): False,
            'form-1-day_{}'.format(date.day): False,
            'form-0-day_23': True,
            'form-1-day_23': True,
        }
        for index, days in enumerate(resp.context['formset'].initial):
            for key, value in days.items():
                if int(key[4:]) != date.day:
                    data['form-{}-{}'.format(index, key)] = value

        resp = self.client.post(
            reverse('admin:monthly_schedule', kwargs={'year': date.year, 'month': date.month}),
            data=data,
        )
        self.assertEqual(resp.status_code, 302)
        for barber in Barber.objects.all():
            self.assertFalse(Day(self.calendar.events.get_for_object(barber), date=datetime.date(date.year, date.month, date.day)).has_occurrences())
            self.assertTrue(Day(self.calendar.events.get_for_object(barber), date=datetime.date(date.year, date.month, 23)).has_occurrences())
Esempio n. 2
0
    def setUp(self):
        self.day = Day(
            events=Event.objects.all(),
            date=datetime.datetime(datetime.datetime.now().year, 2, 7, 0, 0, tzinfo=pytz.utc))
        self.day_out_of_limit = Day(
            events=Event.objects.all(),
            date=datetime.datetime(datetime.datetime.now().year + 3, 2, 7, 0, 0, tzinfo=pytz.utc))
        self.day_out_of_limit_lower = Day(
            events=Event.objects.all(),
            date=datetime.datetime(datetime.datetime.now().year - 3, 2, 7, 0, 0, tzinfo=pytz.utc))

        rule = Rule.objects.create(frequency='WEEKLY')
        self.cal = Calendar.objects.create(name='MyCal', slug='MyCalSlug')

        data = {
            'title': 'Recent Event',
            'start': datetime.datetime(datetime.datetime.now().year, 1, 5, 8, 0, tzinfo=pytz.utc),
            'end': datetime.datetime(datetime.datetime.now().year, 1, 5, 9, 0, tzinfo=pytz.utc),
            'end_recurring_period': datetime.datetime(datetime.datetime.now().year, 5, 5, 0, 0, tzinfo=pytz.utc),
            'rule': rule,
            'calendar': self.cal,
        }
        Event.objects.create(**data)
        self.period = Period(events=Event.objects.all(),
                             start=datetime.datetime(datetime.datetime.now().year, 1, 4, 7, 0, tzinfo=pytz.utc),
                             end=datetime.datetime(datetime.datetime.now().year, 1, 21, 7, 0, tzinfo=pytz.utc))
class TestDay(TestCase):
    def setUp(self):
        self.day = Day(events=Event.objects.all(),
                           date=datetime.datetime(2008, 2, 7, 9, 0, tzinfo=pytz.utc))

    def test_day_setup(self):
        self.assertEqual( self.day.start, datetime.datetime(2008, 2, 7, 0, 0, tzinfo=pytz.utc))
        self.assertEqual( self.day.end, datetime.datetime(2008, 2, 8, 0, 0, tzinfo=pytz.utc))

    def test_day_convenience_functions(self):
        self.assertEqual( self.day.prev_day().start, datetime.datetime(2008, 2, 6, 0, 0, tzinfo=pytz.utc))
        self.assertEqual( self.day.next_day().start, datetime.datetime(2008, 2, 8, 0, 0, tzinfo=pytz.utc))

    def test_time_slot(self):
        slot_start = datetime.datetime(2008, 2, 7, 13, 30, tzinfo=pytz.utc)
        slot_end = datetime.datetime(2008, 2, 7, 15, 0, tzinfo=pytz.utc)
        period = self.day.get_time_slot( slot_start, slot_end )
        self.assertEqual( period.start, slot_start )
        self.assertEqual( period.end, slot_end )

    def test_time_slot_with_dst(self):
        tzinfo = pytz.timezone('America/Vancouver')
        slot_start = datetime.datetime(2016, 3, 13, 0, 0, tzinfo=tzinfo)
        slot_end = datetime.datetime(2016, 3, 14, 0, 0, tzinfo=tzinfo)
        period = self.day.get_time_slot(slot_start, slot_end)
        self.assertEqual(period.start, slot_start)
        self.assertEqual(period.end, slot_end)
Esempio n. 4
0
class TestDay(TestCase):
    def setUp(self):
        self.day = Day(events=Event.objects.all(),
                       date=datetime.datetime(2008,
                                              2,
                                              7,
                                              9,
                                              0,
                                              tzinfo=pytz.utc))

    def test_day_setup(self):
        self.assertEqual(self.day.start,
                         datetime.datetime(2008, 2, 7, 0, 0, tzinfo=pytz.utc))
        self.assertEqual(self.day.end,
                         datetime.datetime(2008, 2, 8, 0, 0, tzinfo=pytz.utc))

    def test_day_convenience_functions(self):
        self.assertEqual(self.day.prev_day().start,
                         datetime.datetime(2008, 2, 6, 0, 0, tzinfo=pytz.utc))
        self.assertEqual(self.day.next_day().start,
                         datetime.datetime(2008, 2, 8, 0, 0, tzinfo=pytz.utc))

    def test_time_slot(self):
        slot_start = datetime.datetime(2008, 2, 7, 13, 30, tzinfo=pytz.utc)
        slot_end = datetime.datetime(2008, 2, 7, 15, 0, tzinfo=pytz.utc)
        period = self.day.get_time_slot(slot_start, slot_end)
        self.assertEqual(period.start, slot_start)
        self.assertEqual(period.end, slot_end)

    def test_time_slot_with_dst(self):
        tzinfo = pytz.timezone('America/Vancouver')
        slot_start = datetime.datetime(2016, 3, 13, 0, 0, tzinfo=tzinfo)
        slot_end = datetime.datetime(2016, 3, 14, 0, 0, tzinfo=tzinfo)
        period = self.day.get_time_slot(slot_start, slot_end)
        self.assertEqual(period.start, slot_start)
        self.assertEqual(period.end, slot_end)

    def test_get_day_range(self):
        # This test exercises the case where a Day object is initiatized with
        # no date, which causes the Day constructor to call timezone.now(),
        # which always uses UTC.  This can cause a problem if the desired TZ
        # is not UTC, because the _get_day_range method typecasts the
        # tz-aware datetime to a naive datetime.

        # To simulate this case, we will create a NY tz date, localize that
        # date to UTC, then create a Day object with the UTC date and NY TZ

        NY = pytz.timezone('America/New_York')
        user_wall_time = datetime.datetime(2015, 11, 4, 21, 30, tzinfo=NY)
        timezone_now = user_wall_time.astimezone(pytz.utc)

        test_day = Day(events=Event.objects.all(),
                       date=timezone_now,
                       tzinfo=NY)

        expected_start = datetime.datetime(2015, 11, 4, 5, 00, tzinfo=pytz.utc)
        expected_end = datetime.datetime(2015, 11, 5, 5, 00, tzinfo=pytz.utc)

        self.assertEqual(test_day.start, expected_start)
        self.assertEqual(test_day.end, expected_end)
Esempio n. 5
0
def get_events_users_inbetween(calendar, since, until):
    delta = until - since
    result = {}
    for i in range(delta.days + 1):
        that_day = since + timedelta(days=i)
        that_day = timezone.make_aware(that_day, timezone.get_current_timezone())
        day = Day(calendar.events.all(), that_day)
        for o in day.get_occurrences():
            if o.start <= that_day <= o.end:
                items = o.event.title.split(',')
                for item in items:
                    username = item
                    if Group.objects.filter(name=item.strip()) is not None:
                        for user in User.objects.filter(groups__name=item):
                            if user not in result.keys():
                                result.append(user)
                            else:
                                result[username]["end"] = o.end
                    else:
                        if item not in result.keys():
                            user_instance = User.objects.get(username=item.strip())
                            result[username] = {"start": o.start, "person": username.strip(), "end": o.end,
                                            "email": user_instance.email}
                        else:
                            result[username]["end"] = o.end
    return result.values()
Esempio n. 6
0
class TestDay(TestCase):
    def setUp(self):
        self.day = Day(events=Event.objects.all(),
                       date=datetime.datetime(2008,
                                              2,
                                              7,
                                              9,
                                              0,
                                              tzinfo=pytz.utc))

    def test_day_setup(self):
        self.assertEqual(self.day.start,
                         datetime.datetime(2008, 2, 7, 0, 0, tzinfo=pytz.utc))
        self.assertEqual(self.day.end,
                         datetime.datetime(2008, 2, 8, 0, 0, tzinfo=pytz.utc))

    def test_day_convenience_functions(self):
        self.assertEqual(self.day.prev_day().start,
                         datetime.datetime(2008, 2, 6, 0, 0, tzinfo=pytz.utc))
        self.assertEqual(self.day.next_day().start,
                         datetime.datetime(2008, 2, 8, 0, 0, tzinfo=pytz.utc))

    def test_time_slot(self):
        slot_start = datetime.datetime(2008, 2, 7, 13, 30, tzinfo=pytz.utc)
        slot_end = datetime.datetime(2008, 2, 7, 15, 0, tzinfo=pytz.utc)
        period = self.day.get_time_slot(slot_start, slot_end)
        self.assertEqual(period.start, slot_start)
        self.assertEqual(period.end, slot_end)
Esempio n. 7
0
 def setUp(self):
     self.day = Day(events=Event.objects.all(),
                    date=datetime.datetime(2008,
                                           2,
                                           7,
                                           9,
                                           0,
                                           tzinfo=pytz.utc))
Esempio n. 8
0
def monthly_schedule(request, year, month):
    if int(year) > 2037 or int(year) < 1970 or int(month) < 1 or int(month) > 12:
        raise Http404

    MonthlyScheduleFormset = formset_factory(wraps(MonthlyScheduleForm)(partial(MonthlyScheduleForm, days=monthrange(int(year), int(month))[1])), extra=0)

    initial_data = []
    for barber in Barber.objects.all():
        data = {}
        calendar = Calendar.objects.get_or_create(name='barber_schedule')[0]
        month_period = Month(calendar.events.get_for_object(barber), datetime(int(year), int(month), 1))
        for day_period in month_period.get_days():
            if day_period.has_occurrences():
                data['day_{}'.format(day_period.start.day)] = True
        initial_data.append(data)

    if request.method == 'POST':
        formset = MonthlyScheduleFormset(request.POST, initial=initial_data)
        if formset.is_valid():
            for form, barber in zip(formset, Barber.objects.all()):
                for day in form.changed_data:
                    if not form.cleaned_data[day]:
                        calendar = Calendar.objects.get(name='barber_schedule')
                        events = calendar.events.get_for_object(barber)
                        period = Day(events, datetime(int(year), int(month), int(day[4:])))
                        if period.has_occurrences():
                            for occurrence in period.get_occurrences():
                                Event.objects.get(id=occurrence.event_id).delete()
                    else:
                        calendar = Calendar.objects.get_or_create(name='barber_schedule')[0]
                        event = Event(
                            start=make_aware(datetime(int(year), int(month), int(day[4:]), settings.DAY_START)),
                            end=make_aware(datetime(int(year), int(month), int(day[4:]), settings.DAY_END))
                        )
                        event.save()
                        calendar.events.add(event)
                        relation = EventRelation.objects.create_relation(event, barber)
                        relation.save()

    else:
        formset = MonthlyScheduleFormset(initial=initial_data)

    context = dict(
        admin.site.each_context(request),
        days=range(1, monthrange(int(year), int(month))[1] + 1),
        first_weekday=monthrange(int(year), int(month))[0],
        barbers=zip(Barber.objects.all(), formset),
        formset=formset,
        prev_date=(datetime(int(year), int(month), 1) - timedelta(days=1)),
        current_date=datetime.now(),
        next_date=(datetime(int(year), int(month), monthrange(int(year), int(month))[1]) + timedelta(days=1)),
    )
    if request.method == 'POST':
        return redirect(reverse('admin:monthly_schedule', kwargs={'year': year, 'month': month}), context)
    else:
        return render(request, 'admin/monthly_schedule.html', context)
Esempio n. 9
0
def get_current_events_users(calendar):
    now = timezone.make_aware(datetime.now(), timezone.get_current_timezone())
    result = []
    day = Day(calendar.events.all(), now)
    for o in day.get_occurrences():
        if o.start <= now <= o.end:
            usernames = o.event.title.split(',')
            for username in usernames:
                result.append(User.objects.get(username=username.strip()))
    return result
Esempio n. 10
0
def get_current_events_users(calendar):
    now = timezone.make_aware(datetime.now(), timezone.get_current_timezone())
    result = []
    day = Day(calendar.events.all(), now)
    for o in day.get_occurrences():
        if o.start <= now <= o.end:
            usernames = o.event.title.split(',')
            for username in usernames:
                result.append(User.objects.get(username=username.strip()))
    return result
Esempio n. 11
0
def actions(request):
    actions = models.Action.objects.order_by('-created_on')
    period = Day(actions, now())
    # XXX: Hack for don't get tomorow occs
    occs = [occ for occ in period.get_occurrences()
            if occ.start.date() == now().date()]
    return render(request, 'bio/actions.html', {
        'meta': actions.model._meta,
        'objects': occs,
        'calendar': Calendar.objects.get(name='Bio')
    })
Esempio n. 12
0
class TestDay(TestCase):
    def setUp(self):
        self.day = Day(
            events=Event.objects.all(),
            date=datetime.datetime(2008, 2, 7, 9, 0, tzinfo=pytz.utc))

    def test_day_setup(self):
        self.assertEqual(self.day.start, datetime.datetime(2008, 2, 7, 0, 0, tzinfo=pytz.utc))
        self.assertEqual(self.day.end, datetime.datetime(2008, 2, 8, 0, 0, tzinfo=pytz.utc))

    def test_day_convenience_functions(self):
        self.assertEqual(self.day.prev_day().start, datetime.datetime(2008, 2, 6, 0, 0, tzinfo=pytz.utc))
        self.assertEqual(self.day.next_day().start, datetime.datetime(2008, 2, 8, 0, 0, tzinfo=pytz.utc))

    def test_time_slot(self):
        slot_start = datetime.datetime(2008, 2, 7, 13, 30, tzinfo=pytz.utc)
        slot_end = datetime.datetime(2008, 2, 7, 15, 0, tzinfo=pytz.utc)
        period = self.day.get_time_slot(slot_start, slot_end)
        self.assertEqual(period.start, slot_start)
        self.assertEqual(period.end, slot_end)

    def test_time_slot_with_dst(self):
        tzinfo = pytz.timezone('America/Vancouver')
        slot_start = datetime.datetime(2016, 3, 13, 0, 0, tzinfo=tzinfo)
        slot_end = datetime.datetime(2016, 3, 14, 0, 0, tzinfo=tzinfo)
        period = self.day.get_time_slot(slot_start, slot_end)
        self.assertEqual(period.start, slot_start)
        self.assertEqual(period.end, slot_end)

    def test_get_day_range(self):
        # This test exercises the case where a Day object is initiatized with
        # no date, which causes the Day constructor to call timezone.now(),
        # which always uses UTC.  This can cause a problem if the desired TZ
        # is not UTC, because the _get_day_range method typecasts the
        # tz-aware datetime to a naive datetime.

        # To simulate this case, we will create a NY tz date, localize that
        # date to UTC, then create a Day object with the UTC date and NY TZ

        NY = pytz.timezone('America/New_York')
        user_wall_time = datetime.datetime(2015, 11, 4, 21, 30, tzinfo=NY)
        timezone_now = user_wall_time.astimezone(pytz.utc)

        test_day = Day(
            events=Event.objects.all(),
            date=timezone_now,
            tzinfo=NY)

        expected_start = datetime.datetime(2015, 11, 4, 5, 00, tzinfo=pytz.utc)
        expected_end = datetime.datetime(2015, 11, 5, 5, 00, tzinfo=pytz.utc)

        self.assertEqual(test_day.start, expected_start)
        self.assertEqual(test_day.end, expected_end)
Esempio n. 13
0
 def get_free_time(self, date):
     _date = datetime.strptime(date, '%Y-%m-%d')
     day = Day([], _date, tzinfo=None)
     period = day.get_periods(Hour)
     result = []
     begin, end = self.get_working_hours(_date)
     for p in period:
         if (p.start >= begin and p.end <= end):
             if self.get_free_consultants(
                     date, datetime.strftime(p.start, '%H:%M')):
                 result.append(datetime.strftime(p.start, '%H:%M'))
     # return ['9:00', '9:30', '10:00', '10:30']
     return result
Esempio n. 14
0
 def get_data(self):
     period = Day(Event.objects.exclude(appointment=None).filter(
         appointment__customer=self.customer), self.date)
     data = [{'id': x.event.appointment_set.first().pk,
              'title': "{}".format(x.event.appointment_set.first().display_name),
              'userId': [x.event.appointment_set.first().venue.pk],
              'start': x.start.isoformat(),
              'end': x.end.isoformat(),
              'clientId': x.event.appointment_set.first().clientId,
              'status': x.event.appointment_set.first().status,
              'tag': getattr(x.event.appointment_set.first().tag, 'html_name', ""),
              'body': x.event.description
              }
             for x in period.get_occurrences()]
     return data
Esempio n. 15
0
def get_current_events_users(calendar):
    now = timezone.now()
    result = []
    day = Day(calendar.events.all(), now)
    for o in day.get_occurrences():
        if o.start <= now <= o.end:
            items = o.event.title.split(',')
            for item in items:
                if Group.objects.filter(name=item.strip()).exists():
                    for user in User.objects.filter(groups__name=item.strip()):
                        user.came_from_group = item.strip()
                        result.append(user)
                else:
                    result.append(User.objects.get(username=item.strip()))
    return result
    def test_all_day_event_cook_slots(self):
        cal = Calendar(name='MyCal', slug='MyCalSlug')
        cal.save()
        start = datetime.datetime(datetime.datetime.now().year,
                                  1,
                                  5,
                                  0,
                                  0,
                                  tzinfo=pytz.utc)
        end = datetime.datetime(datetime.datetime.now().year,
                                1,
                                6,
                                0,
                                0,
                                tzinfo=pytz.utc)
        data = {
            'title': 'All Day Event',
            'start': start,
            'end': end,
            'calendar': self.cal,
        }
        event = Event(**data)
        event.save()
        period = Day([event], start, end)

        slots = _cook_slots(period, 60)
        self.assertEqual(len(slots), 24)
Esempio n. 17
0
    def setUp(self):
        self.timezone = pytz.timezone('Europe/Amsterdam')
        self.day = Day(
            events=Event.objects.all(),
            date=self.timezone.localize(datetime.datetime(2013, 12, 17, 9, 0)),
            tzinfo=self.timezone
        )

        self.week = Week(
            events=Event.objects.all(),
            date=self.timezone.localize(datetime.datetime(2013, 12, 17, 9, 0)),
            tzinfo=self.timezone,
        )

        self.month = Month(
            events=Event.objects.all(),
            date=self.timezone.localize(datetime.datetime(2013, 12, 17, 9, 0)),
            tzinfo=self.timezone,
        )

        self.year = Year(
            events=Event.objects.all(),
            date=self.timezone.localize(datetime.datetime(2013, 12, 17, 9, 0)),
            tzinfo=self.timezone,
        )
Esempio n. 18
0
 def get_schedule_for_date(self, date):
     all_schedules = list(self.schedule_set.all())
     occurrence = Day(all_schedules, date).get_occurrences()
     if not occurrence:
         raise NoSchedule('No schedule for given date')
     else:
         occurrence = occurrence[0]
     return occurrence_to_schedule(occurrence)
Esempio n. 19
0
def get_events_users_inbetween(calendar, since, until):
    delta = until - since
    result = {}
    for i in range(delta.days + 1):
        that_day = since + timedelta(days=i)
        that_day = timezone.make_aware(that_day, timezone.get_current_timezone())
        day = Day(calendar.events.all(), that_day)
        for o in day.get_occurrences():
            if o.start <= that_day <= o.end:
                usernames = o.event.title.split(',')
                for username in usernames:
                    if username not in result.keys():
                        user_instance = User.objects.get(username=username.strip())
                        result[username] = {"start": o.start, "person": username.strip(), "end": o.end,
                                            "email": user_instance.email}
                    else:
                        result[username]["end"] = o.end
    return result.values()
class TestDay(TestCase):
    def setUp(self):
        self.day = Day(reservations=Reservation.objects.all(), date=datetime.datetime(2008, 2, 7, 9, 0))

    def test_day_setup(self):
        self.assertEqual(self.day.start, datetime.datetime(2008, 2, 7, 0, 0))
        self.assertEqual(self.day.end, datetime.datetime(2008, 2, 8, 0, 0))

    def test_day_convenience_functions(self):
        self.assertEqual(self.day.prev_day().start, datetime.datetime(2008, 2, 6, 0, 0))
        self.assertEqual(self.day.next_day().start, datetime.datetime(2008, 2, 8, 0, 0))

    def test_time_slot(self):
        slot_start = datetime.datetime(2008, 2, 7, 13, 30)
        slot_end = datetime.datetime(2008, 2, 7, 15, 0)
        period = self.day.get_time_slot(slot_start, slot_end)
        self.assertEqual(period.start, slot_start)
        self.assertEqual(period.end, slot_end)
Esempio n. 21
0
 def get_active_barbers(self, day):
     active_barbers = []
     for barber in self.all():
         barber_schedule_data_raw = EventRelation.objects.filter(event__calendar__name='barber_schedule').filter(object_id=barber.id).values('event')
         barber_schedule_data = []
         for item in barber_schedule_data_raw.values():
             barber_schedule_data.append(Event.objects.get(pk=item['event_id']))
         if Day(barber_schedule_data, day).has_occurrences():
             active_barbers.append(barber)
     return active_barbers
Esempio n. 22
0
 def test_weeks_later_day(self):
     # prepare
     schedules = Schedule.objects.all()
     # do
     occur = Day(schedules,
                 timezone.make_aware(datetime(2017, 1,
                                              11))).get_occurrences()
     # check
     self.assertEqual(len(occur),
                      1,
                      msg='Should be only one schedule in day')
Esempio n. 23
0
    def is_available(self, date, duration, customer=None):
        if date.hour < settings.DAY_START or date.hour + int(duration) > settings.DAY_END:
            return False # Not operating on this hours.

        barber_schedule_data = [Event.objects.get(pk=item['event_id']) for item in EventRelation.objects.filter(event__calendar__name='barber_schedule').filter(object_id=self.id).values('event').values()]
        if not Day(barber_schedule_data, date).has_occurrences():
            return False # Barber doesn't work this day.

        client_schedule_data = [Event.objects.get(pk=EventRelation.objects.filter(event__calendar__name='client_schedule').get(object_id=appointment.id).event_id) for appointment in Appointment.objects.filter(barber=self) if appointment.customer != customer]
        if Period(client_schedule_data, date + timedelta(minutes=1), date + timedelta(hours=int(duration)-1, minutes=59)).has_occurrences():
            return False # Barber got another client at this time.

        return True
Esempio n. 24
0
    def get_hours_for_parking(self, obj):
        now = timezone.now()
        occurrences = Day(obj.schedule_lot.schedule_set.all(),
                          now).get_occurrences()
        schedule = occurrences[0] if occurrences else None

        if schedule:
            #start = '{0}:{1}Z'.format(schedule.start.hour, schedule.start.minute)
            #end = '{0}:{1}Z'.format(schedule.end.hour, schedule.end.minute)

            hours = {'start': schedule.start, 'end': schedule.end}
        else:
            hours = dict()
        return hours
Esempio n. 25
0
def get_events_users_inbetween(calendar, since, until):
    delta = until - since
    result = {}
    for i in range(delta.days + 1):
        that_day = since + timedelta(days=i)
        that_day = timezone.make_aware(that_day,
                                       timezone.get_current_timezone())
        day = Day(calendar.events.all(), that_day)
        for o in day.get_occurrences():
            if o.start <= that_day <= o.end:
                usernames = o.event.title.split(',')
                for username in usernames:
                    if username not in result.keys():
                        user_instance = User.objects.get(
                            username=username.strip())
                        result[username] = {
                            "start": o.start,
                            "person": username.strip(),
                            "end": o.end,
                            "email": user_instance.email
                        }
                    else:
                        result[username]["end"] = o.end
    return result.values()
    def setUp(self):
        self.timezone = pytz.timezone('Europe/Amsterdam')

        start = self.timezone.localize(datetime.datetime(2008, 2, 7, 0, 20))
        end = self.timezone.localize(datetime.datetime(2008, 2, 7, 0, 21))
        self.event = Event.objects.create(
            title=
            'One minute long event on january seventh 2008 at 00:20 in Amsterdam.',
            start=start,
            end=end)

        self.day = Day(
            events=Event.objects.all(),
            date=self.timezone.localize(datetime.datetime(2008, 2, 7, 9, 0)),
            tzinfo=self.timezone,
        )
Esempio n. 27
0
    def setUp(self):
        self.event = Event.objects.get(pk=2)
        self.timezone = pytz.timezone('Europe/Amsterdam')

        self.event.show.date.start = self.timezone.localize(
            datetime.datetime(2008, 2, 7, 0, 20))
        self.event.show.date.end = self.timezone.localize(
            datetime.datetime(2008, 2, 7, 0, 21))
        self.event.show.date.save()

        self.day = Day(
            events=[
                self.event,
            ],
            date=self.timezone.localize(datetime.datetime(2008, 2, 7, 9, 0)),
            tzinfo=self.timezone,
        )
    def setUp(self):
        self.timezone = pytz.timezone('Europe/Amsterdam')

        start = self.timezone.localize(datetime.datetime(2008, 2, 7, 0, 20))
        end = self.timezone.localize(datetime.datetime(2008, 2, 7, 0, 21))
        self.event = Event.objects.create(
            subtitle=subtitle,
            start=start,
            end=end,
            calendar=Calendar.objects.create(name="MyCal"),
        )

        self.day = Day(
            events=Event.objects.all(),
            date=self.timezone.localize(datetime.datetime(2008, 2, 7, 9, 0)),
            tzinfo=self.timezone,
        )
Esempio n. 29
0
 def setUp(self):
     self.day = Day(events=Event.objects.all(),
                    date=datetime.datetime(2008, 2, 7, 0, 0, tzinfo=pytz.utc))
     rule = Rule(frequency="WEEKLY")
     rule.save()
     self.cal = Calendar(name="MyCal", slug="MyCalSlug")
     self.cal.save()
     data = {
         'title': 'Recent Event',
         'start': datetime.datetime(2008, 1, 5, 8, 0, tzinfo=pytz.utc),
         'end': datetime.datetime(2008, 1, 5, 9, 0, tzinfo=pytz.utc),
         'end_recurring_period': datetime.datetime(2008, 5, 5, 0, 0, tzinfo=pytz.utc),
         'rule': rule,
         'calendar': self.cal,
     }
     recurring_event = Event(**data)
     recurring_event.save()
     self.period = Period(events=Event.objects.all(),
                          start=datetime.datetime(2008, 1, 4, 7, 0, tzinfo=pytz.utc),
                          end=datetime.datetime(2008, 1, 21, 7, 0, tzinfo=pytz.utc))
Esempio n. 30
0
def daily_schedule(request, year, month, day):
    Calendar.objects.get_or_create(name='barber_schedule')
    Calendar.objects.get_or_create(name='client_schedule')

    events = {}
    active_barbers = Barber.objects.get_active_barbers(make_aware(datetime(int(year), int(month), int(day))))

    for barber in active_barbers:
        app_ids = Appointment.objects.filter(barber=barber).values('id')
        events_list = []
        for app_id in app_ids:
            event = EventRelation.objects.filter(event__calendar__name='client_schedule').filter(object_id=app_id['id']).values('event_id')
            events_list.append(Event.objects.get(pk=event[0]['event_id'])) # F*****g horrible.
        events[barber] = Day(events_list, datetime(int(year), int(month), int(day))).occurrences
        events[barber] = list(zip(events[barber], [Appointment.objects.get(pk=EventRelation.objects.filter(event__calendar__name='client_schedule').get(event__id=occ.event_id).object_id) for occ in events[barber]]))

    table_nodes = {}
    for barber in active_barbers:
        if not table_nodes.get(barber):
            table_nodes[barber] = []

        busy_hours = {occ_tuple[0].start.astimezone(timezone(settings.TIME_ZONE)).hour: occ_tuple[0].end.astimezone(timezone(settings.TIME_ZONE)).hour-occ_tuple[0].start.astimezone(timezone(settings.TIME_ZONE)).hour for occ_tuple in events[barber]}
        hour_skip = 0
        for hour in range(settings.DAY_START, settings.DAY_END):
            if hour in busy_hours.keys():
                table_nodes[barber].append((hour, busy_hours[hour]))
                hour_skip = busy_hours[hour] - 1
            else:
                if hour_skip == 0:
                    table_nodes[barber].append((hour, None))
                else:
                    hour_skip -= 1

    context = dict(
        admin.site.each_context(request),
        hours=['{}-00'.format(hour) for hour in range(settings.DAY_START, settings.DAY_END)],
        events=events,
        barbers=active_barbers,
        table_nodes=table_nodes,
    )
    return render(request, 'admin/daily_schedule.html', context)
    def test_all_day_event_cook_slots(self):
        start = datetime.datetime(datetime.datetime.now().year,
                                  1,
                                  5,
                                  0,
                                  0,
                                  tzinfo=pytz.utc)
        end = datetime.datetime(datetime.datetime.now().year,
                                1,
                                6,
                                0,
                                0,
                                tzinfo=pytz.utc)
        event = Event.objects.create(title="All Day Event",
                                     start=start,
                                     end=end,
                                     calendar=self.cal)
        period = Day([event], start, end)

        slots = _cook_slots(period, 60)
        self.assertEqual(len(slots), 24)
Esempio n. 32
0
    def test_get_day_range(self):
        # This test exercises the case where a Day object is initiatized with
        # no date, which causes the Day constructor to call timezone.now(),
        # which always uses UTC.  This can cause a problem if the desired TZ
        # is not UTC, because the _get_day_range method typecasts the
        # tz-aware datetime to a naive datetime.

        # To simulate this case, we will create a NY tz date, localize that
        # date to UTC, then create a Day object with the UTC date and NY TZ

        NY = pytz.timezone('America/New_York')
        user_wall_time = datetime.datetime(2015, 11, 4, 21, 30, tzinfo=NY)
        timezone_now = user_wall_time.astimezone(pytz.utc)

        test_day = Day(events=Event.objects.all(),
                       date=timezone_now,
                       tzinfo=NY)

        expected_start = datetime.datetime(2015, 11, 4, 5, 00, tzinfo=pytz.utc)
        expected_end = datetime.datetime(2015, 11, 5, 5, 00, tzinfo=pytz.utc)

        self.assertEqual(test_day.start, expected_start)
        self.assertEqual(test_day.end, expected_end)
 def get_day(self, date=None):
     if date is None:
         date = timezone.now()
     local_timezone = pytz.timezone('UTC')
     return Day(self.get_events(), date, None, None, local_timezone)
Esempio n. 34
0
 def setUp(self):
     self.day = Day(events=Event.objects.all(),
                        date=datetime.datetime(2008, 2, 7, 9, 0, tzinfo=pytz.utc))
Esempio n. 35
0
 def setUp(self):
     self.day = Day(reservations=Reservation.objects.all(),
                    date=datetime.datetime(2008, 2, 7, 9, 0))
Esempio n. 36
0
 def setUp(self):
     self.day = Day(events=Event.objects.all(),
                    date=datetime.datetime(2008, 2, 7, 9, 0))
    def setUp(self):
        self.day = Day(
            events=Event.objects.all(),
            date=datetime.datetime(datetime.datetime.now().year,
                                   2,
                                   7,
                                   0,
                                   0,
                                   tzinfo=pytz.utc),
        )
        self.day_out_of_limit = Day(
            events=Event.objects.all(),
            date=datetime.datetime(datetime.datetime.now().year + 3,
                                   2,
                                   7,
                                   0,
                                   0,
                                   tzinfo=pytz.utc),
        )
        self.day_out_of_limit_lower = Day(
            events=Event.objects.all(),
            date=datetime.datetime(datetime.datetime.now().year - 3,
                                   2,
                                   7,
                                   0,
                                   0,
                                   tzinfo=pytz.utc),
        )

        rule = Rule.objects.create(frequency="WEEKLY")
        self.cal = Calendar.objects.create(name="MyCal", slug="MyCalSlug")

        Event.objects.create(
            title="Recent Event",
            start=datetime.datetime(datetime.datetime.now().year,
                                    1,
                                    5,
                                    8,
                                    0,
                                    tzinfo=pytz.utc),
            end=datetime.datetime(datetime.datetime.now().year,
                                  1,
                                  5,
                                  9,
                                  0,
                                  tzinfo=pytz.utc),
            end_recurring_period=datetime.datetime(
                datetime.datetime.now().year, 5, 5, 0, 0, tzinfo=pytz.utc),
            rule=rule,
            calendar=self.cal,
        )
        self.period = Period(
            events=Event.objects.all(),
            start=datetime.datetime(datetime.datetime.now().year,
                                    1,
                                    4,
                                    7,
                                    0,
                                    tzinfo=pytz.utc),
            end=datetime.datetime(datetime.datetime.now().year,
                                  1,
                                  21,
                                  7,
                                  0,
                                  tzinfo=pytz.utc),
        )
Esempio n. 38
0
 def setUp(self):
     self.day = Day(events=Event.objects.all(),
                        date=datetime.datetime(2008, 2, 7, 9, 0))
 def setUp(self):
     self.day = Day(reservations=Reservation.objects.all(), date=datetime.datetime(2008, 2, 7, 9, 0))