source: products/qPloneGoogleMaps/tags/0.1.0/Extensions/Install.py

Last change on this file was 1, checked in by myroslav, 18 years ago

Building directory structure

  • Property svn:eol-style set to native
File size: 9.0 KB
Line 
1import string
2from cStringIO import StringIO
3
4from Products.CMFCore.utils import getToolByName
5from Products.CMFCore.DirectoryView import addDirectoryViews
6
7try:
8    from Products.CMFCore.permissions import ManagePortal
9except ImportError:
10    from Products.CMFCore.CMFCorePermissions import ManagePortal
11
12from Products.CMFPlone.migrations.migration_util import safeEditProperty
13
14from Products.Archetypes.Extensions.utils import installTypes
15from Products.Archetypes.public import listTypes
16
17from Products.qPloneGoogleMaps.config import *
18
19configlets = \
20({'id':PROJECTNAME,
21  'name':'Google Map Api Keys',
22  'action':'string:${portal_url}/prefs_mapkeys_form',
23  'condition':'',
24  'category':'Products',
25  'visible':1,
26  'appId':PROJECTNAME,
27  'permission':ManagePortal},)
28  #'imageUrl':'niimportablefooter.gif'},)
29
30def addProperty(self, out):
31    """ Add property sheet to portal_properties """
32
33    portal_props = getToolByName(self, 'portal_properties')
34    if not hasattr(portal_props, PROPERTY_SHEET):
35        portal_props.addPropertySheet(PROPERTY_SHEET, 'Maps Properties')
36        out.write('Added %s property sheet...\n' % PROPERTY_SHEET)
37    ap = getattr(portal_props, PROPERTY_SHEET)
38    if not hasattr(ap, PROPERTY_FIELD):
39        safeEditProperty(ap, PROPERTY_FIELD, MAP_API_KEYS, 'lines')
40        out.write('Added %s property field to %s property sheet...\n' % (PROPERTY_FIELD, PROPERTY_SHEET))
41    else: out.write('Skipped adding property...\n')
42
43def removeProperty(self, out):
44    """ Remove property sheet from portal_properties """
45
46    portal_props = getToolByName(self, 'portal_properties')
47    if hasattr(portal_props, PROPERTY_SHEET):
48        portal_props.manage_delObjects(PROPERTY_SHEET)
49        out.write('Deleted %s property sheet...\n' % PROPERTY_SHEET)
50
51def addConfiglets(self, out):
52    """ Add configlets to portal control panel """
53
54    configTool = getToolByName(self, 'portal_controlpanel', None)
55    if configTool:
56        for conf in configlets:
57            out.write('Added configlet %s\n' % conf['id'])
58            configTool.registerConfiglet(**conf)
59
60def removeConfiglets(self, out):
61    """ Remove the configlet from the portal control panel """
62
63    configTool = getToolByName(self, 'portal_controlpanel', None)
64    if configTool:
65        for conf in configlets:
66            out.write('Removed configlet %s\n' % conf['id'])
67            configTool.unregisterConfiglet('%s' % conf['id'])
68
69def addCatalogIndex(self, name, out):
70    """ Add to portal catalog given index """
71
72    portal_catalog = getToolByName(self, 'portal_catalog')
73    if name not in portal_catalog.indexes():
74        portal_catalog.addIndex(name=name, type="FieldIndex")
75        out.write('Added %s index to portal_catalog...\n' % name)
76
77def removeCatalogIndex(self, name, out):
78    """ Remove from portal catalog given index """
79
80    portal_catalog = getToolByName(self, 'portal_catalog')
81    if name in portal_catalog.indexes():
82        portal_catalog.delIndex(name=name)
83        out.write('Removed %s index from portal_catalog...\n' % name)
84
85def addCatalogColumn(self, name, out):
86    """ Add to portal catalog given metadata column """
87
88    portal_catalog = getToolByName(self, 'portal_catalog')
89    if name not in portal_catalog.schema():
90        portal_catalog.addColumn(name=name)
91        out.write('Added %s column metadata to portal_catalog...\n' % name)
92
93
94def removeCatalogColumn(self, name, out):
95    """ Remove from portal catalog given metadata column """
96
97    portal_catalog = getToolByName(self, 'portal_catalog')
98    if name in portal_catalog.schema():
99        portal_catalog.delColumn(name=name)
100        out.write('Removed %s column metadata from portal_catalog...\n' % name)
101
102def installContentTypes(self, out):
103    """ Install new portal types and add to the portal_factory """
104
105    typesInfo = listTypes(PROJECTNAME)
106    installTypes(self, out, typesInfo, PROJECTNAME)
107    out.write("Installed types\n")
108    factory_tool = getToolByName(self, 'portal_factory')
109    types = factory_tool.getFactoryTypes().keys()
110    for item in NEW_PORTAL_TYPES:
111        if item not in types:
112            types.append(item)
113            factory_tool.manage_setPortalFactoryTypes(listOfTypeIds = types)
114            out.write('Added %s portal type to portal_factory\n' % item)
115
116def addTopicMapView(self, out, view):
117    """ Add map view template to the topic type """
118
119    portal_types = getToolByName(self, 'portal_types', None)
120    if portal_types is not None:
121        for tp in ['Folder', 'Large Plone Folder', 'Topic']:
122            fti = getattr(portal_types, tp, None)
123            if fti is not None:
124                views = list(getattr(fti, 'view_methods'))
125                if view not in views:
126                    views.append(view)
127                    fti.manage_changeProperties(view_methods = tuple(views))
128                    out.write("Added new view template to '%s' FTI.\n" % tp)
129
130def removeTopicMapView(self, out, view):
131    """ Remove map view template from the topic type """
132
133    portal_types = getToolByName(self, 'portal_types', None)
134    if portal_types is not None:
135        for tp in ['Folder', 'Large Plone Folder', 'Topic']:
136            fti = getattr(portal_types, tp, None)
137            if fti is not None:
138                views = list(getattr(fti, 'view_methods'))
139                if view in views:
140                    views.remove(view)
141                    fti.manage_changeProperties(view_methods = tuple(views))
142                    out.write("Removed maps view template from '%s' FTI.\n" % tp)
143
144def addPortlets(self, out, slots=[]):
145    """ Add portlet to right slot """
146
147    right_slots = getattr(self, 'right_slots', None)
148    if right_slots != None:
149        for slot in slots:
150            if slot not in right_slots:
151                right_slots = list(right_slots) + [slot,]
152                self.right_slots = right_slots
153                out.write('Added %s portlet to right_slots property.\n' % slot)
154
155def removePortlets(self, out, slots=[]):
156    """ Remove portlet from right slot """
157
158    right_slots = list(getattr(self, 'right_slots', ()))
159    for slot in slots:
160        if slot in right_slots:
161            right_slots.remove(slot)
162            self.right_slots = right_slots
163            out.write('Removed %s portlet from right slots property.\n' % slot)
164
165def setupSkin(self, out, skinFolder):
166    """ Setup product skin layer """
167
168    skinstool=getToolByName(self, 'portal_skins')
169    addDirectoryViews(skinstool, SKINS_DIR, GLOBALS)
170    for skin in skinstool.getSkinSelections():
171        path = skinstool.getSkinPath(skin)
172        path = map(string.strip, string.split(path,','))
173        if not skinFolder in path:
174            try:
175                path.insert( path.index('custom')+1, skinFolder)
176            except ValueError:
177                path.append(skinFolder)
178            path = string.join(path, ', ')
179            skinstool.addSkinSelection(skin, path)
180            out.write(%s layer sucessfully installed into skin %s.\n' % (skinFolder, skin))
181        else:
182            out.write(%s layer was already installed into skin %s.\n' % (skinFolder, skin))
183
184def removeSkin(self, skins=[]):
185    """ Setup product skin layer """
186
187    if skins:
188        skinstool = getToolByName(self, 'portal_skins')
189        for skinName in skinstool.getSkinSelections():
190            path = skinstool.getSkinPath(skinName)
191            path = [i.strip() for i in  path.split(',')]
192            for s in skins:
193                if s in path:
194                    path.remove(s)
195                s += '/'
196                for layer in path:
197                    if layer.startswith(s):
198                        path.remove(layer)
199            path = ','.join(path)
200            skinstool.addSkinSelection(skinName, path)
201
202def install(self):
203    """ Product installation """
204
205    out = StringIO()
206
207    # add property field to 'maps_properties' sheet
208    addProperty(self, out)
209
210    # installing configlet
211    addConfiglets(self, out)
212
213    # adding indexes and metadata columns to portal_catalog
214    addCatalogIndex(self, GEO_INDEX, out)
215    addCatalogColumn(self, GEO_INDEX, out)
216
217    out.write('Update catalog...\n')
218    getToolByName(self, 'portal_catalog').refreshCatalog()
219
220    # installin content types
221    installContentTypes(self, out)
222
223    # add portlets to right slot
224    addPortlets(self, out, MAP_PORTLETS)
225
226    # add map view template to the topic portal type
227    addTopicMapView(self, out, 'topic_maps_view')
228
229    # setup skin layer
230    out.write('setupSkin... \n')
231    setupSkin(self, out, PROJECTNAME)
232
233    return out.getvalue()
234
235def uninstall(self):
236    """ Product uninstallation """
237
238    out = StringIO()
239
240    # remove property sheet 'maps_properties' from portal_properties
241    removeProperty(self, out)
242
243    # removing configlet
244    removeConfiglets(self, out)
245
246    # removing indexes and metadata columns from portal_catalog
247    removeCatalogIndex(self, GEO_INDEX, out)
248    removeCatalogColumn(self, GEO_INDEX, out)
249
250    # remove portlets from right slot
251    removePortlets(self, out, MAP_PORTLETS)
252
253    # remove map view template from the topic portal type
254    removeTopicMapView(self, out, 'topic_maps_view')
255
256    # remove skin layer
257    removeSkin(self, [PROJECTNAME,])
258
259    return out.getvalue()
Note: See TracBrowser for help on using the repository browser.