Skip to content

Instantly share code, notes, and snippets.

from random import choice
import string
def genPasswd(length=8):
p = ''.join([choice(string.letters+string.digits) for i in range(length)])
return p
@macndesign
macndesign / Model Pessoa
Created July 18, 2012 13:03
Exemplo de filtro com ListView
from django.db import models
from django.contrib import admin
class Pessoa(models.Model):
nome = models.CharField(max_length=75)
idade = models.IntegerField()
def __unicode__(self):
return self.nome
@macndesign
macndesign / gist:3146212
Created July 19, 2012 19:29
using Class Based Generic ListView to seach forms and List
....
from django.views.generic import ListView
from sgf.utils.decorators import page, permission_required_sgf
from sgf.administrativo.models import ComiteRegional
from sgf.administrativo.forms import FormComiteRegionalBusca
......
class SearchListView(ListView):
@macndesign
macndesign / ajax.js
Created August 4, 2012 15:50
Problemas para post via ajax com django-tastypie
$(document).ready(function(){
$('button').click(function(){
var data = JSON.stringify({
"content": "A new post.",
"is_active": true,
"title": "New Title",
"slug": "new-title",
"user": "/api/v1/users/1/"
});
@macndesign
macndesign / json_response.py
Created August 6, 2012 18:44
Serializar detalhamento ou listagem de dados customizados em json com CBV
# API JSON
from django.http import HttpResponse
from django.utils import simplejson
from django.views.generic.detail import BaseDetailView
from django.views.generic.list import BaseListView
from django.db.models.query import QuerySet
class JSONResponseMixin(object):
def render_to_response(self, context):
return self.get_json_response(self.convert_context_to_json(context))
@macndesign
macndesign / api_urls.py
Created August 8, 2012 12:23
Test: Making a CRUD using django + tastypie
from tastypie.api import Api
from basic.api.resources import NoteResource, UserResource
api = Api(api_name='v1')
api.register(NoteResource(), canonical=True)
api.register(UserResource(), canonical=True)
@macndesign
macndesign / .zshrc.sh
Created August 9, 2012 17:43 — forked from lucasvo/.zshrc.sh
Automatically activate your python virtualenvironment
function auto_activate {
SEARCHPATH=`pwd`
function activate_env {
# ZSH outputs errors for the ls * command if you don't disable the nomatch output
unsetopt nomatch 2>/dev/null
ls $SEARCHPATH/*/bin/activate > /dev/null 2> /dev/null
if [ "$?" = '0' ]; then
deactivate > /dev/null 2> /dev/null
$.preLoadImages = function() {
    var args_len = arguments.length;
    for (var i=0; i < args_len; i++) {
        var cacheImage = document.createElement('img');
        cacheImage.src = arguments[i];
    }
}
 
$.preLoadImages('/img/img1.png', '/img/img2.png')
@macndesign
macndesign / tw_trends.py
Created September 8, 2012 22:53 — forked from fabiocerqueira/tw_trends.py
Get and save Twitter Trends
@macndesign
macndesign / contador.py
Created September 17, 2012 19:17
1a Lista de Exercícios (Objetos Pythonicos)
# coding: utf-8
"""
Código inicial usado na Lista de Exercícios 1 do curso
"Objetos Pythonicos" de Luciano Ramalho, Oficinas Turing.
"""
from collections import Counter
class Contador(object):
def __init__(self):