示例#1
0
 def test_get_calendar_for_object_with_more_than_one_calendar(self):
     calendar_1 = Calendar(name='My Cal 1')
     calendar_1.save()
     calendar_2 = Calendar(name='My Cal 2')
     calendar_2.save()
     rule = Rule()
     rule.save()
     calendar_1.create_relation(rule)
     calendar_2.create_relation(rule)
     with self.assertRaises(AssertionError):
         result = Calendar.objects.get_calendar_for_object(rule)
    def test_get_occurrences_rule_weekday_param(self):
        '''
        Test whether occurrence list get method behaves correctly while using
        complex rule parameters
        '''
        cal = Calendar(name="MyCal")
        # Last Fridays of each month
        rule = Rule(frequency="MONTHLY", params='BYWEEKDAY:FR;BYSETPOS:-1')
        rule.save()

        recurring_event = self.__create_recurring_event(
            'Last Friday of each month', datetime.datetime(2016, 11, 1),
            datetime.datetime(2016, 11, 1), datetime.datetime(2030, 1, 1),
            rule, cal)

        occs = recurring_event.get_occurrences(
            start=datetime.datetime(2016, 11, 1),
            end=datetime.datetime(2017, 12, 31))

        self.assertEqual(["%s to %s" % (o.start, o.end) for o in occs], [
            '2016-11-25 00:00:00 to 2016-11-25 00:00:00',
            '2016-12-30 00:00:00 to 2016-12-30 00:00:00',
            '2017-01-27 00:00:00 to 2017-01-27 00:00:00',
            '2017-02-24 00:00:00 to 2017-02-24 00:00:00',
            '2017-03-31 00:00:00 to 2017-03-31 00:00:00',
            '2017-04-28 00:00:00 to 2017-04-28 00:00:00',
            '2017-05-26 00:00:00 to 2017-05-26 00:00:00',
            '2017-06-30 00:00:00 to 2017-06-30 00:00:00',
            '2017-07-28 00:00:00 to 2017-07-28 00:00:00',
            '2017-08-25 00:00:00 to 2017-08-25 00:00:00',
            '2017-09-29 00:00:00 to 2017-09-29 00:00:00',
            '2017-10-27 00:00:00 to 2017-10-27 00:00:00',
            '2017-11-24 00:00:00 to 2017-11-24 00:00:00',
            '2017-12-29 00:00:00 to 2017-12-29 00:00:00',
        ])
    def test_get_occurrences_timespan_edge_cases(self):
        '''
        Test whether occurrence list get method behave when requesting them on
        timespan limits
        '''
        cal = Calendar(name="MyCal")
        rule = Rule(frequency="WEEKLY")
        rule.save()

        recurring_event = self.__create_recurring_event(
            'Recurring event test', datetime.datetime(2008, 1, 5, 8, 0),
            datetime.datetime(2008, 1, 5, 9, 0),
            datetime.datetime(2008, 5, 5, 0, 0), rule, cal)

        occs1 = recurring_event.get_occurrences(
            start=datetime.datetime(2008, 1, 5, 8, 0),
            end=datetime.datetime(2008, 1, 5, 8, 30))

        self.assertEqual(["%s to %s" % (o.start, o.end) for o in occs1],
                         ['2008-01-05 08:00:00 to 2008-01-05 09:00:00'])

        occs2 = recurring_event.get_occurrences(
            start=datetime.datetime(2008, 1, 5, 7, 0),
            end=datetime.datetime(2008, 1, 5, 8, 0))

        self.assertEqual(["%s to %s" % (o.start, o.end) for o in occs2], [])
示例#4
0
 def setUp(self):
     rule = Rule(frequency="WEEKLY")
     rule.save()
     cal = Calendar(name="MyCal")
     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':
         cal
     }
     recurring_event = Event(**data)
     recurring_event.save()
     self.month = Month(events=Event.objects.all(),
                        date=datetime.datetime(2008,
                                               2,
                                               7,
                                               9,
                                               0,
                                               tzinfo=pytz.utc))
    def setUp(self):
        weekly = Rule(frequency = "WEEKLY")
        weekly.save()
        daily = Rule(frequency = "DAILY")
        daily.save()
        cal = Calendar(name="MyCal")
        cal.save()

        self.event1 = Event(**{
                'title': 'Weekly Event',
                'start': datetime.datetime(2009, 4, 1, 8, 0),
                'end': datetime.datetime(2009, 4, 1, 9, 0),
                'end_recurring_period' : datetime.datetime(2009, 10, 5, 0, 0),
                'rule': weekly,
                'calendar': cal
               })
        self.event1.save()
        self.event2 = Event(**{
                'title': 'Recent Event',
                'start': datetime.datetime(2008, 1, 5, 9, 0),
                'end': datetime.datetime(2008, 1, 5, 10, 0),
                'end_recurring_period' : datetime.datetime(2009, 5, 5, 0, 0),
                'rule': daily,
                'calendar': cal
               })
        self.event2.save()
