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

zopefoundation / grokcore.viewlet / 5776423262

pending completion
5776423262

push

github

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

* Drop support for Python 2.7, 3.5, 3.6.

46 of 54 branches covered (85.19%)

Branch coverage included in aggregate %.

81 of 81 new or added lines in 21 files covered. (100.0%)

580 of 604 relevant lines covered (96.03%)

0.96 hits per line

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

97.96
/src/grokcore/viewlet/tests/functional/viewlet/viewlet_available.py
1
"""
2

3
===================
4
Viewlet availablity
5
===================
6

7
The viewlet manager will filter out "unavailable" viewlets before rendering.
8
The availability of a viewlet is determined by calling available() on the
9
individuel viewlet. Note that the availability check is performed *after* the
10
update() is caled on the viewlet, but *before* the render() is called.
11

12
Set up a content object in the application root::
13

14
  >>> root = getRootFolder()
15
  >>> root['wilma'] = CaveWoman()
16

17
Traverse to the view on the model object and render the viewlets "Brack Bone"
18
and "T-Rex Bone", but not the "Elephant Bone":
19

20
  >>> from zope.testbrowser.wsgi import Browser
21
  >>> browser = Browser()
22
  >>> browser.handleErrors = False
23
  >>> browser.open("http://localhost/wilma/@@bonesview")
24
  >>> print(browser.contents)
25
  Brack Bone
26
  T-Rex Bone
27

28
At some point in time, the Sabre Tooth Bone becomes availalble:
29

30
  >>> SabreToothBone._available = True
31
  >>> browser.open("http://localhost/wilma/@@bonesview")
32
  >>> print(browser.contents)
33
  Brack Bone
34
  Sabre Tooth Bone
35
  T-Rex Bone
36

37
The availability can depend on some flag available in the request and since the
38
update() of a viewlet is called before the check, the availability can be
39
computed as wel.
40

41
First it is there:
42

43
  >>> browser.open("http://localhost/wilma/@@bonesview?requestcounting=true")
44
  >>> print(browser.contents)
45
  Brack Bone
46
  Only for every other request!
47
  Sabre Tooth Bone
48
  T-Rex Bone
49

50
Next request it is not:
51

52
  >>> browser.open("http://localhost/wilma/@@bonesview?requestcounting=true")
53
  >>> print(browser.contents)
54
  Brack Bone
55
  Sabre Tooth Bone
56
  T-Rex Bone
57

58
And then we have it again:
59

60
  >>> browser.open("http://localhost/wilma/@@bonesview?requestcounting=true")
61
  >>> print(browser.contents)
62
  Brack Bone
63
  Only for every other request!
64
  Sabre Tooth Bone
65
  T-Rex Bone
66

67
"""
68

69

70
from zope.interface import Interface
1✔
71

72
import grokcore.viewlet as grok
1✔
73

74

75
class CaveWoman(grok.Context):
1✔
76
    pass
1✔
77

78

79
class BonesView(grok.View):
1✔
80
    grok.context(Interface)
1✔
81

82

83
class Bones(grok.ViewletManager):
1✔
84
    grok.context(Interface)
1✔
85
    grok.name('bones')
1✔
86

87

88
class BrackerBone(grok.Viewlet):
1✔
89
    grok.context(Interface)
1✔
90
    grok.viewletmanager(Bones)
1✔
91

92
    def render(self):
1✔
93
        return "Brack Bone"
1✔
94

95

96
class TRexBone(grok.Viewlet):
1✔
97
    grok.context(Interface)
1✔
98
    grok.viewletmanager(Bones)
1✔
99

100
    def render(self):
1✔
101
        return "T-Rex Bone"
1✔
102

103

104
class ElephantBone(grok.Viewlet):
1✔
105
    grok.context(Interface)
1✔
106
    grok.viewletmanager(Bones)
1✔
107

108
    def available(self):
1✔
109
        # This type of bone has not evolved just yet.
110
        return False
1✔
111

112
    def render(self):
1✔
113
        return "Elephant Bone"
×
114

115

116
class SabreToothBone(grok.Viewlet):
1✔
117
    grok.context(Interface)
1✔
118
    grok.viewletmanager(Bones)
1✔
119

120
    _available = False
1✔
121

122
    def available(self):
1✔
123
        return self._available
1✔
124

125
    def render(self):
1✔
126
        return "Sabre Tooth Bone"
1✔
127

128

129
class OnlyForEvenRequest(grok.Viewlet):
1✔
130
    # Convoluted example of a viewlet that is available only for every
131
    # other request, but only when we're indicating that we're counting
132
    # requests.
133

134
    count = 1
1✔
135

136
    def update(self):
1✔
137
        if 'requestcounting' in self.request.form:
1✔
138
            OnlyForEvenRequest.count += 1
1✔
139

140
    def available(self):
1✔
141
        if not self.count % 2:
1✔
142
            return True
1✔
143
        return False
1✔
144

145
    def render(self):
1✔
146
        return "Only for every other request!"
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