source: trunk/Collection.py @ 433

Revision 433, 6.6 KB checked in by jukka, 13 years ago (diff)

Fixed #505, spent 1h. Made a testing place for Collections, but its still too empty.

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        print new
134        field.set(self, new)
135
136    def moveDownResources(self, REQUEST):
137        """ move down """
138        id = int(REQUEST.get("id"))-1
139        field = self.Schema().get('refsToResources')
140        prev = field.getRaw(self, aslist=True)
141        new = []
142        counter = 0
143        for x in prev:
144            new.append(x)
145            counter = counter + 1
146        if id < counter-1:
147           temp = new[id+1]
148           new[id+1] = new[id]
149           new[id] = temp
150        print new
151        field.set(self, new)
152
153    def isReadyForStory(self):
154        """ returns true or false. Write a story button appears only when
155        there is at least one object from each three sections """
156        has_content = 0
157        has_act = 0
158        has_tool = 0
159        for x in self.getRefsToResources():
160            if x.meta_type in ['Piece', 'Material']: has_content = 1
161            if x.meta_type == 'Activity': has_act = 1
162            if x.meta_type == 'Tool': has_tool = 1
163        if 1 == has_content == has_act == has_tool:
164            return True
165        return False
166
167    def amIOwner(self):
168        """ check owner of object """
169        return self.whoami() == str(self.getOwner())
170
171    def manage_afterAdd(self, item, container):
172        """Replaces the left side portlets with the content type's own action portlet."""
173        BaseContent.manage_afterAdd(self, item, container)
174        if not hasattr(item.aq_base, 'left_slots'):
175            self._setProperty('left_slots', ['here/portlet_%s_actions/macros/portlet' % item.meta_type.lower(),], 'lines')
176
177registerType(Collection, PROJECTNAME)
178
179class CollectionsFolder(BaseFolder):
180    """ container for collection """
181    schema = BaseSchema
182    id = "collections"
183    meta_type = "CollectionsFolder"
184    archetype_name = "CollectionsFolder" 
185
186    actions= (
187    {
188    'id':'view',
189    'name':'view',
190    'action':'string:${object_url}/collections_list',
191    'permission':('View',),
192    },
193    )
194
195    def getCollections(self, obj_id=None):
196        """ return a list of user's collections.
197            When REQUEST.obj_id is set filter collections where object is used
198            XXX: maybe search can be used here too!
199        """
200        collections = self.objectValues('Collection')
201        if obj_id is None:
202            return collections
203        res = []
204        for c in collections:
205            refs = c.getRefsToResources(aslist=True)
206            for r in refs:
207                if r.getId() == obj_id:
208                    res.append(c)
209                    break
210        return res
211
212registerType(CollectionsFolder, PROJECTNAME)
Note: See TracBrowser for help on using the repository browser.