Skip to content

Instantly share code, notes, and snippets.

View vbmendes's full-sized avatar

Vinícius Mendes vbmendes

View GitHub Profile
@vbmendes
vbmendes / truncate.py
Created August 24, 2011 18:03
truncate templatetag
from django.utils.safestring import SafeData, mark_safe
from django.template import TemplateSyntaxError
@register.filter
def truncate(value, arg):
"""
Truncates a string after a given number of chars
Argument: Number of chars to truncate after
"""
mark_safe_if_safe = lambda v: mark_safe(v) if isinstance(value, SafeData) else v
@vbmendes
vbmendes / USAGE.py
Created August 8, 2011 21:48
Django Manager to improve querysets usage
from django.db import models
from query import QuerySet
class CustomQuerySet(QuerySet):
def published(self):
return self.filter(published=True)
class Content(models.Model):
@vbmendes
vbmendes / pdbdebug.py
Created July 13, 2011 18:15
Django template debug
# -*- coding: utf8 -*-
"""
Template tag to stop template rendering to debug.
It's useful to know what is in the context.
Just put this file in a templatetags module inside your app and
insert this code in your templates where you want to debug:
{% load pdbdebug %}{% pdbdebug %}
"""
@vbmendes
vbmendes / lnsp
Created June 4, 2011 15:28
Create symbolic links to my folders in site-packages
#!/bin/bash
# ======================================================================
#
# Function: lnsp
# Creates a symbolic link to a folder inside current site-packages. If
# the file already exists, ask for override.
# Works ok with virtualenv.
#
# Parameters:
@vbmendes
vbmendes / .bash_profile
Created June 4, 2011 14:00
Git completion and PS1 customization
source /usr/local/Cellar/git/1.7.5.4/etc/bash_completion.d/git-completion.bash # path to git completion
export GIT_PS1_SHOWDIRTYSTATE=1
export GIT_PS1_SHOWSTASHSTATE=1
export GIT_PS1_SHOWUNTRACKEDFILES=1
export GIT_PS1_SHOWUPSTREAM="verbose"
PS1='\[$(tput setf 3)\]\u\[$(tput sgr0)\]: \[$(tput setf 6)\]\w \[$(tput setf 2)\]$(__git_ps1 "(%s)")\n\[$(tput bold)$(tput setf 6)\]$ \[$(tput sgr0)\]'
source ~/.bashrc
@vbmendes
vbmendes / forms.py
Created February 24, 2011 20:55
Cria o formulário com o campo de país sendo um ModelChoiceField e no __init__ altera a queryset para contemplar o continente que você passar como parâmetro. E na view apenas passa o continente como parâmetro para o form.
from django import forms
from models import Pais
class MyForm(forms.Form):
pais = forms.ModelChoiceField(queryset = Pais.objects.all())
def __init__(self, *args, **kwargs):
@vbmendes
vbmendes / nodes.py
Created February 19, 2011 15:59
Make it easier to create templatetags with parameters.
from django.template import Node, TemplateSyntaxError
from django.template import resolve_variable
from django.utils.encoding import smart_str
class BaseNode(Node):
def __init__(self, **kwargs):
self.parsed = kwargs
@vbmendes
vbmendes / httprequest.py
Created December 16, 2010 14:07
HTTPRequest
#!/usr/bin/env python
# -*- coding: utf-8 -*-
class HTTPRequest(object):
"""
Class to generate plain HTTP Requests
usage:
>>> req = HTTPRequest('meiocodigo.com', path='/')
>>> req.add_header('Content-Type', 'application/x-www-form-urlencoded')
# -*- coding: utf8 -*-
"""
Você pode criar a mensagem no próprio form.
"""
from django import forms
from django.forms.util import ErrorList
from models import UserDoc
class MyModelAdmin(ModelAdmin):
...
def __getattr__(self, name):
"""
Permite o uso de um atributo no admin com o nome
do atributo mais '_or_blank'. Exemplo: name_or_blank
"""
if name.endswith('_or_blank'):
try:
return getattr(self, name[:-9]) or u''