source: trunk/Collection.py @ 551

Revision 551, 6.9 KB checked in by vahur, 13 years ago (diff)

closes #571 spent 3m
defined accessor

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, ALL_CONTENT_TYPES, CONTENT_TYPES
30
31schema = BaseSchema + Schema((
32
33    StringField('description',
34        accessor='Description',
35        widget=TextAreaWidget(
36            label="Description",
37            description="",
38            label_msgid='label_description',
39            description_msgid='description_description',
40            visible={'view':'invisible','edit':'visible'},
41        ),
42    ),
43   
44    ReferenceField('refsToResources',
45        accessor = 'getRefsToResources',
46        relationship = 'References',
47        mutator = 'addRefsToResources',
48        allowed_types = ALL_CONTENT_TYPES,
49        multiValued = True,
50        widget = ReferenceBrowserWidget(
51            visible = {'view':'invisible', 'edit':'invisible'},
52            ),
53    ),
54))
55
56class Collection(BaseContent):
57    """Collection"""
58   
59    schema = schema
60    actions= (
61    {
62    'id':'view',
63    'name':'view',
64    'action':'string:${object_url}/collection_view',
65    'permission':('View',),
66    },
67    {
68    'id':'edit',
69    'name':'Edit',
70    'action':'string:${object_url}/base_edit',
71    'permission':('View',),
72    },
73    )
74    meta_type = "Collection"
75    archetype_name = "Collection" 
76    typeDescription="Collection of resources."
77    typeDescMsgId='description_collection'
78    global_allow = 1
79    _at_rename_after_creation = True
80
81
82    def at_post_create_script(self):
83        self.at_post_edit_script()
84
85    def at_post_edit_script(self):
86        self._renameAfterCreation()
87
88    def after_add_rename(self):
89        self._renameAfterCreation()
90
91
92    def addRefsToResources(self, ref, empty=0):
93        """ add reference to resource """
94        field = self.Schema().get('refsToResources')
95        prev = field.getRaw(self, aslist=True)
96        new_value = []
97        from types import ListType, TupleType
98        if type(ref) == ListType or type(ref) == TupleType:
99            new_value = prev
100            if empty:
101                new_value = []
102            for x in ref:
103                new_value.append(x)
104        else:
105            new_value = prev+[ref,]
106        field.set(self, new_value)
107
108    def getItemCount(self):
109        """ return how many items are in collection """
110        return len(self.getRefsToResources())
111
112    def getResources(self):
113        """ ... """
114        return self.getRefsToResources()
115
116    def delResources(self, REQUEST):
117        """ delete selected resource """
118        field = self.Schema().get('refsToResources')
119        prev = field.getRaw(self, aslist=True)
120        counter = 1
121        new = []
122        for x in prev:
123            if REQUEST.get(str(counter)) != "True":
124                new.append(x)
125            counter = counter + 1
126        field.set(self, new)
127
128    def moveUpResources(self, REQUEST):
129        """ move up """
130        id = int(REQUEST.get("id"))-1
131        field = self.Schema().get('refsToResources')
132        prev = field.getRaw(self, aslist=True)
133        new = []
134        for x in prev:
135            new.append(x)
136        if id > 0:
137           temp = new[id-1]
138           new[id-1] = new[id]
139           new[id] = temp
140        field.set(self, new)
141
142    def moveDownResources(self, REQUEST):
143        """ move down """
144        id = int(REQUEST.get("id"))-1
145        field = self.Schema().get('refsToResources')
146        prev = field.getRaw(self, aslist=True)
147        new = []
148        counter = 0
149        for x in prev:
150            new.append(x)
151            counter = counter + 1
152        if id < counter-1:
153           temp = new[id+1]
154           new[id+1] = new[id]
155           new[id] = temp
156        field.set(self, new)
157
158    def isReadyForStory(self):
159        """ returns true or false. Write a story button appears only when
160        there is at least one object from each three sections """
161        has_content = 0
162        has_act = 0
163        has_tool = 0
164        for x in self.getRefsToResources():
165            if x.meta_type in CONTENT_TYPES: has_content = 1
166            if x.meta_type == 'Activity': has_act = 1
167            if x.meta_type == 'Tool': has_tool = 1
168        if 1 == has_content == has_act == has_tool:
169            return True
170        return False
171
172    def amIOwner(self):
173        """ check owner of object """
174        return self.whoami() == str(self.getOwner())
175
176    def manage_afterAdd(self, item, container):
177        """Replaces the left side portlets with the content type's own action portlet."""
178        BaseContent.manage_afterAdd(self, item, container)
179        if not hasattr(item.aq_base, 'left_slots'):
180            self._setProperty('left_slots', ['here/portlet_%s_actions/macros/portlet' % item.meta_type.lower(),], 'lines')
181
182registerType(Collection, PROJECTNAME)
183
184class CollectionsFolder(BaseFolder):
185    """ container for collection """
186    schema = BaseSchema
187    id = "collections"
188    meta_type = "CollectionsFolder"
189    archetype_name = "CollectionsFolder" 
190
191    actions= (
192    {
193    'id':'view',
194    'name':'view',
195    'action':'string:${object_url}/collections_list',
196    'permission':('View',),
197    },
198    )
199
200    def getCollections(self, obj_uid=None):
201        """ return a list of user's collections.
202            When REQUEST.obj_uid is set filter collections where object is used
203            XXX: maybe this can be done in more elegant way
204        """
205        res = []
206        if obj_uid is None:
207            return self.objectValues('Collection')
208        q = { 'targetUID': obj_uid }
209        qres = self.reference_catalog(q)
210        for q in qres:
211            v = self.reference_catalog.lookupObject(q.UID)
212            source = v.getSourceObject()
213            if source.meta_type == 'Collection':
214                res.append(source)
215        return res
216
217registerType(CollectionsFolder, PROJECTNAME)
Note: See TracBrowser for help on using the repository browser.