示例#6
0
    def setUp(self):
        doctor = Doctors(name = 'Victor', registration= '112020122',CPF='1637615253',status =True, category='1231233212')
        doctor.save()
        calendar = Calendar(name = 'Test Calendar')
        calendar.save()
        subtitle = Subtitles(1)
        self.event_attr = {
            'id':1,
            'subtitle': subtitle,
            'start': datetime.datetime(2013, 1, 5, 8, 0, tzinfo=pytz.utc),
            'end': datetime.datetime(2013, 1, 5, 9, 0, tzinfo=pytz.utc),
            'doctor': doctor,
            'calendar': calendar
        }

        self.serializer_data = {
            'id':0,
            'subtitle':subtitle,
            'start': datetime.datetime(2013, 1, 5, 8, 0, tzinfo=pytz.utc),
            'end': datetime.datetime(2013, 1, 5, 9, 0, tzinfo=pytz.utc),
            'doctor':doctor,
            'calendar': calendar
        }
        self.event = Event.objects.create(**self.event_attr)
        self.serializer = EventSerializer(instance=self.event)
示例#7
0
    def test_get_occurrences_is_sorted(self):
        rule = Rule(frequency="DAILY")
        rule.save()
        cal = Calendar(name="MyCal")
        cal.save()
        recurring_data = {
            'title':
            'Recent Event',
            'start':
            datetime.datetime(2016, 1, 5, 8, 0, tzinfo=pytz.utc),
            'end':
            datetime.datetime(2016, 1, 5, 9, 0, tzinfo=pytz.utc),
            'end_recurring_period':
            datetime.datetime(2016, 8, 5, 0, 0, tzinfo=pytz.utc),
            'rule':
            rule,
            'calendar':
            cal
        }
        recurring_event = Event.objects.create(**recurring_data)

        start = datetime.datetime(2016, 1, 12, 0, 0, tzinfo=pytz.utc)
        end = datetime.datetime(2016, 1, 27, 0, 0, tzinfo=pytz.utc)
        occurrences = recurring_event.get_occurrences(start=start, end=end)

        sorted_occurrences = sorted(occurrences, key=lambda occ: occ.start)

        self.assertEqual(occurrences, sorted_occurrences)
示例#8
0
    def test_recurring_event_with_moved_get_occurrences_after(self):

        cal = Calendar(name="MyCal")
        cal.save()
        rule = Rule(frequency="WEEKLY")
        rule.save()
        recurring_event = self.__create_recurring_event(
            'Recurrent event test get_occurrence',
            datetime.datetime(2008, 1, 5, 8, 0, tzinfo=pytz.utc),
            datetime.datetime(2008, 1, 5, 9, 0, tzinfo=pytz.utc),
            datetime.datetime(2008, 5, 5, 0, 0, tzinfo=pytz.utc),
            rule,
            cal,
        )

        recurring_event.save()
        occurrence = recurring_event.get_occurrence(
            datetime.datetime(2008, 1, 12, 8, 0, tzinfo=pytz.utc))
        occurrence.move(datetime.datetime(2008, 1, 15, 8, 0, tzinfo=pytz.utc),
                        datetime.datetime(2008, 1, 15, 9, 0, tzinfo=pytz.utc))
        gen = recurring_event.occurrences_after(
            datetime.datetime(2008, 1, 14, 8, 0, tzinfo=pytz.utc))
        occurrence2 = next(gen)
        #    end = datetime.datetime(2008, 1, 6, tzinfo=pytz.utc))
        #occurrence = occurrences[0]
        #occurrence2 = recurring_event.occurrences_after(datetime.datetime(2008, 1, 5, tzinfo=pytz.utc)).next()
        self.assertEqual(occurrence, occurrence2)
