# Calendar overlaying
# -------------------

# SchoolTool lets users overlay calendars of other users and groups, and see all
# calendar events in one view.  When you are logged in, and you look at your own
# calendar, you will see a new portlet on the left:

#     >>> frog.getLink('Calendar').click()
#     >>> print frog.contents
#     <BLANKLINE>
#     ...
#       <div id="portlet-calendar-overlay" class="portlet">
#     <BLANKLINE>
#       <h4>Calendars</h4>
#     <BLANKLINE>
#     ...
#             <td style="width: 100%">My Calendar</td>
#     ...
#           <input name="OVERLAY_APPLY" type="submit"
#                  value="Apply" />
#           <input name="OVERLAY_MORE" type="submit"
#                  value="More..." />
#     ...
#       </form>
#     </div>
#     ...

# Initially it contains only your own calendar.  You can click on "Manage..."
# and add other calendars to this list.

#     >>> frog.getControl('More...').click()

#     >>> print frog.contents
#     <BLANKLINE>
#     ...
#         <fieldset class="inline">
#           <legend>Resources</legend>
#           <p>
#               There are no resources with calendars accessible to you.
#             </p>
#     <BLANKLINE>
#         </fieldset>
#     ...

# Let's allow frog to see all the group calendars:

#     >>> manager.open('http://localhost/access_control.html')
#     >>> manager.getControl('Everyone can view group calendars').click()
#     >>> manager.getControl('Apply').click()

# Reload the overlay list, and there we have all the groups:

#     >>> frog.reload()
#     >>> print frog.contents
#     <BLANKLINE>
#     ...
#         <fieldset class="inline">
#           <legend>Groups</legend>
#     <BLANKLINE>
#           <select size="8" multiple="multiple" id="groups"
#                   name="groups:list">
#             <option value="animals">Animals</option>
#             <option value="clerks">Clerks</option>
#             <option value="administrators">School Administrators</option>
#             <option value="manager">Site Managers</option>
#             <option value="students">Students</option>
#             <option value="teachers">Teachers</option>
#           </select>
#         </fieldset>
#     ...

#     >>> frog.getControl('Animals').click()
#     >>> frog.getControl('Clerks').click()
#     >>> frog.getControl('Apply').click()

# Now, you can see many calendars in the overlay portlet: animals,
# other built-in groups and the application calendar

#     >>> frog.getLink('Calendar').click()
#     >>> 'Animals' in frog.contents
#     True
#     >>> print analyze.queryHTML('//td[text()="Your School"]', frog.contents)[0]
#     <td style="width: 100%">Your School</td>

# Let's remove the application calendar from the overlay

#     >>> frog.getControl('More...').click()

#     >>> frog.getControl('Your School site-wide calendar').click()
#     >>> frog.getControl('Apply').click()

# Now the application calendar is no longer in the overlay portlet

#     >>> frog.getLink('Calendar').click()
#     >>> analyze.queryHTML('//td[text()="Your School"]', frog.contents)
#     []

# If we restrict permissions on the application calendar, it will no longer be
# available in the overlay selection view:

# Demonstrate that it is there now:

#     >>> frog.getControl('More...').click()
#     >>> frog.getControl('Your School site-wide calendar')
#     <ItemControl name='application' type='checkbox' optionValue='application' selected=False>

# Restrict permissions on the application calendar

#     >>> manager.open('http://localhost/access_control.html')
#     >>> manager.getControl('Everyone can view the application calendar').click()
#     >>> manager.getControl('Apply').click()

# And now it's gone

#     >>> frog.reload()
#     >>> frog.getControl('Your School site-wide calendar')
#     Traceback (most recent call last):
#     ...
#     LookupError: label 'Your School site-wide calendar'

# Regression test for Issue243 (Calendars with identical titles are
# throwing system error):

#     >>> frog.getLink('Calendar').click()
#     >>> len(analyze.queryHTML('//td[text()="Clerks"]', frog.contents))
#     1

# Rename the group so its title would be Managers:

#     >>> manager.open('http://localhost')
#     >>> manager.getLink('Manage').click()
#     >>> manager.getLink('Groups').click()
#     >>> manager.getLink('Animals').click()
#     >>> manager.getLink('Edit Info').click()

#     >>> manager.getControl('Title').value = 'Clerks'
#     >>> manager.getControl('Apply').click()

# Calendars with identical titles will get sorted in random order so I
# am just checking that they are there:

#     >>> frog.reload()
#     >>> len(analyze.queryHTML('//td[text()="Clerks"]', frog.contents))
#     2

# Rename the group back to Animals:

#     >>> manager.getLink('Edit Info').click()
#     >>> manager.getControl('Title').value = 'Animals'
#     >>> manager.getControl('Apply').click()

# Now, if there are events in the animals' and toad's calendars, you can
# see them:

#     >>> manager.getLink('Manage').click()
#     >>> manager.getLink('Groups').click()
#     >>> manager.getLink('Clerks').click()
#     >>> manager.getLink('View Calendar').click()
#     >>> manager.getLink('New Event').click()

#     >>> manager.getControl('Title').value = 'Eat flies'
#     >>> manager.getControl('Date').value = '2005-02-03'
#     >>> manager.getControl('Time').value = '09:00'
#     >>> manager.getControl('Duration').value = '30'
#     >>> manager.getControl('Add').click()

