1 from PLC.Faults import *
2 from PLC.Method import Method
3 from PLC.Parameter import Parameter, Mixed
4 from PLC.Addresses import Address, Addresses
5 from PLC.Auth import Auth
6 from PLC.Sites import Site, Sites
8 can_update = lambda (field, value): field in \
9 ['line1', 'line2', 'line3',
10 'city', 'state', 'postalcode', 'country']
12 class AddSiteAddress(Method):
14 Adds a new address to a site. Fields specified in
15 address_fields are used; some are not optional.
17 PIs may only add addresses to their own sites.
19 Returns the new address_id (> 0) if successful, faults otherwise.
22 roles = ['admin', 'pi']
24 address_fields = dict(filter(can_update, Address.fields.items()))
28 Mixed(Site.fields['site_id'],
29 Site.fields['login_base']),
33 returns = Parameter(int, 'New address_id (> 0) if successful')
38 def call(self, auth, site_id_or_login_base, address_fields):
39 address_fields = dict(filter(can_update, address_fields.items()))
41 # Get associated site details
42 sites = Sites(self.api, [site_id_or_login_base])
44 raise PLCInvalidArgument, "No such site"
47 if 'admin' not in self.caller['roles']:
48 if site['site_id'] not in self.caller['site_ids']:
49 raise PLCPermissionDenied, "Address must be associated with one of your sites"
51 address = Address(self.api, address_fields)
52 address.sync(commit = False)
53 site.add_address(address, commit = True)
56 self.object_ids = [site['site_id'], address['address_id']]
57 self.message = 'Address %d assigned to Site %d' % \
58 (address['address_id'], site['site_id'])
60 return address['address_id']