595399f52cdb3efcffe7f58f8ef73e8e18326996
[plcapi.git] / PLC / Methods / AddPersonToSite.py
1 # $Id$
2 # $URL$
3 from PLC.Faults import *
4 from PLC.Method import Method
5 from PLC.Parameter import Parameter, Mixed
6 from PLC.Persons import Person, Persons
7 from PLC.Sites import Site, Sites
8 from PLC.Auth import Auth
9
10 class AddPersonToSite(Method):
11     """
12     Adds the specified person to the specified site. If the person is
13     already a member of the site, no errors are returned. Does not
14     change the person's primary site.
15
16     Returns 1 if successful, faults otherwise.
17     """
18
19     roles = ['admin']
20
21     accepts = [
22         Auth(),
23         Mixed(Person.fields['person_id'],
24               Person.fields['email']),
25         Mixed(Site.fields['site_id'],
26               Site.fields['login_base'])
27         ]
28
29     returns = Parameter(int, '1 if successful')
30
31     def call(self, auth, person_id_or_email, site_id_or_login_base):
32         # Get account information
33         persons = Persons(self.api, [person_id_or_email])
34         if not persons:
35             raise PLCInvalidArgument, "No such account"
36         person = persons[0]
37
38         if person['peer_id'] is not None:
39             raise PLCInvalidArgument, "Not a local account"
40
41         # Get site information
42         sites = Sites(self.api, [site_id_or_login_base])
43         if not sites:
44             raise PLCInvalidArgument, "No such site"
45         site = sites[0]
46
47         if site['peer_id'] is not None:
48             raise PLCInvalidArgument, "Not a local site"
49
50         if site['site_id'] not in person['site_ids']:
51             site.add_person(person)
52
53         # Logging variables
54         self.event_objects = {'Site': [site['site_id']],
55                               'Person': [person['person_id']]}
56         self.message = 'Person %d added to site %d' % \
57                        (person['person_id'], site['site_id'])
58
59         return 1