summaryrefslogtreecommitdiffstats
path: root/ViewsManager.py
blob: edf2c0f8424e5419c4f2ce0a9953cb4a0b29e53c (plain)
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
#####################################################################
#
# ViewsManager   A folderish container for simple views management
#
# This software is governed by a license. See
# LICENSE.txt for the terms of this license.
#
#####################################################################
__version__='$Revision$'[11:-2]

# General python imports
import os

# Zope imports
from Products.PageTemplates.PageTemplateFile import PageTemplateFile
from Globals import InitializeClass, package_home, PersistentMapping
from AccessControl import ClassSecurityInfo
from AccessControl.Permissions import view_management_screens
from OFS.Folder import Folder
from BTrees.OOBTree import OOBTree
from Acquisition import aq_base

# Zoftware package imports
from Permissions import ManageViews

_registered_types = PersistentMapping()

_wwwdir = os.path.join(package_home(globals()), 'www')
addViewsManagerForm = PageTemplateFile('addViewsManager.pt', _wwwdir)


def registerType(meta_type, view_info):
    """ Register a product with the ViewsManager """
    _registered_types[meta_type] = view_info.values()


class ViewsManager(Folder):
    """ ViewsManager - A tool to manage views """
    meta_type = 'ViewsManager'
    security = ClassSecurityInfo()
    id = 'views_manager'

    manage_options = ( (Folder.manage_options[0],)
                     + ( { 'label' : 'Mappings'
                         , 'action' : 'manage_views' 
                         , 'help' : ('ViewsManager', 'Mappings.stx')
                         }, )
                     + Folder.manage_options[2:]
                     )

    security.declareProtected(view_management_screens, 'manage_views')
    manage_views = PageTemplateFile('views.pt', _wwwdir)


    def __init__(self , id , title=''):
        """ Create a new ViewsManager instance """
        self.title = title
        self._mapping = OOBTree()


    security.declareProtected(ManageViews, 'manage_edit')
    def manage_edit(self, title):
        """ Edit the ViewsManager Object """
        self.title = title


    security.declareProtected(ManageViews, 'getRegisteredViews')
    def getRegisteredViews(self):
        """ Show all currently registered views """
        registered = []
        reg_keys = _registered_types.keys()
        reg_keys.sort()

        for key in reg_keys:
            for view in _registered_types.get(key):
                v_label = view.get('view_label')

                registered.append( { 'map_key'    : '%s:%s' % (key, v_label)
                                   , 'meta_type'  : key
                                   , 'view_label' : v_label
                                   , 'view_id'    : view.get('view_id')
                                   } )

        return tuple(registered)


    security.declareProtected(ManageViews, 'getViewMapping')
    def getViewMapping(self):
        """ Return the complete views mapping """
        mapping = []
        keys = list(self._mapping.keys())
        keys.sort()

        for key in keys:
            mapping.append(self._mapping.get(key))

        return mapping


    security.declareProtected(ManageViews, 'setViewMapping')
    def setViewMapping(self, map_key, internal_id, REQUEST=None):
        """ Set a view for a specific meta_type and view label """
        elems = map_key.split(':')
        meta_type = elems[0]
        view_label = ''.join(elems[1:])

        self._mapping[map_key] = { 'map_key'     : map_key
                                 , 'meta_type'   : meta_type
                                 , 'view_label'  : view_label
                                 , 'internal_id' : internal_id
                                 }

        if REQUEST is not None:
            msg = 'View mapping added'
            return self.manage_views(manage_tabs_message=msg)


    security.declareProtected(ManageViews, 'deleteViewMappings')
    def deleteViewMappings(self, keys=[], REQUEST=None):
        """ Remove view mappings """
        for key in keys:
            if self._mapping.has_key(key):
                del self._mapping[key]

        if REQUEST is not None:
            msg = 'View mapping(s) deleted'
            return self.manage_views(manage_tabs_message=msg)


    security.declareProtected(ManageViews, 'getViewId')
    def getViewId(self, meta_type, view_label):
        """ Get a view ID by meta_type and view label """
        view_data =  self._mapping.get('%s:%s' % (meta_type, view_label), None)

        if view_data is not None:
            return view_data.get('internal_id')
        else:
            return None


    security.declarePrivate('getViewObject')
    def getViewObject(self, object, view_label):
        """ Retrieve a view by meta_type and view label """
        id = self.getViewId(getattr(object, 'meta_type', None), view_label)
        view_ob = id and self._getOb(id, default=None) or None
        
        if view_ob is None:
            return None

        return aq_base(view_ob).__of__(object)


InitializeClass(ViewsManager)


def manage_addViewsManager(self, title='', REQUEST=None):
    """ Factory used to install new ViewsManager instances """
    real_self = self.this()
    
    if id in real_self.objectIds():
        msg = 'Duplicate+ID+%s' % id
    else:
        vm = ViewsManager(id, title)
        self._setObject(vm.getId(), vm)
        msg = 'ViewsManager+added'

    if REQUEST is not None:
        ret_url = '%s/manage_main' % REQUEST['URL1']
        REQUEST.RESPONSE.redirect('%s?manage_tabs_message=%s' % (ret_url, msg))