| 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 | import MaKaC.conference as conference |
|---|
| 22 | import MaKaC.webinterface.urlHandlers as urlHandlers |
|---|
| 23 | import MaKaC.webinterface.wcomponents as wcomponents |
|---|
| 24 | from MaKaC.common.Configuration import Config |
|---|
| 25 | from MaKaC.i18n import _ |
|---|
| 26 | |
|---|
| 27 | class WSpecialMaterialCreationBase( wcomponents.WMaterialCreation ): |
|---|
| 28 | |
|---|
| 29 | def __init__( self, owner, factory ): |
|---|
| 30 | wcomponents.WMaterialCreation.__init__(self, owner) |
|---|
| 31 | self._factory = factory |
|---|
| 32 | |
|---|
| 33 | def getVars( self ): |
|---|
| 34 | vars = wcomponents.WMaterialCreation.getVars( self ) |
|---|
| 35 | vars["typeMaterial"] = self._factory.getId() |
|---|
| 36 | vars["title"] = self._factory.getTitle() |
|---|
| 37 | return vars |
|---|
| 38 | |
|---|
| 39 | |
|---|
| 40 | class MaterialFactory: |
|---|
| 41 | """Defines the class base for Material factories. |
|---|
| 42 | """ |
|---|
| 43 | _id = "" |
|---|
| 44 | _title = "" |
|---|
| 45 | _iconURL = "" |
|---|
| 46 | _creationWC = None |
|---|
| 47 | _modificationURL = "" |
|---|
| 48 | _needsCreationPage = True |
|---|
| 49 | _materialKlasses=[] |
|---|
| 50 | |
|---|
| 51 | def getId(cls): |
|---|
| 52 | return cls._id |
|---|
| 53 | getId = classmethod( getId ) |
|---|
| 54 | |
|---|
| 55 | def getTitle(cls): |
|---|
| 56 | return _(cls._title) |
|---|
| 57 | getTitle = classmethod( getTitle ) |
|---|
| 58 | |
|---|
| 59 | def getIconURL(cls): |
|---|
| 60 | return cls._iconURL |
|---|
| 61 | getIconURL = classmethod( getIconURL ) |
|---|
| 62 | |
|---|
| 63 | def get( owner ): |
|---|
| 64 | """returns the material""" |
|---|
| 65 | return None |
|---|
| 66 | get = staticmethod(get) |
|---|
| 67 | |
|---|
| 68 | def remove( owner ): |
|---|
| 69 | """performs the deletion of the material from the owner""" |
|---|
| 70 | return |
|---|
| 71 | remove = staticmethod( remove ) |
|---|
| 72 | |
|---|
| 73 | def canAdd( target ): |
|---|
| 74 | """checks whether the material can be added to the target object""" |
|---|
| 75 | return True |
|---|
| 76 | canAdd = staticmethod( canAdd ) |
|---|
| 77 | |
|---|
| 78 | def canDelete( target ): |
|---|
| 79 | """checks whether the material can be deleted from the target object""" |
|---|
| 80 | return False |
|---|
| 81 | canDelete = staticmethod( canDelete ) |
|---|
| 82 | |
|---|
| 83 | def getModificationURL( cls, mat ): |
|---|
| 84 | """returns the URL for accessing to the modification view of the |
|---|
| 85 | material""" |
|---|
| 86 | return cls._modificationURL |
|---|
| 87 | getModificationURL = classmethod(getModificationURL) |
|---|
| 88 | |
|---|
| 89 | def getCreationWC( cls, owner ): |
|---|
| 90 | if not cls._creationWC: |
|---|
| 91 | return None |
|---|
| 92 | return cls._creationWC(owner, cls) |
|---|
| 93 | getCreationWC = classmethod( getCreationWC ) |
|---|
| 94 | |
|---|
| 95 | def needsCreationPage( cls ): |
|---|
| 96 | return cls._needsCreationPage |
|---|
| 97 | needsCreationPage = classmethod( needsCreationPage ) |
|---|
| 98 | |
|---|
| 99 | def create( cls, target ): |
|---|
| 100 | return None |
|---|
| 101 | create = classmethod( create ) |
|---|
| 102 | |
|---|
| 103 | def appliesToMaterial(cls,mat): |
|---|
| 104 | for klass in cls._materialKlasses: |
|---|
| 105 | if isinstance(mat,klass): |
|---|
| 106 | return True |
|---|
| 107 | return False |
|---|
| 108 | appliesToMaterial=classmethod(appliesToMaterial) |
|---|
| 109 | |
|---|
| 110 | |
|---|
| 111 | class WPaperCreation( WSpecialMaterialCreationBase ): |
|---|
| 112 | pass |
|---|
| 113 | |
|---|
| 114 | class PaperFactory(MaterialFactory): |
|---|
| 115 | |
|---|
| 116 | _id = "paper" |
|---|
| 117 | _title = "Paper" |
|---|
| 118 | _iconURL = Config.getInstance().getSystemIconURL("paper") |
|---|
| 119 | _creationWC = WPaperCreation |
|---|
| 120 | _materialKlasses=[conference.Paper] |
|---|
| 121 | _needsCreationPage = False |
|---|
| 122 | |
|---|
| 123 | def get( owner ): |
|---|
| 124 | """returns the material""" |
|---|
| 125 | return owner.getPaper() |
|---|
| 126 | get = staticmethod(get) |
|---|
| 127 | |
|---|
| 128 | def remove( owner ): |
|---|
| 129 | """performs the deletion of the paper from the owner""" |
|---|
| 130 | owner.removePaper() |
|---|
| 131 | remove = staticmethod( remove ) |
|---|
| 132 | |
|---|
| 133 | def canAdd( target ): |
|---|
| 134 | #only one paper can be added to a contribution |
|---|
| 135 | return target.getPaper() == None |
|---|
| 136 | canAdd = staticmethod( canAdd ) |
|---|
| 137 | |
|---|
| 138 | def canDelete( target ): |
|---|
| 139 | #only a paper which is already set in a contribution can be deleted |
|---|
| 140 | return target.getPaper() != None |
|---|
| 141 | canDelete = staticmethod( canDelete ) |
|---|
| 142 | |
|---|
| 143 | def getModificationURL( cls, mat ): |
|---|
| 144 | """returns the URL for accessing to the modification view of the |
|---|
| 145 | material""" |
|---|
| 146 | return urlHandlers.UHMaterialModification.getURL( mat ) |
|---|
| 147 | getModificationURL = classmethod(getModificationURL) |
|---|
| 148 | |
|---|
| 149 | def create(cls, target ): |
|---|
| 150 | m = conference.Paper() |
|---|
| 151 | m.setTitle(cls.getTitle()) |
|---|
| 152 | target.setPaper( m ) |
|---|
| 153 | return m |
|---|
| 154 | create = classmethod( create ) |
|---|
| 155 | |
|---|
| 156 | |
|---|
| 157 | class WSlidesCreation( WSpecialMaterialCreationBase ): |
|---|
| 158 | pass |
|---|
| 159 | |
|---|
| 160 | |
|---|
| 161 | class SlidesFactory(MaterialFactory): |
|---|
| 162 | |
|---|
| 163 | _id = "slides" |
|---|
| 164 | _title = "Slides" |
|---|
| 165 | _iconURL = Config.getInstance().getSystemIconURL("slides") |
|---|
| 166 | _materialKlasses=[conference.Slides] |
|---|
| 167 | _needsCreationPage = False |
|---|
| 168 | |
|---|
| 169 | def get( owner ): |
|---|
| 170 | """returns the material""" |
|---|
| 171 | return owner.getSlides() |
|---|
| 172 | get = staticmethod(get) |
|---|
| 173 | |
|---|
| 174 | def remove( owner ): |
|---|
| 175 | """performs the deletion of the slides from the owner""" |
|---|
| 176 | owner.removeSlides() |
|---|
| 177 | remove = staticmethod( remove ) |
|---|
| 178 | |
|---|
| 179 | def canAdd( target ): |
|---|
| 180 | #only one slide can be added to a contribution |
|---|
| 181 | return target.getSlides() == None |
|---|
| 182 | canAdd = staticmethod( canAdd ) |
|---|
| 183 | |
|---|
| 184 | def canDelete( target ): |
|---|
| 185 | #only a slide which is already set in a contribution can be deleted |
|---|
| 186 | return target.getSlides() != None |
|---|
| 187 | canDelete = staticmethod( canDelete ) |
|---|
| 188 | |
|---|
| 189 | def getModificationURL( cls, mat ): |
|---|
| 190 | """returns the URL for accessing to the modification view of the |
|---|
| 191 | material""" |
|---|
| 192 | return urlHandlers.UHMaterialModification.getURL( mat ) |
|---|
| 193 | getModificationURL = classmethod(getModificationURL) |
|---|
| 194 | |
|---|
| 195 | def create(cls, target ): |
|---|
| 196 | m = conference.Slides() |
|---|
| 197 | m.setTitle(cls.getTitle()) |
|---|
| 198 | target.setSlides( m ) |
|---|
| 199 | return m |
|---|
| 200 | create = classmethod( create ) |
|---|
| 201 | |
|---|
| 202 | class WVideoCreation( WSpecialMaterialCreationBase ): |
|---|
| 203 | pass |
|---|
| 204 | |
|---|
| 205 | class VideoFactory(MaterialFactory): |
|---|
| 206 | |
|---|
| 207 | _id = "video" |
|---|
| 208 | _title = "Video" |
|---|
| 209 | _iconURL = Config.getInstance().getSystemIconURL("video") |
|---|
| 210 | _creationWC = WVideoCreation |
|---|
| 211 | _materialKlasses=[conference.Video] |
|---|
| 212 | _needsCreationPage = False |
|---|
| 213 | |
|---|
| 214 | def get( owner ): |
|---|
| 215 | """returns the material""" |
|---|
| 216 | return owner.getVideo() |
|---|
| 217 | get = staticmethod(get) |
|---|
| 218 | |
|---|
| 219 | def remove( owner ): |
|---|
| 220 | """performs the deletion of the video from the owner""" |
|---|
| 221 | owner.removeVideo() |
|---|
| 222 | remove = staticmethod( remove ) |
|---|
| 223 | |
|---|
| 224 | def canAdd( target ): |
|---|
| 225 | #only one video can be added to a contribution |
|---|
| 226 | return target.getVideo() == None |
|---|
| 227 | canAdd = staticmethod( canAdd ) |
|---|
| 228 | |
|---|
| 229 | def canDelete( target ): |
|---|
| 230 | #only a video which is already set in a contribution can be deleted |
|---|
| 231 | return target.getVideo() != None |
|---|
| 232 | canDelete = staticmethod( canDelete ) |
|---|
| 233 | |
|---|
| 234 | def getModificationURL( cls, mat ): |
|---|
| 235 | """returns the URL for accessing to the modification view of the |
|---|
| 236 | material""" |
|---|
| 237 | return urlHandlers.UHMaterialModification.getURL( mat ) |
|---|
| 238 | getModificationURL = classmethod(getModificationURL) |
|---|
| 239 | |
|---|
| 240 | def create(cls, target ): |
|---|
| 241 | m = conference.Video() |
|---|
| 242 | m.setTitle(cls.getTitle()) |
|---|
| 243 | target.setVideo( m ) |
|---|
| 244 | return m |
|---|
| 245 | create = classmethod( create ) |
|---|
| 246 | |
|---|
| 247 | class WPosterCreation( WSpecialMaterialCreationBase ): |
|---|
| 248 | pass |
|---|
| 249 | |
|---|
| 250 | class PosterFactory(MaterialFactory): |
|---|
| 251 | |
|---|
| 252 | _id = "poster" |
|---|
| 253 | _title = "Poster" |
|---|
| 254 | _iconURL = Config.getInstance().getSystemIconURL("poster") |
|---|
| 255 | _materialKlasses=[conference.Poster] |
|---|
| 256 | _needsCreationPage = False |
|---|
| 257 | |
|---|
| 258 | def get( owner ): |
|---|
| 259 | """returns the material""" |
|---|
| 260 | return owner.getPoster() |
|---|
| 261 | get = staticmethod(get) |
|---|
| 262 | |
|---|
| 263 | def remove( owner ): |
|---|
| 264 | """performs the deletion of the poster from the owner""" |
|---|
| 265 | owner.removePoster() |
|---|
| 266 | remove = staticmethod( remove ) |
|---|
| 267 | |
|---|
| 268 | def canAdd( target ): |
|---|
| 269 | #only one poster can be added to a contribution |
|---|
| 270 | return target.getPoster() == None |
|---|
| 271 | canAdd = staticmethod( canAdd ) |
|---|
| 272 | |
|---|
| 273 | def canDelete( target ): |
|---|
| 274 | #only a poster which is already set in a contribution can be deleted |
|---|
| 275 | return target.getPoster() != None |
|---|
| 276 | canDelete = staticmethod( canDelete ) |
|---|
| 277 | |
|---|
| 278 | def getModificationURL( cls, mat ): |
|---|
| 279 | """returns the URL for accessing to the modification view of the |
|---|
| 280 | material""" |
|---|
| 281 | return urlHandlers.UHMaterialModification.getURL( mat ) |
|---|
| 282 | getModificationURL = classmethod(getModificationURL) |
|---|
| 283 | |
|---|
| 284 | def create(cls, target ): |
|---|
| 285 | m = conference.Poster() |
|---|
| 286 | m.setTitle(cls.getTitle()) |
|---|
| 287 | target.setPoster( m ) |
|---|
| 288 | return m |
|---|
| 289 | create = classmethod( create ) |
|---|
| 290 | |
|---|
| 291 | class MinutesFactory(MaterialFactory): |
|---|
| 292 | |
|---|
| 293 | _id = "minutes" |
|---|
| 294 | _title = "Minutes" |
|---|
| 295 | _iconURL = Config.getInstance().getSystemIconURL("material") |
|---|
| 296 | _needsCreationPage = False |
|---|
| 297 | _materialKlasses=[conference.Minutes] |
|---|
| 298 | |
|---|
| 299 | def get( owner ): |
|---|
| 300 | """returns the material""" |
|---|
| 301 | return owner.getMinutes() |
|---|
| 302 | get = staticmethod(get) |
|---|
| 303 | |
|---|
| 304 | def remove( owner ): |
|---|
| 305 | """performs the deletion of the minutes from the owner""" |
|---|
| 306 | owner.removeMinutes() |
|---|
| 307 | remove = staticmethod( remove ) |
|---|
| 308 | |
|---|
| 309 | def canAdd( target ): |
|---|
| 310 | #only one minutes can be added to a contribution |
|---|
| 311 | return target.getMinutes() == None |
|---|
| 312 | canAdd = staticmethod( canAdd ) |
|---|
| 313 | |
|---|
| 314 | def canDelete( target ): |
|---|
| 315 | #only a minutes which is already set in a contribution can be deleted |
|---|
| 316 | return target.getMinutes() != None |
|---|
| 317 | canDelete = staticmethod( canDelete ) |
|---|
| 318 | |
|---|
| 319 | def getModificationURL( cls, mat ): |
|---|
| 320 | """returns the URL for accessing to the modification view of the |
|---|
| 321 | material""" |
|---|
| 322 | return urlHandlers.UHMaterialModification.getURL( mat ) |
|---|
| 323 | getModificationURL = classmethod(getModificationURL) |
|---|
| 324 | |
|---|
| 325 | def create( target ): |
|---|
| 326 | return target.createMinutes() |
|---|
| 327 | create = staticmethod( create ) |
|---|
| 328 | |
|---|
| 329 | class DefaultMaterialFactory(MaterialFactory): |
|---|
| 330 | |
|---|
| 331 | """ Default factory, searches for material with same name, |
|---|
| 332 | and creates new one if needed. """ |
|---|
| 333 | |
|---|
| 334 | def __init__(self, matId): |
|---|
| 335 | self.name = matId |
|---|
| 336 | |
|---|
| 337 | def get( self, owner ): |
|---|
| 338 | """returns the material""" |
|---|
| 339 | |
|---|
| 340 | for mat in owner.getMaterialList(): |
|---|
| 341 | if mat.getTitle().lower() == self.name.lower(): |
|---|
| 342 | return mat |
|---|
| 343 | |
|---|
| 344 | def remove( self, owner ): |
|---|
| 345 | """performs the deletion of the slides from the owner""" |
|---|
| 346 | owner.removeMaterial(self.get(owner)) |
|---|
| 347 | |
|---|
| 348 | def canAdd( self, target ): |
|---|
| 349 | return True |
|---|
| 350 | |
|---|
| 351 | def canDelete( self, target ): |
|---|
| 352 | return len(target.getMaterialList) > 0 |
|---|
| 353 | |
|---|
| 354 | def getModificationURL( self, mat ): |
|---|
| 355 | """returns the URL for accessing to the modification view of the |
|---|
| 356 | material""" |
|---|
| 357 | return urlHandlers.UHMaterialModification.getURL( mat ) |
|---|
| 358 | |
|---|
| 359 | def create(self, target): |
|---|
| 360 | m = conference.Material() |
|---|
| 361 | m.setTitle(self.name) |
|---|
| 362 | target.addMaterial( m ) |
|---|
| 363 | return m |
|---|
| 364 | |
|---|
| 365 | |
|---|
| 366 | @classmethod |
|---|
| 367 | def getInstance(cls, name): |
|---|
| 368 | return cls(name) |
|---|
| 369 | |
|---|
| 370 | |
|---|
| 371 | class MaterialFactoryRegistry: |
|---|
| 372 | """Keeps a list of material factories. When a new material type wants to be |
|---|
| 373 | added one just needs to implement the corresponding factory and add the |
|---|
| 374 | entry in the "_registry" attribute of this class |
|---|
| 375 | """ |
|---|
| 376 | _registry = { PaperFactory._id: PaperFactory, \ |
|---|
| 377 | SlidesFactory._id: SlidesFactory, \ |
|---|
| 378 | MinutesFactory._id: MinutesFactory, \ |
|---|
| 379 | VideoFactory._id: VideoFactory, \ |
|---|
| 380 | PosterFactory._id: PosterFactory } |
|---|
| 381 | |
|---|
| 382 | _allowedMaterials = { |
|---|
| 383 | 'simple_event': [ "paper", "slides", "poster", "minutes", "agenda", "pictures", "text", "more information", "document", "list of actions", "drawings", "proceedings", "live broadcast", "video", "streaming video", "downloadable video" ], |
|---|
| 384 | 'meeting': [ "paper", "slides", "poster", "minutes", "agenda", "video", "pictures", "text", "more information", "document", "list of actions", "drawings", "proceedings", "live broadcast" ], |
|---|
| 385 | 'conference' : ["paper", "slides", "poster", "minutes"], |
|---|
| 386 | 'category': [ "paper", "slides", "poster", "minutes", "agenda", "video", "pictures", "text", "more information", "document", "list of actions", "drawings", "proceedings", "live broadcast" ] |
|---|
| 387 | } |
|---|
| 388 | |
|---|
| 389 | |
|---|
| 390 | @classmethod |
|---|
| 391 | def getById( cls, matId ): |
|---|
| 392 | return cls._registry.get(matId, DefaultMaterialFactory.getInstance(matId)) |
|---|
| 393 | |
|---|
| 394 | @classmethod |
|---|
| 395 | def getList( cls ): |
|---|
| 396 | return cls._registry.values() |
|---|
| 397 | |
|---|
| 398 | @classmethod |
|---|
| 399 | def getIdList( cls ): |
|---|
| 400 | return cls._registry.keys() |
|---|
| 401 | |
|---|
| 402 | @classmethod |
|---|
| 403 | def get(cls, material): |
|---|
| 404 | for factory in cls._registry.values(): |
|---|
| 405 | if factory.appliesToMaterial(material): |
|---|
| 406 | return factory |
|---|
| 407 | |
|---|
| 408 | @classmethod |
|---|
| 409 | def getAllowed(cls, target): |
|---|
| 410 | return cls._allowedMaterials[target.getConference().getType()] |
|---|
| 411 | |
|---|
| 412 | @classmethod |
|---|
| 413 | def getMaterialList(cls, target): |
|---|
| 414 | """ |
|---|
| 415 | Generates a list containing all the materials, with the |
|---|
| 416 | corresponding Ids for those that already exist. |
|---|
| 417 | The format is [(id, title), ...] and materials from the allowed |
|---|
| 418 | material list and the existing material list are unified by title. |
|---|
| 419 | """ |
|---|
| 420 | |
|---|
| 421 | # NOTE: This method is a bit alien. It's just here because |
|---|
| 422 | # we couldn't find a better place |
|---|
| 423 | |
|---|
| 424 | matDict = dict((title.lower(), title) for title in cls.getAllowed(target)) |
|---|
| 425 | |
|---|
| 426 | for material in target.getMaterialList(): |
|---|
| 427 | title = material.getTitle().lower() |
|---|
| 428 | matDict[title] = material.getId() |
|---|
| 429 | |
|---|
| 430 | return sorted(list((matId, title.title()) |
|---|
| 431 | for title, matId in matDict.iteritems())) |
|---|
| 432 | |
|---|
| 433 | |
|---|
| 434 | |
|---|
| 435 | class CategoryMFRegistry(MaterialFactoryRegistry): |
|---|
| 436 | |
|---|
| 437 | @classmethod |
|---|
| 438 | def getAllowed(cls, target): |
|---|
| 439 | return cls._allowedMaterials['category'] |
|---|
| 440 | |
|---|
| 441 | |
|---|
| 442 | class ConfMFRegistry(MaterialFactoryRegistry): |
|---|
| 443 | pass |
|---|
| 444 | |
|---|
| 445 | |
|---|
| 446 | class SessionMFRegistry(MaterialFactoryRegistry): |
|---|
| 447 | |
|---|
| 448 | _registry = { MinutesFactory._id: MinutesFactory } |
|---|
| 449 | |
|---|
| 450 | |
|---|
| 451 | class ContribMFRegistry(MaterialFactoryRegistry): |
|---|
| 452 | pass |
|---|
| 453 | |
|---|
| 454 | |
|---|
| 455 | class SubContributionMFRegistry(MaterialFactoryRegistry): |
|---|
| 456 | pass |
|---|