5 from PLC.Faults import *
6 from PLC.Parameter import Parameter
7 from PLC.Table import Row, Table
8 from PLC.Persons import Person, Persons
9 from PLC.Nodes import Node, Nodes
13 Representation of a row in the sessions table. To use, instantiate
14 with a dict of values.
17 table_name = 'sessions'
18 primary_key = 'session_id'
19 join_tables = ['person_session', 'node_session']
21 'session_id': Parameter(str, "Session key"),
22 'person_id': Parameter(int, "Account identifier, if applicable"),
23 'node_id': Parameter(int, "Node identifier, if applicable"),
24 'expires': Parameter(int, "Date and time when session expires, in seconds since UNIX epoch"),
27 def validate_expires(self, expires):
28 if expires < time.time():
29 raise PLCInvalidArgument, "Expiration date must be in the future"
31 return time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime(expires))
33 def add_person(self, person, commit = True):
35 Associate person with session.
38 assert 'session_id' in self
39 assert isinstance(person, Person)
40 assert 'person_id' in person
42 session_id = self['session_id']
43 person_id = person['person_id']
45 self.api.db.do("INSERT INTO person_session (session_id, person_id)" \
46 " VALUES(%(session_id)s, %(person_id)d)",
52 self['person_id'] = person_id
54 def add_node(self, node, commit = True):
56 Associate node with session.
59 assert 'session_id' in self
60 assert isinstance(node, Node)
61 assert 'node_id' in node
63 session_id = self['session_id']
64 node_id = node['node_id']
66 # Nodes can have only one session at a time
67 self.api.db.do("DELETE FROM node_session WHERE node_id = %(node_id)d",
70 self.api.db.do("INSERT INTO node_session (session_id, node_id)" \
71 " VALUES(%(session_id)s, %(node_id)d)",
77 self['node_id'] = node_id
79 def sync(self, commit = True, insert = None):
80 if not self.has_key('session_id'):
81 # Before a new session is added, delete expired sessions
82 expired = Sessions(self.api, expires = -int(time.time()))
83 for session in expired:
84 session.delete(commit)
86 # Generate 32 random bytes
87 bytes = random.sample(xrange(0, 256), 32)
88 # Base64 encode their string representation
89 self['session_id'] = base64.b64encode("".join(map(chr, bytes)))
93 Row.sync(self, commit, insert)
95 class Sessions(Table):
97 Representation of row(s) from the session table in the database.
100 def __init__(self, api, session_ids = None, expires = int(time.time())):
101 Table.__init__(self, api, Session)
103 sql = "SELECT %s FROM view_sessions WHERE True" % \
104 ", ".join(Session.fields)
107 sql += " AND session_id IN (%s)" % ", ".join(map(api.db.quote, session_ids))
109 if expires is not None:
111 sql += " AND expires > %(expires)d"
114 sql += " AND expires < %(expires)d"
116 self.selectall(sql, locals())