1 | """Basic layout components. |
---|
2 | """ |
---|
3 | import grok |
---|
4 | from hurry import yui |
---|
5 | from megrok.layout import Page, Layout, EditForm, DisplayForm, AddForm |
---|
6 | from z3c.flashmessage.interfaces import IMessageSource, IMessageReceiver |
---|
7 | from zope.component import getUtility, queryUtility, ComponentLookupError |
---|
8 | from zope.interface import Interface |
---|
9 | try: |
---|
10 | from zope.site.hooks import getSite |
---|
11 | except ImportError: |
---|
12 | # BBB |
---|
13 | from zope.app.component.hooks import getSite |
---|
14 | from waeup.sirp.interfaces import IWAeUPObject |
---|
15 | |
---|
16 | grok.templatedir('templates') |
---|
17 | |
---|
18 | def NullValidator(*args, **kw): |
---|
19 | """A validator that does not validate. |
---|
20 | |
---|
21 | This is needed especially for cancel buttons. We don't want data |
---|
22 | to be validated that will be thrown away in the next step. |
---|
23 | |
---|
24 | You can use it with ``grok.action`` decorator like this:: |
---|
25 | |
---|
26 | @grok.action('Cancel', validator=NullValidator) |
---|
27 | def cancel(self, **data): |
---|
28 | self.redirect(<whereever-you-go>) |
---|
29 | """ |
---|
30 | return dict() |
---|
31 | |
---|
32 | class Messages(grok.View): |
---|
33 | """Display messages of message receivers. |
---|
34 | """ |
---|
35 | |
---|
36 | grok.context(Interface) |
---|
37 | |
---|
38 | @property |
---|
39 | def messages(self): |
---|
40 | receiver = getUtility(IMessageReceiver) |
---|
41 | return receiver.receive() |
---|
42 | |
---|
43 | class UtilityView(object): |
---|
44 | """A view mixin with useful methods. |
---|
45 | |
---|
46 | The ``pnav`` attribute (a number) tells, to which primary |
---|
47 | navigation tab a page declares to belong. |
---|
48 | """ |
---|
49 | title = u'' # What appears in the content title... |
---|
50 | pnav = 0 # Primary navigation index... |
---|
51 | |
---|
52 | def application_url(self, name=None): |
---|
53 | """Return the URL of the nearest site. |
---|
54 | """ |
---|
55 | site = getSite() |
---|
56 | #if not site: |
---|
57 | # raise ComponentLookupError("No site found.") |
---|
58 | return self.url(site, name) |
---|
59 | |
---|
60 | def flash(self, message, type='message'): |
---|
61 | """Send a short message to the user. |
---|
62 | """ |
---|
63 | source = queryUtility(IMessageSource, name='session') |
---|
64 | if source is None: |
---|
65 | return None |
---|
66 | source.send(message, type) |
---|
67 | return True |
---|
68 | |
---|
69 | class WAeUPLayout(Layout, UtilityView): |
---|
70 | """A megrok.layout.Layout with additional methods. |
---|
71 | """ |
---|
72 | grok.baseclass() |
---|
73 | |
---|
74 | class WAeUPPage(Page): |
---|
75 | """A megrok.layout page with additional methods. |
---|
76 | """ |
---|
77 | grok.baseclass() |
---|
78 | |
---|
79 | class WAeUPDisplayFormPage(DisplayForm, UtilityView): |
---|
80 | """A megrok.layout.DisplayForm with additional methods. |
---|
81 | """ |
---|
82 | grok.baseclass() |
---|
83 | |
---|
84 | class WAeUPEditFormPage(EditForm, UtilityView): |
---|
85 | """A megrok.layout.EditForm with additional methods. |
---|
86 | """ |
---|
87 | grok.baseclass() |
---|
88 | |
---|
89 | class WAeUPAddFormPage(AddForm, UtilityView): |
---|
90 | """A megrok.layout.AddForm with additional methods. |
---|
91 | """ |
---|
92 | grok.baseclass() |
---|
93 | |
---|
94 | class SiteLayout(WAeUPLayout): |
---|
95 | """ The general site layout. |
---|
96 | """ |
---|
97 | grok.context(IWAeUPObject) |
---|
98 | |
---|
99 | @property |
---|
100 | def site(self): |
---|
101 | return grok.getSite() |
---|
102 | |
---|
103 | def getAppTitle(self): |
---|
104 | return getattr(grok.getSite(), 'name', u'Sample University') |
---|
105 | |
---|
106 | def isAuthenticated(self): |
---|
107 | """Return True if the calling user is authenticated. |
---|
108 | """ |
---|
109 | usertitle = self.request.principal.title |
---|
110 | return usertitle != 'Unauthenticated User' |
---|
111 | |
---|
112 | def getUserTitle(self): |
---|
113 | """Return principal title of current user. |
---|
114 | """ |
---|
115 | usertitle = self.request.principal.title |
---|
116 | if usertitle == 'Unauthenticated User': |
---|
117 | return u'' |
---|
118 | return usertitle |
---|
119 | |
---|
120 | def update(self): |
---|
121 | yui.reset_fonts_grids.need() |
---|
122 | yui.sam.need() |
---|