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

zopefoundation / z3c.form / 5104192483

pending completion
5104192483

push

github

web-flow
Config with pure python template (#114)

* Bumped version for breaking release.

* Drop support for Python 2.7, 3.5, 3.6.

* Add support for Python 3.11.

1316 of 1408 branches covered (93.47%)

Branch coverage included in aggregate %.

420 of 420 new or added lines in 39 files covered. (100.0%)

3716 of 3838 relevant lines covered (96.82%)

0.97 hits per line

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

97.71
/src/z3c/form/group.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 Group Implementation
1✔
15

16
$Id$
17
"""
18
__docformat__ = "reStructuredText"
1✔
19

20
import zope.component
1✔
21
import zope.event
1✔
22
from zope.interface import implementer
1✔
23

24
from z3c.form import form
1✔
25
from z3c.form import interfaces
1✔
26
from z3c.form.events import DataExtractedEvent
1✔
27

28

29
@implementer(interfaces.IGroup)
1✔
30
class Group(form.BaseForm):
1✔
31

32
    groups = ()
1✔
33

34
    def __init__(self, context, request, parentForm):
1✔
35
        self.context = context
1✔
36
        self.request = request
1✔
37
        self.parentForm = self.__parent__ = parentForm
1✔
38

39
    def updateWidgets(self, prefix=None):
1✔
40
        '''See interfaces.IForm'''
41
        self.widgets = zope.component.getMultiAdapter(
1✔
42
            (self, self.request, self.getContent()), interfaces.IWidgets)
43
        for attrName in ('mode', 'ignoreRequest', 'ignoreContext',
1✔
44
                         'ignoreReadonly'):
45
            value = getattr(self.parentForm.widgets, attrName)
1✔
46
            setattr(self.widgets, attrName, value)
1✔
47
        if prefix is not None:
1!
48
            self.widgets.prefix = prefix
×
49
        self.widgets.update()
1✔
50

51
    def update(self):
1✔
52
        '''See interfaces.IForm'''
53
        self.updateWidgets()
1✔
54
        groups = []
1✔
55
        for groupClass in self.groups:
1✔
56
            # only instantiate the groupClass if it hasn't already
57
            # been instantiated
58
            if interfaces.IGroup.providedBy(groupClass):
1✔
59
                group = groupClass
1✔
60
            else:
61
                group = groupClass(self.context, self.request, self)
1✔
62
            group.update()
1✔
63
            groups.append(group)
1✔
64
        self.groups = tuple(groups)
1✔
65

66
    def extractData(self, setErrors=True):
1✔
67
        '''See interfaces.IForm'''
68
        data, errors = super().extractData(setErrors=setErrors)
1✔
69
        for group in self.groups:
1✔
70
            groupData, groupErrors = group.extractData(setErrors=setErrors)
1✔
71
            data.update(groupData)
1✔
72
            if groupErrors:
1✔
73
                if errors:
1✔
74
                    errors += groupErrors
1✔
75
                else:
76
                    errors = groupErrors
1✔
77
        zope.event.notify(DataExtractedEvent(data, errors, self))
1✔
78
        return data, errors
1✔
79

80
    def applyChanges(self, data):
1✔
81
        '''See interfaces.IEditForm'''
82
        content = self.getContent()
1✔
83
        changed = form.applyChanges(self, content, data)
1✔
84
        for group in self.groups:
1✔
85
            groupChanged = group.applyChanges(data)
1✔
86
            for interface, names in groupChanged.items():
1✔
87
                changed[interface] = changed.get(interface, []) + names
1✔
88
        return changed
1✔
89

90

91
@implementer(interfaces.IGroupForm)
1✔
92
class GroupForm:
1✔
93
    """A mix-in class for add and edit forms to support groups."""
94

95
    groups = ()
1✔
96

97
    def extractData(self, setErrors=True):
1✔
98
        '''See interfaces.IForm'''
99
        data, errors = super().extractData(setErrors=setErrors)
1✔
100
        for group in self.groups:
1✔
101
            groupData, groupErrors = group.extractData(setErrors=setErrors)
1✔
102
            data.update(groupData)
1✔
103
            if groupErrors:
1✔
104
                if errors:
1✔
105
                    errors += groupErrors
1✔
106
                else:
107
                    errors = groupErrors
1✔
108
        zope.event.notify(DataExtractedEvent(data, errors, self))
1✔
109
        return data, errors
1✔
110

111
    def applyChanges(self, data):
1✔
112
        '''See interfaces.IEditForm'''
113
        descriptions = []
1✔
114
        content = self.getContent()
1✔
115
        changed = form.applyChanges(self, content, data)
1✔
116
        for group in self.groups:
1✔
117
            groupChanged = group.applyChanges(data)
1✔
118
            for interface, names in groupChanged.items():
1✔
119
                changed[interface] = changed.get(interface, []) + names
1✔
120
        if changed:
1!
121
            for interface, names in changed.items():
1✔
122
                descriptions.append(
1✔
123
                    zope.lifecycleevent.Attributes(interface, *names))
124
            # Send out a detailed object-modified event
125
            zope.event.notify(
1✔
126
                zope.lifecycleevent.ObjectModifiedEvent(
127
                    content, *descriptions))
128

129
        return changed
1✔
130

131
    def update(self):
1✔
132
        '''See interfaces.IForm'''
133
        self.updateWidgets()
1✔
134
        groups = []
1✔
135
        for groupClass in self.groups:
1✔
136
            # only instantiate the groupClass if it hasn't already
137
            # been instantiated
138
            if interfaces.IGroup.providedBy(groupClass):
1✔
139
                group = groupClass
1✔
140
            else:
141
                group = groupClass(self.context, self.request, self)
1✔
142
            group.update()
1✔
143
            groups.append(group)
1✔
144
        self.groups = tuple(groups)
1✔
145
        self.updateActions()
1✔
146
        self.actions.execute()
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