Skip to content

Instantly share code, notes, and snippets.

View pablorecio's full-sized avatar

Pablo Recio pablorecio

View GitHub Profile
commit 5198e31796e2b191a51459441b773fe77b7795fb
Author: Pablo Recio <pablo@recio.me>
Date: Mon Mar 21 22:01:42 2016 +0000
Class-based views are back to stay
diff --git a/pubnamegenerator/urls.py b/pubnamegenerator/urls.py
index 92656ee..8525b59 100644
--- a/pubnamegenerator/urls.py
+++ b/pubnamegenerator/urls.py
@pablorecio
pablorecio / gist:7022796
Last active March 8, 2023 11:41
Example TestCase for unit test Django middlewares
from django.test import RequestFactory, TestCase
from myapp.middleware import MyMiddleware
class MyMiddlewareTestCase(TestCase):
def setUp(self):
super(MyMiddlewareTestCase, self).setUp()
self.factory = RequestFactory()
@pablorecio
pablorecio / gist:5850956
Created June 24, 2013 15:38
Simple jQuery module for fixing contrast according to W3 formula: http://www.w3.org/TR/AERT#color-contrast
if(jQuery) (function($) {
$.extend($.fn, {
fixContrast: function() {
$(this).each(function(element){
var backgroundcolor = $(this).css('background-color');
var rgb = backgroundcolor.replace("rgb(", "").replace(")", "").split(", ");
var ratio = Math.round((
(parseInt(rgb[0]) * 299) +
(parseInt(rgb[1]) * 587) +
@pablorecio
pablorecio / gist:5685144
Last active December 17, 2015 22:49
Really basic AJAXResponseMixin for being used as a Mixin on Django's Class Based Views
class AJAXResponseMixin(object):
def get(self, request, *args, **kwargs):
response = super(AJAXResponseMixin, self).get(request, *args, **kwargs)
if self.request.is_ajax():
return HttpResponse(json.dumps({
'response': response.status_code,
'location': response['Location'],
# whatever else you want to return
}), mimetype='application/json')
@pablorecio
pablorecio / models.py
Created August 31, 2011 06:31 — forked from anonymous/models.py
Avoid infinite signals when saving an object instance in Django's pre_save or post_save
from django.db.models.signals import post_save
from django.dispatch import receiver
# models definition here ...
@receiver(post_save, sender=MyModel)
def handler_that_saves_a_mymodel_instance(sender, instance, created, **kwargs):
# without this check the save() below causes infinite post_save signals
if created:
instance.some_field = complex_calculation()