Open Repositories 2012: Review

Offered for the benefit of present and future OR conference organizers.  Rated on the A-B-C-D-F scale.

Edinburgh: Wonderful city!  I hope to go back some day!
A+

Pollock Halls: More or less what I expected from a dorm.  Good breakfast.
B

Pre-conference: Good offerings.
A

Opening session:  Thought-provoking and context-setting keynote.
A

Lunches: Not enough food (or water!).  Friday’s entree served cold.  Inexplicable.
F

Breaks: Suggestion: early morning beverages (at least self-service).
B

Presentations: Good variety and depth.
A

Dinner and Ceilidh: What a treat!  So much fun that I’ll forgive the service staff for constantly removing our water glasses during the ceilidh.
A+

Overall: Great job!
A-

Leave a comment

Export Plone 3 folder to zip file

Motivation

To add functionality to a Plone 3 site to enable a user to download the contents of a folder and all subfolders as a .zip file.

Environment in which this tool was developed

Plone 3.3.1
Zope 2.10.9-final
Python 2.4.6
linux2

Important Notes

Excluded Content

The following types of content are excluded from the .zip file:

  • Links
  • Events
  • Empty Folders

Permissions and workflow state

The .zip file created by this tool includes only files and folders.  Permissions and workflow states on folders or content items are not retained in any way.  Permissions, however, are not bypassed — i.e., only content objects on which the current user has the View permission are included.

HTML Documents: Pages and News Items

“Pages” (a.k.a. “Documents”) and “News Items” are processed in the following way:

  • If the .html file extension is missing, it is added
  • A complete, yet simple, HTML document is created for the content — i.e., the Plone “wrapper” is removed.
  • The body of the document consists of the “cooked” document content, with the addition of an H1 element at the top containing the document title.
    If the document has a description, it is inserted in a paragraph element below the title.
  • The document creator and last modified date are added below the document content.

The Pieces

External module

The easiest way to implement is to create an “old-style” Zope product, which is simply a Python package, and put it in the products directory.  In my case, that directory is /opt/plone/zeoserver/products.  You may want to put an empty file called refresh.txt in the root of the package — this can help in development to avoid having to restart Zope clients to pick up changes — although you do still have to re-save the External Method that references the module, and I found that restarting the clients was often required anyway.  In the Python package, create a subdirectory, not a subpackage, called Extensions, and create your code module there.

For purposes of this article, I’ll call the product package is ExportTool, and the module export.py.

Note: To use the code as-is, you’ll need to create /opt/plone/temp and make it writeable by the user Unix under which the Plone clients run.  Alternatively, you could assign the TEMPDIR global variable in the module to tempfile.gettempdir().

Source:

import cgi
import os
import shutil
import subprocess
import tempfile

TEMPDIR = '/opt/plone/temp'

DOC_TEMPLATE = """\
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  <head>
    <meta content="text/html; charset=utf-8" http-equiv="Content-Type"/>
    <title>%(title)s</title>
  </head>
  <body>
    <h1>%(title)s</h1>
    %(body)s
    <hr/>
    <p>
      Created by: %(creator)s<br/>
      Last modified: %(modified)s
    </p>
  </body>
</html>
"""

def export_folder(context, response):

    transform_tool = context.portal_transforms
    # temp dir for this export job
    tempdir = tempfile.mkdtemp(dir=TEMPDIR)

    def _export(folder, tempdir):
        # create dir into which folder contents will be exported as files
        folder_path = folder.getPhysicalPath()[1:]
        export_dir = os.path.join(tempdir, os.path.join(*folder_path))
        os.makedirs(export_dir)
        for obj in folder.getFolderContents(full_objects=True):
            if obj.portal_type == 'Folder':
                # recursive call
                _export(obj, tempdir)
            elif obj.portal_type in ['Image', 'File', 'Document', 'News Item']:
                filename = obj.getId()
                if obj.portal_type in ['Image', 'File']:
                    content = obj.data
                else:
                    if not filename.endswith('.html'):
                        filename += '.html'
                    body = obj.CookedBody()
                    description = obj.Description()
                    if description:
                        body = '<p>%s</p>\n%s' % (cgi.escape(description, quote=True), body)
                    content = DOC_TEMPLATE % {
                        'body': body,
                        'title': cgi.escape(obj.Title(), quote=True),                        
                        'modified': context.toLocalizedTime(obj.ModificationDate(), long_format=1),
                        'creator': obj.Creator(),
                        }
                outfile = open(os.path.join(export_dir, filename), 'wb')
                outfile.write(content)
                outfile.close()

    try:
        # export the content
        _export(context, tempdir)
        # create a zip file            
        os.chdir(tempdir)
        zipprefix = '-'.join(context.getPhysicalPath()[1:])
        subprocess.call(['/usr/bin/zip', '-r', zipprefix, 'intranet'])
        zipname = zipprefix + '.zip'
        zipped = os.path.join(tempdir, zipname)
        # set response headers
        response.setHeader('Content-Type', 'application/zip')
        response.setHeader('Content-Disposition', 'attachment; filename=%s' % zipname)
        # write zip file to response
        z = open(zipped, 'rb')
        while 1:
            chunk = z.read(8192)
            if chunk:
                response.write(chunk)
            else:
                break
        z.close()        
    finally:
        # delete the temp dir
        shutil.rmtree(tempdir)        
    return

