Version initiale.
[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 $Id: container.py 622 2008-11-16 23:38:18Z pin $
13 $URL: http://svn.luxia.fr/svn/labo/projects/zope/Portfolio/trunk/container.py $
14 """
15
16 from AccessControl import ClassSecurityInfo, Unauthorized
17 from Globals import InitializeClass
18 from zExceptions import NotFound
19 from zope.component.factory import Factory
20 from BTrees.OOBTree import OOSet
21 from Products.CMFCore.permissions import ModifyPortalContent, View
22 from Products.CMFCore.utils import getToolByName
23 from Products.Plinn.HugePlinnFolder import HugePlinnFolder
24 from random import randrange
25
26
27 class Portfolio(HugePlinnFolder) :
28 """ Container for photos """
29
30 security = ClassSecurityInfo()
31
32 def __init__( self, id, title='' ) :
33 super__init__ = super(Portfolio, self).__init__
34 super__init__(id, title=title)
35 self.samplePhotoPath = None
36 self.presentation_page = None
37
38
39 security.declareProtected(View, 'randomPhoto')
40 def randomPhoto(self):
41 " return a ramdom photo or None "
42 ctool = getToolByName(self, 'portal_catalog')
43 res = ctool(path = '/'.join(self.getPhysicalPath()),
44 portal_type='Photo')
45 length = len(res)
46 if length :
47 brain = res[randrange(length)]
48 infos = { 'src' : '%s/getThumbnail' % brain.getURL()
49 , 'alt' : brain.Title}
50 size = brain.getThumbnailSize
51 infos.update(size)
52 return infos
53 else :
54 return None
55
56 security.declareProtected(ModifyPortalContent, 'setSamplePhoto')
57 def setSamplePhoto(self, photoPath):
58 """ set photo used to represents portfolio content.
59 """
60 self.samplePhotoPath = photoPath
61 return True
62
63 security.declareProtected(View, 'samplePhoto')
64 def samplePhoto(self):
65 """ returns sample photo or random photo if not found.
66 """
67 if self.samplePhotoPath is None :
68 return self.randomPhoto()
69 else :
70 try :
71 sample = self.restrictedTraverse(self.samplePhotoPath)
72 infos = { 'src' : '%s/getThumbnail' % sample.absolute_url()
73 , 'alt' : sample.Title()}
74 size = sample.getThumbnailSize()
75 infos.update(size)
76 return infos
77
78 except (KeyError, NotFound) :
79 self.samplePhotoPath = None
80 return self.randomPhoto()
81 except Unauthorized :
82 return self.randomPhoto()
83
84 security.declareProtected(View, 'hasPresentationPage')
85 def hasPresentationPage(self):
86 return self.presentation_page is not None
87
88
89 security.declareProtected(ModifyPortalContent, 'createPresentationPage')
90 def createPresentationPage(self):
91 #create a presentation page
92 self.presentation_page = ''
93 return True
94
95 security.declareProtected(ModifyPortalContent, 'deletePresentationPage')
96 def deletePresentationPage(self):
97 self.presentation_page = None
98 return True
99
100
101 security.declareProtected(ModifyPortalContent, 'editPresentationPage')
102 def editPresentationPage(self, text):
103 """editPresentationPage documentation
104 """
105 self.presentation_page = text
106 self.reindexObject()
107 return True
108
109 security.declareProtected(View, 'SearchableText')
110 def SearchableText(self):
111 base = super(Portfolio, self).SearchableText()
112 if self.hasPresentationPage() :
113 return '%s %s' % (base, self.presentation_page)
114 else :
115 return base
116
117
118
119 InitializeClass(Portfolio)
120
121 PortfolioFactory = Factory(Portfolio)