Custom Django filters in Google App Engine

You want to create your own custom Django filters in App Engine without running a whole Django stack? Here’s how in a few lines of code.

First create a specific python file to hold your custom filters at the root of your application. In my case I use customfilters.py like this:

import re
from google.appengine.ext import webapp

register = webapp.template.create_template_register()

def escapeimg(body):
	return re.sub(r'

', '[IMG]', body)

register.filter(escapeimg)

Then in your main application source file, call the following line outside of the main() definition, for example just after your modules loading:

"""Load custom Django template filters"""
webapp.template.register_template_library('customfilters')

You should then be able to use the new filters you registered in customfilters.py straight away in any of your Django templates without any % load foobar % call

Crossdomain proxy on Google App Engine

Here’s a crossdomain proxy to pipe Javascript Ajax calls from you Google App Engine application to the Flickr API, since most browser will not let you call another domain directly.

import cgi
import urllib
from google.appengine.ext import webapp
from google.appengine.api import urlfetch

class FlickrController(webapp.RequestHandler):
	"""Proxy for Ajax calls to flickr"""
	def get(self):
		flickrapiendpoint = 'http://api.flickr.com/services/rest/'
		flickrapikey = 'you_flicker_api_key'
		
		params = self.request.GET
		params.add('api_key', flickrapikey)
		params.add('format', 'json')
		apiquery = urllib.urlencode(params)
		
		result = urlfetch.fetch(url=flickrapiendpoint + '?' + apiquery, method=urlfetch.GET)
		self.response.out.write(result.content)

def main():
	application = webapp.WSGIApplication(
		[('/flickr/', FlickrController)],
		debug=True)
	wsgiref.handlers.CGIHandler().run(application)

if __name__ == "__main__":
	main()

I didn’t see examples of such a script anywhere else so I thought I’d post it here for all to see.

Google UI genius with suggestions

I was just using Google as a calculator this morning as usual when I noticed something new, at least to me: if you type a simple operation in Firefox’s Google Search box, the result will pop out as a suggestion without having to hit enter and going to the results page.

Operation results appear as suggestions in Google Search box

I love to stumble on those constant little UI and usability improvements that make you wonder if they were there yesterday. It’s brilliant.