Zope External Method

id: export_folder_to_zip
Module Name: ExportTool.export
Function Name: export_folder

Zope Script (Python)

id: exportFolderToZip

Source:

if context.portal_type == 'Folder' and len(context.getPhysicalPath()) > 3:
    container.export_folder_to_zip(context, container.REQUEST.RESPONSE)

Note: The guard limiting path length is to prevent downloading top-level folders (path element 1 is a slash, 2 is the Plone site, 3 is the first folder).

Add Action to Folder portal type

Title: Zip
Id: zip
URL (Expression): string:${folder_url}/exportFodlerToZip
Condition (Expression): (blank)
Permission: Modify portal content
Category: Object
Visible? (check)

Leave a comment

WSGI daemon mode solves python-ldap connection issues

I was porting a Django/python-ldap application to another server and getting sporadic ldap.SERVER_DOWN errors.  Some basic troubleshooting showed that the problem was occurring specifically when requests routed through mod_wsgi.  If I just ran the Python code — no problem; just the Django app via the dev server — fine; Apache straight to Django (proxying to dev server) — OK.  Now, while I had been planning to investigate mod_wsgi’s “daemon mode” for some time, I was still running all my apps in “embedded mode”.  But with this python-ldap problem, I had to dig deeper into the docs.  The mod_wsgi ApplicationIssues page discusses a number of problems related to C extension modules, and while not specifically mentioning python-ldap, it does make this generalization:

Because of the possibilty that extension module writers have not written their code to take into consideration it being used from multiple sub interpreters, the safest approach is to force all WSGI applications to run within the same application group, with that preferably being the first interpreter instance created by Python.

To force a specific WSGI application to be run within the very first Python sub interpreter created when Python is initialised, the WSGIApplicationGroup directive should be used and the group set to ‘%{GLOBAL}’.

WSGIApplicationGroup %{GLOBAL}

If it is not feasible to force all WSGI applications to run in the same interpreter, then daemon mode of mod_wsgi should be used to assign different WSGI applications to their own daemon processes. Each would then be made to run in the first Python sub interpreter instance within their respective processes.

I did try WSGIApplicationGroup %{GLOBAL} first, but (assuming I implemented it correctly), the problem remained. So I tried WSGI daemon mode and the process has proved stable.

, ,

Leave a comment

A detached user object for Django

Django’s authentication framework (django.contrib.auth) is both pluggable and stackable, which makes integrating custom authentication requirements into Django pretty smooth and easy in most cases.  But I had an edge case: the user ids of the backend system could not be guaranteed to conform to Django’s restriction on user names (even with the recent expansion to accommodate email addresses).  Thus the usual pattern of creating a Django user corresponding to each backend account required a workaround.  At first I tried a hack in which user id from the backend were base64-encoded and munged to conform to Django’s user name limits.  Communication with the backend then required de-munging and decoding the Django user name, etc.  While this seemed to work it was ugly as hell, and in any case, I was using neither the Django admin site nor Django permissions, so I didn’t need a real Django User model instance for that.  On the other hand, I did want to keep the goodness of my pluggable authentication backend and the login view from django.contrib.auth.views, both of which expect User-like objects.

So, I decided to try something kinda crazy: subclassing django.contrib.auth.models.AnonymousUser  and overriding the “anonymity”.  AnonymousUser really takes advantage of Python duck-typing by mimicking the django.contrib.auth.models.User without being a Django model itself, and so isn’t tied to Django’s database.  Here’s what I came up with:

