persons & tags : allow tags to be passed to AddPerson, and update
[plcapi.git] / PLC / Methods / AddPersonToSite.py
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
10
11 class AddPersonToSite(Method):
12     """
13     Adds the specified person to the specified site. If the person is
14     already a member of the site, no errors are returned. Does not
15     change the person's primary site.
16
17     Returns 1 if successful, faults otherwise.
18     """
19
20     roles = ['admin']
21
22     accepts = [
23         Auth(),
24         Mixed(Person.fields['person_id'],
25               Person.fields['email']),
26         Mixed(Site.fields['site_id'],
27               Site.fields['login_base'])
28         ]
29
30     returns = Parameter(int, '1 if successful')
31
32     def call(self, auth, person_id_or_email, site_id_or_login_base):
33         # Get account information
34         persons = Persons(self.api, [person_id_or_email])
35         if not persons:
36             raise PLCInvalidArgument, "No such account"
37         person = persons[0]
38
39         if person['peer_id'] is not None:
40             raise PLCInvalidArgument, "Not a local account"
41
42         # Get site information
43         sites = Sites(self.api, [site_id_or_login_base])
44         if not sites:
45             raise PLCInvalidArgument, "No such site"
46         site = sites[0]
47
48         if site['peer_id'] is not None:
49             raise PLCInvalidArgument, "Not a local site"
50
51         if site['site_id'] not in person['site_ids']:
52             site.add_person(person)
53
54         # Logging variables
55         self.event_objects = {'Site': [site['site_id']],
56                               'Person': [person['person_id']]}
57         self.message = 'Person %d added to site %d' % \
58                        (person['person_id'], site['site_id'])
59
60         # maintain person's hrn
61         # only if at this point we have a single site 
62         # which means, there was no site attached to person upon entering this call
63         try:
64             had_no_site= (len (person['site_ids']) == 0)
65             if had_no_site: 
66                 login_base=site['login_base']
67                 root_auth = self.api.config.PLC_HRN_ROOT
68                 hrn=email_to_hrn("%s.%s"%(root_auth,login_base),person['email'])
69                 tagname='hrn'
70                 tag_type = TagTypes(self.api,{'tagname':tagname})[0]
71                 person_tags=PersonTags(self.api,{'tagname':tagname,'person_id':person['person_id']})
72                 if not person_tags:
73                     person_tag = PersonTag(self.api)
74                     person_tag['person_id'] = person['person_id']
75                     person_tag['tag_type_id'] = tag_type['tag_type_id']
76                     person_tag['tagname']  = tagname
77                     person_tag['value'] = hrn
78                     person_tag.sync()
79                 else:
80                     person_tag = person_tags[0]
81                     person_tag['value'] = value
82                     person_tag.sync() 
83         except Exception,e:
84             print "Warning, cannot maintain person's hrn, %s"%e
85                 
86
87         return 1