CherryPy only implements a few of AOP concepts. I don't have a PHD in AOP and it is not my goal to make CherryPy an AOP reference :-)
Instead, I implemented a few features that I thought might be useful (and they already are, since both HttpAuthenticate and CookieAuthenticate modules use AOP features).
CherryClass NormalHeaderAndFooter: mask: def header(self): <html><body>I'm the header<br><br> def footer(self): <br><br>I'm the footer</body></html>
Then, all you have to do is derive your CherryClasses from NormalHeaderAndFooter and call header and footer in each of your masks or views:
CherryClass Root(NormalHeaderAndFooter): mask: def index(self): <py-eval="self.header()"> Hello, world ! <py-eval="self.footer()"> def otherPage(self): <py-eval="self.header()"> I love cherry pie ! <py-eval="self.footer()">
That's the "classic" object oriented approach. It's pretty good, but if we look at it, we'll realize that we have to repeat <py-eval="self.header()"> and <py-eval="self.footer()"> for each mask and view, and this has several drawbacks:
That's where AOP comes in ... Instead of just declaring "normal" header and footer methods, we'll declare "aspect" methods. This basically adds another information which is: "include my code at the beginning or at the end of each of the methods of all derived classes".
Therefore, the implementation of our simple example using AOP would be:
CherryClass NormalHeaderAndFooter: aspect: (1) start: _page.append("<html><body>I'm the header<br><br>") (1) end: _page.append("<br><br>I'm the footer</body></html>") CherryClass Root(NormalHeaderAndFooter): mask: def index(self): Hello, world ! def otherPage(self): I love cherry pie !
See ... we got rid of the <py-eval="self.header()"> and <py-eval="self.footer()"> lines ...
Let's see what we can notice from looking at the code:
Here is how it works:
The header for an aspect method has two parts:
(condition) startOrEnd:
condition is a python expression used to indicated which method the aspect should apply to. If the condition is always true (as in "1"), then it means that the aspect will apply to all methods of the derived CherryClasses. The python expression can use a special variable called method that contains the following member variables:
Let's go back to our example: let's imagine that we want to use the regular header and footer for both pages index and otherPage, but we want a third page called yetAnotherPage that has its own header and footer. Here is what the code would look like:
CherryClass NormalHeaderAndFooter: aspect: (method.type=='mask' and method.name!='yetAnotherPage') start: _page.append("<html><body>I'm the header<br><br>") (method.type=='mask' and method.name!='yetAnotherPage') end: _page.append("<br><br>I'm the footer</body></html>") CherryClass Root(NormalHeaderAndFooter): mask: def index(self): Hello, world ! def otherPage(self): I love cherry pie ! def yetAnotherPage(self): <html><body bgcolor=red> I love cherry pie ! </body></html>
Basically, when you declare a CherryClass that inherits from CookieAuthenticate or HttpAuthenticate, some code is automatically added at the beginning of each of your masks and views. This code checks if the user is authenticated or not. If not, it returns the login page instead of the regular page ...