- added logging variable 'object_type'
[plcapi.git] / PLC / Methods / DeletePersonFromSite.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 Auth
7
8 class DeletePersonFromSite(Method):
9     """
10     Removes the specified person from the specified site. If the
11     person is not a member of the specified site, no error is
12     returned.
13
14     Returns 1 if successful, faults otherwise.
15     """
16
17     roles = ['admin']
18
19     accepts = [
20         Auth(),
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     object_type = 'Site'
30     
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'] in person['site_ids']:
52             site.remove_person(person)
53
54         # Logging variables
55         self.object_ids = [site['site_id']]
56         self.message = 'Person %d deleted from site %d  ' % \
57                 (person['person_id'], site['site_id'])
58         return 1