def get_occurrences(self, start, end): """ >>> rule = Rule(frequency="MONTHLY", name="Monthly") >>> rule.save() >>> event = Event(rule=rule, start=datetime.datetime(2008,1,1,tzinfo=pytz.utc), end=datetime.datetime(2008,1,2)) >>> event.rule <Rule: Monthly> >>> occurrences = event.get_occurrences(datetime.datetime(2008,1,24), datetime.datetime(2008,3,2)) >>> ["%s to %s" %(o.start, o.end) for o in occurrences] ['2008-02-01 00:00:00+00:00 to 2008-02-02 00:00:00+00:00', '2008-03-01 00:00:00+00:00 to 2008-03-02 00:00:00+00:00'] Ensure that if an event has no rule, that it appears only once. >>> event = Event(start=datetime.datetime(2008,1,1,8,0), end=datetime.datetime(2008,1,1,9,0)) >>> occurrences = event.get_occurrences(datetime.datetime(2013,1,24), datetime.datetime(2014,3,2)) >>> ["%s to %s" %(o.start, o.end) for o in occurrences] [] """ persisted_occurrences = self.occurrence_set.all() occ_replacer = OccurrenceReplacer(persisted_occurrences) occurrences = self._get_occurrence_list(start, end) final_occurrences = [] for occ in occurrences: # replace occurrences with their persisted counterparts if occ_replacer.has_occurrence(occ): p_occ = occ_replacer.get_occurrence(occ) # ...but only if they are within this period if p_occ.start < end and p_occ.end >= start: final_occurrences.append(p_occ) else: final_occurrences.append(occ) # then add persisted occurrences which originated outside of this period but now # fall within it final_occurrences += occ_replacer.get_additional_occurrences(start, end) return final_occurrences
def occurrences_after(self, after=None): """ returns a generator that produces occurrences after the datetime ``after``. Includes all of the persisted Occurrences. """ occ_replacer = OccurrenceReplacer(self.occurrence_set.all()) generator = self._occurrences_after_generator(after) while True: next = generator.next() yield occ_replacer.get_occurrence(next)
def get_occurrences(self, start, end): """ >>> rule = Rule(frequency="MONTHLY", name="Monthly") >>> rule.save() >>> event = Event(rule=rule, start=datetime.datetime(2008,1,1,tzinfo=pytz.utc), end=datetime.datetime(2008,1,2)) >>> event.rule <Rule: Monthly> >>> occurrences = event.get_occurrences(datetime.datetime(2008,1,24), datetime.datetime(2008,3,2)) >>> ["%s to %s" %(o.start, o.end) for o in occurrences] ['2008-02-01 00:00:00+00:00 to 2008-02-02 00:00:00+00:00', '2008-03-01 00:00:00+00:00 to 2008-03-02 00:00:00+00:00'] Ensure that if an event has no rule, that it appears only once. >>> event = Event(start=datetime.datetime(2008,1,1,8,0), end=datetime.datetime(2008,1,1,9,0)) >>> occurrences = event.get_occurrences(datetime.datetime(2013,1,24), datetime.datetime(2014,3,2)) >>> ["%s to %s" %(o.start, o.end) for o in occurrences] [] """ persisted_occurrences = self.occurrence_set.all() occ_replacer = OccurrenceReplacer(persisted_occurrences) occurrences = self._get_occurrence_list(start, end) final_occurrences = [] for occ in occurrences: # replace occurrences with their persisted counterparts if occ_replacer.has_occurrence(occ): p_occ = occ_replacer.get_occurrence(occ) # ...but only if they are within this period if p_occ.start < end and p_occ.end >= start: final_occurrences.append(p_occ) else: final_occurrences.append(occ) # then add persisted occurrences which originated outside of this period but now # fall within it final_occurrences += occ_replacer.get_additional_occurrences( start, end) return final_occurrences