Skip to content

Instantly share code, notes, and snippets.

View luzfcb's full-sized avatar

Fábio C. Barrionuevo da Luz luzfcb

View GitHub Profile
@dcramer
dcramer / track_data.py
Created December 6, 2010 19:15
Tracking changes on properties in Django
from django.db.models.signals import post_init
def track_data(*fields):
"""
Tracks property changes on a model instance.
The changed list of properties is refreshed on model initialization
and save.
>>> @track_data('name')
@nicpottier
nicpottier / view_permission.py
Created March 22, 2011 07:43
Adds a view permission to all your models in Django
from django.db.models.signals import post_syncdb
from django.contrib.contenttypes.models import ContentType
from django.contrib.auth.models import Permission
def add_view_permissions(sender, **kwargs):
"""
This syncdb hooks takes care of adding a view permission too all our
content types.
"""
# for each of our content types
@specialunderwear
specialunderwear / staticfiles.py
Created August 23, 2011 14:58
Use django collectstatic with apps that have 'media' folders instead of 'static' folders. Also make run server serve files in the MEDIA_ROOT automatically, even if it is inside STATIC_ROOT.
import os
from django.conf import settings
from django.contrib.staticfiles.finders import BaseFinder, AppDirectoriesFinder
from django.contrib.staticfiles.storage import AppStaticStorage
from django.core.files.storage import FileSystemStorage
from django.utils._os import safe_join
class AppMediaStorage(AppStaticStorage):
@sjzabel
sjzabel / urls.py
Created November 18, 2011 22:41
django: adding required decorators to entire urlpatterns
'''
Author: Stephen J. Zabel
License: BSD
This module exposed a helper function for
wrapping views at the urls.py/resolver level
My personal little itch as an example...
urlpatterns += required(
@luzfcb
luzfcb / staticfiles.py
Created November 24, 2011 12:28 — forked from specialunderwear/staticfiles.py
Use django collectstatic with apps that have 'media' folders instead of 'static' folders. Also make run server serve files in the MEDIA_ROOT automatically, even if it is inside STATIC_ROOT.
import os
from django.conf import settings
from django.contrib.staticfiles.finders import BaseFinder, AppDirectoriesFinder
from django.contrib.staticfiles.storage import AppStaticStorage
from django.core.files.storage import FileSystemStorage
from django.utils._os import safe_join
class AppMediaStorage(AppStaticStorage):
# --*-- coding: utf-8 --*--
from django.db import models
# Create your models here.
class TipoLicencaDeUso(models.Model):
nome = models.CharField(max_length=255)
descricao = models.TextField(verbose_name=u"Descrição")
sigla = models.CharField(max_length=20)
@hrldcpr
hrldcpr / tree.md
Last active June 8, 2024 18:11
one-line tree in python

One-line Tree in Python

Using Python's built-in defaultdict we can easily define a tree data structure:

def tree(): return defaultdict(tree)

That's it!

@wickman
wickman / README.md
Created April 12, 2012 22:55
Python development in Pants (tutorial)

Python development using Pants

brian wickman - @wickman

[TOC]

Why use Pants for Python development?

Pants makes the manipulation and distribution of hermetically sealed Python environments

@btd
btd / jQuery.print.js
Created April 15, 2012 07:25
jQuery.print.js
// Create a jquery plugin that prints the given element.
jQuery.fn.print = function(){
// NOTE: We are trimming the jQuery collection down to the
// first element in the collection.
if (this.size() > 1){
this.eq( 0 ).print();
return;
} else if (!this.size()){
return;
}
@peterbe
peterbe / gist:2913338
Created June 11, 2012 23:13
A very practical decorator for Django Class Based Views
from django.utils.decorators import method_decorator
def class_decorator(decorator):
def inner(cls):
orig_dispatch = cls.dispatch
@method_decorator(decorator)
def new_dispatch(self, request, *args, **kwargs):
return orig_dispatch(self, request, *args, **kwargs)
cls.dispatch = new_dispatch
return cls