from django.contrib.auth.models import AnonymousUser

class DetachedUser(AnonymousUser):
    """
    Implements a user-like object for user authentication
    when linkage of a backend user identity to a Django user
    is undesirable or unnecessary.
    """

    # mark as not hashable -- see also __eq__() and __ne__(), below
    __hash__ = None 

    # is_active might matter in some contexts, so override
    is_active = True 

    #
    # AnonymousUser sets username to '' and id to None, so we need to at least
    # override those values.
    #
    def __init__(self, username):
        self.username = username
        self.id = username

    def __unicode__(self):
        return self.username

    #
    # is_anonymous() and is_authenticated() are key to distinguishing truly 
    # anonymous/unauthenticated users from known/authenticated ones.
    #
    def is_anonymous(self):
        return False

    def is_authenticated(self):
        return True

    #
    # __eq__ and __ne__ are related to hashing, so be consistent with __hash__, above.
    #
    def __eq__(self, other):
        return NotImplemented

    def __ne__(self, other):
        return NotImplemented

    #
    # Some django.contrib.auth code may call this method,
    # e.g, to update the last login time
    #
    def save(self):
        pass

Now I can code the get_user and authenticate methods of my custom authentication backend to return DetachedUser objects instead of Django users. So far, so good.

Leave a comment

Django-Apache-WSGI reverse proxy and load balancing

Serving Django apps behind a reverse proxy is really pretty straightforward once you’ve set it up, but you might run into a few snags along the way, depending on your requirements.  Load-balancing only adds a little more complexity.  Here’s how I’ve done it.

Example Architecture

  • Front end web server (www.example.com): Apache 2.2 + mod_proxy, mod_proxy_balancer, mod_ssl.
  • Back end application servers (apps-01.example.com, apps-02.example.com): Apache 2.2 + mod_wsgi, mod_ssl; Python 2.6; Django 1.3.1.
  • Backend database server.
  • Additional requirements: Remote user authentication; SSL and non-SSL proxies.

Let’s start with the application servers and deal with the front end later.

Application Servers

Obviously both app servers will be configured the same way.  How to keep them in sync will be discussed briefly.

Django Settings Module

In order for Django to properly create fully-qualified URLs for the front-end client, you must set:

USE_X_FORWARDED_HOST = True

This setting, new in Django 1.3.1, affects the get_host() and build_absolute_uri() methods of django.http.HttpRequest.  If not set, Django will use the value of the HTTP_HOST or SERVER_NAME variables, which are most likely set to the host name of the app server, not the front end.

If you’re using Django’s RemoteUserMiddleware and RemoteUserBackend for authentication, you will need to replace RemoteUserMiddleware with a custom subclass:

from django.contrib.auth.middleware import RemoteUserMiddleware

class ProxyRemoteUserMiddleware(RemoteUserMiddleware):
    header = 'HTTP_REMOTE_USER'

Then update your settings:

MIDDLEWARE_CLASSES = (
    'path.to.ProxyRemoteUserMiddleware',
    )

(It is possible to avoid this by setting REMOTE_USER on the app web server to the value of HTTP_REMOTE_USER, but here I will assume a default setup.)

If you’re using Django’s “sites” framework, you will probably want to set SITE_ID to correspond to the front-end site.  And if your WSGIScriptAlias path differs from the proxied path on the front-end server (not covered in detail here), you may have to use FORCE_SCRIPT_NAME (check the docs).

Django Application Modules and Templates

If your code or templates contain references to REMOTE_ADDR, REMOTE_USER or other server variables (via HttpRequest.META) affected by proxies, you will probably have to change them.  If you’re using Django’s RemoteUserMiddleware or the ProxyRemoteUserMiddleware subclass shown above, you should probably code with request.user.username instead of request.META['REMOTE_USER']; otherwise, you’ll want to reference HTTP_REMOTE_USER.  REMOTE_ADDR will be set to the IP address of the app server, not the proxy front-end; instead you will have to use HTTP_X_FORWARDED_FOR, which can have multiple comma-separated values.

Django Projects and Python Environments

Since we’ve got two app servers, each will have its own Python environment (created with virtualenv) and Django project.  In my setup I decided to serve the Django MEDIA_ROOT from network storage mounted at the same point on each server to avoid synchronization issues.  Otherwise, it seems OK to keep each instance separate (YMMV).  I use Fabric for ensuring that the Python environments and Django projects stay in sync across the two servers.  The precise way you do this syncing depends on your preferences, the available tools, etc.

