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.

MQL baffles me

My job consists of writing functional specifications and generally managing the architecture of a big R&D information system based on a wonderful jack-of-all-trade application called eMatrix from Dassault.

I often delve deep into the application with the MQL console (for Matrix Query Language) to dig out some insight into the data we manage.

And, more often than not, I find things that makes me cringe:

MQL<45> print bus ECO ZCO3171 - select history.promote;
business object ECO ZCO3171 -
history.promote = time: 10/22/2007 9:56 state: Design Work
history.promote = time: 10/22/2007 17:23 state: Review
history.promote = time: 10/22/2007 18:28 state: Release
history.promote = time: 10/24/2007 21:33 state: Implemented
MQL<46> print bus ECO ZCO3171 - select history.promote[1];
business object ECO ZCO3171 -
history.promote[1] = time: 10/22/2007 17:23 state: Review
MQL<47> print bus ECO ZCO3171 - select history.promote[0];
business object ECO ZCO3171 -
history.promote[0] = time: 10/22/2007 9:56 state: Design Work
history.promote[0] = time: 10/22/2007 17:23 state: Review
history.promote[0] = time: 10/22/2007 18:28 state: Release
history.promote[0] = time: 10/24/2007 21:33 state: Implemented

If someone has any insight on how a query language can be this flawed, I’m all ears.

PS: If you don’t understand anything about this post, I’m deeply sorry for boring you with my tech rants…