示例#9
0
    def test_recurring_event_get_occurrence_in_timezone(self):
        cal = Calendar(name="MyCal")
        cal.save()
        rule = Rule(frequency="WEEKLY")
        rule.save()

        # Event start and end are UTC because that is what is coming
        # from the database
        event = self.__create_recurring_event(
            'Recurrent event test get_occurrence',
            datetime.datetime(2014, 3, 21, 6, 0, tzinfo=pytz.utc),
            datetime.datetime(2014, 3, 21, 8, 0, tzinfo=pytz.utc),
            datetime.datetime(2014, 4, 11, 0, 0, tzinfo=pytz.utc),
            rule,
            cal,
        )
        event.save()
        tzinfo = pytz.timezone('Europe/Helsinki')
        start = tzinfo.localize(datetime.datetime(2014, 3, 28, 8, 0))  # +2
        occurrence = event.get_occurrence(start)
        self.assertEqual(occurrence.start, start)
        occurrence.save()
        # DST change on March 30th from +2 to +3
        start = tzinfo.localize(datetime.datetime(2014, 4, 4, 8, 0))  # +3
        occurrence = event.get_occurrence(start)
        self.assertEqual(occurrence.start, start)
示例#10
0
 def setUp(self):
     rule = Rule(frequency="WEEKLY")
     rule.save()
     cal = Calendar(name="MyCal")
     cal.save()
     self.recurring_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':
         cal
     }
     self.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),
         'calendar':
         cal
     }
     self.recurring_event = Event.objects.create(**self.recurring_data)
     self.start = datetime.datetime(2008, 1, 12, 0, 0, tzinfo=pytz.utc)
     self.end = datetime.datetime(2008, 1, 27, 0, 0, tzinfo=pytz.utc)
    def test_get_calendar_for_object(self):
        user = User(username="******")
        user.save()
        calendar = Calendar(name='My Cal', slug='My Cal')
        calendar.save()
        calendar.create_relation(user)
        calendar_object = Calendar.objects.get_calendar_for_object(user)
        self.assertEqual(calendar_object.slug, 'My Cal')

        calendar = Calendar(name='My 2nd Cal')
        calendar.save()
        try:
            calendar.create_relation(user)
        except Exception as error:
            self.assertRaises(
                error, AssertionError("More than one calendars were found."))
示例#12
0
 def save(self, force_insert=False, force_update=False):
     new_task = True
     super(PostEater, self).save(force_insert, force_update)
     end = self.startDateTime + timedelta(minutes=1)
     color = '#A41515'
     description = "http://127.0.0.1:8000/posts/" + str(
         self.submit_time.strftime("%Y/%b/%d"))
     user_short = self.user.profile.housename
     if new_task:
         event = Event(start=self.startDateTime,
                       end=end,
                       title=user_short,
                       description=description,
                       calendar_id=1)
         event.save()
         rel = EventRelation.objects.create_relation(event, self)
         rel.save()
         try:
             cal = Calendar.objects.get(name="hooggeacht")
         except Calendar.DoesNotExist:
             cal = Calendar(name="bla")
             cal.save()
         cal.events.add(event)
     else:
         event = Event.objects.get_for_object(self)[0]
         event.start = self.startDateTime
         event.end = end
         event.title = title
         event.description = self.description
         event.eventColor = self.color
         event.save()
示例#13
0
    def test_edge_case_events(self):
        cal = Calendar(name="MyCal")
        cal.save()
        data_1 = {
            'title': 'Edge case event test one',
            'start': datetime.datetime(2013, 1, 5, 8, 0, tzinfo=pytz.utc),
            'end': datetime.datetime(2013, 1, 5, 9, 0, tzinfo=pytz.utc),
            'calendar': cal
        }
        data_2 = {
            'title': 'Edge case event test two',
            'start': datetime.datetime(2013, 1, 5, 9, 0, tzinfo=pytz.utc),
            'end': datetime.datetime(2013, 1, 5, 12, 0, tzinfo=pytz.utc),
            'calendar': cal
        }
        event_one = Event(**data_1)
        event_two = Event(**data_2)
        event_one.save()
        event_two.save()
        occurrences_two = event_two.get_occurrences(datetime.datetime(2013, 1, 5, 9, 0, tzinfo=pytz.utc),
                                                    datetime.datetime(2013, 1, 5, 12, 0, tzinfo=pytz.utc))
        self.assertEqual(1, len(occurrences_two))

        occurrences_one = event_one.get_occurrences(datetime.datetime(2013, 1, 5, 9, 0, tzinfo=pytz.utc),
                                                    datetime.datetime(2013, 1, 5, 12, 0, tzinfo=pytz.utc))
        self.assertEqual(0, len(occurrences_one))
