def test_unknown_week_start_day_raises_value_error(self):
     try:
         datetimes.termweek_to_date(2012,
                                    "michaelmas",
                                    1,
                                    "thu",
                                    week_start="fsd")
         self.fail("\"fsd\" is not a day")
     except ValueError:
         pass
Exemple #2
0
    def get_week_range(self, year, term, week, day):
        week_start_date = termweek_to_date(year, term, week, day)
        week_end_date = week_start_date + relativedelta(weeks=1)

        return ("{} {} week {}".format(year, term, week),
                self.get_timestamp(week_start_date),
                self.get_timestamp(week_end_date))
Exemple #3
0
    def _save_extra_fields(self, event):
        """
        Saves our custom form fields in the event.
        """

        term = self.cleaned_data["term_name"]
        week = self.cleaned_data["term_week"]
        day = self.cleaned_data["day_of_week"]

        year = int(settings.DEFAULT_ACADEMIC_YEAR)
        date = datetimes.termweek_to_date(year, term, week, day)
        
        start_hour = self.cleaned_data["start_hour"]
        start_minute = self.cleaned_data["start_minute"]
        end_hour = self.cleaned_data["end_hour"]
        end_minute = self.cleaned_data["end_minute"]

        tz = timezone.get_current_timezone()

        start_naive = datetime.datetime(date.year, date.month, date.day,
                start_hour, start_minute)
        event.start = tz.localize(start_naive)

        end_naive = datetime.datetime(date.year, date.month, date.day,
                end_hour, end_minute)
        event.end = tz.localize(end_naive)

        event.metadata["people"] = self.cleaned_data["people"]
        event.metadata["type"] =  self.cleaned_data["event_type"]

        if self.cleaned_data["cancel"] is True:
            event.status = models.Event.STATUS_CANCELLED
        else:
            event.status = models.Event.STATUS_LIVE
Exemple #4
0
    def move_datetime(self, dt):
        year, term, week, day = datetimes.date_to_termweek(dt.date())

        assert year == self.from_year, (
            "Moved datetimes must be in the from_year")

        new_date = datetimes.termweek_to_date(self.to_year, term, week, day)
        return datetime.datetime.combine(new_date, dt.timetz())
    def test_expected_values(self):
        expectations = [
            ((2012, "michaelmas", 0, "thu"), datetime.date(2012, 9, 27)),
            ((2012, "michaelmas", 1, "thu"), datetime.date(2012, 10, 4)),
            ((2012, "michaelmas", 1, "fri"), datetime.date(2012, 10, 5)),
            ((2012, "michaelmas", 1, "wed"), datetime.date(2012, 10, 10)),
            ((2012, "michaelmas", 2, "thu"), datetime.date(2012, 10, 11)),
            ((2012, "michaelmas", 2, "mon"), datetime.date(2012, 10, 15)),
            ((2012, "michaelmas", -1, "tue"), datetime.date(2012, 9, 25)),
        ]

        for args, expected in expectations:
            actual = datetimes.termweek_to_date(*args)

            self.assertEqual(
                expected, actual, "expected: %s, actual: %s, "
                "args: %s" % (expected, actual, args))
    def for_year(cls, year):
        from timetables.utils.datetimes import termweek_to_date

        if not year in TERM_STARTS:
            raise ValueError("Unknown year: %s. Expected one of: %s" %
                             (year, TERM_STARTS.keys()))

        start_boundary = date(year, YEAR_BOUNDARIES["START"]["MONTH"],
                              YEAR_BOUNDARIES["START"]["DAY"])

        end_boundary = date(year + 1, YEAR_BOUNDARIES["END"]["MONTH"],
                            YEAR_BOUNDARIES["END"]["DAY"])

        terms = []
        for term_name in TERMS:
            start_date = termweek_to_date(year, term_name, 1, TERM_START_DAY)
            terms.append(Term(term_name, start_date))

        return cls(year, start_boundary, end_boundary, terms)
    def test_default_week_start_is_thursday(self):
        actual = datetimes.termweek_to_date(2012, "michaelmas", 1, "thu")

        self.assertTrue(actual.weekday() == 3,
                        "The first day of term should be Thursday.")
 def test_unknown_day_raises_value_error(self):
     try:
         datetimes.termweek_to_date(2012, "michaelmas", 1, "acksdfs")
         self.fail("\"acksdfs\" is not a day")
     except ValueError:
         pass
 def test_unknown_month_raises_value_error(self):
     try:
         datetimes.termweek_to_date(2012, "blah", 1, "mon")
         self.fail("\"blah\" is not a term")
     except ValueError:
         pass
 def test_unknown_year_raises_value_error(self):
     try:
         datetimes.termweek_to_date(1066, "michaelmas", 1, "mon")
         self.fail("1066 should not exist as a year")
     except ValueError:
         pass
 def calculate_date(self, year_num, term_name, week_num):
     return datetimes.termweek_to_date(year_num, term_name, week_num,
             self.WEEK_START).isoformat()