|
1 # |
|
2 # Copyright (c) 2008-2015 Thierry Florac <tflorac AT ulthar.net> |
|
3 # All Rights Reserved. |
|
4 # |
|
5 # This software is subject to the provisions of the Zope Public License, |
|
6 # Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution. |
|
7 # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED |
|
8 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
|
9 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS |
|
10 # FOR A PARTICULAR PURPOSE. |
|
11 # |
|
12 |
|
13 __docformat__ = 'restructuredtext' |
|
14 |
|
15 |
|
16 # import standard library |
|
17 |
|
18 # import interfaces |
|
19 from pyams_content.component.extfile.interfaces import IExtFileContainer, IExtFileContainerTarget, \ |
|
20 EXTFILE_CONTAINER_KEY, IExtFileLinksContainer, IExtFileLinksContainerTarget, EXTFILE_LINKS_CONTAINER_KEY |
|
21 from pyams_file.interfaces import IMediaFile, IImage, IVideo, IAudio |
|
22 from pyams_i18n.interfaces import II18n |
|
23 from zope.annotation.interfaces import IAnnotations |
|
24 from zope.location.interfaces import ISublocations |
|
25 from zope.schema.interfaces import IVocabularyFactory |
|
26 from zope.traversing.interfaces import ITraversable |
|
27 |
|
28 # import packages |
|
29 from persistent import Persistent |
|
30 from persistent.list import PersistentList |
|
31 from pyams_utils.adapter import adapter_config, ContextAdapter |
|
32 from pyams_utils.traversing import get_parent |
|
33 from pyramid.threadlocal import get_current_registry |
|
34 from zope.container.contained import Contained |
|
35 from zope.container.folder import Folder |
|
36 from zope.interface import implementer, provider |
|
37 from zope.lifecycleevent import ObjectCreatedEvent |
|
38 from zope.location import locate |
|
39 from zope.schema.vocabulary import SimpleVocabulary, getVocabularyRegistry, SimpleTerm |
|
40 |
|
41 |
|
42 # |
|
43 # External files container |
|
44 # |
|
45 |
|
46 @implementer(IExtFileContainer) |
|
47 class ExtFileContainer(Folder): |
|
48 """External files container""" |
|
49 |
|
50 last_id = 1 |
|
51 |
|
52 def __setitem__(self, key, value): |
|
53 key = str(self.last_id) |
|
54 super(ExtFileContainer, self).__setitem__(key, value) |
|
55 self.last_id += 1 |
|
56 |
|
57 @property |
|
58 def files(self): |
|
59 return (file for file in self.values() if not IMediaFile.providedBy(II18n(file).query_attribute('data'))) |
|
60 |
|
61 @property |
|
62 def medias(self): |
|
63 return (file for file in self.values() if IMediaFile.providedBy(II18n(file).query_attribute('data'))) |
|
64 |
|
65 @property |
|
66 def images(self): |
|
67 return (file for file in self.values() if IImage.providedBy(II18n(file).query_attribute('data'))) |
|
68 |
|
69 @property |
|
70 def videos(self): |
|
71 return (file for file in self.values() if IVideo.providedBy(II18n(file).query_attribute('data'))) |
|
72 |
|
73 @property |
|
74 def audios(self): |
|
75 return (file for file in self.values() if IAudio.providedBy(II18n(file).query_attribute('data'))) |
|
76 |
|
77 |
|
78 @adapter_config(context=IExtFileContainerTarget, provides=IExtFileContainer) |
|
79 def extfile_container_factory(target): |
|
80 """External files container factory""" |
|
81 annotations = IAnnotations(target) |
|
82 container = annotations.get(EXTFILE_CONTAINER_KEY) |
|
83 if container is None: |
|
84 container = annotations[EXTFILE_CONTAINER_KEY] = ExtFileContainer() |
|
85 get_current_registry().notify(ObjectCreatedEvent(container)) |
|
86 locate(container, target, '++files++') |
|
87 return container |
|
88 |
|
89 |
|
90 @adapter_config(name='files', context=IExtFileContainerTarget, provides=ITraversable) |
|
91 class ExtFileContainerNamespace(ContextAdapter): |
|
92 """++files++ namespace adapter""" |
|
93 |
|
94 def traverse(self, name, furtherpath=None): |
|
95 return IExtFileContainer(self.context) |
|
96 |
|
97 |
|
98 @adapter_config(name='extfile', context=IExtFileContainerTarget, provides=ISublocations) |
|
99 class ExtFileContainerSublocations(ContextAdapter): |
|
100 """External files container sublocations""" |
|
101 |
|
102 def sublocations(self): |
|
103 return IExtFileContainer(self.context).values() |
|
104 |
|
105 |
|
106 @provider(IVocabularyFactory) |
|
107 class ExtFileContainerFilesVocabulary(SimpleVocabulary): |
|
108 """External files container files vocabulary""" |
|
109 |
|
110 def __init__(self, context): |
|
111 target = get_parent(context, IExtFileContainerTarget) |
|
112 terms = [SimpleTerm(file.__name__, title=II18n(file).query_attribute('title')) |
|
113 for file in IExtFileContainer(target).values()] |
|
114 super(ExtFileContainerFilesVocabulary, self).__init__(terms) |
|
115 |
|
116 getVocabularyRegistry().register('PyAMS content external files', ExtFileContainerFilesVocabulary) |
|
117 |
|
118 |
|
119 # |
|
120 # External file links container |
|
121 # |
|
122 |
|
123 @implementer(IExtFileLinksContainer) |
|
124 class ExtFileLinksContainer(Persistent, Contained): |
|
125 """External files links container""" |
|
126 |
|
127 def __init__(self): |
|
128 self.files = PersistentList() |
|
129 |
|
130 |
|
131 @adapter_config(context=IExtFileLinksContainerTarget, provides=IExtFileLinksContainer) |
|
132 def extfile_links_container_factory(target): |
|
133 """External files links container factory""" |
|
134 annotations = IAnnotations(target) |
|
135 container = annotations.get(EXTFILE_LINKS_CONTAINER_KEY) |
|
136 if container is None: |
|
137 container = annotations[EXTFILE_LINKS_CONTAINER_KEY] = ExtFileLinksContainer() |
|
138 get_current_registry().notify(ObjectCreatedEvent(container)) |
|
139 locate(container, target, '++files-links++') |
|
140 return container |
|
141 |
|
142 |
|
143 @adapter_config(name='files-links', context=IExtFileLinksContainerTarget, provides=ITraversable) |
|
144 class ExtFileLinksContainerNamespace(ContextAdapter): |
|
145 """++files-links++ namespace adapter""" |
|
146 |
|
147 def traverse(self, name, furtherpath=None): |
|
148 return IExtFileLinksContainer(self.context) |