Ramblings

May 7, 2008

Snippet to update Django tables to utf8

Filed under: cool, django, orm, python, snippet, tip, tool — michaelangela @ 11:54 pm

So you get the dreaded illegal mix of collations:

Pythoneer » Convert MySQL table to utf8

Illegal mix of collations (latin1_swedish_ci,IMPLICIT) and (utf8_general_ci,COERCIBLE)

What do you do? Not too bad fortunately. And if you’re working with Django, it’s a little bit easier. Launch the shell with python manage.py shell. Then in the shell do something like this:

from django.db import connection
cursor = connection.cursor()
cursor.execute('SHOW TABLES')
results=[]
for row in cursor.fetchall(): results.append(row)
for row in results: cursor.execute('ALTER TABLE %s CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci;' % (row[0]))

I’m sure this can be optimized a bit but it got the job done simply enough.

Advertisements

April 25, 2008

A Django HttpRequest QueryDict is not your normal Python dict – i.e. getting ids from a friend request-form

Filed under: dev, django, facebook, python, snippet, tip — michaelangela @ 5:34 pm

Doesn’t work:

ids=request.GET['ids[]']

Does work:

ids=request.GET.getlist('ids[]')

A typical Facebook friend invite sends the ids to be invited to your site as an array in 'ids[]'. Normally, you can pull values out with just request.GET['key']. But since the value of this particular key is an array and not just a string, you can’t (at least I wasn’t able to) just do ids=request.GET['ids[]']. That returns the first value of the array as a string. Not very helpful.

But, this is a QueryDict object, not a standard {} dict. As such, Django provides the getlist(key) method.

Django | Request and response objects | Django Documentation

getlist(key) — Returns the data with the requested key, as a Python
list. Returns an empty list if the key doesn’t exist. It’s guaranteed to
return a list of some sort.

April 22, 2008

Django snippets to check http_referer

Filed under: deep-linking, dev, django, snippet, tip — michaelangela @ 6:31 pm

Django snippets: Referer-checking view decorators

Here are a couple of Django decorators for limiting access to a view based on the request’s HTTP_REFERER. Both raise a Django PermissionDenied exception if the referer test fails (or a referer simply isn’t provided).

The first, referer_matches_hostname, takes a hostname
(and port, if specified) and matches it against the referer’s. If
multiple arguments are supplied a match against any of the hostnames
will be considered valid.

The second, referer_matches_re, takes a regex pattern (like Django’s urlpattern) and tests if it matches the referer. This is obviously more flexible than referer_matches_hostname providing the ability to match not just the hostname, but any part of the referer url.

Finally there’s an simple example decorator, local_referer_only, that limits a view to the current site by using Django’s django.contrib.sites to look up the current hostname.

Working with field level permissions with Django

Filed under: dev, django, snippet — michaelangela @ 5:46 pm

This might be useful but, buyer beware.

Django snippets: FieldLevelPermissionsAdmin

Have you ever needed to customize permissions, for example, allow only some fields for editing by some group of users, display some fields as read-only, and some to hide completely? FieldLevelPermissionsAdmin class does this for newforms-admin branch. Not tested well yet (>100 LOC!).

April 20, 2008

1and1 and http compression

Filed under: 1and1, gzip, modrewrite, php, snippet, tip, tool — michaelangela @ 12:11 am

I have not been able to find any info about 1and1 and http compression. It’s a shame as it really reduces bandwidth, increases transfer speed, etc. Fortunately this little script does a really nice job of it. It’s in PHP and it works a treat on 1and1. With a bit of rewrite magic I should be able to access the files normally and the gzipped ones will be passed in when needed. Nice.

Julien Lecomte’s Blog » Gzip Your Minified JavaScript Files

If your web hosting company does not offer HTTP compression, but gives
you access to PHP (that’s the case of Yahoo! Web Hosting), I wrote a simple PHP script that will compress files and set the cache control headers so that the requested files actually get cached by the browser.

April 18, 2008

Neat little Facebook snippet for Django

Filed under: dev, django, facebook, python, snippet, tip — michaelangela @ 7:51 pm

You can use this to set up a Facebook User within you Django app. This of course uses the middleware which is optional as described here.

Django snippets: Little middleware that create a facebook user

  1. Install **PyFacebook** package.
  2. After make all steps in tutorial above, put this code in your app’s
    models.py module (you maybe prefer split it and put the middleware
    class in some other file).
  3. Put the FacebookUserMiddleware python-path in the
    MIDDLEWARE_CLASSES in your settings.py (after
    facebook.djangofb.FacebookMiddleware).

Create a free website or blog at WordPress.com.