X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=PLC%2FSessions.py;h=33fd3b1ce607e32e257335952288fc5abccbba96;hb=c6009814e9fd3b53be36ac5f8c32058ba7465f13;hp=305e55bdffa7141b3fdf6728b025f520c530df10;hpb=e347fc823bbba9d88a3fddf07d5c21024dfd1e55;p=plcapi.git diff --git a/PLC/Sessions.py b/PLC/Sessions.py index 305e55b..33fd3b1 100644 --- a/PLC/Sessions.py +++ b/PLC/Sessions.py @@ -1,9 +1,14 @@ +# $Id$ +# $URL$ +from types import StringTypes import random import base64 import time 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 @@ -30,51 +35,15 @@ class Session(Row): 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()) - - if commit: - self.api.db.commit() + self.api.db.do("DELETE FROM node_session WHERE node_id = %d" % \ + node['node_id']) - 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'): @@ -97,14 +66,22 @@ class Sessions(Table): Representation of row(s) from the session table in the database. """ - def __init__(self, api, session_ids = None, expires = int(time.time())): + 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 = filter(lambda x: isinstance(x, (int, long)), session_filter) + strs = filter(lambda x: isinstance(x, StringTypes), session_filter) + 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") if expires is not None: if expires >= 0: