~vcs-imports/silva/trunk

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
# Copyright (c) 2003-2010 Infrae. All rights reserved.
# See also LICENSE.txt
# $Id: ContainerPolicy.py 44766 2010-08-25 14:11:15Z sylvain $

# Python
from bisect import insort_right

# Zope
from five import grok
from AccessControl import ClassSecurityInfo
from App.class_init import InitializeClass
from Persistence import Persistent

# Silva
from silva.core.interfaces import IContainerPolicy
from silva.core.services.interfaces import IContainerPolicyService
from silva.core.services.base import SilvaService


class _Policy:
    def __init__(self, name, priority):
        self._name = name
        self._priority = priority

    def __cmp__(self, other):
        sort = cmp(self._priority, other._priority)
        if sort == 0:
            sort = cmp(self._name, other._name)
        return sort


class ContainerPolicyRegistry(SilvaService):

    grok.implements(IContainerPolicyService)
    default_service_identifier = 'service_containerpolicy'
    meta_type = 'Silva Container Policy Registry'

    security = ClassSecurityInfo()

    def __init__(self, identifier):
        super(ContainerPolicyRegistry, self).__init__(identifier)
        self.__policies = {}
        self.register('None', NothingPolicy, 100)

    security.declareProtected(
        'Access contents information', 'get_policy')
    def get_policy(self, name):
        return self.__policies[name][0]

    security.declareProtected(
        'Access contents information', 'list_policies')
    def list_policies(self):
        sorted_policies = []
        for key, value in self.__policies.items():
            insort_right(sorted_policies, _Policy(key, value[1]))
        return [p._name for p in sorted_policies]

    security.declareProtected(
        'Access contents information', 'list_addable_policies')
    def list_addable_policies(self, content):
        allowed_addables = content.get_silva_addables_allowed()
        return [p for p in self.list_policies()
                if p in allowed_addables or p == 'None']

    security.declareProtected(
        'View management screens', 'register')
    def register(self, name, policy, priority=0.0):
        """Register policy.
        """
        assert IContainerPolicy.implementedBy(policy)
        self.__policies[name] = (policy(), priority)
        self._p_changed = 1

    security.declareProtected(
        'View management screens', 'unregister')
    def unregister(self, name):
        """Unregister policy.
        """
        try:
            del self.__policies[name]
        except KeyError:
            pass
        self._p_changed = 1


InitializeClass(ContainerPolicyRegistry)


class NothingPolicy(Persistent):
    grok.implements(IContainerPolicy)

    def createDefaultDocument(self, container, title):
        pass