[Grok-dev] Beauty and style
Paul Sephton
prsephton at gmail.com
Sun Oct 21 10:29:43 UTC 2012
Hi, List,
Instead of the typical "please help" mails, I thought I would share my
appreciation of one of those "little" useful things made trivial by the
use of python, zope components & grok. It reads so naturally that
comments are almost unnecessary.
class IScriptMail(Interface):
''' For embedding email links as scripted elements with obfuscated text '''
def __call__(self, email):
''' Returns a scripted email link '''
class ScriptMail(grok.GlobalUtility):
''' For embedding email links as scripted elements with obfuscated text '''
grok.implements(IScriptMail)
""" Translate an email address into an embed-able script that protects the address """
def translate(self, email): # translates text into html codes
return "".join(["&#%i;"%i for i in [ord(q) for q in email]])
def __call__(self, email, linkText=None):
email=self.translate(email)
linkText=self.translate(linkText) if linkText is not None else email
script="""document.write("<a href='mailto:%s'>%s</a>");"""%(email, linkText)
return """<script type='text/javascript'>\n%s\n</script>\n""" % script
... later in some python file somewhere, possibly completely unrelated...
class MyViewlet(grok.Viewlet):
grok.viewletmanager(SomeViewletMagnaget)
grok.context(MyContext)
def email(self, etext):
script = getUtility(IScriptMail)
return script(etext)... and in the page template 'myviewlet.pt' ....
<p>You may contact us via email at <span tal:content="structure python:viewlet.email('someone at somewhere.com')" /></p>
The elegance of this sort of thing just leaves one gasping. The
simplicity, code re-usability, maintainability, and pure beauty- don't
you agree? I never want to have to code without the zope component
framework again, and grok makes it so easy!
It's nice to get all enthusiastic about something once in a while,
Paul
--
We will have solar energy as soon as the utility companies solve one
technical problem -- how to run a sunbeam through a meter.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: prsephton.vcf
Type: text/x-vcard
Size: 433 bytes
Desc: not available
URL: <http://mail.zope.org/pipermail/grok-dev/attachments/20121021/72ccd8d1/attachment.vcf>
More information about the Grok-dev
mailing list