1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 from caldavclientlibrary.admin.xmlaccounts import recordtypes
18 from caldavclientlibrary.admin.xmlaccounts import tags
19
20 from caldavclientlibrary.protocol.utils.xmlhelpers import SubElementWithData
21
22 from xml.etree.ElementTree import Element
23
25 """
26 Represents a single principal record. This class can parse and generate the appropriate XML.
27 """
28
42
44 """
45 Parse a single principal record from XML.
46
47 @param node: the XML element for the principal being parsed.
48 @type node: C{xml.etree.ElementTree.Element}
49 """
50 self.recordType = recordtypes.TAGS_TO_RECORD_TYPES[node.tag]
51 self.repeat = int(node.get(tags.ATTRIBUTE_REPEAT, "0"))
52 for child in node.getchildren():
53 if child.tag == tags.ELEMENT_UID:
54 self.uid = child.text
55 elif child.tag == tags.ELEMENT_GUID:
56 self.guid = child.text
57 elif child.tag == tags.ELEMENT_PASSWORD:
58 self.password = child.text
59 elif child.tag == tags.ELEMENT_NAME:
60 self.name = child.text
61 elif child.tag == tags.ELEMENT_MEMBERS:
62 self._parseMembers(child, self.members)
63 elif child.tag == tags.ELEMENT_CUADDR:
64 self.calendarUserAddresses.add(child.text)
65 elif child.tag == tags.ELEMENT_AUTOSCHEDULE:
66
67 if self.recordType not in (recordtypes.recordType_resources, recordtypes.recordType_locations,):
68 raise ValueError("<auto-schedule> element only allowed for Resources and Locations: %s" % (child.tag,))
69 self.autoSchedule = True
70 elif child.tag == tags.ELEMENT_DISABLECALENDAR:
71
72 if self.recordType not in (recordtypes.recordType_users, recordtypes.recordType_groups,):
73 raise ValueError("<disable-calendar> element only allowed for Groups: %s" % (child.tag,))
74 self.enabledForCalendaring = False
75 elif child.tag == tags.ELEMENT_PROXIES:
76
77 if self.recordType not in (recordtypes.recordType_resources, recordtypes.recordType_locations,):
78 raise ValueError("<proxies> element only allowed for Resources and Locations: %s" % (child.tag,))
79 self._parseMembers(child, self.proxies)
80 else:
81 raise RuntimeError("Unknown account attribute: %s" % (child.tag,))
82
84 """
85 Parse an XML <members> or <proxies> element list.
86
87 @param node: the <members> or <proxies> element to parse.
88 @type node: C{xml.etree.ElementTree.Element}
89 @param addto: the list to add the parsed information into. The items in the list are tuples
90 of the record type and record uid.
91 @type addto: C{list}
92 """
93 for child in node.getchildren():
94 if child.tag == tags.ELEMENT_MEMBER:
95 recordType = child.get(tags.ATTRIBUTE_RECORDTYPE, recordtypes.recordType_users)
96 addto.add((recordType, child.text))
97
99 """
100 Generate a single XML principal record element.
101
102 @return: the root element for the principal record.
103 @rtype: C{xml.etree.ElementTree.Element}
104 """
105
106 root = Element(recordtypes.RECORD_TYPES_TO_TAGS[self.recordType])
107 if self.repeat:
108 root.set(tags.ATTRIBUTE_REPEAT, str(self.repeat))
109
110 SubElementWithData(root, tags.ELEMENT_UID, self.uid)
111 SubElementWithData(root, tags.ELEMENT_GUID, self.guid)
112 SubElementWithData(root, tags.ELEMENT_PASSWORD, self.password)
113 SubElementWithData(root, tags.ELEMENT_NAME, self.name)
114 if self.recordType == recordtypes.recordType_groups:
115 members = SubElementWithData(root, tags.ELEMENT_MEMBERS)
116 for member in self.members:
117 SubElementWithData(members, tags.ELEMENT_MEMBER, member[1], {tags.ATTRIBUTE_RECORDTYPE:member[0]})
118 if self.calendarUserAddresses:
119 for cuaddr in self.calendarUserAddresses:
120 SubElementWithData(root, tags.ELEMENT_CUADDR, cuaddr)
121 if self.recordType in (recordtypes.recordType_resources, recordtypes.recordType_locations,) and self.autoSchedule:
122 SubElementWithData(root, tags.ELEMENT_AUTOSCHEDULE)
123 if self.recordType in (recordtypes.recordType_users, recordtypes.recordType_groups,) and not self.enabledForCalendaring:
124 SubElementWithData(root, tags.ELEMENT_DISABLECALENDAR)
125 if self.recordType in (recordtypes.recordType_resources, recordtypes.recordType_locations,):
126 proxies = SubElementWithData(root, tags.ELEMENT_PROXIES)
127 for proxy in self.proxies:
128 SubElementWithData(proxies, tags.ELEMENT_MEMBER, proxy[1], {tags.ATTRIBUTE_RECORDTYPE:proxy[0]})
129
130 return root
131