paul@69 | 1 | #!/usr/bin/env python |
paul@69 | 2 | |
paul@69 | 3 | import cgi, os, sys |
paul@69 | 4 | |
paul@69 | 5 | sys.path.append("/var/lib/imip-agent") |
paul@69 | 6 | |
paul@73 | 7 | from imiptools.content import format_datetime, get_datetime, get_item, \ |
paul@73 | 8 | get_utc_datetime, get_values, parse_object, \ |
paul@73 | 9 | to_timezone |
paul@73 | 10 | from imiptools.period import have_conflict |
paul@69 | 11 | import markup |
paul@69 | 12 | import imip_store |
paul@69 | 13 | |
paul@69 | 14 | getenv = os.environ.get |
paul@69 | 15 | setenv = os.environ.__setitem__ |
paul@69 | 16 | |
paul@69 | 17 | class CGIEnvironment: |
paul@69 | 18 | |
paul@69 | 19 | "A CGI-compatible environment." |
paul@69 | 20 | |
paul@69 | 21 | def __init__(self): |
paul@69 | 22 | self.args = None |
paul@69 | 23 | self.method = None |
paul@69 | 24 | self.path = None |
paul@69 | 25 | self.path_info = None |
paul@69 | 26 | self.user = None |
paul@69 | 27 | |
paul@69 | 28 | def get_args(self): |
paul@69 | 29 | if self.args is None: |
paul@69 | 30 | if self.get_method() != "POST": |
paul@69 | 31 | setenv("QUERY_STRING", "") |
paul@69 | 32 | self.args = cgi.parse(keep_blank_values=True) |
paul@69 | 33 | return self.args |
paul@69 | 34 | |
paul@69 | 35 | def get_method(self): |
paul@69 | 36 | if self.method is None: |
paul@69 | 37 | self.method = getenv("REQUEST_METHOD") or "GET" |
paul@69 | 38 | return self.method |
paul@69 | 39 | |
paul@69 | 40 | def get_path(self): |
paul@69 | 41 | if self.path is None: |
paul@69 | 42 | self.path = getenv("SCRIPT_NAME") or "" |
paul@69 | 43 | return self.path |
paul@69 | 44 | |
paul@69 | 45 | def get_path_info(self): |
paul@69 | 46 | if self.path_info is None: |
paul@69 | 47 | self.path_info = getenv("PATH_INFO") or "" |
paul@69 | 48 | return self.path_info |
paul@69 | 49 | |
paul@69 | 50 | def get_user(self): |
paul@69 | 51 | if self.user is None: |
paul@69 | 52 | self.user = getenv("REMOTE_USER") or "" |
paul@69 | 53 | return self.user |
paul@69 | 54 | |
paul@69 | 55 | def get_output(self): |
paul@69 | 56 | return sys.stdout |
paul@69 | 57 | |
paul@69 | 58 | def get_url(self): |
paul@69 | 59 | path = self.get_path() |
paul@69 | 60 | path_info = self.get_path_info() |
paul@69 | 61 | return "%s%s" % (path.rstrip("/"), path_info) |
paul@69 | 62 | |
paul@69 | 63 | class Manager: |
paul@69 | 64 | |
paul@69 | 65 | "A simple manager application." |
paul@69 | 66 | |
paul@69 | 67 | def __init__(self): |
paul@69 | 68 | self.store = imip_store.FileStore() |
paul@69 | 69 | self.env = CGIEnvironment() |
paul@69 | 70 | user = self.env.get_user() |
paul@69 | 71 | self.user = user and "mailto:%s" % user or None |
paul@69 | 72 | self.out = self.env.get_output() |
paul@69 | 73 | self.page = markup.page() |
paul@69 | 74 | self.encoding = "utf-8" |
paul@69 | 75 | |
paul@69 | 76 | def new_page(self, title): |
paul@69 | 77 | self.page.init(title=title, charset=self.encoding) |
paul@69 | 78 | |
paul@69 | 79 | def status(self, code, message): |
paul@69 | 80 | print >>self.out, "Status:", code, message |
paul@69 | 81 | |
paul@69 | 82 | def no_user(self): |
paul@69 | 83 | self.status(403, "Forbidden") |
paul@69 | 84 | self.new_page(title="Forbidden") |
paul@69 | 85 | self.page.p("You are not logged in and thus cannot access scheduling requests.") |
paul@69 | 86 | |
paul@70 | 87 | def no_page(self): |
paul@70 | 88 | self.status(404, "Not Found") |
paul@70 | 89 | self.new_page(title="Not Found") |
paul@70 | 90 | self.page.p("No page is provided at the given address.") |
paul@70 | 91 | |
paul@69 | 92 | def show_requests(self): |
paul@69 | 93 | |
paul@69 | 94 | "Show requests for the current user." |
paul@69 | 95 | |
paul@69 | 96 | # NOTE: This list could be more informative, but it is envisaged that |
paul@69 | 97 | # NOTE: the requests would be visited directly anyway. |
paul@69 | 98 | |
paul@69 | 99 | self.new_page(title="Pending Requests") |
paul@69 | 100 | self.page.ul() |
paul@69 | 101 | |
paul@69 | 102 | requests = self.store.get_requests(self.user) |
paul@70 | 103 | |
paul@69 | 104 | for request in requests: |
paul@69 | 105 | self.page.li() |
paul@69 | 106 | self.page.a(request, href="%s/%s" % (self.env.get_url(), request)) |
paul@69 | 107 | self.page.li.close() |
paul@69 | 108 | |
paul@69 | 109 | self.page.ul.close() |
paul@69 | 110 | |
paul@70 | 111 | def show_request(self, path_info): |
paul@70 | 112 | |
paul@70 | 113 | "Show a request using the given 'path_info' for the current user." |
paul@70 | 114 | |
paul@70 | 115 | uid = path_info.lstrip("/").split("/", 1)[0] |
paul@70 | 116 | f = uid and self.store.get_event(self.user, uid) or None |
paul@70 | 117 | |
paul@70 | 118 | if not f: |
paul@70 | 119 | return False |
paul@70 | 120 | |
paul@70 | 121 | request = parse_object(f, "utf-8") |
paul@70 | 122 | |
paul@70 | 123 | if not request: |
paul@70 | 124 | return False |
paul@70 | 125 | |
paul@73 | 126 | # Handle a submitted form. |
paul@73 | 127 | |
paul@73 | 128 | args = self.env.get_args() |
paul@73 | 129 | show_form = False |
paul@73 | 130 | |
paul@73 | 131 | if args.has_key("accept"): |
paul@73 | 132 | pass |
paul@73 | 133 | elif args.has_key("decline"): |
paul@73 | 134 | pass |
paul@73 | 135 | elif args.has_key("ignore"): |
paul@73 | 136 | pass |
paul@73 | 137 | else: |
paul@73 | 138 | show_form = True |
paul@73 | 139 | |
paul@70 | 140 | self.new_page(title="Request") |
paul@73 | 141 | |
paul@73 | 142 | # Provide a summary of the request. |
paul@73 | 143 | |
paul@70 | 144 | self.page.p("The following request was received:") |
paul@70 | 145 | self.page.dl() |
paul@70 | 146 | |
paul@73 | 147 | for name in ["SUMMARY", "DTSTART", "DTEND", "ORGANIZER", "ATTENDEE"]: |
paul@70 | 148 | for value in get_values(request, name): |
paul@70 | 149 | self.page.dt(name) |
paul@70 | 150 | self.page.dd(value) |
paul@70 | 151 | |
paul@70 | 152 | self.page.dl.close() |
paul@73 | 153 | |
paul@73 | 154 | dtstart = format_datetime(get_utc_datetime(request, "DTSTART")) |
paul@73 | 155 | dtend = format_datetime(get_utc_datetime(request, "DTEND")) |
paul@73 | 156 | |
paul@73 | 157 | # Indicate whether there are conflicting events. |
paul@73 | 158 | |
paul@73 | 159 | freebusy = self.store.get_freebusy(self.user) |
paul@73 | 160 | if freebusy: |
paul@73 | 161 | |
paul@73 | 162 | # Obtain any time zone details from the suggested event. |
paul@73 | 163 | |
paul@73 | 164 | _dtstart, attr = get_item(request, "DTSTART") |
paul@73 | 165 | tzid = attr.get("TZID") |
paul@73 | 166 | |
paul@73 | 167 | # Show any conflicts. |
paul@73 | 168 | |
paul@73 | 169 | for start, end, uid in have_conflict(freebusy, [(dtstart, dtend)], True): |
paul@73 | 170 | start = format_datetime(to_timezone(get_datetime(start), tzid)) |
paul@73 | 171 | end = format_datetime(to_timezone(get_datetime(end), tzid)) |
paul@73 | 172 | self.page.p("Event conflicts with another from %s to %s." % (start, end)) |
paul@73 | 173 | |
paul@73 | 174 | # Show a form if no action has just been taken. |
paul@73 | 175 | |
paul@73 | 176 | if show_form: |
paul@73 | 177 | self.page.p("Action to take for this request:") |
paul@73 | 178 | self.page.form(method="POST") |
paul@73 | 179 | self.page.p() |
paul@73 | 180 | self.page.input(name="accept", type="submit", value="Accept") |
paul@73 | 181 | self.page.add(" ") |
paul@73 | 182 | self.page.input(name="decline", type="submit", value="Decline") |
paul@73 | 183 | self.page.add(" ") |
paul@73 | 184 | self.page.input(name="ignore", type="submit", value="Ignore") |
paul@73 | 185 | self.page.p.close() |
paul@73 | 186 | self.page.form.close() |
paul@73 | 187 | |
paul@70 | 188 | return True |
paul@70 | 189 | |
paul@69 | 190 | def select_action(self): |
paul@69 | 191 | |
paul@69 | 192 | "Select the desired action and show the result." |
paul@69 | 193 | |
paul@69 | 194 | path_info = self.env.get_path_info().rstrip("/") |
paul@69 | 195 | if not path_info: |
paul@69 | 196 | self.show_requests() |
paul@70 | 197 | elif self.show_request(path_info): |
paul@70 | 198 | pass |
paul@70 | 199 | else: |
paul@70 | 200 | self.no_page() |
paul@69 | 201 | |
paul@69 | 202 | def show(self): |
paul@69 | 203 | |
paul@69 | 204 | "Interpret a request and show an appropriate response." |
paul@69 | 205 | |
paul@69 | 206 | if not self.user: |
paul@69 | 207 | self.no_user() |
paul@69 | 208 | else: |
paul@69 | 209 | self.select_action() |
paul@69 | 210 | |
paul@70 | 211 | # Write the headers and actual content. |
paul@70 | 212 | |
paul@69 | 213 | print >>self.out, "Content-Type: text/html; charset=%s" % self.encoding |
paul@69 | 214 | print >>self.out |
paul@69 | 215 | self.out.write(unicode(self.page).encode(self.encoding)) |
paul@69 | 216 | |
paul@69 | 217 | if __name__ == "__main__": |
paul@69 | 218 | Manager().show() |
paul@69 | 219 | |
paul@69 | 220 | # vim: tabstop=4 expandtab shiftwidth=4 |