cosmetic changes in sendmail.py - please the linter
[plcapi.git] / PLC / Sessions.py
index 3c1543e..720565d 100644 (file)
@@ -4,6 +4,8 @@ import time
 
 from PLC.Faults import *
 from PLC.Parameter import Parameter
 
 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
 from PLC.Table import Row, Table
 from PLC.Persons import Person, Persons
 from PLC.Nodes import Node, Nodes
@@ -26,7 +28,7 @@ class Session(Row):
 
     def validate_expires(self, expires):
         if expires < time.time():
 
     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))
 
 
         return time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime(expires))
 
@@ -41,16 +43,16 @@ class Session(Row):
         add(self, node, commit = commit)
 
     def sync(self, commit = True, insert = None):
         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
             # 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
             # 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
 
             # Force insert
             insert = True
 
@@ -61,14 +63,30 @@ class Sessions(Table):
     Representation of row(s) from the session table in the database.
     """
 
     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)
 
 
         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:
 
         if expires is not None:
             if expires >= 0: