Skip to content

Instantly share code, notes, and snippets.

View edgabaldi's full-sized avatar
🏠
Working from home

Edgar Gabaldi edgabaldi

🏠
Working from home
View GitHub Profile
@edgabaldi
edgabaldi / navigation.py
Created November 12, 2012 11:43 — forked from elyezer/navigation.py
Template tag to add active class in navigation links
<!-- Specific path, only when visiting /accounts/ -->
<li class="{% active request "^/accounts/$" %}"><a href="/accounts/">Accounts</a></li>
<!-- Glob path, when visiting /blog/* example /blog/, /blog/post1/, /blog/post2/, /blog/post2/subitem/, ... -->
<li class="{% active request "^/blog/" %}"><a href="/blog/">Blog</a></li>
@edgabaldi
edgabaldi / gist:4494208
Last active December 10, 2015 21:18
extended model for common fields
from django.db import models
from django.contrib.auth.models import User
class IdentModel(models.Model):
user = models.ForeignKey(User)
created = models.DateTimeField(auto_now_add=True, editable=False)
modified = models.DateTimeField(auto_now=True, editable=False)
class Meta:
abstract = True
@edgabaldi
edgabaldi / test_stdout.py
Last active August 29, 2015 14:00
testing stdout in python
import sys
from StringIO import StringIO
from unittest import TestCase
class Foo(object):
def hello(self):
print 'world'
class StdoutTest(TestCase):
@edgabaldi
edgabaldi / patch_example.py
Last active August 29, 2015 14:07
patch example
import random
from unittest import TestCase
from mock import patch
def get_age():
return random.randint(15, 99)
class AgeTest(TestCase):
@patch('patch_example.random.randint')
from middleware import get_current_schema
class OtherRouter(object):
def db_for_read(self, model, **hints):
return get_current_schema()
def db_for_write(self, model, **hints):
return get_current_schema()
@edgabaldi
edgabaldi / tasks.py
Created December 2, 2014 16:29
unit test for logging and traceback
import logging
import traceback
logger = logging.getLogger('django')
class PDFParserManager(object):
def run(self):
veiculo_list = list()
for key, value in enumerate(self.pesquisas):
@edgabaldi
edgabaldi / admin.py
Created December 2, 2014 18:23
unittest for django admin calling a celery task
class PreparaLeilaoAdmin(admin.ModelAdmin):
model = Leilao
extra = 0
can_delete = False
form = LeilaoForm
def save_model(self, request, obj, form, change):
super(PreparaLeilaoAdmin, self).save_model(request, obj, form, change)
arquivo = form.cleaned_data.get('arquivo')
@edgabaldi
edgabaldi / views.py
Last active August 29, 2015 14:11
overriding get_context_data in class based generic view
from django.views.generic import View
class MyView(View):
def get_modulos_permitidos(self):
"""
Escreva o queryset que retorna os módulos
permitidos aqui.
"""
return []
@edgabaldi
edgabaldi / models.py
Created December 15, 2014 13:35
running tests very fast
COLOR_CHOICES = (
('red', 'Red'),
('green', 'Green'),
('blue', 'Blue'),
)
class Vehicle(models.Model):
year = models.CharField(max_length=4)
color = models.CharField(max_length=30, choices = COLOR_CHOICES)
@edgabaldi
edgabaldi / script.py
Created January 11, 2015 21:43
setup django environment outside django
import os
import django
from myapp import models
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myapp.settings")
django.setup()
print models.MyModel.objects.get(pk=1)