Archive for the ‘zope’ Category

Zope/Plone day at Birkbeck

Wednesday, February 13th, 2008

We held a Zope/Plone users and developers day at Birkbeck yesterday (12 February 2008), the second in hopefully a long running series of events aimed at users of Zope and Plone in UK higher education.

As with the first one in Bristol in May 2007, it was very useful. We had a number of presentations on various themes covering all aspects of Plone development and roll-out (customisation, performance, testing and end-user training). It was also great just to chat to other Plone developers and compare notes; not to mention reassuring that others were experiencing the same kind of issues as us.

I’ll put a summary of my presentation up on this blog, but we’re hoping the integrate the presentations and other notes into the Plone4Universities site (we had the logo designer for this site as one of our delegates) which seems like the natural home for this kind of thing.

Variables in Python regular expressions: an example

Monday, February 11th, 2008

This is something very useful which I picked up from a mailing list, so I’m certainly not claiming to have come up with it.

The problem: basically, you need to replace some text or code using a regular expression in Python, but you need that regular expression to contain a variable reference so it can be run within a loop etc. How do you do this?

The example below is the actual one I was working on — I needed to replace a <div> tag with a changing id with the actual contents of a file within Zope, in order to insert HTML snippets (YouTube video, Google code etc.) into a page. The page editing tool we use (EditonPro) sensibly strips out this kind of stuff, but this was a special case where the site editor really needed to do this. The inserted tags looked something like this:

 <div id="snippet-123456">
   [snippet: YouTube video of something]
 </div>

Then I defined a method (getSnippet.py) with the following code:

import re

def getSnippet(self, content):

   content = content.replace('\n','')
   pattern = re.compile(r"""(<div id="snippet

-)([^"]*)(">[^<]*)(</div>)""", re.MULTILINE)

   reg = re.findall(pattern, content)

   if reg:

      count = len(reg)

      i=0

      while i < count:

         snpid = str(reg[i][1])

         sr =  r"""<div id="snippet-%s"[^<]*</div>"""

         src = re.compile(sr % snpid)

         srch = src.findall(content)

         srch = srch[0]

         try:

           repl = self.aq_parent[snpid].data

         except:

           repl = ''

      content = content.replace(srch, repl)

      i=i+1

   return content

Briefly, this is how this works: in the document_view page template a call is made to getSnippet thus:

<div tal:define="content here/CookedBody"
     tal:replace="structure python: here.getSnippet(content)"/>

The page content is passed to the getSnippet External Method which first does a regex to find all the relevant bits of code. If anything is returned in the variable “reg”, a while loop is initiated.

The div ids, which map to file names of the Zope files, are extracted from “reg” while this loop runs. We use them to construct a regular expression which will match the relevant snippet code. The key lines are these:

sr =  r"""<div id="snippet-%s"[^<]*</div>"""

src = re.compile(sr % snpid)

We use the %s syntax when defining our regular expression and pass the variable to it when compiling it.

It works, great stuff.

Mod proxy magic

Monday, February 11th, 2008

One of the useful things about getting my Plone hosting sorted is that I’ve been forced to dabble in some of the dark arts of systems administration. I now know a little more about such things as mod_rewrite and mod_proxy in Apache. I was attempting to seemingly integrate my Plone site with my existing PHP-based site, doing something like this:

RewriteEngine On
RewriteBase /

RewriteRule ^/plone($|/.*)
http://myplonesite:9080/VirtualHostBase/http/www.littled.net:80/VirtualHostRoot/_vh_plone$1
[L,P]

So littled.net/plone would lead you straight into the Plone site. When I finally got the .htaccess file syntax correct which took a few passes (like getting the RewriteBase set properly) it kind of worked, except my hosting company don’t have mod_proxy installed (I guess for security reasons which is reasonable).  When the flags were taken out, the redirect did work, rewriting the URLs on the Plone page, but leaving the hideous Virtual Host Monster URL in the browser location bar. So, what I learned is this:

Even if you are using Rewrite rules and not using mod_proxy directly, you still need mod_proxy enabled for it to work properly.

Never mind, my Plone site is now going to be separate site from this with its own URL.