Skip to content

Instantly share code, notes, and snippets.

@atabary
atabary / fields.py
Last active September 6, 2022 00:32
CompressedBinaryField for Django
import bz2
import StringIO
from django.db import models
from django.utils.six import with_metaclass
class CompressedBinaryField(with_metaclass(models.SubfieldBase, models.BinaryField)):
description = "A binary field with bzip2 compression"
@atabary
atabary / angularjs.py
Created December 19, 2012 04:53
Django and angular.js are using a conflicting templating syntax, relying on double curly brackets, eg. `{{ something }}`. This template-tag mitigate the problem by allowing the use of {% ng something %} in a django template, which is then outputed as {{ something }} for angular.js to use.
"""
filename: angularjs.py
Usage:
{% ng Some.angular.scope.content %}
e.g.
{% load angularjs %}
<div ng-init="yourName = 'foobar'">
<p>{% ng yourName %}</p>
@atabary
atabary / fabfile.py
Created December 18, 2012 03:13
Simple fabric task that returns the current revision of a git repository.
from fabric.api import local
def current_revision():
revision = local("git log -1 | grep commit | awk '{ print $2 }'", capture=True)
print 'current revision: %s' % revision
return revision
@atabary
atabary / dblock.py
Created May 26, 2012 08:45
MySQL table lock with django
#-*- coding: utf-8 -*-
import contextlib
from django.db import connection
@contextlib.contextmanager
def acquire_table_lock(read, write):
'''Acquire read & write locks on tables.