X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=PLC%2FMethods%2FAddPersonToSite.py;h=0e0f6a392a001c8a24eb2e3eb1266c174f86cd77;hb=d20644a48d03667bb25dc583517de06e94606c64;hp=fb77a31e7a806b7e3c340946ffc6d3c47dff53e9;hpb=dd6f6871abdf5412d908dc31777ce88b09192efe;p=plcapi.git diff --git a/PLC/Methods/AddPersonToSite.py b/PLC/Methods/AddPersonToSite.py index fb77a31..0e0f6a3 100644 --- a/PLC/Methods/AddPersonToSite.py +++ b/PLC/Methods/AddPersonToSite.py @@ -1,9 +1,14 @@ 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 + +from PLC.Logger import logger class AddPersonToSite(Method): """ @@ -30,24 +35,55 @@ class AddPersonToSite(Method): # Get account information persons = Persons(self.api, [person_id_or_email]) if not persons: - raise PLCInvalidArgument, "No such account" - + raise PLCInvalidArgument("No such account") person = persons[0] - PLCCheckLocalPerson(person,"AddPersonToSite") + + if person['peer_id'] is not None: + raise PLCInvalidArgument("Not a local account") # Get site information sites = Sites(self.api, [site_id_or_login_base]) if not sites: - raise PLCInvalidArgument, "No such site" - + raise PLCInvalidArgument("No such site") site = sites[0] - PLCCheckLocalSite(site,"AddPersonToSite") + + if site['peer_id'] is not None: + raise PLCInvalidArgument("Not a local site") if site['site_id'] not in person['site_ids']: site.add_person(person) - - # Logging variables - self.object_ids = [site['site_id']] - self.message = 'Person %d added to site %d' % \ - (person['person_id'], site['site_id']) + + # Logging variables + self.event_objects = {'Site': [site['site_id']], + 'Person': [person['person_id']]} + 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'] = hrn + person_tag.sync() + except Exception as e: + logger.exception("ERROR cannot maintain person's hrn, {}" + .format(person_id_or_email)) + return 1