source: trunk/Collection.py @ 1911

Revision 1911, 32.1 KB checked in by gabor, 12 years ago (diff)

ref #738 spent 60h, Rewritten the code. It is now possible to download collections, all files are in the zip including HTML and media files. All three flash contents works, as well, including Pilots (XML files are processed). Still needs testing, creating download links, handling links in the HTML files, enhancing the standalone_view.pt file and creating SCORM manifest files.

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 SharedMetadata import score
30
31from config import PROJECTNAME, ALL_CONTENT_TYPES, CONTENT_TYPES, DEFAULT_ICONS, ACTIVITY_TYPES, TOOLS_TYPES
32from Resources import Resource
33from permissions import MODIFY_CONTENT
34
35import sre
36from cStringIO import StringIO
37import zipfile
38from urllib2 import urlopen, HTTPError, URLError
39
40schema = BaseSchema + score + Schema((
41
42    StringField('description',
43        accessor='Description',
44        widget=TextAreaWidget(
45            label="Learning and teaching story",
46            description="",
47            label_msgid='label_learning_story',
48            description_msgid='description_description',
49            i18n_domain = "lemill",
50            visible={'view':'invisible','edit':'visible'},
51        ),
52    ),
53   
54    ReferenceField('refsToResources',
55        accessor = 'getRefsToResources',
56        relationship = 'References',
57        mutator = 'addRefsToResources',
58        allowed_types = ALL_CONTENT_TYPES,
59        multiValued = True,
60        widget = ReferenceBrowserWidget(
61            visible = {'view':'invisible', 'edit':'invisible'},
62            ),
63    ),
64    ReferenceField('relatedContent',
65        relationship = 'relatesToContent',
66        multiValued = True,
67        isMetadata = True,
68        languageIndependent = False,
69        index = 'KeywordIndex',
70        write_permission = ModifyPortalContent,
71        allowed_types= ALL_CONTENT_TYPES,
72        widget = ReferenceBrowserWidget(
73            allow_search = True,
74            allow_browse = True,
75            show_indexes = False,
76            force_close_on_insert = True,
77            startup_directory = "content",
78            size = 4,
79            i18n_domain = "lemill",
80            label = "Related Content",
81            label_msgid = "label_related_content",
82            description = "",
83            description_msgid = "help_story_related_content",
84            visible = {'edit' : 'invisible', 'view' : 'invisible' }
85            )
86        ),
87    ReferenceField('relatedMethods',
88        relationship = 'relatesToMethods',
89        multiValued = True,
90        isMetadata = True,
91        languageIndependent = False,
92        index = 'KeywordIndex',
93        write_permission = ModifyPortalContent,
94        allowed_types=('Document','Activity',),
95        widget = ReferenceBrowserWidget(
96            allow_search = True,
97            allow_browse = True,
98            show_indexes = False,
99            force_close_on_insert = True,
100            startup_directory = "methods",
101            size = 4,
102            i18n_domain = "lemill",
103            label = "Related Methods",
104            label_msgid = "label_related_methods",
105            description = "",
106            description_msgid = "help_story_related_methods",
107            visible = {'edit' : 'invisible', 'view' : 'invisible' }
108            )
109        ),
110    ReferenceField('relatedTools',
111        relationship = 'relatesToTools',
112        multiValued = True,
113        isMetadata = True,
114        languageIndependent = False,
115        index = 'KeywordIndex',
116        write_permission = ModifyPortalContent,
117        allowed_types=('Document','Tool',),
118        widget = ReferenceBrowserWidget(
119            allow_search = True,
120            allow_browse = True,
121            show_indexes = False,
122            force_close_on_insert = True,
123            startup_directory = "tools",
124            size = 4,
125
126            i18n_domain = "lemill",
127            label = "Related Tools",
128            label_msgid = "label_related_tools",
129            description = "",
130            description_msgid = "help_story_related_tools",
131            visible = {'edit' : 'invisible', 'view' : 'invisible' }
132            )
133        ),
134    ComputedField('goodStory',
135        index = 'FieldIndex',
136        expression = 'here.isThisGoodStory()',
137        isMetadata = True
138        )
139))
140
141schema = schema.copy()
142
143class Collection(Resource):
144    """Collection"""
145   
146    schema = schema
147    actions= (
148    {
149    'id':'view',
150    'name':'view',
151    'action':'string:${object_url}/collection_view',
152    'permission':('View',),
153    },
154    {
155    'id':'edit',
156    'name':'Edit',
157    'action':'string:${object_url}/base_edit',
158    'permission':('View',),
159    },
160    )
161    meta_type = "Collection"
162    archetype_name = "Collection" 
163    typeDescription="Collection of resources."
164    typeDescMsgId='description_collection'
165    global_allow = 1
166    security = ClassSecurityInfo()
167    _at_rename_after_creation = True
168
169    def at_post_create_script(self):
170        self.at_post_edit_script()
171
172    def at_post_edit_script(self):
173        self._renameAfterCreation()
174        self.setGoodStory()
175
176    def after_add_rename(self):
177        self._renameAfterCreation()
178       
179    def setGoodStory(self):
180        """ Update goodStory -flag """
181        # after trying to get field and then setting the field by recalculating self.isThisGoodStory()
182        # it appears to be *always* up-to-date. (field.get()==self.isThisGoodStory())
183        # so the problem is indexing.
184        self.reindexObject()
185               
186#        field = self.getField('goodStory')
187#        wasgood=field.get(self)
188#        isgood= self.isThisGoodStory()
189#        print 'setting story %s. wasgood:%s isgood:%s' % (self.getId(), wasgood, isgood)
190#        ct = getToolByName(self,'portal_catalog')
191#        print ct({'getGoodStory':True})
192#        self.reindexObject()
193#        print ct({'getGoodStory':True})
194#        if wasgood!=isgood:
195#            field.set(self, isgood)
196#            self.reindexObject()
197#            ct = self.getToolByName('portal_catalog')
198           
199
200    def getLargestCount(self, reftype='relatedContent'):
201        field = self.Schema().get(reftype)
202        rtype=field.relationship
203        f = self.getReferenceImpl(relationship=rtype)
204
205        highest = 1001
206        for a in f:
207            try:
208                highest = a.collection_position
209            except AttributeError:
210                pass
211        highest += 100
212        return highest
213
214
215    security.declareProtected(MODIFY_CONTENT,'addRelatedContent')
216    def addRelatedContent(self, ref, empty=0):
217        """ add reference to content """
218        self.addRefsToResources(ref, empty, reftype="relatedContent")
219        self.setGoodStory()
220
221    security.declareProtected(MODIFY_CONTENT,'addRelatedMethods')
222    def addRelatedMethods(self, ref, empty=0):
223        """ add reference to methods """
224        self.addRefsToResources(ref, empty, reftype="relatedMethods")
225        self.setGoodStory()
226
227    security.declareProtected(MODIFY_CONTENT,'addRelatedTools')
228    def addRelatedTools(self, ref, empty=0):
229        """ add reference to tools """
230        self.addRefsToResources(ref, empty, reftype="relatedTools")
231        self.setGoodStory()
232
233    security.declareProtected(MODIFY_CONTENT,'addRefsToResources')
234    def addRefsToResources(self, ref, empty=0, reftype=None):
235        """ add reference to resource """
236        from types import ListType, TupleType
237        if reftype:
238            field = self.Schema().get(reftype)
239        else:
240            if type(ref) == ListType or type(ref) == TupleType:
241                for x in ref: # if entry is a list of mixed type entries, run this method again for each component.
242                    self.addRefsToResources(x)
243                return 1
244            uc=getToolByName(self, 'uid_catalog')
245            obj=uc(UID=ref)
246            objtype=obj[0].portal_type
247            if objtype in CONTENT_TYPES:
248                reftype='relatedContent'
249            elif objtype in ACTIVITY_TYPES:
250                reftype='relatedMethods'
251            elif objtype in TOOLS_TYPES:
252                reftype='relatedTools'
253            else:
254                reftype='refsToResources'
255            field = self.Schema().get(reftype)
256
257        prev = field.getRaw(self)
258        new_value = []
259        if type(ref) == ListType or type(ref) == TupleType:
260            new_value = prev
261            if empty:
262                new_value = []
263            for x in ref:
264                new_value.append(x)
265        else:
266            new_value = prev+[ref,]
267        field.set(self, new_value, collection_position=self.getLargestCount(reftype))
268        self.setGoodStory()
269
270    def isThisGoodStory(self):
271        """ Check if there is content,method,tool and description """       
272        desc=self.Description()
273        c=self.getRelatedContent()
274        m=self.getRelatedMethods()
275        t=self.getRelatedTools()
276        wtool=getToolByName(self, 'portal_workflow')
277        r= wtool.getInfoFor(self,'review_state',None)!='deleted'
278        if desc and c and m and t and r:
279            return True
280        else:
281            return False
282
283    def getItemCount(self):
284        """ return how many items are in collection """
285        return len(self.getRelatedMethods())+len(self.getRelatedTools())+len(self.getRelatedContent())
286
287
288    def getRelatedContent(self):
289        """ make sure that related content comes in correct order even when programmer forgets to ask for it"""
290        return self.getResources()
291
292    def getSortedList(self, reftype='relatedContent'):
293        #f = self.getField('refsToResources')
294        field = self.Schema().get(reftype)
295        rtype=field.relationship
296        f = self.getReferenceImpl(relationship=rtype)
297        arr = []
298        err_base = 12345123
299        for a in f:
300            try:
301                i = a.collection_position
302            except AttributeError:
303                i = err_base + 100
304                a.collection_position = i
305            arr.append([i, a])
306        arr.sort()
307        return arr
308
309    def getBlurp(self):
310        """ Some 1000 chars or something FIXME TO FIND NICE PLACES TO CUT (<p><br> etc.)"""
311        lt = getToolByName(self, 'lemill_tool')
312        return lt.split_at_p_or_br(lt.shorten_link_names(lt.htmlify(self.Description())))
313
314    def getResources(self, reftype='relatedContent'):
315        """ ... """
316        #return self.getRefsToResources()
317        sorted = self.getSortedList(reftype)
318        arr = []
319        for x in sorted:
320            arr.append(x[1].getTargetObject())
321        # Only Tools and Methods should get sorted by name
322        if reftype != 'relatedContent':
323            mod_resources = [(x.Title(), x) for x in arr]
324            mod_resources.sort()
325            arr = [x[1] for x in mod_resources]
326        return arr
327
328
329    security.declareProtected(MODIFY_CONTENT,'delContent')
330    def delContent(self, objid,REQUEST=None):
331        """ front for delResources """
332        if objid:
333            self.delResources(objid, reftype='relatedContent')
334        if REQUEST:
335            return REQUEST.RESPONSE.redirect(self.absolute_url() + "/collection_edit")
336
337    security.declareProtected(MODIFY_CONTENT,'delMethods')
338    def delMethods(self, objid,REQUEST=None):
339        """ front for delResources """
340        if objid:
341            self.delResources(objid, reftype='relatedMethods')
342        if REQUEST:
343            return REQUEST.RESPONSE.redirect(self.absolute_url() + "/collection_edit")
344
345    security.declareProtected(MODIFY_CONTENT,'delTools')
346    def delTools(self, objid,REQUEST=None):
347        """ front for delResources """
348        if objid:
349            self.delResources(objid, reftype='relatedTools')
350        if REQUEST:
351            return REQUEST.RESPONSE.redirect(self.absolute_url() + "/collection_edit")
352
353    security.declareProtected(MODIFY_CONTENT,'delResources')
354    def delResources(self, objid, reftype='relatedContent'):
355        """ delete selected resource """
356        field = self.Schema().get(reftype)
357        objlist = field.get(self)
358
359        newlist = [o.UID() for o in objlist if o.id!=objid]
360        field.set(self, newlist)
361        # if collection is empty, delete it too
362#        if new == []:
363#            self.collections.delCollection(self.getId())
364#            return True
365#        else:
366#            return False
367        self.setGoodStory()
368        return False
369
370    def getCollectionContent(self):
371        """ Returns a list of content, methods and tools, ordered by their modification date """
372        objects=self.getRelatedContent()+self.getRelatedMethods()+self.getRelatedTools()
373        objects = [(x.ModificationDate(), x) for x in objects]
374        objects.sort()
375        return [x[1] for x in objects]
376       
377
378
379    security.declareProtected(MODIFY_CONTENT,'moveUpContent')
380    def moveUpContent(self, objid,REQUEST=None):
381        """ Front for more general moveUpResources """
382        if objid:
383            self.moveUpResources(objid, reftype='relatedContent')
384        return REQUEST.RESPONSE.redirect(self.absolute_url() + "/collection_edit")
385
386    security.declareProtected(MODIFY_CONTENT,'moveUpResources')
387    def moveUpResources(self, objid, reftype='relatedContent'):
388        """ move up """
389        field = self.getField(reftype)
390        sorted = self.getSortedList(reftype)
391        cur_pos = 0
392        count = 0
393        for x in sorted:
394            if x[1].getTargetObject().id == objid:
395                cur_pos = count
396                break
397            count += 1
398        if cur_pos > 0:
399            prev_uid = sorted[cur_pos-1][1].getTargetObject().UID()
400            prev_pos = sorted[cur_pos-1][1].collection_position
401            curr_uid = sorted[cur_pos][1].getTargetObject().UID()
402            curr_pos = sorted[cur_pos][1].collection_position
403
404            sorted.remove(sorted[cur_pos])
405            sorted.remove(sorted[cur_pos-1])
406            new_list = []
407            [ new_list.append(x[1].getTargetObject()) for x in sorted ]
408           
409            field.set(self, new_list)
410            new_list.append(curr_uid)
411            field.set(self, new_list, collection_position=prev_pos)
412            new_list.append(prev_uid)
413            field.set(self, new_list, collection_position=curr_pos)
414            #new_list = []
415            #for x in sorted:
416            #    new_list.append(x[1].getTargetObject())
417            #field.set(self, new_list)
418
419
420    security.declareProtected(MODIFY_CONTENT,'moveDownContent')
421    def moveDownContent(self, objid,REQUEST=None):
422        """ Front for more general moveDownResources """
423        if objid:
424            self.moveDownResources(objid, reftype='relatedContent')
425        return REQUEST.RESPONSE.redirect(self.absolute_url() + "/collection_edit")
426
427
428    security.declareProtected(MODIFY_CONTENT,'moveDownResources')
429    def moveDownResources(self, objid, reftype='relatedContent'):
430        """ move down """
431        field = self.getField(reftype)
432        sorted = self.getSortedList(reftype=reftype)
433        cur_pos = 0
434        count = 0
435        for x in sorted:
436            if x[1].getTargetObject().id == objid:
437                cur_pos = count
438                break
439            count += 1
440        if cur_pos+1 < len(sorted):
441            next_uid = sorted[cur_pos+1][1].getTargetObject().UID()
442            next_pos = sorted[cur_pos+1][1].collection_position
443            curr_uid = sorted[cur_pos][1].getTargetObject().UID()
444            curr_pos = sorted[cur_pos][1].collection_position
445
446            sorted.remove(sorted[cur_pos+1])
447            sorted.remove(sorted[cur_pos])
448            new_list = []
449            [ new_list.append(x[1].getTargetObject()) for x in sorted ]
450           
451            field.set(self, new_list)
452            new_list.append(curr_uid)
453            field.set(self, new_list, collection_position=next_pos)
454            new_list.append(next_uid)
455            field.set(self, new_list, collection_position=curr_pos)
456
457    def amIOwner(self):
458        """ check owner of object """
459        roles = self.portal_membership.getAuthenticatedMember().getRolesInContext(self)
460        return 'Owner' in roles
461
462
463    def manage_afterAdd(self, item, container):
464        #Replaces the left side portlets with the content type's own action portlet.
465        BaseContent.manage_afterAdd(self, item, container)
466        if not hasattr(item.aq_base, 'left_slots'):
467            self._setProperty('left_slots', ['here/portlet_%s_actions/macros/portlet' % item.meta_type.lower(),], 'lines')
468
469    security.declareProtected(MODIFY_CONTENT,'cleanRefsToResources')
470    def cleanRefsToResources(self):
471        """ Check if (deprecated) RefsToResources contains stuff that should be in relatedContent etc. """
472        if not self.Schema().get('relatedContent'): # update schema if old type collection
473            #print 'schema update required for %s' % self.getId()
474            self._updateSchema()
475        objlist=self.getRefsToResources()
476        field=self.getField('refsToResources')
477        newlist=[]
478        for o in objlist:
479            objtype=o.meta_type
480            if objtype in CONTENT_TYPES:
481                reftype='relatedContent'
482            elif objtype in ACTIVITY_TYPES:
483                reftype='relatedMethods'
484            elif objtype in TOOLS_TYPES:
485                reftype='relatedTools'
486            else:
487                reftype='keeper'
488
489            if reftype=='keeper':
490                newlist.append(o.UID())
491            else:
492                self.addRefsToResources(o.UID(),reftype=reftype)
493        field.set(self, newlist)
494
495    def getLearningStoryText(self):
496        """ will return the nice version for the story text """
497        lt = getToolByName(self, 'lemill_tool')
498        return lt.shorten_link_names(lt.htmlify(self.Description()))
499
500#Zip / SCORM download stuff begin --------------------------------------------------------------------------------
501    def _splitHTML(self, htmlPage):
502
503        def flatten(seq):
504            res = []
505            for item in seq:
506                if isinstance(item, (list, tuple)):
507                    res.extend(flatten(item))
508                else:
509                    res.append(item)
510            return res
511
512        def combineNones(inputList, infoList):
513            previousInfo = ''
514            text = ''
515            list = range(len(infoList))
516            list.reverse()
517            for i in list:
518                if infoList[i] == None:
519                    if previousInfo == None:
520                        text = inputList[i] + text
521                    else:
522                        previousInfo = None
523                        text = inputList[i]
524                    del infoList[i]
525                    del inputList[i]
526                elif previousInfo == None:
527                    previousInfo = ''
528                    infoList.insert(i+1, None)
529                    inputList.insert(i+1, text)
530            if previousInfo == None:
531                infoList.insert(0, None)
532                inputList.insert(0, text)
533
534        def reqursiveRegularSplit(inputList, infoList, info, reg, first, next):
535            if isinstance(inputList, (list, tuple)):
536                for i in range(len(inputList)):
537                    if not isinstance(infoList[i], str):
538                        inputList[i], infoList[i] = reqursiveRegularSplit(inputList[i], infoList[i], info, reg, first, next)
539            else:
540                reg = sre.compile(reg, sre.DOTALL | sre.IGNORECASE)
541                inputList = sre.split(reg, inputList)
542                if len(inputList) > 1:
543                    infoList = []
544                    for i in range(len(inputList)):
545                        if i % next == first:
546                            infoList.append(info)
547                        else:
548                            infoList.append(None)
549                    combineNones(inputList, infoList)
550                else:
551                    infoList = [infoList]
552            return inputList, infoList
553
554        splitHtml, infoList = reqursiveRegularSplit(htmlPage, None, 'base', '<base href="(.*?)".*?/>', 2, 4)
555        if len(splitHtml) > 1:
556            baseURL = splitHtml[-2]
557            del splitHtml[1::2]
558            del infoList[1::2]
559        else:
560            baseURL = self.portal_url()
561        if not baseURL.endswith('/'):
562            baseURL += '/'
563        splitHtml, infoList = reqursiveRegularSplit(splitHtml, infoList, None, r'<a[^>]*?>(\s*<img.*?>\s*)</a>', 1, 2)
564        splitHtml, infoList = reqursiveRegularSplit(splitHtml, infoList, 'fvars', '(<embed[^>]*?flashvars=")(.*?)(")', 2, 4)
565        splitHtml, infoList = reqursiveRegularSplit(splitHtml, infoList, 'XMLmp3', '(<voiceover[^>]+)(src=")(.*?)(")', 3, 5)
566        splitHtml, infoList = reqursiveRegularSplit(splitHtml, infoList, 'src', '(<[^>]+)(src=")(.*?)(")', 3, 5)
567        splitHtml, infoList = reqursiveRegularSplit(splitHtml, infoList, 'src', '(<param.+?name="movie".*?value=")(.*?)(")', 2, 4)
568        splitHtml, infoList = reqursiveRegularSplit(splitHtml, infoList, 'fvars', r"(<script.*?>.*?AC_FL_RunContent\(.*?'flashvars', ')(.*?)((?<!\\)'.*?</script>)", 2, 4)
569        splitHtml, infoList = reqursiveRegularSplit(splitHtml, infoList, 'jsfname', "(<script.*?>.*?AC_FL_RunContent\(.*?'movie', ')(.*?)(')", 2, 4)
570        splitHtml, infoList = reqursiveRegularSplit(splitHtml, infoList, 'fvars', '(<param.+?name="flashvars".*?value=")(.*?)(")', 2, 4)
571
572        return flatten(splitHtml), flatten(infoList), baseURL
573
574    def _getAbsoluteURL(self, url, info, baseURL=None):
575        flash = None
576        if info in ('src', 'jsfname', 'XMLmp3'):
577            if url.startswith('http://') or baseURL == None:
578                url2 = url
579            else:
580                url2 = baseURL + url
581            if info == 'jsfname':
582                url2 += '.swf'
583        elif info == 'fvars':
584            if url.startswith('file='):
585                url2 = url[5:-5]
586                flash = 'mp3'
587            elif url.startswith('xml='):
588                url2 = url[4:]
589                flash = 'pilot'
590            elif url.startswith("config={videoFile: '"):
591                url2 = url[20:-2]
592                flash = 'flv1'
593            elif url.startswith("config={videoFile: \\'"):
594                url2 = url[21:-3]
595                flash = 'flv2'
596        return url2, flash
597
598    def _getFileNameOfURL(self, url):
599        return sre.split(r'[/\\]', url.split('?')[0])[-1]
600
601    def _hasFileNameInSrcs(self, fileName, srcs):
602        for absURL in srcs:
603            src = srcs[absURL]
604            if src.has_key('fileName') and src['fileName'] == fileName:
605                return True, src
606        return False, None
607
608    def _splitFileName(self, fileName):
609        splitted = fileName.split('.')
610        if len(splitted) > 1:
611            return '.'.join(splitted[:-1]), splitted[-1]
612        return fileName, ''
613
614    def _findUniqueFileName(self, absURL, srcs):
615        fileName = self._getFileNameOfURL(absURL)
616        hasFileName, src2 = self._hasFileNameInSrcs(fileName, srcs)
617        if hasFileName:
618            src2['counter'] += 1
619            fileName, extension = self._splitFileName(fileName)
620            return self._findUniqueFileName('%s(%d)%s' % (fileName, src2['counter'], extension), srcs)
621        else:
622            return fileName
623
624    def _processHtml(self, splitHtml, infoList, srcs, baseURL, htmlIndex):
625        for i in range(len(infoList)):
626            if infoList[i] in ('src', 'jsfname', 'fvars', 'XMLmp3'):
627                absURL, flash = self._getAbsoluteURL(splitHtml[i], infoList[i], baseURL)
628                if not srcs.has_key(absURL):
629                    srcs[absURL] = {}
630                    src = srcs[absURL]
631                    src['fileName'] = self._findUniqueFileName(absURL, srcs)
632                    src['finalURL'] = ''
633                    src['usedBy'] = set([htmlIndex])
634                    src['counter'] = 0
635                    if flash == 'mp3':
636                        src['extension'] = 'mp3'
637                    elif flash == 'pilot':
638                        src['extension'] = 'xml'
639                    elif flash in ('flv1', 'flv2'):
640                        src['extension'] = 'flv'
641                    elif infoList[i] == 'XMLmp3':
642                        src['extension'] = 'mp3'
643                    else:
644                        src['extension'] = ''
645                else:
646                    srcs[absURL]['usedBy'].add(htmlIndex)
647
648    def _processPilotXMLs(self, srcs, baseURLs, baseDirs):
649        xmlDatas = {}
650        xmlList = [absURL for absURL in srcs if srcs[absURL]['extension'] == 'xml']
651        for absURL in xmlList:
652            try:
653                src = srcs[absURL]
654                file = urlopen(absURL)
655                #file = urlopen('http://localhost:81/buildXML.xml')
656                xml = file.read().decode('utf-16')
657                file.close()
658                xmlDatas[absURL]={}
659                xmlData = xmlDatas[absURL]
660                xmlData['split'], xmlData['info'], baseURL = self._splitHTML(xml)
661                xmlData['fileName'] = src['fileName']
662                xmlData['baseDirs'] = []
663                for htmlIndex in src['usedBy']:
664                    self._processHtml(xmlData['split'], xmlData['info'], srcs, baseURLs[htmlIndex], htmlIndex)
665                    xmlData['baseDirs'].append(baseDirs[htmlIndex])
666            except (HTTPError, URLError):
667                print "File download error: " + absURL
668        return xmlDatas
669
670    def _addExtension(self, fileName, extension):
671        name, ext = self._splitFileName(fileName)
672        if ext == '' and extension != '':
673            fileName = '%s.%s' % (fileName, extension)
674        return fileName
675
676    def _downloadFiles(self, srcs, zip, baseDirs = []):
677        for absURL in srcs:
678            src = srcs[absURL]
679            data = ''
680            subType = ''
681            try:
682                if src['extension'] != 'xml':
683                    file = urlopen(absURL)
684                    data = file.read()
685                    file.close()
686                    mediaType, subType = file.headers.getheader('Content-Type').split('/')
687                    subType = subType.split(';')[0]
688                    if mediaType == 'image':
689                        if subType == 'jpeg':
690                           src['extension'] = 'jpg'
691                        elif subType in ('png', 'gif', 'bmp'):
692                            src['extension'] = subType
693                    pass
694            except (HTTPError, URLError):
695                print "File download error: " + absURL
696
697            fileName = self._addExtension(src['fileName'], src['extension'])
698            if len(src['usedBy']) > 1:
699                src['finalURL'] = '_SharedFiles/' + fileName
700                if src['extension'] != 'xml':
701                    zip.writestr((src['finalURL']).encode('latin-1'), data)
702                src['finalURL'] = '../../' + src['finalURL']
703            else:
704                src['finalURL'] = '_Files/' + fileName
705                if src['extension'] != 'xml':
706                    for i in src['usedBy']:
707                        zip.writestr((baseDirs[i] + src['finalURL']).encode('latin-1'), data)
708
709    def _updateHtmls(self, splitHtmls, infoLists, baseURLs, srcs):
710        for i in range(len(splitHtmls)):
711            splitHtml = splitHtmls[i]
712            infoList = infoLists[i]
713            for j in range(len(splitHtml)):
714                if infoList[j] in ('src', 'jsfname', 'fvars'):
715                    absURL, flash = self._getAbsoluteURL(splitHtml[j], infoList[j], baseURLs[i])
716                    if infoList[j] == 'src':
717                        splitHtml[j] = srcs[absURL]['finalURL']
718                    elif infoList[j] == 'jsfname':
719                        splitHtml[j] = '.'.join(srcs[absURL]['finalURL'].split('.')[:-1])
720                    elif infoList[j] == 'fvars':
721                        if flash == 'mp3':
722                            splitHtml[j] = 'file=%s' % srcs[absURL]['finalURL']
723                        elif flash == 'pilot':
724                            splitHtml[j] = 'xml=%s' % srcs[absURL]['finalURL']
725                        elif flash == 'flv1':
726                            splitHtml[j] = "config={videoFile: '../%s'}" % srcs[absURL]['finalURL']
727                        elif flash == 'flv2':
728                            splitHtml[j] = "config={videoFile: \\'../%s\\'}" % srcs[absURL]['finalURL']
729
730    def _updatePilotXMLs(self, xmlDatas, baseURLs, srcs):
731        for absURL in xmlDatas:
732            splitXML = xmlDatas[absURL]['split']
733            xmlInfoList = xmlDatas[absURL]['info']
734            for j in range(len(splitXML)):
735                if xmlInfoList[j] in ('src', 'XMLmp3'):
736                    absURL2, flash = self._getAbsoluteURL(splitXML[j], xmlInfoList[j])
737                    splitXML[j] = srcs[absURL2]['finalURL']
738
739    def _addHtmlsToZip(self, splitHtmls, zip, baseDirs = None):
740        i = 0
741        baseDir = ''
742        for splitHtml in splitHtmls:
743            if isinstance(baseDirs, (list, tuple)):
744                baseDir = baseDirs[i]
745            zip.writestr(baseDir + 'index.html', ''.join(splitHtml))
746            i += 1
747
748    def _addPilotXMLsToZip(self, xmlDatas, srcs, zip):
749        for absURL in xmlDatas:
750            xmlData = xmlDatas[absURL]
751            xml = ''.join(xmlData['split']).encode('utf-16')
752            for baseDir in xmlData['baseDirs']:
753                zip.writestr(baseDir + srcs[absURL]['finalURL'], xml)
754                break
755
756    def buildZip(self):
757        """ Builds a Zip """
758        splitHtmls = []
759        infoLists = []
760        baseURLs = []
761        baseDirs = []
762        srcs = {}
763        htmlIndex = 0
764        resourceTypes = ('Content', 'Methods', 'Tools')
765        htmlList = [(self, '')]
766        for resourceType in resourceTypes:
767            for resource in self.getResources(reftype = 'related%s' % resourceType):
768                htmlList.append((resource, '%s/%s/' % (resourceType.lower(), resource.getId())))
769               
770        for html in htmlList:
771            splitHtml, infoList, baseURL = self._splitHTML(html[0].standalone_view())
772            splitHtmls.append(splitHtml)
773            infoLists.append(infoList)
774            baseURLs.append(baseURL)
775            baseDirs.append(html[1])
776            self._processHtml(splitHtml, infoList, srcs, baseURL, htmlIndex)
777            htmlIndex += 1
778
779        xmlDatas = self._processPilotXMLs(srcs, baseURLs, baseDirs)
780        zipStr = StringIO()
781        zip = zipfile.ZipFile(zipStr, 'w', compression = zipfile.ZIP_DEFLATED)
782        self._downloadFiles(srcs, zip, baseDirs)
783        self._updateHtmls(splitHtmls, infoLists, baseURLs, srcs)
784        self._updatePilotXMLs(xmlDatas, baseURLs, srcs)
785        self._addHtmlsToZip(splitHtmls, zip, baseDirs)
786        self._addPilotXMLsToZip(xmlDatas, srcs, zip)
787        zip.close()
788        response = self.REQUEST.RESPONSE
789        response.setHeader('Content-Type', 'application/zip')
790        response.setHeader('Content-Disposition', 'attachment; filename="%s.zip"' % self.getId())
791        return zipStr.getvalue()
792#Zip / SCORM download sruff end ----------------------------------------------------------------------------------
793
794    #XXX These additional meta-type='' and obj=None might not be needed, but I will leave them here just to be on the safe side.
795    def getDefaultIcon(self, meta_type='', obj=None):
796        """ Will return a portrait of the creator or his default icon """
797        owner = self.Creator()
798        mtool = getToolByName(self, 'portal_membership')
799        member_folder = mtool.getHomeFolder(owner)
800        given_image = member_folder.getCoverImageURL()
801        return given_image
802       
803
804registerType(Collection, PROJECTNAME)
805
806class CollectionsFolder(BaseFolder):
807    """ container for collection """
808    schema = BaseSchema
809    id = "collections"
810    meta_type = "CollectionsFolder"
811    archetype_name = "CollectionsFolder" 
812
813    actions= (
814    {
815    'id':'view',
816    'name':'view',
817    'action':'string:${object_url}/collections_list',
818    'permission':('View',),
819    },
820    )
821               
822registerType(CollectionsFolder, PROJECTNAME)
Note: See TracBrowser for help on using the repository browser.