Unicode array elements are always cast to regular Python strings; always cast strings...
[plcapi.git] / PLC / Persons.py
index 195879b..39f5252 100644 (file)
@@ -4,7 +4,7 @@
 # Mark Huang <mlhuang@cs.princeton.edu>
 # Copyright (C) 2006 The Trustees of Princeton University
 #
-# $Id: Persons.py,v 1.8 2006/10/03 19:25:55 mlhuang Exp $
+# $Id: Persons.py,v 1.15 2006/10/27 15:32:56 mlhuang Exp $
 #
 
 from types import StringTypes
@@ -19,8 +19,6 @@ from PLC.Faults import *
 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
 
@@ -37,15 +35,15 @@ class Person(Row):
         'person_id': Parameter(int, "Account identifier"),
         'first_name': Parameter(str, "Given name", max = 128),
         'last_name': Parameter(str, "Surname", max = 128),
-        'title': Parameter(str, "Title", max = 128),
+        'title': Parameter(str, "Title", max = 128, nullok = True),
         'email': Parameter(str, "Primary e-mail address", max = 254),
-        'phone': Parameter(str, "Telephone number", max = 64),
-        'url': Parameter(str, "Home page", max = 254),
-        'bio': Parameter(str, "Biography", max = 254),
+        'phone': Parameter(str, "Telephone number", max = 64, nullok = True),
+        'url': Parameter(str, "Home page", max = 254, nullok = True),
+        'bio': Parameter(str, "Biography", max = 254, nullok = True),
         'enabled': Parameter(bool, "Has been enabled"),
         'password': Parameter(str, "Account password in crypt() form", max = 254),
-        'last_updated': Parameter(str, "Date and time of last update", ro = True),
-        'date_created': Parameter(str, "Date and time when account was created", ro = True),
+        'last_updated': Parameter(int, "Date and time of last update", ro = True),
+        'date_created': Parameter(int, "Date and time when account was created", ro = True),
         'role_ids': Parameter([int], "List of role identifiers", ro = True),
         'roles': Parameter([str], "List of roles", ro = True),
         'site_ids': Parameter([int], "List of site identifiers", ro = True),
@@ -53,10 +51,6 @@ class Person(Row):
         'slice_ids': Parameter([int], "List of slice identifiers", ro = True),
         }
 
-    def __init__(self, api, fields = {}):
-        Row.__init__(self, fields)
-        self.api = api
-
     def validate_email(self, email):
         """
         Validate email address. Stolen from Mailman.
@@ -195,6 +189,51 @@ class Person(Row):
                 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):
         """
@@ -236,7 +275,7 @@ class Person(Row):
             key.delete(commit = False)
 
         # Clean up miscellaneous join tables
-        for table in ['person_role', 'person_site', 'slice_person']:
+        for table in ['person_role', 'person_site', 'slice_person', 'person_session']:
             self.api.db.do("DELETE FROM %s" \
                            " WHERE person_id = %d" % \
                            (table, self['person_id']))