PageRenderTime 63ms CodeModel.GetById 34ms RepoModel.GetById 0ms app.codeStats 0ms

/indico/MaKaC/services/implementation/category.py

https://github.com/davidmorrison/indico
Python | 240 lines | 143 code | 48 blank | 49 comment | 22 complexity | 3eeefeaa96d18bd74072949272196032 MD5 | raw file
  1. # -*- coding: utf-8 -*-
  2. ##
  3. ##
  4. ## This file is part of CDS Indico.
  5. ## Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007 CERN.
  6. ##
  7. ## CDS Indico is free software; you can redistribute it and/or
  8. ## modify it under the terms of the GNU General Public License as
  9. ## published by the Free Software Foundation; either version 2 of the
  10. ## License, or (at your option) any later version.
  11. ##
  12. ## CDS Indico is distributed in the hope that it will be useful, but
  13. ## WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. ## General Public License for more details.
  16. ##
  17. ## You should have received a copy of the GNU General Public License
  18. ## along with CDS Indico; if not, write to the Free Software Foundation, Inc.,
  19. ## 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
  20. """
  21. Asynchronous request handlers for category-related services.
  22. """
  23. import datetime
  24. from itertools import islice
  25. from MaKaC.services.implementation.base import ProtectedModificationService, ParameterManager
  26. from MaKaC.services.implementation.base import ProtectedDisplayService, ServiceBase
  27. from MaKaC.services.implementation.base import TextModificationBase
  28. import MaKaC.conference as conference
  29. from MaKaC.common.logger import Logger
  30. from MaKaC.services.interface.rpc.common import ServiceError, ServiceAccessError
  31. import MaKaC.webinterface.locators as locators
  32. from MaKaC.webinterface.wcomponents import WConferenceList, WConferenceListEvents
  33. from MaKaC.common.fossilize import fossilize
  34. from MaKaC.user import PrincipalHolder, Avatar, Group
  35. from indico.core.index import Catalog
  36. class CategoryBase(object):
  37. """
  38. Base class for category
  39. """
  40. def _checkParams( self ):
  41. try:
  42. l = locators.WebLocator()
  43. l.setCategory( self._params )
  44. self._target = self._categ = l.getObject()
  45. except:
  46. #raise ServiceError("ERR-E4", "Invalid category id.")
  47. self._target = self._categ = conference.CategoryManager().getRoot()
  48. class CategoryModifBase(ProtectedModificationService, CategoryBase):
  49. def _checkParams(self):
  50. CategoryBase._checkParams(self)
  51. ProtectedModificationService._checkParams(self)
  52. class CategoryDisplayBase(ProtectedDisplayService, CategoryBase):
  53. def _checkParams(self):
  54. CategoryBase._checkParams(self)
  55. ProtectedDisplayService._checkParams(self)
  56. class CategoryTextModificationBase(TextModificationBase, CategoryModifBase):
  57. #Note: don't change the order of the inheritance here!
  58. pass
  59. class GetCategoryList(CategoryDisplayBase):
  60. def _checkProtection( self ):
  61. self._accessAllowed = False
  62. try:
  63. CategoryDisplayBase._checkProtection(self)
  64. self._accessAllowed = True
  65. except Exception, e:
  66. pass
  67. def _getAnswer( self ):
  68. # allowed is used to report to the user in case of forbidden access.
  69. allowed = self._accessAllowed
  70. # if the category access is forbidden we return the previous
  71. # one which the user can access.
  72. if not self._accessAllowed:
  73. while (not self._accessAllowed and not self._target.isRoot()):
  74. self._target = self._target.getOwner()
  75. self._checkProtection()
  76. target = self._target
  77. # if the category is final we return the previous one.
  78. if not target.hasSubcategories():
  79. target = target.getOwner()
  80. # We get the parent category. If no parent (Home) we keep the same as target.
  81. parent = target.getOwner()
  82. if not parent:
  83. parent = target
  84. # Breadcrumbs
  85. breadcrumbs = target.getCategoryPathTitles()
  86. # Getting the list of subcategories
  87. categList=[]
  88. for cat in target.getSubCategoryList():
  89. #if cat.hasAnyProtection():
  90. # protected = True
  91. #elif cat.isConferenceCreationRestricted():
  92. # protected = not cat.canCreateConference( self._getUser() )
  93. #else:
  94. # protected = False
  95. categList.append({"id":cat.getId(), "title":cat.getTitle(), "subcatLength":len(cat.getSubCategoryList()), "final": not cat.hasSubcategories()})
  96. return {"parentCateg":
  97. {"id":parent.getId(), "title":parent.getTitle()},
  98. "currentCateg":
  99. {"id":target.getId(), "title":target.getTitle(), "breadcrumb": breadcrumbs},
  100. "categList":categList,
  101. "accessAllowed": allowed
  102. }
  103. class CanCreateEvent(CategoryDisplayBase):
  104. """
  105. This service returns whether or not the user can create
  106. an event in this category along with the protection of
  107. the chosen category.
  108. """
  109. def _checkProtection( self ):
  110. self._accessAllowed = False
  111. try:
  112. CategoryDisplayBase._checkProtection(self)
  113. self._accessAllowed = True
  114. except Exception, e:
  115. pass
  116. def _getAnswer( self ):
  117. canCreate = False
  118. protection = "public"
  119. if (self._accessAllowed and self._categ.canCreateConference( self._getUser() )):
  120. canCreate = True
  121. if self._categ.isProtected() :
  122. protection = "private"
  123. return {"canCreate": canCreate,
  124. "protection": protection}
  125. class GetPastEventsList(CategoryDisplayBase):
  126. def _checkParams(self):
  127. CategoryDisplayBase._checkParams(self)
  128. self._lastIdx = int(self._params.get("lastIdx"))
  129. def _getAnswer( self ):
  130. index = Catalog.getIdx('categ_conf_sd').getCategory(self._categ.getId())
  131. pastEvents = list(islice(index.itervalues(), self._lastIdx))
  132. return WConferenceListEvents(pastEvents, self._aw).getHTML()
  133. class SetShowPastEventsForCateg(CategoryDisplayBase):
  134. def _checkParams(self):
  135. CategoryDisplayBase._checkParams(self)
  136. self._showPastEvents = bool(self._params.get("showPastEvents",False))
  137. def _getAnswer( self ):
  138. session = self._aw.getSession()
  139. if not session.getVar("fetchPastEventsFrom"):
  140. session.setVar("fetchPastEventsFrom",set())
  141. if self._showPastEvents:
  142. fpef = session.getVar("fetchPastEventsFrom")
  143. fpef.add(self._categ.getId())
  144. session.setVar("fetchPastEventsFrom", fpef)
  145. else:
  146. fpef = session.getVar("fetchPastEventsFrom")
  147. fpef.remove(self._categ.getId())
  148. session.setVar("fetchPastEventsFrom", fpef)
  149. class CategoryProtectionUserList(CategoryModifBase):
  150. def _getAnswer(self):
  151. #will use IAvatarFossil or IGroupFossil
  152. return fossilize(self._categ.getAllowedToAccessList())
  153. class CategoryProtectionAddUsers(CategoryModifBase):
  154. def _checkParams(self):
  155. CategoryModifBase._checkParams(self)
  156. self._usersData = self._params['value']
  157. self._user = self.getAW().getUser()
  158. def _getAnswer(self):
  159. for user in self._usersData :
  160. userToAdd = PrincipalHolder().getById(user['id'])
  161. if not userToAdd :
  162. raise ServiceError("ERR-U0","User does not exist!")
  163. self._categ.grantAccess(userToAdd)
  164. class CategoryProtectionRemoveUser(CategoryModifBase):
  165. def _checkParams(self):
  166. CategoryModifBase._checkParams(self)
  167. self._userData = self._params['value']
  168. self._user = self.getAW().getUser()
  169. def _getAnswer(self):
  170. userToRemove = PrincipalHolder().getById(self._userData['id'])
  171. if not userToRemove :
  172. raise ServiceError("ERR-U0","User does not exist!")
  173. elif isinstance(userToRemove, Avatar) or isinstance(userToRemove, Group) :
  174. self._categ.revokeAccess(userToRemove)
  175. class CategoryContactInfoModification( CategoryTextModificationBase ):
  176. """
  177. Category contact email modification
  178. """
  179. def _handleSet(self):
  180. self._categ.getAccessController().setContactInfo(self._value)
  181. def _handleGet(self):
  182. return self._categ.getAccessController().getContactInfo()
  183. methodMap = {
  184. "getCategoryList": GetCategoryList,
  185. "getPastEventsList": GetPastEventsList,
  186. "setShowPastEventsForCateg": SetShowPastEventsForCateg,
  187. "canCreateEvent": CanCreateEvent,
  188. "protection.getAllowedUsersList": CategoryProtectionUserList,
  189. "protection.addAllowedUsers": CategoryProtectionAddUsers,
  190. "protection.removeAllowedUser": CategoryProtectionRemoveUser,
  191. "protection.changeContactInfo": CategoryContactInfoModification
  192. }