File: Customizers.py

package info (click to toggle)
zope-zpatterns 0.4.3p2-2
  • links: PTS
  • area: main
  • in suites: woody
  • size: 476 kB
  • ctags: 814
  • sloc: python: 2,817; ansic: 310; makefile: 52; sh: 39
file content (190 lines) | stat: -rw-r--r-- 4,887 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
from Providers import ProviderContainer
from DataManagers import DataManager
from Products.PlugIns import PlugInContainer, PlugInGroup, \
    defaultConstructors, genericAddForm
from Globals import DTMLFile, default__class_init__
from OFS.Folder import Folder

_marker = []

class Customizer(DataManager):
    """DataManager for non-rackmounted DataSkins"""

    __plugin_kind__ = "Customizer"
    meta_type = "Customizer"
    MetaTypes = ()
    
    manage_typesForm = DTMLFile('www/typesForm', globals())

    manage_options_left = (
        {'label':'Customize Types','action':'manage_typesForm'},
    ) + DataManager.manage_options_left
    
    def namesForRegistration(self, container):
        return self.MetaTypes

    def _isSelected(self, name, path, klass):
        return klass._zclass_.meta_type in self.MetaTypes
        
    def manage_setTypes(self, meta_types=(), REQUEST=None):
        """Change types applicable to this Customizer"""
        self.MetaTypes = meta_types
        self.aq_inner.aq_parent.manage_refreshPlugIns()
        if REQUEST is not None:
            return self.manage_typesForm(self, REQUEST,
                manage_tabs_message="Types updated.")

    __ac_permissions__ = (
        ('View management screens',
            ('manage_typesForm','manage_setTypes')
        ),
    )
    _initialized = 0
    
    def _setup(self):
        # set up default providers
        
        if self._initialized:
            # We must only initialize once, since _setup gets called on pastes,
            # imports, etc.
            return

        from Providers import LinkToParentProviders
        self._installPlugIn(
            LinkToParentProviders('ParentPlugIns', 
                title='Plug-ins from parent Folder w/Customization Support')
        )
        
        from SheetProviders import SheetProvider
        self._installPlugIn(
            SheetProvider('PersistentSheets', title='Sheets stored in ZODB w/objects')
        )
        
        self._initialized = 1



















class CustomizerGroup(PlugInGroup):
    """
    PlugInGroup that manages the more complex registration needs 
    of Customizers and their containers.
    """

    def _PlugInsChanged(self, container, plugins=()):

        """Update the registry of customizer names"""

        custs = container.__customizerRegistry__ = {}

        for plugin in plugins:

            for n in plugin.namesForRegistration(container):
                if not custs.has_key(n):
                    custs[n]=plugin


    def manage_registry(self):
        """ """
        return self.aq_parent.__customizerRegistry__.items()

    manage_below = DTMLFile('www/showCustomizers', globals())

    __ac_permissions__ = (
        ('View management screens',
            ('manage_registry','manage_below')
        ),
    )


default__class_init__(Customizer)
default__class_init__(CustomizerGroup)







class CustomizerFolder(ProviderContainer,PlugInContainer):

    """Folder which can contain objects managed by Customizers"""

    Customizers_ = CustomizerGroup(
        'Customizers_',
        ['Customizer'],
        attr='__customizerList__', 
        title='Customizers'
    )

    __plugin_groups__ = (Customizers_,) + ProviderContainer.__plugin_groups__

    __customizerList__ = ()
    __customizerRegistry__ = {}

    meta_type = "Folder w/Customizer Support"
    
    # We want to look like a folder, but with extra tabs after 'Contents'

    manage_options_left = Folder.manage_options[:1]
    manage_options_right = Folder.manage_options[1:]

    def _getDataManagerFor(self,client,default=None):

        """Determine data manager based on client's meta_type"""

        dm = self.__customizerRegistry__.get(client.meta_type, _marker)
        if dm is not _marker:
            return dm.__of__(self)._getDataManagerFor(client)   # forward to DM

        try:
            _gdmf = self.aq_inner.aq_parent.aq_acquire('_getDataManagerFor')
        except AttributeError:
            return default

        return _gdmf(client,default)

default__class_init__(CustomizerFolder)


manage_addCustomizerFolderForm = genericAddForm(CustomizerFolder,globals())

def manage_addCustomizerFolder(self, id, title='', REQUEST=None):
    """Add a CustomizerFolder"""
    self._setObject(id, CustomizerFolder(id, title))
    if REQUEST: return self.manage_main(self,REQUEST,update_menu=1)



def initialize(context):

    context.registerPlugInClass(
        Customizer,
        permission = 'Add Customizers',
        constructors = defaultConstructors(Customizer,globals()),
        icon = 'www/customizer.gif'
    )

    context.registerClass(
        CustomizerFolder,
        permission = 'Add Folders w/Customizer Support',
        constructors = (manage_addCustomizerFolderForm,
                        manage_addCustomizerFolder),
        icon = 'www/custfolder.gif'
    )