Пример #1
0
 def test_returning_a_generator(self):
     g = utils.options()
     self.assertEqual(g.next(), 1)
     self.assertEqual(g.next(), 2)
     self.assertEqual(g.next(), 4)
     self.assertEqual(g.next(), 8)
     self.assertEqual(g.next(), 16)
     self.assertEqual(g.next(), 32)
Пример #2
0
 def test_returning_a_generator(self):
     g = utils.options()
     self.assertEqual(g.next(), 1)
     self.assertEqual(g.next(), 2)
     self.assertEqual(g.next(), 4)
     self.assertEqual(g.next(), 8)
     self.assertEqual(g.next(), 16)
     self.assertEqual(g.next(), 32)
Пример #3
0
 def test_returning_a_list(self):
     g = utils.options(6)
     self.assertEqual(g, [1, 2, 4, 8, 16, 32])
Пример #4
0
 def test_returning_a_list(self):
     g = utils.options(6)
     self.assertEqual(g, [1, 2, 4, 8, 16, 32])
Пример #5
0
class Period(models.Model):
    """Represents a time period that sections are held for during the week.

    For particular details about a section, refer to the SectionPeriod model.
    """
    start = models.TimeField(default=None, null=True)
    end = models.TimeField(default=None, null=True)

    MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY = options(7)
    DAYS_OF_WEEK = (
        (MONDAY, 'Monday'),
        (TUESDAY, 'Tuesday'),
        (WEDNESDAY, 'Wednesday'),
        (THURSDAY, 'Thursday'),
        (FRIDAY, 'Friday'),
        (SATURDAY, 'Saturday'),
        (SUNDAY, 'Sunday'),
    )

    #objects = managers.QuerySetManager()

    days_of_week_flag = models.IntegerField()

    class Meta:
        unique_together = ('start', 'end', 'days_of_week_flag')

    def __unicode__(self):
        return u"%s to %s on %s" % (self.start, self.end, ', '.join(
            self.days_of_week))

    def toJSON(self, select_related=()):
        return {
            'start_time': self.start,
            'end_time': self.end,
            'days_of_the_week': self.days_of_week,
            #'is_to_be_announced': self.is_to_be_announced,
        }

    @property
    def is_to_be_announced(self):
        return None in (self.start, self.end)

    def conflicts_with(self, period):
        "Returns True if the given period conflicts the current one."
        if self == period:
            return True
        days = self.days_of_week_flag & period.days_of_week_flag
        return days and (
            (self.start <= period.start <= self.end) or \
            (self.start <= period.end <= self.end) or \
            (period.start <= self.start <= period.end) or \
            (period.start <= self.end <= period.end)
        )

    def is_on_day(self, day):
        return self.days_of_week_flag & day

    @property
    def days_of_the_week(self):
        return ', '.join(self.days_of_week)

    @property
    def days_of_week(self):
        "Returns a tuple of days of the week (str)."
        days = []
        for value, name in self.DAYS_OF_WEEK:
            if self.is_on_day(value):
                days.append(name)
        return days

    @days_of_week.setter
    def days_of_week(self, days):
        "Sets the days of the week using a tuple of strings or MONDAY-SUNDAY constants."
        dow = dict((n, v) for v, n in self.DAYS_OF_WEEK)
        value = 0
        for name in set(days):
            if type(name) in (str, unicode):
                value += dow.get(capitalized(name), 0)
            else:
                value += int(name) or 0
        self.days_of_week_flag = value

    def to_tuple(self):
        return (self.start, self.end, self.days_of_week_flag)