Archive for the ‘Uncategorized’ Category

Side-stepping the absolute URL requirement in WordPress

This is a limited use-case, but it’s one I’ve just successfully figured out how to deal with. Hope it helps.

I have a WordPress codebase that is shared between three domains. The code executes the same on all three domains — only the domain itself is different. Which, there should be nothing inherently problematic about doing this.

Problem is, when you set up your blog’s settings, WordPress requires that you enter a fully-qualified absolute URL for the blog’s root directory — and that URL is inserted throughout every nook and cranny of the app. For whatever reason, you can’t supply it a domain-less path, even though it seems like this would be a perfectly reasonable thing to expect. (I read one explanation that this would be too problematic for many users.) There are way too many references to this absolute URL to fix by hand, and if it can be avoided, it’s not generally a good idea to go monkeying around in the guts of the app to try to change its behavior. Maintenance becomes a lot more hands-on. Upgrading can easily become a migraine.

So: database hacks to the rescue.

You can actually force WordPress to use a domain-less root path, by altering two settings directly in the database. In the wp_options table, change the siteurl and home values to whatever URL you like. Like, say, the perfectly reasonable / or /blog. The only downside is that if you ever try to change anything on the General Settings page from within wp-admin, WordPress will bark at you and give you a couple of error messages. It’s okay. You can ignore them. Everything else works just fine.

Update: Apparently, everything else does not work just fine. Setting the URL in the database broke Akismet.

Akismet requires the home value in the database options to be the URL associated with your API key. Unsurprisingly, if it doesn’t have that URL, the service requests will fail.

WordPress is great and all, but like every other off-the-shelf software package, there are times where if you want to customize it and color outside the lines they’ve drawn for you, you end up diving down a frustrating rabbit hole.

I’m sure I’ll port this bad boy over to Django soon enough. Then: sweet, satisfying, complete control.

Version-controlled

Got my databases under version control.

This was the helpful start. I adapted the instructions to create self-contained backup scripts — one each for mysql and postgres — which can be called on-demand or nightly in crontab. The scripts get a list of the available databases and iterate through, creating a single dump file for each database. The contents are then committed to git and then pushed to the remote backup site.

Done.

Integrating Jinja2 and Django

Jinja2 is a real improvement over the standard Django template system. It has a number of useful features that the built-in Django templates just don’t have — especially when it comes to basic visual markup logic (elif statements, basic macros). Thankfully, Django is built to be pluggable and modular, and it’s not terribly difficult to get Jinja2 working with it.

When I wrote my first Jinja2-based app a couple of years ago, I did so using a little helper script I cobbled together from various Jinja2/Django-type pioneers, and the result was a workable — if a little hacky and kludgy — module that I could reuse in each of my apps. I import it in my views.py, and it gives me the most basic template-type methods like render_to_response and such. It’s limited, but I really haven’t had need for much else. (And when I’ve needed to expand its functionality, I have.) But it’s always felt like a temporary fix — a little incomplete and hackish. I took a look around recently to see if there were any other solutions that integrated Jinja2 into Django a little more gracefully and completely.

It wasn’t long before I found coffin — a library that’s more or less a port of Django’s template functionality, but using Jinja2′s rendering. Its real killer feature, IMO, is how easy it is to use Django template tags and filters within coffin — which is something you might not expect to need, but it’s very handy to have. For example, coffin includes a ton of useful tags and filters out of the box — like the super-useful {% spaceless %}. And you can easily add others, without having to write a complete port.

Coffin works a lot like my little helper module. You import it in every page, explicitly.

But I found something that I think is just a tad better: this fork of coffin. Its best feature is that it can be plugged in directly to Django’s native template loading system, so that it is integrated a little more neatly. All it takes is just a little adjustment to your project’s settings file:


TEMPLATE_LOADERS = (
'coffin.template.loaders.Loader',
)

JINJA2_TEMPLATE_LOADERS = (
'django.template.loaders.app_directories.Loader',
'django.template.loaders.filesystem.Loader',
)

The coffin template handling is enabled more or less transparently — using Django’s syntax while the templates are handled behind the scenes. Thus, in your apps, instead of this:

from coffin.shortcuts import render_to_response

You can use the usual Django syntax:

from django.shortcuts import render_to_response

Helpfully, this fork also allows you to turn off Jinja2 rendering on an app-by-app basis, with a simple tuple in your settings file. So it’s easy to have, say, the Django debug toolbar running with Django templates alongside your app that uses Jinja2 templates.

I really like this approach, mainly for the reason that enabling Jinja2 can be enabled — and overridden — completely from the settings file.

I don’t have a strong opinion about the pros and cons of these two approaches, but I’ll be trying out the template-loader fork of coffin to see how well it runs and how easy it is to use. I’ll be curious to see if anyone has any input as to whether the template-loader approach has any design concerns that I haven’t considered.

Meanwhile, give it a shot! Enjoy.

Pro Tip: Using Fluid for Google Apps mail in OS X

I just switched to a MacBook Pro (finally), so I’ve been spending some time porting my usual apps and whatnot over to the new machine. Probably the one immediate disappointment I’ve encountered is that the Mac version of Chrome can’t (yet) create application shortcuts, which is a convenience I’ve grown to depend on. It’s really quite a clever convenience — Chrome will take any website and create a desktop-like app out of it, all self-contained in its own tidy little window. It makes checking my mail and calendar a lot faster and more convenient, and the way I use them, my desktop clutter is cut way down.

So I’ve had to find an alternative, and the #1 recommendation is an app called Fluid. It’s essentially a single-minded utility dedicated to creating these self-contained web apps, and it’s not bad.

Unfortunately, I encountered some problems after creating the app for my Google Apps mail account. When I launched the new app, three new browser windows would open in addition to the main app window. Not desirable, and I had no desire to go on a deep troubleshooting expedition.

I figured that Fluid was somehow misinterpreting something in the Google Apps startup code, leading it to think that it needed to open up several new windows. The big clue was that all of the windows were pointed to some version of mail.google.com — not my domain. Apparently, when Fluid parses URLs from a different domain, its default behavior is to spawn new browser windows. Obviously, the discrepancy between my mail domain and gmail was causing the problem. After spending a couple of minutes looking through the settings, I discovered a helpful workaround that stops this behavior.

Within the app for your Google Apps mail account, go to Preferences > Advanced. Then under “Allow browsing to URLs matching these patterns”, add “*mail.google.com*” to the list. This tells Fluid that mail.google.com should be considered part of the app — and that it shouldn’t open new browser windows when it loads URLs from those addresses. Now it works fine.

The ultimate result of shielding man from the effects of folly is to people the world with fools.

– Herbert Spencer

These ever-expanding enforcement powers miss the point: The threat posed by drunk driving comes not from drinking per se but from the impairment drinking can cause. [...]

If our ultimate goals are to reduce driver impairment and maximize highway safety, we should be punishing reckless driving. It shouldn’t matter if it’s caused by alcohol, sleep deprivation, prescription medication, text messaging, or road rage.

Radley Balko

It is the working man who is the happy man

It is the working man who is the happy man. Man was made to be active, and he is never so happy as when he is so. It is the idle man who is the miserable man.

– David Alfred Doudney, Old Jonathan, or the Parish Helper (though often misattributed to Benjamin Franklin)

Q: How does one eat an elephant?

A: One bite at a time.

Getting things done

Changing the status quo.

Desires or daydreams?

If I am unwilling to take responsibility for the attainment of my desires, they are not really desires — they are merely daydreams. For any professed desire to be taken seriously, I must be prepared to answer, in realistic terms: What am I willing to do to get what I want?

— Nathaniel Branden

Return top