source: trunk/Collection.py @ 459

Revision 459, 6.7 KB checked in by vahur, 13 years ago (diff)

closes #463 spent 2h

Line 
1# Copyright 2006 by the LeMill Team (see AUTHORS)
2#
3# This file is part of LeMill.
4#
5# LeMill is free software; you can redistribute it and/or modify
6# it under the terms of the GNU General Public License as published by
7# the Free Software Foundation; either version 2 of the License, or
8# (at your option) any later version.
9#
10# LeMill is distributed in the hope that it will be useful,
11# but WITHOUT ANY WARRANTY; without even the implied warranty of
12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13# GNU General Public License for more details.
14#
15# You should have received a copy of the GNU General Public License
16# along with LeMill; if not, write to the Free Software
17# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18
19from Products.Archetypes.public import *
20from Products.ATReferenceBrowserWidget.ATReferenceBrowserWidget import ReferenceBrowserWidget
21from Products.CMFCore.permissions import ModifyPortalContent
22from Products.Archetypes.public import BaseContent, registerType, BaseFolder
23from Products.Archetypes.public import StringField
24from Products.Archetypes.public import TextAreaWidget
25from Products.Archetypes.atapi import DisplayList
26from Globals import InitializeClass
27from Products.CMFCore.utils import getToolByName
28from AccessControl import ClassSecurityInfo, Unauthorized
29from config import PROJECTNAME
30
31schema = BaseSchema + Schema((
32
33    StringField('description',
34    widget=TextAreaWidget(
35        label="Description",
36        description="",
37        label_msgid='label_description',
38        description_msgid='description_description',
39        visible={'view':'invisible','edit':'visible'},
40        ),
41    ),
42   
43    ReferenceField('refsToResources',
44        accessor = 'getRefsToResources',
45        relationship = 'References',
46        mutator = 'addRefsToResources',
47        allowed_types = ('Piece','Material','Tool','Activity'),
48        multiValued = True,
49        widget = ReferenceBrowserWidget(
50            visible = {'view':'invisible', 'edit':'invisible'},
51            ),
52    ),
53))
54
55class Collection(BaseContent):
56    schema = schema
57    actions= (
58    {
59    'id':'view',
60    'name':'view',
61    'action':'string:${object_url}/collection_view',
62    'permission':('View',),
63    },
64    {
65    'id':'edit',
66    'name':'Edit',
67    'action':'string:${object_url}/base_edit',
68    'permission':('View',),
69    },
70    )
71    meta_type = "Collection"
72    archetype_name = "Collection" 
73    global_allow = 1
74    _at_rename_after_creation = True
75
76
77    def at_post_create_script(self):
78        self.at_post_edit_script()
79
80    def at_post_edit_script(self):
81        self._renameAfterCreation()
82
83    def after_add_rename(self):
84        self._renameAfterCreation()
85
86
87    def addRefsToResources(self, ref):
88        """ add reference to resource """
89        field = self.Schema().get('refsToResources')
90        prev = field.getRaw(self, aslist=True)
91        new_value = []
92        from types import ListType, TupleType
93        if type(ref) == ListType or type(ref) == TupleType:
94            new_value = prev
95            for x in ref:
96                new_value.append(x)
97        else:
98            new_value = prev+[ref,]
99        field.set(self, new_value)
100
101    def getItemCount(self):
102        """ return how many items are in collection """
103        return len(self.getRefsToResources())
104
105    def getResources(self):
106        """ ... """
107        return self.getRefsToResources()
108
109    def delResources(self, REQUEST):
110        """ delete selected resource """
111        field = self.Schema().get('refsToResources')
112        prev = field.getRaw(self, aslist=True)
113        counter = 1
114        new = []
115        for x in prev:
116            if REQUEST.get(str(counter)) != "True":
117                new.append(x)
118            counter = counter + 1
119        field.set(self, new)
120
121    def moveUpResources(self, REQUEST):
122        """ move up """
123        id = int(REQUEST.get("id"))-1
124        field = self.Schema().get('refsToResources')
125        prev = field.getRaw(self, aslist=True)
126        new = []
127        for x in prev:
128            new.append(x)
129        if id > 0:
130           temp = new[id-1]
131           new[id-1] = new[id]
132           new[id] = temp
133        field.set(self, new)
134
135    def moveDownResources(self, REQUEST):
136        """ move down """
137        id = int(REQUEST.get("id"))-1
138        field = self.Schema().get('refsToResources')
139        prev = field.getRaw(self, aslist=True)
140        new = []
141        counter = 0
142        for x in prev:
143            new.append(x)
144            counter = counter + 1
145        if id < counter-1:
146           temp = new[id+1]
147           new[id+1] = new[id]
148           new[id] = temp
149        field.set(self, new)
150
151    def isReadyForStory(self):
152        """ returns true or false. Write a story button appears only when
153        there is at least one object from each three sections """
154        has_content = 0
155        has_act = 0
156        has_tool = 0
157        for x in self.getRefsToResources():
158            if x.meta_type in ['Piece', 'Material']: has_content = 1
159            if x.meta_type == 'Activity': has_act = 1
160            if x.meta_type == 'Tool': has_tool = 1
161        if 1 == has_content == has_act == has_tool:
162            return True
163        return False
164
165    def amIOwner(self):
166        """ check owner of object """
167        return self.whoami() == str(self.getOwner())
168
169    def manage_afterAdd(self, item, container):
170        """Replaces the left side portlets with the content type's own action portlet."""
171        BaseContent.manage_afterAdd(self, item, container)
172        if not hasattr(item.aq_base, 'left_slots'):
173            self._setProperty('left_slots', ['here/portlet_%s_actions/macros/portlet' % item.meta_type.lower(),], 'lines')
174
175registerType(Collection, PROJECTNAME)
176
177class CollectionsFolder(BaseFolder):
178    """ container for collection """
179    schema = BaseSchema
180    id = "collections"
181    meta_type = "CollectionsFolder"
182    archetype_name = "CollectionsFolder" 
183
184    actions= (
185    {
186    'id':'view',
187    'name':'view',
188    'action':'string:${object_url}/collections_list',
189    'permission':('View',),
190    },
191    )
192
193    def getCollections(self, obj_uid=None):
194        """ return a list of user's collections.
195            When REQUEST.obj_uid is set filter collections where object is used
196            XXX: maybe this can be done in more elegant way
197        """
198        res = []
199        if obj_uid is None:
200            return self.objectValues('Collection')
201        q = { 'targetUID': obj_uid }
202        qres = self.reference_catalog(q)
203        for q in qres:
204            v = self.reference_catalog.lookupObject(q.UID)
205            source = v.getSourceObject()
206            if source.meta_type == 'Collection':
207                res.append(source)
208        return res
209
210registerType(CollectionsFolder, PROJECTNAME)
Note: See TracBrowser for help on using the repository browser.