Apache Configuration

The Apache config on each app server follows the normal Django/WSGI pattern, so I’ll skip the details here.  Note that while it is possible for WSGIScriptAlias path on the app server to differ from the proxied path on the front-end web server (which we’ll get to), this introduces some additional complexities which we will avoid here.  Some issues can be handled on the reverse proxy (front-end) server by Apache directives such as ProxyPassReverse and ProxyPassReverseCookiePath, but you may also need to use Django’s FORCE_SCRIPT_PATH setting in your project settings module.

Front-end Server

At this point you should have working Django projects on each app server under both SSL and non-SSL virtual hosts.  Now we’re going to set up the reverse proxy and load balancing on the front-end server.

Let’s assume your apps are served under the path /webapps on both port 80 and port 443 (SSL) virtual hosts.

Then, you can add to your port 80 virtual host:

<Proxy balancer://django-http>
    BalancerMember http://apps-01.example.com/webapps route=http-1
    BalancerMember http://apps-02.example.com/webapps route=http-2
</Proxy>

<Location /webapps>
    ProxyPass balancer://django-http stickysession=sessionid
    ProxyPassReverse http://apps-01.example.com/webapps
    ProxyPassReverse http://apps-02.example.com/webapps
    ProxyPassReverseCookieDomain apps-01.example.com www.example.com
    ProxyPassReverseCookieDomain apps-02.example.com www.example.com
</Location>

And to your SSL virtual host on port 443:

<Proxy balancer://django-https>
    BalancerMember https://apps-01.example.com/webapps route=https-1
    BalancerMember https://apps-02.example.com/webapps route=https-2
</Proxy>

<Location /webapps>
    ProxyPass balancer://django-https stickysession=sessionid
    ProxyPassReverse https://apps-01.example.com/webapps
    ProxyPassReverse https://apps-02.example.com/webapps
    ProxyPassReverseCookieDomain apps-01.example.com www.example.com
    ProxyPassReverseCookieDomain apps-02.example.com www.example.com
</Location>

This isn’t the only way to do it, of course, and you may have different requirements, but I’ve tried to cover the basics.

1 Comment

SVN 1.7 breaks Python packaging

Forget my problems with Emacs and SVN 1.7.  A much worse problem is that my Python package distributions are breaking because neither setuptools nor distribute groks the new working copy format introduced in SVN 1.7, and so not all files which are actually under version control get included in the output of dist commands (without using an explicit manifest).  So, I’m forced to downgrade to SVN 1.6, which is alright by me.  First I had to uninstall from Cygwin SVN and all packages that depend on it.  Then I installed the SlikSVN Win64 distribution of SVN 1.6.  And, finally, I had to trash all my SVN 1.7 working copies (no way to downgrade those) and re-checkout everything.

Leave a comment

Emacs + SVN 1.7 = Ouch

It’s only partly in jest when I say that working in IT means being annoyed most of the time.   Sometimes it feels that way.  The latest annoyance came last week: updating my Cygwin install brought SVN 1.7.  I knew about the required updating of working copies to the new format.  What I didn’t know was that my beloved Emacs version control integration would break so horribly.  I suppose this is another reason for Emacs-haters to chuckle, and I’m rarely inclined to tinker with the config or try alternative Emacs modes, so I’m basically stuck with the brokenness until a fix comes down stream.  All in a day’s work.

3 Comments

Use the framework

I don’t know how many times I’ve developed or implemented some clever enhancement of an application or framework (e.g., Plone, Django, others) only to regret it later.  Of course it always seems like a reasonable idea at the time, but the more repetitions of this pattern I experience, the more skeptical I am of fancy solutions (included many of my own published on this site) and add-ons that promise short-term feature additions but too often incur long-term maintenance costs that simply aren’t worth the benefits.  Here’s how I’m thinking about it now:

  • Don’t reverse engineer the system.  By all means, read the code, just don’t try to outsmart it.
  • If you can help it, don’t use methods or attributes marked as private (yes, I have a beef with Django’s Model._meta).
  • Don’t use a third-party product for something you can do relatively easily without it.  Stop and ask yourself if the coding you may save is worth the headache of maintaining the product through framework upgrades, etc.
  • Keep an eye on framework developments.  Watch the roadmap.  Read the release notes!  I’ve been pleasantly surprised a number of times to find out about new (or existing) features by re-reading documentation.
  • Participate in the core community.  Everybody benefits from improvements to the core framework.  This is not to say that every good product should ultimately aim for inclusion in the framework, but with a strong group of core developers new features are subjected to critical analysis, rigorous testing and benefit from wide usage and long term support.

