Esempio n. 1
0
    def __call__(self):
        app = ISchoolToolApplication(None)
        person = IPerson(self.request.principal, None)
        if not person:
            raise Unauthorized("Only logged in users can book resources.")
        cal = ISchoolToolCalendar(person)
        if self.request.has_key('event_id'):
            event = cal.find(self.request['event_id'])
        else:
            start_date = self.request.get('start_date')
            start_time = self.request.get('start_time')
            title = self.request.get('title')
            start_datetime = "%s %s" % (start_date, start_time)
            start_datetime = datetime(*strptime(start_datetime,
                                                "%Y-%m-%d %H:%M")[0:6])
            start_datetime = self.timezone.localize(start_datetime)
            start_datetime = start_datetime.astimezone(pytz.UTC)
            duration = timedelta(seconds=int(self.request.get('duration')))
            event = CalendarEvent(dtstart = start_datetime,
                                  duration = duration,
                                  title = title)
            cal.addEvent(event)

        if event:
            resource = app["resources"].get(self.request['resource_id'])
            if resource is not None:
                resource_calendar = ISchoolToolCalendar(resource)
                if not canAccess(resource_calendar, "addEvent"):
                    raise Unauthorized("You don't have the right to"
                                       " book this resource!")
                event.bookResource(resource)
        self.request.response.redirect(self.nextURL(event))
Esempio n. 2
0
 def generate(self, app, seed=None):
     self.random = PortableRandom(seed)
     event_titles = self._readLines('event_titles.txt')
     person_ids = [person for person in app['persons'].keys()
                   if person.startswith('student') or
                      person.startswith('teacher')]
     dates = []
     for term in ITermContainer(app).values():
         dates.append(term.first)
         dates.append(term.last)
     first = min(dates)
     last = max(dates)
     days = DateRange(first, last)
     for person_id in person_ids:
         person = app['persons'][person_id]
         calendar = ISchoolToolCalendar(person)
         for day in days:
             if self.random.randrange(0, 100) < self.probability:
                 event_title = self.random.choice(event_titles)
                 time_hour = self.random.randint(6, 23)
                 time_min = self.random.choice((0, 30))
                 event_time = datetime.datetime(day.year,
                                                day.month,
                                                day.day,
                                                time_hour,
                                                time_min,
                                                tzinfo=utc)
                 event_duration = datetime.timedelta(
                                    minutes=self.random.randint(1, 12)*30)
                 event = CalendarEvent(event_time,
                                       event_duration,
                                       event_title)
                 calendar.addEvent(event)
Esempio n. 3
0
 def generate(self, app, seed=None):
     self.random = PortableRandom(seed)
     event_titles = self._readLines('event_titles.txt')
     person_ids = [person for person in app['persons'].keys()
                   if person.startswith('student') or
                      person.startswith('teacher')]
     dates = []
     for term in ITermContainer(app).values():
         dates.append(term.first)
         dates.append(term.last)
     first = min(dates)
     last = max(dates)
     days = DateRange(first, last)
     for person_id in person_ids:
         person = app['persons'][person_id]
         calendar = ISchoolToolCalendar(person)
         for day in days:
             if self.random.randrange(0, 100) < self.probability:
                 event_title = self.random.choice(event_titles)
                 time_hour = self.random.randint(6, 23)
                 time_min = self.random.choice((0, 30))
                 event_time = datetime.datetime(day.year,
                                                day.month,
                                                day.day,
                                                time_hour,
                                                time_min,
                                                tzinfo=utc)
                 event_duration = datetime.timedelta(
                                    minutes=self.random.randint(1, 12)*30)
                 event = CalendarEvent(event_time,
                                       event_duration,
                                       event_title)
                 calendar.addEvent(event)
Esempio n. 4
0
 def bookResource(self, resource):
     calendar = ISchoolToolCalendar(resource)
     if resource in self.resources:
         raise ValueError("resource already booked")
     if calendar is self.__parent__:
         raise ValueError("cannot book itself")
     self._resources += (resource,)
     if self.__parent__ is not None:
         calendar.addEvent(self)
Esempio n. 5
0
 def bookResource(self, resource):
     calendar = ISchoolToolCalendar(resource)
     if resource in self.resources:
         raise ValueError('resource already booked')
     if calendar is self.__parent__:
         raise ValueError('cannot book itself')
     self._resources += (resource, )
     if self.__parent__ is not None:
         calendar.addEvent(self)
