fix return_fields filtering
authorTony Mack <tmack@paris.CS.Princeton.EDU>
Sun, 10 Mar 2013 02:26:29 +0000 (21:26 -0500)
committerTony Mack <tmack@paris.CS.Princeton.EDU>
Sun, 10 Mar 2013 02:26:29 +0000 (21:26 -0500)
PLC/Persons.py
PLC/Storage/AlchemyObject.py
PLC/Storage/Record.py

index a990099..0e9c79c 100644 (file)
@@ -92,6 +92,23 @@ class Person(AlchemyObj):
         
         return email
 
+    def validate_password(self, password):
+        """
+        Encrypt password if necessary before committing to the
+        database.
+        """
+
+        magic = "$1$"
+
+        if len(password) > len(magic) and \
+           password[0:len(magic)] == magic:
+            return password
+        else:
+            # Generate a somewhat unique 8 character salt string
+            salt = str(time.time()) + str(Random().random())
+            salt = md5(salt).hexdigest()[:8]
+            return crypt.crypt(password.encode(self.api.encoding), magic + salt + "$")
+
     def can_update(self, person):
         """
         Returns true if we can update the specified person. We can
@@ -257,7 +274,7 @@ class Persons(list):
             raise PLCInvalidArgument, "Wrong person filter %r"%person_filter
 
         for person in persons:
-            person = Person(self.api, object=person) 
+            person = Person(self.api, object=person, columns=columns
             keystone_user = self.api.client_shell.keystone.users.find(id=person['keystone_id'])
             if not columns or 'site_ids' in columns:
                 site_persons = SitePerson().select(filter={'person_id': person['person_id']})
index d99a227..ed9eca8 100644 (file)
@@ -16,8 +16,8 @@ from PLC.Logger import logger
  
 class AlchemyObj(Record):
 
-    def __init__(self, api=None, fields = {}, object=None):
-        Record.__init__(self, dict=fields, object=object)
+    def __init__(self, api=None, fields = {}, object=None, columns=None):
+        Record.__init__(self, dict=fields, object=object, columns=columns)
         self.api=api
 
     def __iter__(self):
index eca606e..347004e 100644 (file)
@@ -6,20 +6,25 @@ class Record(dict):
 
     fields = {}
     tags = {}
-    def __init__(self, dict=None, object=None):
+    def __init__(self, dict=None, object=None, columns=None):
         self.object = object 
         if dict:
+            if columns:
+                for column in columns:
+                    if dict.has_key(column):
+                        del dict[column]
             self.update(dict)
         if self.object:
-            self.update_fields()
+            self.update_fields(columns)
 
     def get_field(self, field):
         return self.__dict__.get(field, None)
 
-    def update_fields(self):
+    def update_fields(self, columns=None):
         for field in self.fields:
             if hasattr(self.object, field):
-                self[field] = getattr(self.object, field)
+                if not columns or field in columns:
+                    self[field] = getattr(self.object, field)
 
     # xxx fixme
     # turns out the date_created field is received by the client as a 'created' int