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

zopefoundation / five.formlib / 16399733854

15 Feb 2025 02:34PM UTC coverage: 83.302%. Remained the same
16399733854

push

github

icemac
Back to development: 5.1

49 of 92 branches covered (53.26%)

Branch coverage included in aggregate %.

390 of 435 relevant lines covered (89.66%)

0.9 hits per line

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

71.05
/src/five/formlib/metaconfigure.py
1
##############################################################################
2
#
3
# Copyright (c) 2004, 2005 Zope Corporation 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

15
import zope.component
1✔
16
from AccessControl.class_init import InitializeClass
1✔
17
from AccessControl.security import protectClass
1✔
18
from ExtensionClass import Base
1✔
19
from Products.Five.browser.metaconfigure import SimpleViewClass
1✔
20
from Products.Five.browser.pagetemplatefile import ZopeTwoPageTemplateFile
1✔
21
from zope.app.form.browser.metaconfigure import BaseFormDirective
1✔
22
from zope.browser.interfaces import IAdding
1✔
23
from zope.browsermenu.metaconfigure import menuItemDirective
1✔
24
from zope.i18nmessageid import MessageFactory
1✔
25
from zope.interface import Interface
1✔
26
from zope.publisher.interfaces.browser import IDefaultBrowserLayer
1✔
27

28
from five.formlib import AddView
1✔
29
from five.formlib import EditView
1✔
30

31

32
_ = MessageFactory('zope')
1✔
33

34

35
def EditViewFactory(name, schema, label, permission, layer,
1✔
36
                    template, default_template, bases, for_, fields,
37
                    fulledit_path=None, fulledit_label=None, menu=''):
38
    class_ = SimpleViewClass(template, globals(), used_for=schema,
1✔
39
                             bases=bases)
40
    class_.schema = schema
1✔
41
    class_.label = label
1✔
42
    class_.fieldNames = fields
1✔
43

44
    class_.fulledit_path = fulledit_path
1✔
45
    if fulledit_path and (fulledit_label is None):
1!
46
        fulledit_label = "Full edit"
×
47

48
    class_.fulledit_label = fulledit_label
1✔
49

50
    class_.generated_form = ZopeTwoPageTemplateFile(default_template)
1✔
51

52
    if layer is None:
1!
53
        layer = IDefaultBrowserLayer
×
54

55
    s = zope.component.getGlobalSiteManager()
1✔
56
    s.registerAdapter(class_, (for_, layer), Interface, name)
1✔
57

58
    # Reminder: the permission we got has already been processed by
59
    # BaseFormDirective, that means that zope.Public has been
60
    # translated to the CheckerPublic object
61
    protectClass(class_, permission)
1✔
62
    InitializeClass(class_)
1✔
63

64

65
class FiveFormDirective(BaseFormDirective):
1✔
66

67
    def _processWidgets(self):
1✔
68
        if self._widgets:
1✔
69
            customWidgetsObject = type(
1✔
70
                'CustomWidgetsMixin', (Base,), self._widgets)
71
            self.bases = self.bases + (customWidgetsObject,)
1✔
72

73

74
class EditFormDirective(FiveFormDirective):
1✔
75

76
    view = EditView
1✔
77
    default_template = 'edit.pt'
1✔
78
    title = _('Edit')
1✔
79

80
    def _handle_menu(self):
1✔
81
        if self.menu:
1!
82
            menuItemDirective(
×
83
                self._context, self.menu, self.for_ or self.schema,
84
                '@@' + self.name, self.title, permission=self.permission,
85
                layer=self.layer)
86

87
    def __call__(self):
1✔
88
        self._processWidgets()
1✔
89
        self._handle_menu()
1✔
90
        self._context.action(
1✔
91
            discriminator=self._discriminator(),
92
            callable=EditViewFactory,
93
            args=self._args(),
94
            kw={'menu': self.menu},
95
        )
96

97

98
def AddViewFactory(name, schema, label, permission, layer,
1✔
99
                   template, default_template, bases, for_,
100
                   fields, content_factory, arguments,
101
                   keyword_arguments, set_before_add, set_after_add,
102
                   menu=''):
103
    class_ = SimpleViewClass(template, globals(), used_for=schema,
1✔
104
                             bases=bases)
105

106
    class_.schema = schema
1✔
107
    class_.label = label
1✔
108
    class_.fieldNames = fields