示例#14
0
 def setUp(self):
     super(TestSchedulesViews, self).setUp()
     self.cal = Calendar(
         name=random_string(),
         slug=random_string(),
     )
     self.cal.save()
    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)
    def test_get_occurrences_timespan_partially_inside_occurrence(self):
        '''
        Test whether occurrences are correctly obtained if selected timespan start
        outside of timepan but ends inside occurrence.
        '''
        cal = Calendar(name="MyCal")
        rule = Rule(frequency="WEEKLY")
        rule.save()

        recurring_event = self.__create_recurring_event(
            'Recurring event test', datetime.datetime(2008, 1, 5, 8, 0),
            datetime.datetime(2008, 1, 5, 9, 0),
            datetime.datetime(2008, 5, 5, 0, 0), rule, cal)

        occs1 = recurring_event.get_occurrences(
            start=datetime.datetime(2006, 1, 1, 0, 0),
            end=datetime.datetime(2008, 1, 19, 8, 30))

        self.assertEqual(["%s to %s" % (o.start, o.end) for o in occs1], [
            '2008-01-05 08:00:00 to 2008-01-05 09:00:00',
            '2008-01-12 08:00:00 to 2008-01-12 09:00:00',
            '2008-01-19 08:00:00 to 2008-01-19 09:00:00',
        ])

        occs2 = recurring_event.get_occurrences(
            start=datetime.datetime(2006, 1, 1, 0, 0),
            end=datetime.datetime(2008, 1, 19, 10, 0))

        self.assertEqual(["%s to %s" % (o.start, o.end) for o in occs2], [
            '2008-01-05 08:00:00 to 2008-01-05 09:00:00',
            '2008-01-12 08:00:00 to 2008-01-12 09:00:00',
            '2008-01-19 08:00:00 to 2008-01-19 09:00:00',
        ])
示例#17
0
 def save(self, force_insert=False, force_update=False):
     super(CircleEvent, self).save(force_insert, force_update)
     try:
         cal = Calendar.objects.get(pk=1)
     except Calendar.DoesNotExist:
         cal = Calendar(name="Community Calendar")
         cal.save()
     cal.events.add(self)
示例#18
0
 def test_get_calendar_for_object(self):
     calendar = Calendar(name='My Cal')
     calendar.save()
     rule = Rule()
     rule.save()
     calendar.create_relation(rule)
     result = Calendar.objects.get_calendar_for_object(rule)
     self.assertEqual(result.name, 'My Cal')
 def test_create_calendar_relation(self):
     user = User.objects.create(username='******',
                                password='******')
     user.save()
     calendar = Calendar(name='Test Calendar')
     calendar.save()
     relation = calendar.create_relation(user)
     self.assertEqual(relation, None)
 def __create_event(self, start, end, calendar):
     calendar = Calendar(1)
     return Event.objects.create(
         subtitle=subtitle,
         start=start,
         end=end,
         calendar=calendar,
     )
 def save(self, *args, **kwargs):
     if not self.pk:
         if not hasattr(self, 'calendar'):
             if not self.slug:
                 self.slug = slugify(self.name)
             calendar = Calendar(name=self.name, slug=self.slug)
             calendar.save()
             setattr(self, 'calendar', calendar)
     super(Way, self).save(*args, **kwargs)
示例#22
0
 def test_occurrences_after_with_events_before_returns_empty(self):
     calendar = Calendar()
     calendar.save()
     start_after = timezone.now() + datetime.timedelta(days=-1)
     end_after = start_after + datetime.timedelta(hours=1)
     event = self.__create_event(start_after, end_after)
     calendar.events.add(event)
     occurrences = list(calendar.occurrences_after(timezone.now()))
     self.assertEqual(occurrences, [])
示例#23
0
    def test_event_get_ocurrence(self):

        cal = Calendar(name='MyCal')
        start = timezone.now() + datetime.timedelta(days=1)
        event = self.__create_event('Non recurring event test get_occurrence',
                                    start, start + datetime.timedelta(hours=1),
                                    cal)

        occurrence = event.get_occurrence(start)
        self.assertEquals(occurrence.start, start)
示例#24
0
 def test_occurrences_after_with_events_after_returns_events(self):
     calendar = Calendar()
     calendar.save()
     start_after = timezone.now() + datetime.timedelta(days=1)
     end_after = start_after + datetime.timedelta(hours=1)
     event = self.__create_event(start_after, end_after)
     calendar.events.add(event)
     occurrences = list(calendar.occurrences_after(timezone.now()))
     self.assertEquals(len(occurrences), 1)
     self.assertEquals(occurrences[0].start, start_after)
     self.assertEquals(occurrences[0].end, end_after)