#     >>> manager.getLink('Manage').click()
#     >>> manager.getLink('Groups').click()
#     >>> manager.getLink('Animals').click()
#     >>> manager.getLink('View Calendar').click()
#     >>> manager.getLink('New Event').click()

#     >>> manager.getControl('Title').value = 'Daily meeting'
#     >>> manager.getControl('Date').value = '2005-02-03'
#     >>> manager.getControl('Time').value = '10:00'
#     >>> manager.getControl('Duration').value = '30'
#     >>> manager.getControl('Add').click()

#     >>> frog.open('http://localhost/persons/frog/calendar/2005-02-03')
#     >>> 'Eat flies' in frog.contents
#     True
#     >>> 'Daily meeting' in frog.contents
#     True

# If the permissions to overlaid calendars are revoked, you can still
# see your calendar, albeit without overlaid events:

#     >>> manager.open('http://localhost/access_control.html')
#     >>> manager.getControl('Everyone can view group calendars').click()
#     >>> manager.getControl('Apply').click()

#     >>> frog.open('http://localhost/persons/frog/calendar/2005-02-03')
#     >>> frog.headers['status']
#     '200 Ok'

#     >>> 'Eat flies' in frog.contents
#     False
#     >>> 'Daily meeting' in frog.contents
#     False


# The Tomorrow's events box
# -------------------------

# First of all let's set the date:

#     >>> from datetime import date
#     >>> today = date(2005, 5, 13)
#     >>> manager.open('http://localhost/time')
#     >>> manager.getControl('Today').value = "2005-05-13"
#     >>> manager.getControl('Apply').click()

# And calculate tomorrow:

#     >>> tomorrow = today + date.resolution

# Now, let's add an event which will take place tomorrow:

#     >>> frog.getLink('Calendar').click()
#     >>> frog.getLink('New Event').click()

#     >>> frog.getControl('Title').value = 'Important meeting'
#     >>> frog.getControl('Date').value = str(tomorrow)
#     >>> frog.getControl('Time').value = '12:00'
#     >>> frog.getControl('Duration').value = '60'
#     >>> frog.getControl('Add').click()

# We should see it in the calendar for today:

#     >>> frog.getLink('Calendar').click()
#     >>> print analyze.queryHTML('id("portlet-next-day-events")', frog.contents)[0]
#     <div id="portlet-next-day-events" class="portlet">
#           <h4>Tomorrow's Events</h4>
#           <ul><li>
#               <a href="http://localhost/persons/frog/calendar/..." title="Important meeting" style="color: #7590ae">
#                 <strong>12:00</strong>
#                 Important meeti...
#               </a>
#             </li>
#     </ul></div>

# Now, let's take a look at the daily calendar for an arbitrary day.  It
# should have the event we have just created in the Tomorrow's events box.

#     >>> frog.open('http://localhost/persons/frog/calendar/2005-01-01')
#     >>> print frog.contents
#     <BLANKLINE>
#     ...
#          <div id="portlet-next-day-events" class="portlet">
#            <h4>Tomorrow's Events</h4>
#            <ul>
#     <BLANKLINE>
#              <li>
#                <a href="...
#                   title="Important meeting"
#                   style="color: #7590ae">
#                  <strong>12:00</strong>
#                  Important meeti...
#                </a>
#              </li>
#     <BLANKLINE>
#            </ul>
#          </div>
#     ...


# A Sunbird weekly recurrence workaround
# ======================================

# It seems that Mozilla Calendar does not include the weekday of the
# first occurence of the event to the BYDAY list of weekdays the event
# recurs on.  So, in the recurrence editing form, we must always
# consider the weekday of the first recurrence of the event to be on, by
# adding a hidden input field beside a disabled checkbox:

# Let's add an event on 2005-01-01 (Saturday) that recurs on Tuesdays,
# too.  Notice we don't mark Saturday in the BYDAY list:

#     >>> print http(r"""
#     ... PUT /persons/frog/calendar/calendar.ics HTTP/1.1
#     ... Host: localhost:7080
#     ... Authorization: Basic frog:pwd
#     ... Content-Type: text/calendar
#     ...
#     ... BEGIN:VCALENDAR
#     ... VERSION:2.0
#     ... PRODID:-//SchoolTool.org/NONSGML SchoolTool//EN
#     ... BEGIN:VEVENT
#     ... UID:important@event
#     ... SUMMARY:Important event
#     ... DTSTART:20050101T100000
#     ... DURATION:PT1H
#     ... DTSTAMP:20050203T150000
#     ... RRULE:FREQ=WEEKLY;BYDAY=TU;INTERVAL=1
#     ... END:VEVENT
#     ... END:VCALENDAR
#     ... """, handle_errors=False)
#     HTTP/1.1 200 Ok
#     Content-Length: 0
#     Set-Cookie: ...
#     <BLANKLINE>

# Now, let's get this event and see its RRULE:

#     >>> frog.open('http://localhost/'
#     ...           'persons/frog/calendar/calendar.ics')
#     >>> print frog.contents
#     <BLANKLINE>
#     ...
#     SUMMARY:Important event
#     RRULE:FREQ=WEEKLY;BYDAY=TU,SA;INTERVAL=1
#     ...