1✔
109
    class_._factory = content_factory
1✔
110
    class_._arguments = arguments
1✔
111
    class_._keyword_arguments = keyword_arguments
1✔
112
    class_._set_before_add = set_before_add
1✔
113
    class_._set_after_add = set_after_add
1✔
114

115
    class_.generated_form = ZopeTwoPageTemplateFile(default_template)
1✔
116

117
    if layer is None:
1!
118
        layer = IDefaultBrowserLayer
×
119

120
    s = zope.component.getGlobalSiteManager()
1✔
121
    s.registerAdapter(class_, (for_, layer), Interface, name)
1✔
122

123
    # Reminder: the permission we got has already been processed by
124
    # BaseFormDirective, that means that zope.Public has been
125
    # translated to the CheckerPublic object
126
    protectClass(class_, permission)
1✔
127
    InitializeClass(class_)
1✔
128

129

130
class AddFormDirective(FiveFormDirective):
1✔
131

132
    view = AddView
1✔
133
    default_template = 'add.pt'
1✔
134
    for_ = IAdding
1✔
135

136
    # default add form information
137
    description = None
1✔
138
    content_factory = None
1✔
139
    arguments = None
1✔
140
    keyword_arguments = None
1✔
141
    set_before_add = None
1✔
142
    set_after_add = None
1✔
143

144
    def _handle_menu(self):
1✔
145
        if self.menu or self.title:
1!
146
            if (not self.menu) or (not self.title):
×
147
                raise ValueError("If either menu or title are specified, "
×
148
                                 "they must both be specified")
149
            # Add forms are really for IAdding components, so do not use
150
            # for=self.schema.
151
            menuItemDirective(
×
152
                self._context, self.menu, self.for_, '@@' + self.name,
153
                self.title, permission=self.permission, layer=self.layer,
154
                description=self.description)
155

156
    def _handle_arguments(self, leftover=None):
1✔
157
        schema = self.schema
1✔
158
        fields = self.fields
1✔
159
        arguments = self.arguments
1✔
160
        keyword_arguments = self.keyword_arguments
1✔
161
        set_before_add = self.set_before_add
1✔
162
        set_after_add = self.set_after_add
1✔
163

164
        if leftover is None:
1!
165
            leftover = fields
1✔
166

167
        if arguments:
1!
168
            missing = [n for n in arguments if n not in fields]
×
169
            if missing:
×
170
                raise ValueError("Some arguments are not included in the form",
×
171
                                 missing)
172
            optional = [n for n in arguments if not schema[n].required]
×
173
            if optional:
×
174
                raise ValueError("Some arguments are optional, use"
×
175
                                 " keyword_arguments for them",
176
                                 optional)
177
            leftover = [n for n in leftover if n not in arguments]
×
178

179
        if keyword_arguments:
1!
180
            missing = [n for n in keyword_arguments if n not in fields]
×
181
            if missing:
×
182
                raise ValueError(
×
183
                    "Some keyword_arguments are not included in the form",
184
                    missing)
185
            leftover = [n for n in leftover if n not in keyword_arguments]
×
186

187
        if set_before_add:
1✔
188
            missing = [n for n in set_before_add if n not in fields]
1✔
189
            if missing:
1!
190
                raise ValueError(
×
191
                    "Some set_before_add are not included in the form",
192
                    missing)
193
            leftover = [n for n in leftover if n not in set_before_add]
1✔
194

195
        if set_after_add:
1!
196
            missing = [n for n in set_after_add if n not in fields]
×
197
            if missing:
×
198
                raise ValueError(
×
199
                    "Some set_after_add are not included in the form",
200
                    missing)
201
            leftover = [n for n in leftover if n not in set_after_add]
×
202

203
            self.set_after_add += leftover
×
204

205
        else:
206
            self.set_after_add = leftover
1✔
207

208
    def __call__(self):
1✔
209
        self._processWidgets()
1✔
210
        self._handle_menu()
1✔
211
        self._handle_arguments()
1✔
212

213
        self._context.action(
1✔
214
            discriminator=self._discriminator(),
215
            callable=AddViewFactory,
216
            args=self._args() + (self.content_factory, self.arguments,
217
                                 self.keyword_arguments,
218
                                 self.set_before_add, self.set_after_add),
219
            kw={'menu': self.menu},
220
        )
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