Archive for January, 2010

Morning Surf

January 7, 2010

Today I had an unusual morning, surfing while eating my cereal.  Here are the stars of the session:

Google failed this time.

“I wish these to be my wallpaper” links.

And an article about how to search on the Internet.

I want to monitor!

January 6, 2010

I’m fed of with nagios, and looking for an alternative, again.  I had a quick run over wikipedia and found 3 options after spending 5 minutes, by just judging the products by how they are marketted. I’ll have a deeper look to these three:

  1. http://www.hyperic.com/
  2. http://www.zabbix.com/
  3. http://pandorafms.org/

What can you suggest for a Nagios alternative?

Automatically decorating all views of a django project

January 5, 2010

After a long break I’m back to my blog.  I moved from Turkey to Singapore, which is a little change in my life.  Now the real thing, a middleware for Django.

This middleware auto-decorates each view function in a view module (views.py, or views/) with the decorators defined in settings.py.

An example setting is:

VIEW_AUTHENTICATORS = {
    "module.views.secret": [
        "django.contrib.auth.decorators.login_required",
        "module.decorators.secret_access_required"],
    "module.views.public": [
        "django.contrib.auth.decorators.login_required",
        "ptms.decorators.public_access_required"]
    }

Think this middleware as a firewall.  Whether you decorated your view functions or not, this ensures that your content is protected by using the decorators.

Note that this middleware needs to be at the end of the middlewares list because it always return an HttpResponse by calling the view function; which means no other middleware after this is executed.

Here is the function:

    def process_view(self, request, view_func, view_args, view_kwargs):
        if not hasattr(settings, "VIEW_AUTHENTICATORS"): return None
        mauths = settings.VIEW_AUTHENTICATORS.keys()
        module = view_func.__module__

        # Trying to find if any authenticator is defined for this module
        module_matches = [mauth for mauth in mauths if module.startswith(mauth)]

        # if there is no match, bail out
        if not module_matches: return None

        # if there is a match...
        authenticators = settings.VIEW_AUTHENTICATORS[module_matches[0]]

        functions = []
        # go on parsing the list
        for function_path in authenticators:
            try:
                dot = function_path.rindex('.')
            except ValueError:
                raise exceptions.ImproperlyConfigured, '%s isn\'t a function path (error from settings.VIEW_AUTHENTICATORS)' % function_path

            f_module, f_name = function_path[:dot], function_path[dot+1:]

            try:
                mod = import_module(f_module)
            except ImportError, e:
                raise exceptions.ImproperlyConfigured, 'Error importing module %s from settings.VIEW_AUTHENTICATORS: "%s"' % (f_module, e)
            try:
                f = getattr(mod, f_name)
            except AttributeError:
                raise exceptions.ImproperlyConfigured, 'View authenticator %s is not defined in %s' % (f_module, f_name)

            functions.append(f)

        def compose(f, g): return lambda *args, **kws: f(g(*args, **kws))
        multi_compose = functools.partial(reduce, compose)
        all_decorators = multi_compose(functions)
        decorated_view = all_decorators(view_func)
        return decorated_view(request, *view_args, **view_kwargs)

I await all the comments.