root/communesplone/CPComarquage/tags/1.0/ConfigurationMethods.py

Revision 2503, 4.7 kB (checked in by gbastien, 2 years ago)

Completed tag before announcing stable release 1.0

  • Property svn:eol-style set to native
Line 
1 # -*- coding: utf-8 -*-
2 #
3 # File: ConfigurationMethods.py
4 #
5 # Copyright (c) 2006 by CommunesPlone
6 #
7 # GNU General Public License (GPL)
8 #
9 # This program is free software; you can redistribute it and/or
10 # modify it under the terms of the GNU General Public License
11 # as published by the Free Software Foundation; either version 2
12 # of the License, or (at your option) any later version.
13 #
14 # This program is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
18 #
19 # You should have received a copy of the GNU General Public License
20 # along with this program; if not, write to the Free Software
21 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22 # 02110-1301, USA.
23 #
24
25 __author__ = """Gauthier BASTIEN <gbastien@commune.sambreville.be>"""
26 __docformat__ = 'plaintext'
27
28 from zLOG import INFO, ERROR
29
30 from Products.CMFPlone.setup.SetupBase import SetupWidget
31 from Products.CMFCore.utils import getToolByName
32 from Products.CMFDefault.exceptions import MetadataError
33
34 from Products.CPComarquage.config import PROJECTNAME
35 from Products.CPComarquage.config import MAPNAME
36 from Products.CPComarquage.config import MAXRSS
37
38 from Products.CPComarquage.CustomizationPolicy import CPComarquageCustomizationPolicy
39
40 def installProducts(self, portal):
41     """ Install necessary products """
42     qi = getToolByName(portal, 'portal_quickinstaller')
43     if not qi.isProductInstalled('CMFSin'):
44         qi.installProduct('CMFSin')
45     if not qi.isProductInstalled('CPComarquage'):
46         qi.installProduct('CPComarquage')
47     get_transaction().commit(1)
48
49
50 def updateSubjectsMetadata(self, portal):
51     """ We add the differents subject from the 'Portail de la Région Wallonne' in the lists of Subject in portal_metadata """
52     #retrieve every subjects from RSS
53     meta_tool = getToolByName(portal, 'portal_metadata')
54     sin = getToolByName(portal, 'sin_tool')
55     rows = sin.sin(map_name=MAPNAME, max_size=MAXRSS)
56
57     rss_subjects = []
58     for row in rows:
59         rss_subjects.append(unicode(row.get('category'), 'latin1').encode('utf-8'))
60
61     element = meta_tool.getElementSpec('Subject')
62     existing_subjects = list(element.getPolicy('Document').allowed_vocabulary)
63
64     try:
65         meta_tool.addElementPolicy(element='Subject',
66                                 content_type='Document',
67                                 is_required=0,
68                                 supply_default=0,
69                                 default_value="",
70                                 enforce_vocabulary=1,
71                                 allowed_vocabulary=tuple(rss_subjects)
72                                 )
73
74     except MetadataError:
75         #Policy already exist, we update the allowed_vocabulary
76         #we add non existing subjects
77         for subj in rss_subjects:
78             if subj not in existing_subjects:
79                 existing_subjects.append(subj)
80
81         #we sort by alphabetical order
82         existing_subjects.sort()
83         #we update the Policy
84         meta_tool.updateElementPolicy(element='Subject',
85                                       content_type='Document',
86                                       is_required=0,
87                                       supply_default=0,
88                                       default_value="",
89                                       enforce_vocabulary=1,
90                                       allowed_vocabulary=tuple(existing_subjects)
91                                       )
92
93
94 functions = {
95              'installProducts': installProducts,
96              'updateSubjectsMetadata': updateSubjectsMetadata
97             }
98
99 class CPComarquageSetup(SetupWidget):
100    type = 'CPComarquage Setup'
101
102    description = "Permet d'adapter le portail pour l'utilisation du produit CPComarquage"
103
104    functions = functions
105
106    single = 0
107
108    def __init__(self,portal):
109        self.portal = portal
110
111    def setup(self):
112        pass
113
114    def delItems(self,fns):
115        out = []
116        out.append(('Currently, there is no way to remove a function',INFO))
117        return out
118
119    def addItems(self,fns):
120        out = []
121        for fn in fns:
122            self.functions[fn](self, self.portal)
123            out.append(('Function %s has been applied' % fn, INFO))
124        return out
125
126    def active(self):
127        return 1
128
129    def installed(self):
130        return []
131
132    def available(self):
133        """ Go get the functions """
134        functionList = ('installProducts', 'updateSubjectsMetadata')
135        #we return a list of functions because de dictionnary is not sorted and we need some king of sorting here (installProducts first, ...)
136        #return self.functions.keys()
137        return functionList
Note: See TracBrowser for help on using the browser.