,

Leave a comment

Django management command barfs on sites framework

As always, the story is a bit convoluted …

I had recently made changes to the get_absolute_url() methods on a couple of application models (Django 1.3/Python 2.4).  For various reasons, the admin UI for the app is on an intranet site, which is a different host from the public site (represented by a different Django Site object).  The changes involved calling a new function that uses the current site object to determine the appropriate domain for constructing a fully-qualified URL for get_absolute_url().  As originally implemented, I set a variable at the module level in models.py:

BASE_PUBLIC_URL = 'http://%s/path/to/app' % get_public_domain()

where get_public_domain() imports the Site model class from django.contrib.sites, calls Site.objects.get_current() and returns the appropriate public domain.  The get_absolute_url() methods then used BASE_PUBLIC_URL in constructing the final URL.

This worked fine in the normal application contexts, i.e., the admin site and the public site.  However, a custom management command which updates app data from an external source raised an ImportError on the relevant model.  The significant part of the traceback was as follows:

  File "/path/to/pyenv/lib/python2.4/site-packages/django/contrib/sites/models.py", line 25, in get_current
    current_site = self.get(pk=sid)

Ad hoc testing showed the ImportError to be a red herring — and, in any case, it didn’t square with the fact that the rest of the app (minus the admin command) was functional.

Now, the management command doesn’t actually call get_absolute_url(), so I figured that maybe the solution was to wrap the base public URL in a memoized function, so that the current site object is accessed lazily:

@memoize
def get_base_public_url():
    from path.to.mymodule import get_public_domain
    return 'http://%s/path/to/app' % get_public_domain()

That did the trick.  I’m still not sure exactly why the sites framework barfed, and it doesn’t seem worth digging for …

, ,

Leave a comment

Django 1.3 First Impressions

On the whole I am very pleased with Django 1.3.  The developers did a good job of taking care of some of the outstanding warts in Django, particularly the lack of “static file” management.  While I have not yet used the django.contrib.staticfiles app, it appears to solve the problem in a reasonable way.  Now I can retire the workarounds (such as this) that I had developed to deal with the problem.  The addition of built-in logging support is certainly welcome.  Improving and fixing inconsistencies in certain template tags all seems good.  I previously praised the render() shortcut, which will eliminate the repetitive nonsense of render_to_response(context_instance=RequestContext(request)).  The ORM got an important patch allowing configuration of on-delete behavior for ForeignKey (and OneToOneField) fields, about which I had also posted.  One interesting, small, but nice improvement came unannounced: help_text on a form field is now wrapped by a <span></span> when rendered as HTML by the as_*() methods.  I actually filed a ticket reporting this omission from the announcement.  For some reason, it seems that getting this change into the code and documentation has been a challenge.

I suspect there may be some moaning around the deprecation of function-based generic views in favor of class-based views.   Class-based views make sense, but it looks like there will be a little pain in the transition, partly because the keyword arguments for Django’s built-in generic view functions don’t map exactly to the generic view class keyword arguments.  It would have been nice to provide a little smoother transition there.  Also, the class-based view documentation is rather dense because you have to refer to the mixin classes that compose the actual generic view class you want to use.  I’m sure it will get easier with time, but it does feel like a jump in complexity that could make generic views more difficult for new users.    For example, where I did this before in a URLconf module:

from django.conf.urls.defaults import *
from django.views.generic.simple import direct_to_template

urlpatterns = patterns('',
    (r'^status/$', direct_to_template,
     {'template': 'sitetest/status.txt', 'mimetype': 'text/plain'}),
)

I now have to do something like this:

from django.conf.urls.defaults import *
from django.views.generic.base import TemplateView

class PlainTextTemplateView(TemplateView):
    """A plain text generic template view."""
    def render_to_response(self, context, **kwargs):
        return super(PlainTextTemplateView, self).render_to_response(
            context, content_type='text/plain', **kwargs
            )

urlpatterns = patterns('',
    (r'^status/$', PlainTextTemplateView.as_view(template_name='sitetest/status.txt')),
)

While class-based views may have been a step in the right direction for the framework, I wonder how it will play out.

1 Comment