1 | """Basic layout components. |
---|
2 | """ |
---|
3 | import grok |
---|
4 | from hurry import yui |
---|
5 | from hurry.resource import mode |
---|
6 | from megrok.layout import Page, Layout, EditForm, DisplayForm, AddForm |
---|
7 | from z3c.flashmessage.interfaces import IMessageSource, IMessageReceiver |
---|
8 | from zope.component import getUtility, queryUtility, ComponentLookupError |
---|
9 | from zope.interface import Interface |
---|
10 | try: |
---|
11 | from zope.site.hooks import getSite |
---|
12 | except ImportError: |
---|
13 | # BBB |
---|
14 | from zope.app.component.hooks import getSite |
---|
15 | from waeup.sirp.interfaces import IWAeUPObject |
---|
16 | from waeup.sirp.browser.interfaces import ITheme |
---|
17 | from waeup.sirp.browser.theming import get_all_themes, WAeUPThemeGray1 |
---|
18 | |
---|
19 | grok.templatedir('templates') |
---|
20 | |
---|
21 | def NullValidator(*args, **kw): |
---|
22 | """A validator that does not validate. |
---|
23 | |
---|
24 | This is needed especially for cancel buttons. We don't want data |
---|
25 | to be validated that will be thrown away in the next step. |
---|
26 | |
---|
27 | You can use it with ``grok.action`` decorator like this:: |
---|
28 | |
---|
29 | @grok.action('Cancel', validator=NullValidator) |
---|
30 | def cancel(self, **data): |
---|
31 | self.redirect(<whereever-you-go>) |
---|
32 | """ |
---|
33 | return dict() |
---|
34 | |
---|
35 | class Messages(grok.View): |
---|
36 | """Display messages of message receivers. |
---|
37 | """ |
---|
38 | |
---|
39 | grok.context(Interface) |
---|
40 | |
---|
41 | @property |
---|
42 | def messages(self): |
---|
43 | receiver = getUtility(IMessageReceiver) |
---|
44 | return receiver.receive() |
---|
45 | |
---|
46 | class UtilityView(object): |
---|
47 | """A view mixin with useful methods. |
---|
48 | |
---|
49 | The ``pnav`` attribute (a number) tells, to which primary |
---|
50 | navigation tab a page declares to belong. |
---|
51 | """ |
---|
52 | title = u'' # What appears in the content title... |
---|
53 | pnav = 0 # Primary navigation index... |
---|
54 | |
---|
55 | def application_url(self, name=None): |
---|
56 | """Return the URL of the nearest site. |
---|
57 | """ |
---|
58 | site = getSite() |
---|
59 | #if not site: |
---|
60 | # raise ComponentLookupError("No site found.") |
---|
61 | return self.url(site, name) |
---|
62 | |
---|
63 | def flash(self, message, type='message'): |
---|
64 | """Send a short message to the user. |
---|
65 | """ |
---|
66 | source = queryUtility(IMessageSource, name='session') |
---|
67 | if source is None: |
---|
68 | return None |
---|
69 | source.send(message, type) |
---|
70 | return True |
---|
71 | |
---|
72 | class WAeUPLayout(Layout, UtilityView): |
---|
73 | """A megrok.layout.Layout with additional methods. |
---|
74 | """ |
---|
75 | grok.baseclass() |
---|
76 | |
---|
77 | class WAeUPPage(Page): |
---|
78 | """A megrok.layout page with additional methods. |
---|
79 | """ |
---|
80 | grok.baseclass() |
---|
81 | |
---|
82 | class WAeUPDisplayFormPage(DisplayForm, UtilityView): |
---|
83 | """A megrok.layout.DisplayForm with additional methods. |
---|
84 | """ |
---|
85 | grok.baseclass() |
---|
86 | |
---|
87 | class WAeUPEditFormPage(EditForm, UtilityView): |
---|
88 | """A megrok.layout.EditForm with additional methods. |
---|
89 | """ |
---|
90 | grok.baseclass() |
---|
91 | |
---|
92 | class WAeUPAddFormPage(AddForm, UtilityView): |
---|
93 | """A megrok.layout.AddForm with additional methods. |
---|
94 | """ |
---|
95 | grok.baseclass() |
---|
96 | |
---|
97 | class SiteLayout(WAeUPLayout): |
---|
98 | """ The general site layout. |
---|
99 | """ |
---|
100 | grok.context(IWAeUPObject) |
---|
101 | |
---|
102 | #: An instance of the default theme to use for the site layout |
---|
103 | default_theme = WAeUPThemeGray1() |
---|
104 | |
---|
105 | @property |
---|
106 | def site(self): |
---|
107 | return grok.getSite() |
---|
108 | |
---|
109 | def getAppTitle(self): |
---|
110 | return getattr(grok.getSite(), 'name', u'Sample University') |
---|
111 | |
---|
112 | def isAuthenticated(self): |
---|
113 | """Return True if the calling user is authenticated. |
---|
114 | """ |
---|
115 | usertitle = self.request.principal.title |
---|
116 | return usertitle != 'Unauthenticated User' |
---|
117 | |
---|
118 | def getUserTitle(self): |
---|
119 | """Return principal title of current user. |
---|
120 | """ |
---|
121 | usertitle = self.request.principal.title |
---|
122 | if usertitle == 'Unauthenticated User': |
---|
123 | return u'Anonymous User' |
---|
124 | return usertitle |
---|
125 | |
---|
126 | def getUserId(self): |
---|
127 | """Return id of current user. |
---|
128 | """ |
---|
129 | userid = self.request.principal.id |
---|
130 | return userid |
---|
131 | |
---|
132 | def update(self): |
---|
133 | """Include the resources required by the chosen skin/theme. |
---|
134 | |
---|
135 | University instances provide a 'skin' attribute, that should |
---|
136 | hold the internal name of a theme. |
---|
137 | |
---|
138 | A theme in the waeup.sirp sense consists of a list of |
---|
139 | CSS/JavaScript resources defined in the |
---|
140 | :mod:`waeup.sirp.browser.resources` module. |
---|
141 | |
---|
142 | If the context University object has no such attribute or the |
---|
143 | set value is not a valid theme name, we pick 'gray waeup |
---|
144 | theme' as default. |
---|
145 | """ |
---|
146 | mode('minified') |
---|
147 | theme_name = getattr(grok.getSite(), 'skin', '') |
---|
148 | theme = queryUtility(ITheme, name=theme_name, |
---|
149 | default=self.default_theme) |
---|
150 | for resource in theme.getResources(): |
---|
151 | resource.need() |
---|
152 | return |
---|