getattr -> restrictedTraverse : si on utilise geattr au lieu de restrictedTraverse...
[Portfolio.git] / container.py
1 # -*- coding: utf-8 -*-
2 ############################################################
3 # Copyright © 2005-2008 Benoît PIN <benoit.pin@ensmp.fr> #
4 # Plinn - http://plinn.org #
5 # #
6 # This program is free software; you can redistribute it #
7 # and/or modify it under the terms of the Creative Commons #
8 # "Attribution-Noncommercial 2.0 Generic" #
9 # http://creativecommons.org/licenses/by-nc/2.0/ #
10 ############################################################
11 """ container classes for photo storage.
12 """
13
14 from AccessControl import ClassSecurityInfo, Unauthorized
15 from Globals import InitializeClass
16 from zExceptions import NotFound
17 from zope.component.factory import Factory
18 from BTrees.OOBTree import OOSet
19 from Products.CMFCore.permissions import ModifyPortalContent, View
20 from Products.CMFCore.utils import getToolByName
21 from Products.Plinn.HugePlinnFolder import HugePlinnFolder
22 from random import randrange
23
24
25 class Portfolio(HugePlinnFolder) :
26 """ Container for photos """
27
28 security = ClassSecurityInfo()
29
30 def __init__( self, id, title='' ) :
31 super__init__ = super(Portfolio, self).__init__
32 super__init__(id, title=title)
33 self.samplePhotoPath = None
34 self.presentation_page = None
35
36
37 security.declareProtected(View, 'randomPhoto')
38 def randomPhoto(self):
39 " return a ramdom photo or None "
40 ctool = getToolByName(self, 'portal_catalog')
41 res = ctool(path = '/'.join(self.getPhysicalPath()),
42 portal_type='Photo')
43 length = len(res)
44 if length :
45 brain = res[randrange(length)]
46 infos = { 'src' : '%s/getThumbnail' % brain.getURL()
47 , 'alt' : brain.Title}
48 size = brain.getThumbnailSize
49 infos.update(size)
50 return infos
51 else :
52 return None
53
54 security.declareProtected(ModifyPortalContent, 'setSamplePhoto')
55 def setSamplePhoto(self, photoPath):
56 """ set photo used to represents portfolio content.
57 """
58 self.samplePhotoPath = photoPath
59 return True
60
61 security.declareProtected(View, 'samplePhoto')
62 def samplePhoto(self):
63 """ returns sample photo or random photo if not found.
64 """
65 if self.samplePhotoPath is None :
66 return self.randomPhoto()
67 else :
68 try :
69 sample = self.restrictedTraverse(self.samplePhotoPath)
70 infos = { 'src' : '%s/getThumbnail' % sample.absolute_url()
71 , 'alt' : sample.Title()}
72 size = sample.getThumbnailSize()
73 infos.update(size)
74 return infos
75
76 except (KeyError, NotFound) :
77 self.samplePhotoPath = None
78 return self.randomPhoto()
79 except Unauthorized :
80 return self.randomPhoto()
81
82 security.declareProtected(View, 'hasPresentationPage')
83 def hasPresentationPage(self):
84 return self.presentation_page is not None
85
86
87 security.declareProtected(ModifyPortalContent, 'createPresentationPage')
88 def createPresentationPage(self):
89 #create a presentation page
90 self.presentation_page = ''
91 return True
92
93 security.declareProtected(ModifyPortalContent, 'deletePresentationPage')
94 def deletePresentationPage(self):
95 self.presentation_page = None
96 return True
97
98
99 security.declareProtected(ModifyPortalContent, 'editPresentationPage')
100 def editPresentationPage(self, text):
101 """editPresentationPage documentation
102 """
103 self.presentation_page = text
104 self.reindexObject()
105 return True
106
107 security.declareProtected(View, 'SearchableText')
108 def SearchableText(self):
109 base = super(Portfolio, self).SearchableText()
110 if self.hasPresentationPage() :
111 return '%s %s' % (base, self.presentation_page)
112 else :
113 return base
114
115
116
117 InitializeClass(Portfolio)
118
119 PortfolioFactory = Factory(Portfolio)