summaryrefslogtreecommitdiff
path: root/tools/mmd/MmdSession.py
blob: d5bf96e6e6fc31ac69e26cbdb3986d447cf72853 (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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
"""
Session class for MURSAT1 Mission Dashboard
"""
from cgi import parse_qs, escape
from MmdDb import Db
from MmdUser import User
from MmdSatellite import Satellite
import time

# session timeout set to 2 days
session_timeout = 3600 * 24 * 2
# every 10 minutes we get a new session id and 
# reset the cookie in the user's browser
renewal_timeout = 600

class Session:

  def __init__ (self, session_id):
    self.session_id = session_id
    self.status = 'anon'
    self.headers = []
    self.db = Db ()
    self.user = User ()
    self.satellite = Satellite ()
    self.message = ''
    s = self.db.sessionFind (session_id)
    if not s:
      now = int (time.strftime ('%s'))
      self.expires = now + session_timeout
      self.renewal = now + renewal_timeout
      self.db.sessionInit (self.session_id, self.user.email, self.expires, self.renewal, self.status)
    else:
      self.user.load (s['email'])
      self.status = s['status']
      self.expires = int (s['expires'])
      self.renewal = int (s['renewal'])

  def addHeader (self, header):
    self.headers.append (header)

  def renew (self, token):
    now = int (time.strftime ('%s'))
    self.expires = now + session_timeout
    if self.renewal > now:
      self.db.sessionUpdate (self.session_id, self.expires)
      return False
    self.renewal = now + renewal_timeout
    self.db.sessionRenew (self.session_id, self.expires, self.renewal, token)
    self.session_id = token
    return True

  def setStatus (self, status = 'anon'):
    self.status = status
    self.db.sessionSetStatus (self.session_id, self.status)
    self.db.sessionSetEmail (self.session_id, self.user.email)

  def valid (self):
    if self.expires < int (time.strftime ('%s')):
      return False
    return True

  def delete (self):
    self.db.sessionDelete (self.session_id)

  # session methods to extract strings from QueryString
  def getQsCmd (self):
    return self.getField ('cmd')

  def getQsFirstname (self):
    return self.getField ('firstname')

  def getQsLastname (self):
    return  self.getField ('lastname')

  def getQsEmail (self):
    return self.getField ('email')

  def getQsCallsign (self):
    return self.getField ('callsign')

  def getQsLongitude (self):
    return self.getField ('longitude')

  def getQsLatitude (self):
    return self.getField ('latitude')

  def getQsPassword (self):
    return self.getField ('password')

  def getQsConfirm (self):
    return self.getField ('confirm')

  def getQsCode (self):
    return self.getField ('code')

  def getQsLocationName (self):
    return self.getField ('location_name')

  def getQsAltitude (self):
    return self.getField ('altitude')

  def getField (self, field):
    try:
      return escape (self.qs.get (field)[0])
    except TypeError:
      return ''
  
if __name__ == "__main__":
  pass

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