# Mark Huang <mlhuang@cs.princeton.edu>
# Copyright (C) 2006 The Trustees of Princeton University
#
-# $Id: Persons.py,v 1.6 2006/10/02 16:04:22 mlhuang Exp $
+# $Id: Persons.py,v 1.10 2006/10/10 21:51:35 mlhuang Exp $
#
from types import StringTypes
from PLC.Parameter import Parameter
from PLC.Debug import profile
from PLC.Table import Row, Table
-from PLC.Roles import Roles
-from PLC.Addresses import Address, Addresses
from PLC.Keys import Key, Keys
import PLC.Sites
dict. Commit to the database with sync().
"""
+ table_name = 'persons'
+ primary_key = 'person_id'
fields = {
'person_id': Parameter(int, "Account identifier"),
'first_name': Parameter(str, "Given name", max = 128),
'slice_ids': Parameter([int], "List of slice identifiers", ro = True),
}
- def __init__(self, api, fields):
+ def __init__(self, api, fields = {}):
Row.__init__(self, fields)
self.api = api
assert 'person_id' in self
person_id = self['person_id']
- self.api.db.do("INSERT INTO person_role (person_id, role_id)" \
- " VALUES(%(person_id)d, %(role_id)d)",
- locals())
-
- if commit:
- self.api.db.commit()
- assert 'role_ids' in self
if role_id not in self['role_ids']:
+ self.api.db.do("INSERT INTO person_role (person_id, role_id)" \
+ " VALUES(%(person_id)d, %(role_id)d)",
+ locals())
+
+ if commit:
+ self.api.db.commit()
+
self['role_ids'].append(role_id)
def remove_role(self, role_id, commit = True):
assert 'person_id' in self
person_id = self['person_id']
- self.api.db.do("DELETE FROM person_role" \
- " WHERE person_id = %(person_id)d" \
- " AND role_id = %(role_id)d",
- locals())
-
- if commit:
- self.api.db.commit()
- assert 'role_ids' in self
if role_id in self['role_ids']:
+ self.api.db.do("DELETE FROM person_role" \
+ " WHERE person_id = %(person_id)d" \
+ " AND role_id = %(role_id)d",
+ locals())
+
+ if commit:
+ self.api.db.commit()
+
self['role_ids'].remove(role_id)
+
+ def add_key(self, key, commit = True):
+ """
+ Add key to existing account.
+ """
+
+ assert 'person_id' in self
+ assert isinstance(key, Key)
+ assert 'key_id' in key
+
+ person_id = self['person_id']
+ key_id = key['key_id']
+
+ if key_id not in self['key_ids']:
+ self.api.db.do("INSERT INTO person_key (person_id, key_id)" \
+ " VALUES(%(person_id)d, %(key_id)d)",
+ locals())
+
+ if commit:
+ self.api.db.commit()
+
+ self['key_ids'].append(key_id)
+
+ def remove_key(self, key, commit = True):
+ """
+ Remove key from existing account.
+ """
+
+ assert 'person_id' in self
+ assert isinstance(key, Key)
+ assert 'key_id' in key
+
+ person_id = self['person_id']
+ key_id = key['key_id']
+
+ if key_id in self['key_ids']:
+ self.api.db.do("DELETE FROM person_key" \
+ " WHERE person_id = %(person_id)d" \
+ " AND key_id = %(key_id)d",
+ locals())
+
+ if commit:
+ self.api.db.commit()
+
+ self['key_ids'].remove(key_id)
def set_primary_site(self, site, commit = True):
"""
self['site_ids'].remove(site_id)
self['site_ids'].insert(0, site_id)
- def sync(self, commit = True):
- """
- Commit changes back to the database.
- """
-
- self.validate()
-
- # Fetch a new person_id if necessary
- if 'person_id' not in self:
- rows = self.api.db.selectall("SELECT NEXTVAL('persons_person_id_seq') AS person_id")
- if not rows:
- raise PLCDBError, "Unable to fetch new person_id"
- self['person_id'] = rows[0]['person_id']
- insert = True
- else:
- insert = False
-
- # Filter out fields that cannot be set or updated directly
- persons_fields = self.api.db.fields('persons')
- fields = dict(filter(lambda (key, value): \
- key in persons_fields and \
- (key not in self.fields or not self.fields[key].ro),
- self.items()))
-
- # Parameterize for safety
- keys = fields.keys()
- values = [self.api.db.param(key, value) for (key, value) in fields.items()]
-
- if insert:
- # Insert new row in persons table
- sql = "INSERT INTO persons (%s) VALUES (%s)" % \
- (", ".join(keys), ", ".join(values))
- else:
- # Update existing row in persons table
- columns = ["%s = %s" % (key, value) for (key, value) in zip(keys, values)]
- sql = "UPDATE persons SET " + \
- ", ".join(columns) + \
- " WHERE person_id = %(person_id)d"
-
- self.api.db.do(sql, fields)
-
- if commit:
- self.api.db.commit()
-
def delete(self, commit = True):
"""
Delete existing account.
non-deleted accounts.
"""
- def __init__(self, api, person_id_or_email_list = None, fields = Person.fields, enabled = None):
+ def __init__(self, api, person_id_or_email_list = None, enabled = None):
self.api = api
sql = "SELECT %s FROM view_persons WHERE deleted IS False" % \
- ", ".join(fields)
+ ", ".join(Person.fields)
if enabled is not None:
sql += " AND enabled IS %(enabled)s"