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.Table import Row
6 from PLC.Persons import Person, Persons
7 from PLC.TagTypes import TagTypes
8 from PLC.PersonTags import PersonTags, PersonTag
10 can_update = ['first_name', 'last_name', 'title',
11 'email', 'password', 'phone', 'url', 'bio']
13 required=['email','first_name','last_name']
15 class AddPerson(Method):
17 Adds a new account. Any fields specified in person_fields are
18 used, otherwise defaults are used.
20 Accounts are disabled by default. To enable an account, use
23 Returns the new person_id (> 0) if successful, faults otherwise.
26 roles = ['admin', 'pi']
28 accepted_fields = Row.accepted_fields(can_update,Person.fields)
29 accepted_fields.update(Person.tags)
36 returns = Parameter(int, 'New person_id (> 0) if successful')
38 def call(self, auth, person_fields):
40 # silently ignore 'enabled' if passed, for backward compat
41 # this is forced to False below anyways
42 if 'enabled' in person_fields: del person_fields['enabled']
44 [native,tags,rejected]=Row.split_fields(person_fields,[Person.fields,Person.tags])
47 native = Row.check_fields(native, self.accepted_fields)
49 raise PLCInvalidArgument("Cannot add Person with column(s) %r"%rejected)
51 missing=[ r for r in required if r not in native ]
53 raise PLCInvalidArgument("Missing mandatory arguments %s to AddPerson"%missing)
55 # handle native fields
56 native['enabled'] = False
57 person = Person(self.api, native)
61 for (tagname,value) in tags.items():
62 # the tagtype instance is assumed to exist, just check that
63 tag_types = TagTypes(self.api,{'tagname':tagname})
65 raise PLCInvalidArgument("No such TagType %s"%tagname)
66 tag_type = tag_types[0]
67 person_tags=PersonTags(self.api,{'tagname':tagname,'person_id':person['person_id']})
69 person_tag = PersonTag(self.api)
70 person_tag['person_id'] = person['person_id']
71 person_tag['tag_type_id'] = tag_type['tag_type_id']
72 person_tag['tagname'] = tagname
73 person_tag['value'] = value
76 person_tag = person_tags[0]
77 person_tag['value'] = value
81 self.event_objects = {'Person': [person['person_id']]}
82 self.message = 'Person %d added' % person['person_id']
84 return person['person_id']