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))
- def add_person(self, person, commit = True):
- """
- Associate person with session.
- """
-
- assert 'session_id' in self
- assert isinstance(person, Person)
- assert 'person_id' in person
-
- session_id = self['session_id']
- person_id = person['person_id']
-
- self.api.db.do("INSERT INTO person_session (session_id, person_id)" \
- " VALUES(%(session_id)s, %(person_id)d)",
- locals())
-
- if commit:
- self.api.db.commit()
-
- self['person_id'] = person_id
+ add_person = Row.add_object(Person, 'person_session')
def add_node(self, node, commit = True):
- """
- Associate node with session.
- """
-
- assert 'session_id' in self
- assert isinstance(node, Node)
- assert 'node_id' in node
-
- session_id = self['session_id']
- node_id = node['node_id']
-
# Nodes can have only one session at a time
- self.api.db.do("DELETE FROM node_session WHERE node_id = %(node_id)d",
- locals())
-
- self.api.db.do("INSERT INTO node_session (session_id, node_id)" \
- " VALUES(%(session_id)s, %(node_id)d)",
- locals())
+ self.api.db.do("DELETE FROM node_session WHERE node_id = %d" % \
+ node['node_id'])
- if commit:
- self.api.db.commit()
-
- self['node_id'] = node_id
+ 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: