/gdata/apps/groups/service.py

http://radioappz.googlecode.com/ · Python · 327 lines · 266 code · 24 blank · 37 comment · 24 complexity · dd4f9f8b9c579be64aa91df8ca01ea42 MD5 · raw file

  1. #!/usr/bin/python
  2. #
  3. # Copyright (C) 2008 Google, Inc.
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License");
  6. # you may not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. """Allow Google Apps domain administrators to manage groups, group members and group owners.
  17. GroupsService: Provides methods to manage groups, members and owners.
  18. """
  19. __author__ = 'google-apps-apis@googlegroups.com'
  20. import urllib
  21. import gdata.apps
  22. import gdata.apps.service
  23. import gdata.service
  24. API_VER = '2.0'
  25. BASE_URL = '/a/feeds/group/' + API_VER + '/%s'
  26. GROUP_MEMBER_URL = BASE_URL + '?member=%s'
  27. GROUP_MEMBER_DIRECT_URL = GROUP_MEMBER_URL + '&directOnly=%s'
  28. GROUP_ID_URL = BASE_URL + '/%s'
  29. MEMBER_URL = BASE_URL + '/%s/member'
  30. MEMBER_WITH_SUSPENDED_URL = MEMBER_URL + '?includeSuspendedUsers=%s'
  31. MEMBER_ID_URL = MEMBER_URL + '/%s'
  32. OWNER_URL = BASE_URL + '/%s/owner'
  33. OWNER_WITH_SUSPENDED_URL = OWNER_URL + '?includeSuspendedUsers=%s'
  34. OWNER_ID_URL = OWNER_URL + '/%s'
  35. PERMISSION_OWNER = 'Owner'
  36. PERMISSION_MEMBER = 'Member'
  37. PERMISSION_DOMAIN = 'Domain'
  38. PERMISSION_ANYONE = 'Anyone'
  39. class GroupsService(gdata.apps.service.PropertyService):
  40. """Client for the Google Apps Groups service."""
  41. def _ServiceUrl(self, service_type, is_existed, group_id, member_id, owner_email,
  42. direct_only=False, domain=None, suspended_users=False):
  43. if domain is None:
  44. domain = self.domain
  45. if service_type == 'group':
  46. if group_id != '' and is_existed:
  47. return GROUP_ID_URL % (domain, group_id)
  48. elif member_id != '':
  49. if direct_only:
  50. return GROUP_MEMBER_DIRECT_URL % (domain, urllib.quote_plus(member_id),
  51. self._Bool2Str(direct_only))
  52. else:
  53. return GROUP_MEMBER_URL % (domain, urllib.quote_plus(member_id))
  54. else:
  55. return BASE_URL % (domain)
  56. if service_type == 'member':
  57. if member_id != '' and is_existed:
  58. return MEMBER_ID_URL % (domain, group_id, urllib.quote_plus(member_id))
  59. elif suspended_users:
  60. return MEMBER_WITH_SUSPENDED_URL % (domain, group_id,
  61. self._Bool2Str(suspended_users))
  62. else:
  63. return MEMBER_URL % (domain, group_id)
  64. if service_type == 'owner':
  65. if owner_email != '' and is_existed:
  66. return OWNER_ID_URL % (domain, group_id, urllib.quote_plus(owner_email))
  67. elif suspended_users:
  68. return OWNER_WITH_SUSPENDED_URL % (domain, group_id,
  69. self._Bool2Str(suspended_users))
  70. else:
  71. return OWNER_URL % (domain, group_id)
  72. def _Bool2Str(self, b):
  73. if b is None:
  74. return None
  75. return str(b is True).lower()
  76. def _IsExisted(self, uri):
  77. try:
  78. self._GetProperties(uri)
  79. return True
  80. except gdata.apps.service.AppsForYourDomainException, e:
  81. if e.error_code == gdata.apps.service.ENTITY_DOES_NOT_EXIST:
  82. return False
  83. else:
  84. raise e
  85. def CreateGroup(self, group_id, group_name, description, email_permission):
  86. """Create a group.
  87. Args:
  88. group_id: The ID of the group (e.g. us-sales).
  89. group_name: The name of the group.
  90. description: A description of the group
  91. email_permission: The subscription permission of the group.
  92. Returns:
  93. A dict containing the result of the create operation.
  94. """
  95. uri = self._ServiceUrl('group', False, group_id, '', '')
  96. properties = {}
  97. properties['groupId'] = group_id
  98. properties['groupName'] = group_name
  99. properties['description'] = description
  100. properties['emailPermission'] = email_permission
  101. return self._PostProperties(uri, properties)
  102. def UpdateGroup(self, group_id, group_name, description, email_permission):
  103. """Update a group's name, description and/or permission.
  104. Args:
  105. group_id: The ID of the group (e.g. us-sales).
  106. group_name: The name of the group.
  107. description: A description of the group
  108. email_permission: The subscription permission of the group.
  109. Returns:
  110. A dict containing the result of the update operation.
  111. """
  112. uri = self._ServiceUrl('group', True, group_id, '', '')
  113. properties = {}
  114. properties['groupId'] = group_id
  115. properties['groupName'] = group_name
  116. properties['description'] = description
  117. properties['emailPermission'] = email_permission
  118. return self._PutProperties(uri, properties)
  119. def RetrieveGroup(self, group_id):
  120. """Retrieve a group based on its ID.
  121. Args:
  122. group_id: The ID of the group (e.g. us-sales).
  123. Returns:
  124. A dict containing the result of the retrieve operation.
  125. """
  126. uri = self._ServiceUrl('group', True, group_id, '', '')
  127. return self._GetProperties(uri)
  128. def RetrieveAllGroups(self):
  129. """Retrieve all groups in the domain.
  130. Args:
  131. None
  132. Returns:
  133. A list containing the result of the retrieve operation.
  134. """
  135. uri = self._ServiceUrl('group', True, '', '', '')
  136. return self._GetPropertiesList(uri)
  137. def RetrieveGroups(self, member_id, direct_only=False):
  138. """Retrieve all groups that belong to the given member_id.
  139. Args:
  140. member_id: The member's email address (e.g. member@example.com).
  141. direct_only: Boolean whether only return groups that this member directly belongs to.
  142. Returns:
  143. A list containing the result of the retrieve operation.
  144. """
  145. uri = self._ServiceUrl('group', True, '', member_id, '', direct_only=direct_only)
  146. return self._GetPropertiesList(uri)
  147. def DeleteGroup(self, group_id):
  148. """Delete a group based on its ID.
  149. Args:
  150. group_id: The ID of the group (e.g. us-sales).
  151. Returns:
  152. A dict containing the result of the delete operation.
  153. """
  154. uri = self._ServiceUrl('group', True, group_id, '', '')
  155. return self._DeleteProperties(uri)
  156. def AddMemberToGroup(self, member_id, group_id):
  157. """Add a member to a group.
  158. Args:
  159. member_id: The member's email address (e.g. member@example.com).
  160. group_id: The ID of the group (e.g. us-sales).
  161. Returns:
  162. A dict containing the result of the add operation.
  163. """
  164. uri = self._ServiceUrl('member', False, group_id, member_id, '')
  165. properties = {}
  166. properties['memberId'] = member_id
  167. return self._PostProperties(uri, properties)
  168. def IsMember(self, member_id, group_id):
  169. """Check whether the given member already exists in the given group.
  170. Args:
  171. member_id: The member's email address (e.g. member@example.com).
  172. group_id: The ID of the group (e.g. us-sales).
  173. Returns:
  174. True if the member exists in the group. False otherwise.
  175. """
  176. uri = self._ServiceUrl('member', True, group_id, member_id, '')
  177. return self._IsExisted(uri)
  178. def RetrieveMember(self, member_id, group_id):
  179. """Retrieve the given member in the given group.
  180. Args:
  181. member_id: The member's email address (e.g. member@example.com).
  182. group_id: The ID of the group (e.g. us-sales).
  183. Returns:
  184. A dict containing the result of the retrieve operation.
  185. """
  186. uri = self._ServiceUrl('member', True, group_id, member_id, '')
  187. return self._GetProperties(uri)
  188. def RetrieveAllMembers(self, group_id, suspended_users=False):
  189. """Retrieve all members in the given group.
  190. Args:
  191. group_id: The ID of the group (e.g. us-sales).
  192. suspended_users: A boolean; should we include any suspended users in
  193. the membership list returned?
  194. Returns:
  195. A list containing the result of the retrieve operation.
  196. """
  197. uri = self._ServiceUrl('member', True, group_id, '', '',
  198. suspended_users=suspended_users)
  199. return self._GetPropertiesList(uri)
  200. def RemoveMemberFromGroup(self, member_id, group_id):
  201. """Remove the given member from the given group.
  202. Args:
  203. member_id: The member's email address (e.g. member@example.com).
  204. group_id: The ID of the group (e.g. us-sales).
  205. Returns:
  206. A dict containing the result of the remove operation.
  207. """
  208. uri = self._ServiceUrl('member', True, group_id, member_id, '')
  209. return self._DeleteProperties(uri)
  210. def AddOwnerToGroup(self, owner_email, group_id):
  211. """Add an owner to a group.
  212. Args:
  213. owner_email: The email address of a group owner.
  214. group_id: The ID of the group (e.g. us-sales).
  215. Returns:
  216. A dict containing the result of the add operation.
  217. """
  218. uri = self._ServiceUrl('owner', False, group_id, '', owner_email)
  219. properties = {}
  220. properties['email'] = owner_email
  221. return self._PostProperties(uri, properties)
  222. def IsOwner(self, owner_email, group_id):
  223. """Check whether the given member an owner of the given group.
  224. Args:
  225. owner_email: The email address of a group owner.
  226. group_id: The ID of the group (e.g. us-sales).
  227. Returns:
  228. True if the member is an owner of the given group. False otherwise.
  229. """
  230. uri = self._ServiceUrl('owner', True, group_id, '', owner_email)
  231. return self._IsExisted(uri)
  232. def RetrieveOwner(self, owner_email, group_id):
  233. """Retrieve the given owner in the given group.
  234. Args:
  235. owner_email: The email address of a group owner.
  236. group_id: The ID of the group (e.g. us-sales).
  237. Returns:
  238. A dict containing the result of the retrieve operation.
  239. """
  240. uri = self._ServiceUrl('owner', True, group_id, '', owner_email)
  241. return self._GetProperties(uri)
  242. def RetrieveAllOwners(self, group_id, suspended_users=False):
  243. """Retrieve all owners of the given group.
  244. Args:
  245. group_id: The ID of the group (e.g. us-sales).
  246. suspended_users: A boolean; should we include any suspended users in
  247. the ownership list returned?
  248. Returns:
  249. A list containing the result of the retrieve operation.
  250. """
  251. uri = self._ServiceUrl('owner', True, group_id, '', '',
  252. suspended_users=suspended_users)
  253. return self._GetPropertiesList(uri)
  254. def RemoveOwnerFromGroup(self, owner_email, group_id):
  255. """Remove the given owner from the given group.
  256. Args:
  257. owner_email: The email address of a group owner.
  258. group_id: The ID of the group (e.g. us-sales).
  259. Returns:
  260. A dict containing the result of the remove operation.
  261. """
  262. uri = self._ServiceUrl('owner', True, group_id, '', owner_email)
  263. return self._DeleteProperties(uri)