Initial checkin of new API implementation
[plcapi.git] / PLC / Methods / AdmAddPersonToSite.py
1 from PLC.Faults import *
2 from PLC.Method import Method
3 from PLC.Parameter import Parameter, Mixed
4 from PLC.Persons import Person, Persons
5 from PLC.Sites import Site, Sites
6 from PLC.Auth import PasswordAuth
7
8 class AdmAddPersonToSite(Method):
9     """
10     Adds the specified person to the specified site. If the person is
11     already a member of the site, no errors are returned. Does not
12     change the person's primary site.
13
14     Returns 1 if successful, faults otherwise.
15     """
16
17     roles = ['admin']
18
19     accepts = [
20         PasswordAuth(),
21         Mixed(Person.fields['person_id'],
22               Person.fields['email']),
23         Mixed(Site.fields['site_id'],
24               Site.fields['login_base'])
25         ]
26
27     returns = Parameter(int, '1 if successful')
28
29     def call(self, auth, person_id_or_email, site_id_or_login_base):
30         # Get account information
31         persons = Persons(self.api, [person_id_or_email])
32         if not persons:
33             raise PLCInvalidArgument, "No such account"
34
35         person = persons.values()[0]
36
37         # Get site information
38         sites = Sites(self.api, [site_id_or_login_base])
39         if not sites:
40             raise PLCInvalidArgument, "No such site"
41
42         site = sites.values()[0]
43
44         if site['site_id'] not in person['site_ids']:
45             person_id = person['person_id']
46             site_id = site['site_id']
47             self.api.db.do("INSERT INTO person_site (person_id, site_id)" \
48                            " VALUES(%(person_id)d, %(site_id)d)",
49                            locals())
50
51         return 1