1 from types import StringTypes
6 from PLC.Faults import *
7 from PLC.Parameter import Parameter
8 from PLC.Debug import profile
9 from PLC.Storage.AlchemyObject import AlchemyObj
10 from PLC.Persons import Person, Persons
11 from PLC.Nodes import Node, Nodes
13 class Session(AlchemyObj):
15 Representation of a row in the sessions table. To use, instantiate
16 with a dict of values.
19 tablename = 'sessions'
20 join_tables = ['person_session', 'node_session']
22 'session_id': Parameter(str, "Session key", primary_key=True),
23 'person_id': Parameter(int, "Account identifier, if applicable"),
24 'node_id': Parameter(int, "Node identifier, if applicable"),
25 'expires': Parameter(int, "Date and time when session expires, in seconds since UNIX epoch"),
28 def validate_expires(self, expires):
29 if expires < time.time():
30 raise PLCInvalidArgument, "Expiration date must be in the future"
32 return time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime(expires))
34 #add_person = Row.add_object(Person, 'person_session')
36 def sync(self, commit = True, insert = None, validate=True):
37 AlchemyObj.sync(commit=commit, validate=validate)
38 if not self.has_key('session_id'):
39 # Before a new session is added, delete expired sessions
40 expired = Sessions(self.api, expires = -int(time.time()))
41 for session in expired:
42 session.delete(commit)
44 # Generate 32 random bytes
45 bytes = random.sample(xrange(0, 256), 32)
46 # Base64 encode their string representation
47 self['session_id'] = base64.b64encode("".join(map(chr, bytes)))
49 AlchemyObj.insert(self, dict(self))
53 Representation of row(s) from the session table in the database.
56 def __init__(self, api, session_filter = None, expires = int(time.time())):
57 if session_filter is not None:
58 sessions = Session().select()
59 if isinstance(session_filter, (list, tuple, set)):
60 # Separate the list into integers and strings
61 ints = filter(lambda x: isinstance(x, (int, long)), session_filter)
62 strs = filter(lambda x: isinstance(x, StringTypes), session_filter)
63 session_filter = {'person_id': ints, 'session_id': strs}
64 sessions = Session().select(filter=session_filter)
65 elif isinstance(session_filter, dict):
66 sessions = Session().select(filter=session_filter)
67 elif isinstance(session_filter, (int, long)):
68 sessions = Session().select(filter={'person_id': session_filter})
69 elif isinstance(session_filter, StringTypes):
70 sessions = Session().select(filter={'session_id': session_filter})
72 raise PLCInvalidArgument, "Wrong session filter"%session_filter
74 if expires is not None:
76 sql += " AND expires > %(expires)d"
79 sql += " AND expires < %(expires)d"