示例#25
0
    def post(self, request, *args, **kwargs):
        form = self.form_class(request.POST)
        if form.is_valid():
            self.user = form.save(commit=True)

            # create the calendar
            cal = Calendar(name='Nutriton Calendar ' + self.user.name,
                           slug='nutrition' + self.user.name.lower() +
                           str(self.user.pk))
            cal.save()
            # create events from prototypes
            b_event = Event.objects.get(pk=9)
            b_event.pk = None
            b_event.save()
            b_event.calendar = cal
            b_event.save()
            l_event = Event.objects.get(pk=12)
            l_event.pk = None
            l_event.save()
            l_event.calendar = cal
            l_event.save()
            d_event = Event.objects.get(pk=11)
            d_event.pk = None
            d_event.save()
            d_event.calendar = cal
            d_event.save()
            s1_event = Event.objects.get(pk=10)
            s1_event.pk = None
            s1_event.save()
            s1_event.calendar = cal
            s1_event.save()
            s2_event = Event.objects.get(pk=13)
            s2_event.pk = None
            s2_event.save()
            s2_event.calendar = cal
            s2_event.save()

            self.user.calendar = cal
            self.user.save()

            today = datetime.date.today()
            start = today - datetime.timedelta(days=today.weekday())
            start = datetime.datetime.combine(start, datetime.time(0, 0))
            end = start + datetime.timedelta(days=6)
            end = datetime.datetime.combine(end, datetime.time(23, 59))
            plan = self.user.create_meals_for_week(start=start, end=end)
            #self.user.plan = json.dumps(plan, default=dumper, indent=2)
            self.user.save()

            return HttpResponseRedirect(
                reverse('mealplan', kwargs={'pk': self.user.id}))
        else:
            return render(request, self.template_name, {'form': form})
示例#26
0
    def save_model(self, request, obj, form, change):
        if not hasattr(obj, 'host') and request.user.hosts.count() == 1:
            obj.host = request.user.hosts.all()[0]

        try:
            calendar = Calendar.objects.get(slug=obj.host.slug)
        except Calendar.DoesNotExist:
            calendar = Calendar(name=obj.host.name, slug=obj.host.slug)
            calendar.save()
        obj.calendar = calendar
        if not obj.creator:
            obj.creator = request.user
        super().save_model(request, obj, form, change)
示例#27
0
    def test_occurences_after_with_no_params(self):

        cal = Calendar(name='MyCal')
        start = timezone.now() + datetime.timedelta(days=1)
        event = self.__create_event('Non recurring event test get_occurrence',
                                    start, start + datetime.timedelta(hours=1),
                                    cal)

        occurrences = list(event.occurrences_after())
        self.assertEquals(len(occurrences), 1)
        self.assertEquals(occurrences[0].start, start)
        self.assertEquals(occurrences[0].end,
                          start + datetime.timedelta(hours=1))
示例#28
0
 def test_get_absolute(self):
     cal = Calendar(name='MyCal')
     cal.save()
     rule = Rule(frequency="DAILY")
     rule.save()
     start = timezone.now() + datetime.timedelta(days=1)
     event = self.__create_recurring_event(
         'Non recurring event test get_occurrence', start,
         start + datetime.timedelta(hours=1),
         start + datetime.timedelta(days=10), rule, cal)
     event.save()
     url = event.get_absolute_url()
     self.assertEqual(reverse('event', kwargs={'event_id': event.id}), url)
示例#29
0
    def test_occurences_with_recurrent_event_end_recurring_period_edge_case(
            self):

        cal = Calendar(name='MyCal')

        rule = Rule(frequency="DAILY")
        rule.save()
        start = timezone.now() + datetime.timedelta(days=1)
        event = self.__create_recurring_event(
            'Non recurring event test get_occurrence', start,
            start + datetime.timedelta(hours=1),
            start + datetime.timedelta(days=10), rule, cal)
        occurrences = list(event.occurrences_after())
        self.assertEquals(len(occurrences), 11)
示例#30
0
def optionnal_calendar(sender, **kwargs):
    event = kwargs.pop('instance')

    if not isinstance(event, Event):
        return True
    if not event.calendar:
        try:
            calendar = Calendar._default_manager.get(name='default')
        except Calendar.DoesNotExist:
            calendar = Calendar(name='default', slug='default')
            calendar.save()

        event.calendar = calendar
    return True