1 from PLC.Faults import *
2 from PLC.Auth import Auth
3 from PLC.Method import Method
4 from PLC.Parameter import Parameter, Mixed
5 from PLC.Sites import Site, Sites
6 from PLC.Persons import Person, Persons
7 from PLC.PersonTags import PersonTags, PersonTag
8 from PLC.Namespace import email_to_hrn
9 from PLC.TagTypes import TagTypes
11 from PLC.Logger import logger
13 class AddPersonToSite(Method):
15 Adds the specified person to the specified site. If the person is
16 already a member of the site, no errors are returned. Does not
17 change the person's primary site.
19 Returns 1 if successful, faults otherwise.
26 Mixed(Person.fields['person_id'],
27 Person.fields['email']),
28 Mixed(Site.fields['site_id'],
29 Site.fields['login_base'])
32 returns = Parameter(int, '1 if successful')
34 def call(self, auth, person_id_or_email, site_id_or_login_base):
35 # Get account information
36 persons = Persons(self.api, [person_id_or_email])
38 raise PLCInvalidArgument("No such account")
41 if person['peer_id'] is not None:
42 raise PLCInvalidArgument("Not a local account")
44 # Get site information
45 sites = Sites(self.api, [site_id_or_login_base])
47 raise PLCInvalidArgument("No such site")
50 if site['peer_id'] is not None:
51 raise PLCInvalidArgument("Not a local site")
53 if site['site_id'] not in person['site_ids']:
54 site.add_person(person)
57 self.event_objects = {'Site': [site['site_id']],
58 'Person': [person['person_id']]}
59 self.message = 'Person %d added to site %d' % \
60 (person['person_id'], site['site_id'])
62 # maintain person's hrn
63 # only if at this point we have a single site
64 # which means, there was no site attached to person upon entering this call
66 had_no_site= (len (person['site_ids']) == 0)
68 login_base = site['login_base']
69 root_auth = self.api.config.PLC_HRN_ROOT
70 hrn = email_to_hrn("%s.%s"%(root_auth,login_base),person['email'])
72 tag_type = TagTypes(self.api,{'tagname':tagname})[0]
73 person_tags = PersonTags(self.api,{'tagname':tagname,'person_id':person['person_id']})
75 person_tag = PersonTag(self.api)
76 person_tag['person_id'] = person['person_id']
77 person_tag['tag_type_id'] = tag_type['tag_type_id']
78 person_tag['tagname'] = tagname
79 person_tag['value'] = hrn
82 person_tag = person_tags[0]
83 person_tag['value'] = hrn
85 except Exception as e:
86 logger.exception("ERROR cannot maintain person's hrn, {}"
87 .format(person_id_or_email))