- make Add() calling convention consistent among all functions that
[plcapi.git] / PLC / Methods / UpdatePerson.py
index 5e84f7d..66392fe 100644 (file)
@@ -4,9 +4,14 @@ from PLC.Parameter import Parameter, Mixed
 from PLC.Persons import Person, Persons
 from PLC.Auth import PasswordAuth
 
+can_update = lambda (field, value): field in \
+             ['first_name', 'last_name', 'title', 'email',
+              'password', 'phone', 'url', 'bio', 'accepted_aup',
+              'enabled']
+
 class UpdatePerson(Method):
     """
-    Updates a person. Only the fields specified in update_fields are
+    Updates a person. Only the fields specified in person_fields are
     updated, all other fields are left untouched.
 
     To remove a value without setting a new one in its place (for
@@ -22,31 +27,21 @@ class UpdatePerson(Method):
 
     roles = ['admin', 'pi', 'user', 'tech']
 
-    can_update = lambda (field, value): field in \
-                 ['first_name', 'last_name', 'title', 'email',
-                  'password', 'phone', 'url', 'bio', 'accepted_aup',
-                 'enabled']
-    update_fields = dict(filter(can_update, Person.fields.items()))
+    person_fields = dict(filter(can_update, Person.fields.items()))
+    for field in person_fields.values():
+        field.optional = True
 
     accepts = [
         PasswordAuth(),
         Mixed(Person.fields['person_id'],
               Person.fields['email']),
-        update_fields
+        person_fields
         ]
 
     returns = Parameter(int, '1 if successful')
 
-    def call(self, auth, person_id_or_email, update_fields):
-        valid_fields = self.update_fields.keys()
-
-       # Remove admin only fields
-       if 'admin' not in self.caller['roles']:
-            for key in ['enabled']:
-                valid_fields.remove(key)
-
-       if filter(lambda field: field not in valid_fields, update_fields):
-            raise PLCInvalidArgument, "Invalid field specified"
+    def call(self, auth, person_id_or_email, person_fields):
+        person_fields = dict(filter(can_update, person_fields.items()))
 
         # Get account information
         persons = Persons(self.api, [person_id_or_email])
@@ -62,7 +57,7 @@ class UpdatePerson(Method):
         if not self.caller.can_update(person):
             raise PLCPermissionDenied, "Not allowed to update specified account"
 
-        person.update(update_fields)
+        person.update(person_fields)
         person.sync()
 
         return 1