Esempio n. 6
0
def create_user_and_recurrent_events(app):
    """Create a user with some recurrent events in his calendar.

    The user's username will be 'manager', and his calendar will have
    exactly four events, recurring daily, weekly, monthly, and yearly
    respectively.  All these events start on the same day.
    """
    person = Person('manager', 'Manager')
    app['persons']['manager'] = person
    calendar = ISchoolToolCalendar(person)

    recurrence = DailyRecurrenceRule()
    daily_event = CalendarEvent(datetime(2005, 1, 1, 10, 0),
                                timedelta(60),
                                'Daily Event',
                                recurrence=recurrence)
    calendar.addEvent(daily_event)

    recurrence = WeeklyRecurrenceRule(weekdays=(0, 1, 2, 3, 4, 5, 6))
    weekly_event = CalendarEvent(datetime(2005, 1, 1, 11, 0),
                                 timedelta(60),
                                 'Weekly event',
                                 recurrence=recurrence)
    calendar.addEvent(weekly_event)

    recurrence = MonthlyRecurrenceRule()
    monthly_event = CalendarEvent(datetime(2005, 1, 1, 12, 0),
                                  timedelta(60),
                                  'Monthly event',
                                  recurrence=recurrence)
    calendar.addEvent(monthly_event)

    recurrence = YearlyRecurrenceRule()
    yearly_event = CalendarEvent(datetime(2005, 1, 1, 13, 0),
                                 timedelta(60),
                                 'Yearly event',
                                 recurrence=recurrence)
    calendar.addEvent(yearly_event)
Esempio n. 7
0
def create_user_and_recurrent_events(app):
    """Create a user with some recurrent events in his calendar.

    The user's username will be 'manager', and his calendar will have
    exactly four events, recurring daily, weekly, monthly, and yearly
    respectively.  All these events start on the same day.
    """
    person = Person('manager', 'Manager')
    app['persons']['manager'] = person
    calendar = ISchoolToolCalendar(person)

    recurrence = DailyRecurrenceRule()
    daily_event = CalendarEvent(datetime(2005, 1, 1, 10, 0),
                                timedelta(60),
                                'Daily Event',
                                recurrence=recurrence)
    calendar.addEvent(daily_event)

    recurrence = WeeklyRecurrenceRule(weekdays=(0, 1, 2, 3, 4, 5, 6))
    weekly_event = CalendarEvent(datetime(2005, 1, 1, 11, 0),
                                timedelta(60),
                                'Weekly event',
                                recurrence=recurrence)
    calendar.addEvent(weekly_event)

    recurrence = MonthlyRecurrenceRule()
    monthly_event = CalendarEvent(datetime(2005, 1, 1, 12, 0),
                                  timedelta(60),
                                  'Monthly event',
                                  recurrence=recurrence)
    calendar.addEvent(monthly_event)

    recurrence = YearlyRecurrenceRule()
    yearly_event = CalendarEvent(datetime(2005, 1, 1, 13, 0),
                                 timedelta(60),
                                 'Yearly event',
                                 recurrence=recurrence)
    calendar.addEvent(yearly_event)
Esempio n. 8
0
    def update(self):
        self.template = self.date_template
        if 'CANCEL' in self.request:
            self.request.response.redirect(
                absoluteURL(self.context, self.request))
            return
        if 'date' in self.request:
            try:
                self.date = parse_date(self.request['date'])
            except ValueError:
                self.error = _("The date you entered is invalid."
                               "  Please use the YYYY-MM-DD format.")
                return
            if not self.date in self.context:
                self.error = _("The date you entered does not belong to"
                               " this term.")
                return
            if not self.context.isSchoolday(self.date):
                self.error = _("The date you entered is not a schoolday.")
                return

            self.template = self.replacement_template
        if 'replacement' in self.request:
            try:
                self.replacement = parse_date(self.request['replacement'])
            except ValueError:
                self.error = _("The replacement date you entered is invalid.")
                self.template = self.replacement_template
                return

        if self.date and self.replacement:
            if self.context.last < self.replacement:
                # XXX: I wonder if all places that are dependent on
                #      term start/end dates are updated properly
                self.context.last = self.replacement

            # XXX: assert?!
            assert not self.context.isSchoolday(self.replacement)
            assert self.context.isSchoolday(self.date)
            self.context.add(self.replacement)

            notify(EmergencyDayEvent(self.date, self.replacement))

            # XXX: Following code should be move to the event subscriber, but!
            #      It wants to store translated messages, and in current default
            #      case the event description will be stored in the language
            #      from somebodys browser settings.  Now that's naughty!
            #      If only we had 'apllication language' separate form 'user
            #      presentation language' this problem would go away.
            #
            # Post calendar events to schoolwide calendar
            calendar = ISchoolToolCalendar(ISchoolToolApplication(None))
            dtstart = datetime.datetime.combine(self.date, datetime.time())
            msg = _('School cancelled due to emergency.'
                    ' Replacement day $replacement.',
                    mapping={'replacement': str(self.replacement)})
            msg = translate(msg, context=self.request)
            calendar.addEvent(
                CalendarEvent(dtstart, datetime.timedelta(),
                              msg, allday=True))

            dtstart = datetime.datetime.combine(self.replacement,
                                                datetime.time())
            msg = _('Replacement day for emergency day $emergency.',
                    mapping={'emergency': str(self.date)})
            msg = translate(msg, context=self.request)
            calendar.addEvent(
                CalendarEvent(dtstart, datetime.timedelta(),
                              msg, allday=True))

            self.request.response.redirect(
                absoluteURL(self.context, self.request))