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