geholimportapp / coursecalendar.py

import os
import urlparse
from google.appengine.ext import webapp
from google.appengine.ext.webapp import template
from status import is_status_down, get_last_status_update
from geholwrapper import get_calendar, rebuild_course_gehol_url
from utils import is_course_mnemo_valid, render_course_notfound_page
from savedrequests import PreviousRequest
from gehol.utils import convert_weekspan_to_dates

def rebuild_gehol_url(group_id):
    return "http://164.15.72.157:8080/Reporting/Individual;Student%20Set%20Groups;id;"+group_id+"?&template=Ann%E9e%20d%27%E9tude&weeks=1-14&days=1-6&periods=5-33&width=0&height=0"



class CourseCalendar(webapp.RequestHandler):
    def get(self):
        parsed = urlparse.urlparse(self.request.uri)
        course_mnemo = self._get_course_mnemo(parsed.path)


        if is_course_mnemo_valid(course_mnemo):
            if is_status_down():
                self._render_gehol_down_page(course_mnemo)
            else:
                cal = get_calendar(course_mnemo)
                if cal:
                    ical_url, csv_url = self._build_file_urls(course_mnemo)
                    start, end = convert_weekspan_to_dates("1-36", "20/09/2010")
                    caption = "Schedule from %s to %s" % (start.strftime("%B %d, %Y"),
                                                          end.strftime("%B %d, %Y"))

                    template_values = {'gehol_is_down': is_status_down(),
                                     'last_status_update': get_last_status_update(),
                                    'mnemo':course_mnemo,
                                    'ical_url':ical_url,
                                    'csv_url':csv_url,
                                    'caption':caption,
                                    'gehol_url': rebuild_course_gehol_url(course_mnemo)
                    }

                    template_values.update(cal.metadata)

                    path = os.path.join(os.path.dirname(__file__), 'templates/course.html')
                    self.response.out.write(template.render(path, template_values))

                    request = PreviousRequest()
                    request.content = course_mnemo
                    request.put()
                else:
                    self._render_not_found_page(course_mnemo)
        else:
            self._render_not_found_page(course_mnemo)
            

    def _render_not_found_page(self, course_mnemo):
        render_course_notfound_page(self, course_mnemo, "summary page")


    def _render_gehol_down_page(self, course_mnemo):
        reason = "You asked the schedule for the following course : %s." % course_mnemo
        template_values = {'gehol_is_down': is_status_down(),
                           'last_status_update': get_last_status_update(),
                           'request':reason
        }

        path = os.path.join(os.path.dirname(__file__), 'templates/gehol_down.html')
        self.response.out.write(template.render(path, template_values))

    @staticmethod
    def _build_file_urls(course_mnemo):
        return ("/course/ical/%s.ics" % course_mnemo,
                "/course/csv/%s.csv" % course_mnemo)

    @staticmethod
    def _get_course_mnemo(path):
        return path.split('/')[2]
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.