persons & tags : allow tags to be passed to AddPerson, and update
authorThierry Parmentelat <thierry.parmentelat@inria.fr>
Tue, 11 Dec 2012 14:25:59 +0000 (15:25 +0100)
committerThierry Parmentelat <thierry.parmentelat@inria.fr>
Tue, 11 Dec 2012 14:25:59 +0000 (15:25 +0100)
'hrn' upon AddPersonToSite

PLC/Methods/AddPerson.py
PLC/Methods/AddPersonToSite.py

index 8840975..4ced974 100644 (file)
@@ -1,13 +1,17 @@
 from PLC.Faults import *
+from PLC.Auth import Auth
 from PLC.Method import Method
 from PLC.Parameter import Parameter, Mixed
+from PLC.Table import Row
 from PLC.Persons import Person, Persons
-from PLC.Auth import Auth
+from PLC.TagTypes import TagTypes
+from PLC.PersonTags import PersonTags, PersonTag
 
-can_update = lambda (field, value): field in \
-             ['first_name', 'last_name', 'title',
+can_update = ['first_name', 'last_name', 'title',
               'email', 'password', 'phone', 'url', 'bio']
 
+required=['email','first_name','last_name']
+
 class AddPerson(Method):
     """
     Adds a new account. Any fields specified in person_fields are
@@ -21,21 +25,58 @@ class AddPerson(Method):
 
     roles = ['admin', 'pi']
 
-    person_fields = dict(filter(can_update, Person.fields.items()))
+    accepted_fields = Row.accepted_fields(can_update,Person.fields)
+    accepted_fields.update(Person.tags)
 
     accepts = [
         Auth(),
-        person_fields
+        accepted_fields
         ]
 
     returns = Parameter(int, 'New person_id (> 0) if successful')
 
     def call(self, auth, person_fields):
-        person_fields = dict(filter(can_update, person_fields.items()))
-        person_fields['enabled'] = False
-        person = Person(self.api, person_fields)
+
+        # silently ignore 'enabled' if passed, for backward compat
+        # this is forced to False below anyways
+        if 'enabled' in person_fields: del person_fields['enabled']
+
+        [native,tags,rejected]=Row.split_fields(person_fields,[Person.fields,Person.tags])
+
+        # type checking
+        native = Row.check_fields(native, self.accepted_fields)
+        if rejected:
+            raise PLCInvalidArgument, "Cannot add Person with column(s) %r"%rejected
+
+        missing=[ r for r in required if r not in native ]
+        if missing:
+            raise PLCInvalidArgument, "Missing mandatory arguments %s to AddPerson"%missing
+
+        # handle native fields
+        native['enabled'] = False
+        person = Person(self.api, native)
         person.sync()
 
+        # handle tags
+        for (tagname,value) in tags.iteritems():
+            # the tagtype instance is assumed to exist, just check that
+            tag_types = TagTypes(self.api,{'tagname':tagname})
+            if not tag_types:
+                raise PLCInvalidArgument,"No such TagType %s"%tagname
+            tag_type = tag_types[0] 
+            person_tags=PersonTags(self.api,{'tagname':tagname,'person_id':person['person_id']})
+            if not person_tags:
+                person_tag = PersonTag(self.api)
+                person_tag['person_id'] = person['person_id']
+                person_tag['tag_type_id'] = tag_type['tag_type_id']
+                person_tag['tagname']  = tagname
+                person_tag['value'] = value
+                person_tag.sync()
+            else:
+                person_tag = person_tags[0]
+                person_tag['value'] = value
+                person_tag.sync() 
+
         # Logging variables
         self.event_objects = {'Person': [person['person_id']]}
         self.message = 'Person %d added' % person['person_id']
index 2144404..ce12ecb 100644 (file)
@@ -1,9 +1,12 @@
 from PLC.Faults import *
+from PLC.Auth import Auth
 from PLC.Method import Method
 from PLC.Parameter import Parameter, Mixed
-from PLC.Persons import Person, Persons
 from PLC.Sites import Site, Sites
-from PLC.Auth import Auth
+from PLC.Persons import Person, Persons
+from PLC.PersonTags import PersonTags, PersonTag
+from PLC.Namespace import email_to_hrn
+from PLC.TagTypes import TagTypes
 
 class AddPersonToSite(Method):
     """
@@ -54,4 +57,31 @@ class AddPersonToSite(Method):
         self.message = 'Person %d added to site %d' % \
                        (person['person_id'], site['site_id'])
 
+        # maintain person's hrn
+        # only if at this point we have a single site 
+        # which means, there was no site attached to person upon entering this call
+        try:
+            had_no_site= (len (person['site_ids']) == 0)
+            if had_no_site: 
+                login_base=site['login_base']
+                root_auth = self.api.config.PLC_HRN_ROOT
+                hrn=email_to_hrn("%s.%s"%(root_auth,login_base),person['email'])
+                tagname='hrn'
+                tag_type = TagTypes(self.api,{'tagname':tagname})[0]
+                person_tags=PersonTags(self.api,{'tagname':tagname,'person_id':person['person_id']})
+                if not person_tags:
+                    person_tag = PersonTag(self.api)
+                    person_tag['person_id'] = person['person_id']
+                    person_tag['tag_type_id'] = tag_type['tag_type_id']
+                    person_tag['tagname']  = tagname
+                    person_tag['value'] = hrn
+                    person_tag.sync()
+                else:
+                    person_tag = person_tags[0]
+                    person_tag['value'] = value
+                    person_tag.sync() 
+        except Exception,e:
+            print "Warning, cannot maintain person's hrn, %s"%e
+                
+
         return 1