• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

zopefoundation / z3c.form / 10347759702

12 Aug 2024 07:34AM UTC coverage: 95.627% (+0.2%) from 95.448%
10347759702

Pull #122

github

icemac
Update Python version support.
Pull Request #122: Update Python version support.

1248 of 1339 branches covered (93.2%)

Branch coverage included in aggregate %.

10 of 17 new or added lines in 10 files covered. (58.82%)

5 existing lines in 2 files now uncovered.

3694 of 3829 relevant lines covered (96.47%)

0.96 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

97.92
/src/z3c/form/datamanager.py
1
##############################################################################
2
#
3
# Copyright (c) 2007 Zope Foundation and Contributors.
4
# All Rights Reserved.
5
#
6
# This software is subject to the provisions of the Zope Public License,
7
# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
8
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
9
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
10
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
11
# FOR A PARTICULAR PURPOSE.
12
#
13
##############################################################################
14
"""Widget Framework Implementation."""
15

16
import zope.component
1✔
17
import zope.interface
1✔
18
import zope.schema
1✔
19
from zope.interface.common import mapping
1✔
20
from zope.security.checker import Proxy
1✔
21
from zope.security.checker import canAccess
1✔
22
from zope.security.checker import canWrite
1✔
23
from zope.security.interfaces import ForbiddenAttribute
1✔
24

25
from z3c.form import interfaces
1✔
26

27

28
_marker = []
1✔
29

30
ALLOWED_DATA_CLASSES = [dict]
1✔
31
try:
1✔
32
    import persistent.mapping
1✔
33
    ALLOWED_DATA_CLASSES.append(persistent.mapping.PersistentMapping)
1✔
NEW
34
except ModuleNotFoundError:
×
UNCOV
35
    pass
×
36

37

38
@zope.interface.implementer(interfaces.IDataManager)
1✔
39
class DataManager:
1✔
40
    """Data manager base class."""
41

42

43
class AttributeField(DataManager):
1✔
44
    """Attribute field."""
45
    zope.component.adapts(
1✔
46
        zope.interface.Interface, zope.schema.interfaces.IField)
47

48
    def __init__(self, context, field):
1✔
49
        self.context = context
1✔
50
        self.field = field
1✔
51

52
    @property
1✔
53
    def adapted_context(self):
1✔
54
        # get the right adapter or context
55
        context = self.context
1✔
56
        # NOTE: zope.schema fields defined in inherited interfaces will point
57
        # to the inherited interface. This could end in adapting the wrong
58
        # item. This is very bad because the widget field offers an explicit
59
        # interface argument which doesn't get used in Widget setup during
60
        # IDataManager lookup. We should find a concept which allows to adapt
61
        # the IDataManager use the widget field interface instead of the
62
        # zope.schema field.interface, ri
63
        if self.field.interface is not None:
1✔
64
            context = self.field.interface(context)
1✔
65
        return context
1✔
66

67
    def get(self):
1✔
68
        """See z3c.form.interfaces.IDataManager"""
69
        return getattr(self.adapted_context, self.field.__name__)
1✔
70

71
    def query(self, default=interfaces.NO_VALUE):
1✔
72
        """See z3c.form.interfaces.IDataManager"""
73
        try:
1✔
74
            return self.get()
1✔
75
        except ForbiddenAttribute as e:
1✔
76
            raise e
1✔
77
        except AttributeError:
1✔
78
            return default
1✔
79

80
    def set(self, value):
1✔
81
        """See z3c.form.interfaces.IDataManager"""
82
        if self.field.readonly:
1✔
83
            raise TypeError("Can't set values on read-only fields "
1✔
84
                            "(name=%s, class=%s.%s)"
85
                            % (self.field.__name__,
86
                               self.context.__class__.__module__,
87
                               self.context.__class__.__name__))
88
        # get the right adapter or context
89
        setattr(self.adapted_context, self.field.__name__, value)
1✔
90

91
    def canAccess(self):
1✔
92
        """See z3c.form.interfaces.IDataManager"""
93
        context = self.adapted_context
1✔
94
        if isinstance(context, Proxy):
1✔
95
            return canAccess(context, self.field.__name__)
1✔
96
        return True
1✔
97

98
    def canWrite(self):
1✔
99
        """See z3c.form.interfaces.IDataManager"""
100
        context = self.adapted_context
1✔
101
        if isinstance(context, Proxy):
1✔
102
            return canWrite(context, self.field.__name__)
1✔
103
        return True
1✔
104

105

106
class DictionaryField(DataManager):
1✔
107
    """Dictionary field.
108

109
    NOTE: Even though, this data manager allows nearly all kinds of
110
    mappings, by default it is only registered for dict, because it
111
    would otherwise get picked up in undesired scenarios. If you want
112
    to it use for another mapping, register the appropriate adapter in
113
    your application.
114

115
    """
116

117
    zope.component.adapts(dict, zope.schema.interfaces.IField)
1✔
118

119
    _allowed_data_classes = tuple(ALLOWED_DATA_CLASSES)
1✔
120

121
    def __init__(self, data, field):
1✔
122
        if (not isinstance(data, self._allowed_data_classes) and
1✔
123
                not mapping.IMapping.providedBy(data)):
124
            raise ValueError("Data are not a dictionary: %s" % type(data))
1✔
125
        self.data = data
1✔
126
        self.field = field
1✔
127

128
    def get(self):
1✔
129
        """See z3c.form.interfaces.IDataManager"""
130
        value = self.data.get(self.field.__name__, _marker)
1✔
131
        if value is _marker:
1✔
132
            raise AttributeError
1✔
133
        return value
1✔
134

135
    def query(self, default=interfaces.NO_VALUE):
1✔
136
        """See z3c.form.interfaces.IDataManager"""
137
        return self.data.get(self.field.__name__, default)
1✔
138

139
    def set(self, value):
1✔
140
        """See z3c.form.interfaces.IDataManager"""
141
        if self.field.readonly:
1✔
142
            raise TypeError("Can't set values on read-only fields name=%s"
1✔
143
                            % self.field.__name__)
144
        self.data[self.field.__name__] = value
1✔
145

146
    def canAccess(self):
1✔
147
        """See z3c.form.interfaces.IDataManager"""
148
        return True
1✔
149

150
    def canWrite(self):
1✔
151
        """See z3c.form.interfaces.IDataManager"""
152
        return True
1✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc