blind 2to3
[plcapi.git] / PLC / Methods / AddPerson.py
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
9
10 can_update = ['first_name', 'last_name', 'title',
11               'email', 'password', 'phone', 'url', 'bio']
12
13 required=['email','first_name','last_name']
14
15 class AddPerson(Method):
16     """
17     Adds a new account. Any fields specified in person_fields are
18     used, otherwise defaults are used.
19
20     Accounts are disabled by default. To enable an account, use
21     UpdatePerson().
22
23     Returns the new person_id (> 0) if successful, faults otherwise.
24     """
25
26     roles = ['admin', 'pi']
27
28     accepted_fields = Row.accepted_fields(can_update,Person.fields)
29     accepted_fields.update(Person.tags)
30
31     accepts = [
32         Auth(),
33         accepted_fields
34         ]
35
36     returns = Parameter(int, 'New person_id (> 0) if successful')
37
38     def call(self, auth, person_fields):
39
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']
43
44         [native,tags,rejected]=Row.split_fields(person_fields,[Person.fields,Person.tags])
45
46         # type checking
47         native = Row.check_fields(native, self.accepted_fields)
48         if rejected:
49             raise PLCInvalidArgument("Cannot add Person with column(s) %r"%rejected)
50
51         missing=[ r for r in required if r not in native ]
52         if missing:
53             raise PLCInvalidArgument("Missing mandatory arguments %s to AddPerson"%missing)
54
55         # handle native fields
56         native['enabled'] = False
57         person = Person(self.api, native)
58         person.sync()
59
60         # handle tags
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})
64             if not tag_types:
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']})
68             if not person_tags:
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
74                 person_tag.sync()
75             else:
76                 person_tag = person_tags[0]
77                 person_tag['value'] = value
78                 person_tag.sync() 
79
80         # Logging variables
81         self.event_objects = {'Person': [person['person_id']]}
82         self.message = 'Person %d added' % person['person_id']
83
84         return person['person_id']