from PLC.Faults import *
from PLC.Parameter import Parameter
+from PLC.Filter import Filter
+from PLC.Debug import profile
from PLC.Table import Row, Table
from PLC.Persons import Person, Persons
from PLC.Nodes import Node, Nodes
def validate_expires(self, expires):
if expires < time.time():
- raise PLCInvalidArgument, "Expiration date must be in the future"
+ raise PLCInvalidArgument("Expiration date must be in the future")
return time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime(expires))
self.api.db.do("DELETE FROM node_session WHERE node_id = %d" % \
node['node_id'])
- add = Row.add_object(Node, 'person_session')
- add(self, node, commit)
+ add = Row.add_object(Node, 'node_session')
+ add(self, node, commit = commit)
def sync(self, commit = True, insert = None):
- if not self.has_key('session_id'):
+ if 'session_id' not in self:
# Before a new session is added, delete expired sessions
expired = Sessions(self.api, expires = -int(time.time()))
for session in expired:
session.delete(commit)
# Generate 32 random bytes
- bytes = random.sample(xrange(0, 256), 32)
+ int8s = random.sample(range(0, 256), 32)
# Base64 encode their string representation
- self['session_id'] = base64.b64encode("".join(map(chr, bytes)))
+ self['session_id'] = base64.b64encode(bytes(int8s)).decode()
# Force insert
insert = True
Representation of row(s) from the session table in the database.
"""
- def __init__(self, api, session_ids = None, expires = int(time.time())):
- Table.__init__(self, api, Session)
+ def __init__(self, api, session_filter = None, expires = int(time.time())):
+ Table.__init__(self, api, Session)
sql = "SELECT %s FROM view_sessions WHERE True" % \
", ".join(Session.fields)
- if session_ids:
- sql += " AND session_id IN (%s)" % ", ".join(map(api.db.quote, session_ids))
+ if session_filter is not None:
+ if isinstance(session_filter, (list, tuple, set)):
+ # Separate the list into integers and strings
+ ints = [x for x in session_filter if isinstance(x, int)]
+ strs = [x for x in session_filter if isinstance(x, str)]
+ session_filter = Filter(Session.fields, {'person_id': ints, 'session_id': strs})
+ sql += " AND (%s) %s" % session_filter.sql(api, "OR")
+ elif isinstance(session_filter, dict):
+ session_filter = Filter(Session.fields, session_filter)
+ sql += " AND (%s) %s" % session_filter.sql(api, "AND")
+ elif isinstance(session_filter, int):
+ session_filter = Filter(Session.fields, {'person_id': session_filter})
+ sql += " AND (%s) %s" % session_filter.sql(api, "AND")
+ elif isinstance(session_filter, str):
+ session_filter = Filter(Session.fields, {'session_id': session_filter})
+ sql += " AND (%s) %s" % session_filter.sql(api, "AND")
+ else:
+ raise PLCInvalidArgument("Wrong session filter"%session_filter)
if expires is not None:
if expires >= 0: