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

zopefoundation / grokcore.formlib / 16861221675

18 Jun 2025 06:33AM UTC coverage: 96.005%. Remained the same
16861221675

push

github

icemac
Back to development: 5.1

92 of 106 branches covered (86.79%)

Branch coverage included in aggregate %.

701 of 720 relevant lines covered (97.36%)

0.97 hits per line

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

94.74
/src/grokcore/formlib/tests/functional/form/addform_applydata.py
1
"""
2
We can use AddFrom.applyData to save changes to a newly created
3
object.  The object doesn't yet need to have the attributes that are
4
going to be set on it.
5

6
  >>> getRootFolder()["zoo"] = Zoo()
7

8
  >>> from zope.testbrowser.wsgi import Browser
9
  >>> browser = Browser()
10
  >>> browser.handleErrors = False
11

12
AddForm.applyData() sends an IObjectModifiedEvent after having
13
modified the object.  Its return value is True in a Boolean sense when
14
the object has been modified:
15

16
  >>> browser.open("http://localhost/zoo/@@addmammoth")
17
  >>> browser.getControl(name="form.name").value = "Ellie the Mammoth"
18
  >>> browser.getControl(name="form.size").value = "Really small"
19
  >>> browser.getControl("Add entry").click()
20
  An IObjectModifiedEvent was sent for a mammoth with the following changes:
21
  IMammoth: name, size
22
  >>> print(browser.contents)
23
  There were changes according to applyData.
24

25
  >>> browser.open("http://localhost/zoo/ellie")
26
  >>> print(browser.contents)
27
  Hi, my name is Ellie the Mammoth, and I\'m "Really small"
28

29
"""
30
from zope import schema
1✔
31
from zope.container.btree import BTreeContainer
1✔
32
from zope.container.interfaces import IContainer
1✔
33
from zope.interface import Interface
1✔
34
from zope.interface import implementer
1✔
35
from zope.lifecycleevent.interfaces import IObjectModifiedEvent
1✔
36

37
import grokcore.formlib as grok
1✔
38

39

40
class Zoo(grok.testing.Model, BTreeContainer):
1✔
41
    grok.testing.protect_get(grok.Public, *IContainer)
1✔
42

43

44
class IMammoth(Interface):
1✔
45
    name = schema.TextLine(title="Name")
1✔
46
    size = schema.TextLine(title="Size")
1✔
47

48

49
@implementer(IMammoth)
1✔
50
class Mammoth(grok.testing.Model):
1✔
51
    grok.testing.protect_get(grok.Public, 'name', 'size')
1✔
52
    grok.testing.protect_set(grok.Public, 'name', 'size')
1✔
53

54

55
class Index(grok.View):
1✔
56
    grok.context(Mammoth)
1✔
57

58
    def render(self):
1✔
59
        return 'Hi, my name is {}, and I\'m "{}"'.format(self.context.name,
1✔
60
                                                         self.context.size)
61

62

63
class AddMammoth(grok.AddForm):
1✔
64
    grok.context(Zoo)
1✔
65

66
    form_fields = grok.AutoFields(IMammoth)
1✔
67

68
    @grok.action('Add entry')
1✔
69
    def add(self, **data):
1✔
70
        self.context['ellie'] = ellie = Mammoth()
1✔
71
        if self.applyData(ellie, **data):
1!
72
            return 'There were changes according to applyData.'
1✔
73
        return 'There were no changes according to applyData.'
×
74

75

76
@grok.subscribe(Mammoth, IObjectModifiedEvent)
1✔
77
def notify_change_event(mammoth, event):
1✔
78
    print("An IObjectModifiedEvent was sent for a mammoth with the "
1✔
79
          "following changes:")
80
    for descr in event.descriptions:
1✔
81
        print(descr.interface.__name__ + ": " + ", ".join(descr.attributes))
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