def on_date(self, *args): first_week = Date.first_date_of_year(self.date.year, self.date.isoweekday()).isocalendar()[1] last_week = Date.last_date_of_year(self.date.year, self.date.isoweekday()).isocalendar()[1] min_week = first_week if first_week <= 2 else 0 # 0 is for last week of preceding year max_week = Date.last_date_of_year(self.date.year, 4).isocalendar()[1] + 1 if last_week == 1 else last_week isoyear, isoweek, _ = self.date.isocalendar() if isoyear < self.date.year: isoweek = 0 elif isoyear > self.date.year: isoweek = max_week self.cycle = max_week - min_week + 1 self.offset = min_week self.selected_value = isoweek - self.offset
def on_year(self, *args): self.max_year_week = Date.last_date_of_year(self.year, 4).isocalendar()[1] self.max_prev_year_week = Date.last_date_of_year(self.year - 1, 4).isocalendar()[1]
def on_selected_value(self, *args): if self.date is not None: week = self.selected_value - (0 if self.zero_indexed else 1) self.date = (Date.first_date_of_year(self.date.year, self.date.isoweekday()) + datetime.timedelta(days=7 * week))