-
posts
-
Transactions on App Engine
The way to store data on App Engine is with Google’s Datastore which has support for transactions. However, the transactions are quite limited in that, You can only execute callables inside transactions. Which means you basically call run_in_transaction() on a function. This can sometimes be a pain but can generally be worked around with decorators and the like. def my_update_function(): # Some update code here ent.put() run_in_transaction(my_update_function) You can only update entities in the same entity group. This means all entities must be in the same ancestor tree. This can make updating entities with various relationships hard or impossible to...
-
Jaiku on App Engine
Yesterday Google’s Twitter-like service, Jaiku was released as open source running on Google App Engine so I decided to take it for a spin. It has a lot of neat parts like XMPP and google contacts integration, but what I’m interested in most is how it implements it’s publisher/subscriber model. I brought the code down from svn and tried to follow the instructions, but I got a “No module named django” error. One of the problems currently with App Engine is that you have a limit of 1000 files you can upload. Because of this limit when deploying jaiku you...
-
Werkzeug and reverse urls
I wanted to impove a Google App Engine application that a friend of mine created (ほぼ汎用イベント管理ツール(jp)) and noticed that he was redirecting directly to urls. He is using Werkzeug to handle url routing so I wondered if there was a method for generating urls from a name like you can in Django. It turns out you can but you give it an endpoint name rather than a url name. urls.py: from werkzeug.routing import Map, Rule, RuleTemplate, Submount, EndpointPrefix resource = RuleTemplate([ Rule('/${name}/', endpoint='${name}_index'), Rule('/${name}/create/', endpoint='create_${name}'), Rule('/${name}/update/<string:${var}>/', endpoint='update_${name}'), Rule('/${name}/delete/<string:${var}>/', endpoint='delete_${name}'), ]) url_map = Map([ Rule('/', endpoint='index'), Rule('/<string:slug>/', endpoint='project_or_event'), Rule('/form/<string:key>/<string:slug>/', endpoint='form'), Submount('/account',...
-
QueryDict and update()
Yesterday I ran into an interesting quirk with Django’s QueryDict object and the normal dictionary update() method. Normally the update method will allow you to merge two dictionary or dictionary like objects but because the QueryDict internally holds it’s values as lists you get some unexpected behavior. For instance with a normal dictionary you can do this: >> x = {"name": "Ian"} >> x.update({"age":27}) >> x {'age': 27, 'name': 'Ian'} But with Django’s request.POST and request.GET QueryDict objects the internal representation of the values are list so you get unexpected behavior even though normally accessing the value by key does...
-
IE, JSON, and the script tag
My coworker recently introduced me to one of the most blatantly bad behaviors
in web browser history. He introduced it thus:
Out[1]: simplejson.dumps({'foo': '<script>alert(document.cookie);</script>'})
Out[2]: '{"foo": "<script>alert(document.cookie);</script>"}'
The thing is, that there is nothing wrong with what simplejson is doing. The
problem is that this little piece of json is not handled properly in IE and IE
actually executes the JavaScript in the script tag regardless of the fact that
it’s inside a string. This can leave an application wide open to XSS attacks.
IE seems to do this for at least the text/plain mime-type.
-
Field/column Queries in Django
One of the neat things making it’s way into Django 1.1 is F object queries. The F object is kind of like the Q object as it can be used it queries but it represents a database field on the right hand side of an equality/inequality. For the example I’ll use the example models from the “Making Queries” section of the Django Documentation. class Blog(models.Model): name = models.CharField(max_length=100) tagline = models.TextField() def __unicode__(self): return self.name class Author(models.Model): name = models.CharField(max_length=50) email = models.EmailField() def __unicode__(self): return self.name class Entry(models.Model): blog = models.ForeignKey(Blog) headline = models.CharField(max_length=255) body_text = models.TextField() pub_date =...
-
jQuery Multi-Pageslide
Earlier this week I came across the jQuery Pageslide plugin via Ajaxian and was impressed with the design. I set about using it to display help messages to the user for a site I am working on. It worked well and all but I found that you can only have one pageslide per page. Say you want to have multiple links one one page that each invoke a page slide but with different settings. So I made some changes to the plugin to allow multiple pageslides per page. The demo includes a version of page slide that allows multiple pageslide...
-
Python date range iterator
I couldn’t find something that gave me quite what I wanted so I created a simple Python generator to give me the dates between two datetimes. def datetimeIterator(from_date, to_date): from datetime import timedelta if from_date > to_date: return else: while from_date <= to_date: yield from_date from_date = from_date + timedelta(days = 1) return Update: It didn’t take me long to realize that it wasn’t as nice as it could have been. from datetime import datetime,timedelta def datetimeIterator(from_date=datetime.now(), to_date=None): while to_date is None or from_date <= to_date: yield from_date from_date = from_date + timedelta(days = 1) return Another Update based on...
-
Introduction to Algorithms
Today my copy of Introduction to Algorithms came in the mail (a gift from the family). I’ve decided, mostly inspired by Peteris Krumins to revisit classic algorithms as it’s been a while since I’ve taken a look at them. I have decided to also take a look at the MIT Intro to Algorithms course in order to revisit algorithms and concepts. I won’t provide any lecture notes or anything since Peteris did a much better job of of writing lecture notes that I ever could but I did go ahead and create some python implementations of the sorting algorithms covered...
-
Make Firefox look like Chrome
Recently Google Chrome has been pretty popular with web folks and Google fans. It’s fast and has only a little “chrome” or window trimmings which makes the overall screen bigger when viewing webpages. However it lacks a lot of features that are present in Firefox that I pretty much can’t do without so today I set about making Firefox have all the small interface improvements that make Chrome so nice. Fortunately you can do this with existing plugins. The first thing I did was try to reduce Firefox’s “chrome” by tightening up the menus. I right clicked on the menu’s...