|
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 from persistent import Persistent |
|
18 from persistent.mapping import PersistentMapping |
|
19 |
|
20 # import interfaces |
|
21 from pyams_content.component.links.interfaces import ILinkContainerTarget |
|
22 from pyams_content.shared.imagemap.interfaces import IMAGEMAP_CONTENT_TYPE, IMAGEMAP_CONTENT_NAME, \ |
|
23 IWfImageMap, IImageMap, IImageMapArea |
|
24 from zope.location.interfaces import ISublocations |
|
25 from zope.traversing.interfaces import ITraversable |
|
26 |
|
27 # import packages |
|
28 from pyams_content.shared.common import WfSharedContent, register_content_type, SharedContent |
|
29 from pyams_i18n.property import I18nFileProperty |
|
30 from pyams_utils.adapter import adapter_config, ContextAdapter |
|
31 from pyramid.threadlocal import get_current_registry |
|
32 from zope.container.contained import Contained |
|
33 from zope.interface import implementer |
|
34 from zope.lifecycleevent import ObjectModifiedEvent |
|
35 from zope.location import locate |
|
36 from zope.schema.fieldproperty import FieldProperty |
|
37 |
|
38 |
|
39 @implementer(IImageMapArea) |
|
40 class ImageMapArea(Persistent, Contained): |
|
41 """Image map area class""" |
|
42 |
|
43 title = FieldProperty(IImageMapArea['title']) |
|
44 link = FieldProperty(IImageMapArea['link']) |
|
45 area = FieldProperty(IImageMapArea['area']) |
|
46 |
|
47 |
|
48 @implementer(IWfImageMap, ILinkContainerTarget) |
|
49 class WfImageMap(WfSharedContent): |
|
50 """Base image map""" |
|
51 |
|
52 content_type = IMAGEMAP_CONTENT_TYPE |
|
53 content_name = IMAGEMAP_CONTENT_NAME |
|
54 |
|
55 _image = I18nFileProperty(IWfImageMap['image']) |
|
56 areas = FieldProperty(IWfImageMap['areas']) |
|
57 |
|
58 _index = 0 |
|
59 |
|
60 def __init__(self): |
|
61 self.areas = PersistentMapping() |
|
62 |
|
63 @property |
|
64 def image(self): |
|
65 return self._image |
|
66 |
|
67 @image.setter |
|
68 def image(self, value): |
|
69 self._image = value |
|
70 if value: |
|
71 self.areas = PersistentMapping() |
|
72 |
|
73 def add_area(self, area): |
|
74 self._index += 1 |
|
75 key = str(self._index) |
|
76 self.areas[key] = area |
|
77 locate(area, self, '++areas++{0}'.format(key)) |
|
78 get_current_registry().notify(ObjectModifiedEvent(self)) |
|
79 |
|
80 def remove_area(self, key): |
|
81 if key in self.areas: |
|
82 del self.areas[key] |
|
83 get_current_registry().notify(ObjectModifiedEvent(self)) |
|
84 |
|
85 register_content_type(WfImageMap) |
|
86 |
|
87 |
|
88 @implementer(IImageMap) |
|
89 class ImageMap(SharedContent): |
|
90 """Workflow managed image map class""" |
|
91 |
|
92 content_class = WfImageMap |
|
93 |
|
94 |
|
95 @adapter_config(name='areas', context=IWfImageMap, provides=ITraversable) |
|
96 class ImapemapAreasNamespace(ContextAdapter): |
|
97 """++areas++ namespace traverser""" |
|
98 |
|
99 def traverse(self, name, furtherpath=None): |
|
100 return self.context.areas[name] |
|
101 |
|
102 |
|
103 @adapter_config(name='areas', context=IWfImageMap, provides=ISublocations) |
|
104 class ImagemapAreasSublocations(ContextAdapter): |
|
105 """Image map area sublocations""" |
|
106 |
|
107 def sublocations(self): |
|
108 return self.context.areas.values() |