summaryrefslogtreecommitdiff
path: root/tools/mmd/mmd.wsgi
blob: 4292b467cd07b03d5a35f24fc18a23998189b768 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#!/usr/bin/env python

"""
Check http://code.google.com/p/modwsgi/wiki/QuickConfigurationGuide
for server configuration guidelines
"""

import sys
from cgi import escape
from urlparse import parse_qs
from mmd.MmdCommands import *
from mmd.MmdSession import Session
import Cookie

def application (environ, response):
  """
  beginning of WSGI/python application for
  MURSAT1 Dashboard
  """

  # session and cookie stuff
  cookie = Cookie.SimpleCookie ()
  try:
    cookie.load (environ['HTTP_COOKIE'])
    session = Session (cookie['mmd'].value)
    if not session.valid ():
      session.delete ()
      session = Session (environ['UNIQUE_ID'])
      cookie['mmd'] = session.session_id
      session.addHeader (('Set-Cookie', cookie.output (header = '').strip ()))
  except KeyError:
    # no cookie, so we make a new session
    session = Session (environ['UNIQUE_ID'])
    cookie['mmd'] = session.session_id
    session.addHeader (('Set-Cookie', cookie.output (header = '').strip ()))

  try:
    session.qs = parse_qs (environ['wsgi.input'].read (int (environ['CONTENT_LENGTH'])), True)
  except KeyError:
    session.qs = parse_qs (environ['QUERY_STRING'], True)

  if session.renew (environ['UNIQUE_ID']):
    cookie['mmd'] = environ['UNIQUE_ID']
    session.addHeader (('Set-Cookie', cookie.output (header = '').strip ()))
  session.ip = environ['REMOTE_ADDR']
  session.http_status = '200 OK'
  
  # evaluate cmd
  cmd = session.getQsCmd ()
  content_type = 'text/html'

  if cmd == 'loginForm':
    body = loginForm (session)
  elif cmd == 'login':
    body = login (session)
  elif cmd == 'logout':
    body = logout (session)
  elif cmd == 'registerForm':
    body = registerForm (session)
  elif cmd == 'register':
    body = register (session)
  elif cmd == 'submitForm':
    body = submitForm (session)
  elif cmd == 'submit':
    body = submit (session)
  elif cmd == 'viewlog':
    body = viewlog (session)
  elif cmd == 'confirmRegistration':
    body = confirmRegistration (session)
  elif cmd == 'completeRegistration':
    body = completeRegistration (session)
  elif cmd == 'cancelRegistration':
    body = cancelRegistration (session)
  elif cmd == 'mmdCredits':
    body = mmdCredits (session)
  elif cmd == 'mmdtest':
    body = mmdtest (environ, cookie)
    content_type = 'text/plain'
  else:
    body = index (session)

  session.addHeader (('Content-Type', content_type))
  session.addHeader (('Content-Length', str (len (body))))
  response (session.http_status, session.headers)

  return [body]

# vim: tw=0 ts=2 expandtab
# EOF