Skip to content

Instantly share code, notes, and snippets.

@alfredo
alfredo / postmkvirtualenv
Created July 22, 2011 10:08
Adding this to the postmkvirtualenv hook will make your virtualenv to move to the current directory once activated
#!/bin/bash
# This hook is run after a new virtualenv is activated.
echo cd `pwd` > $VIRTUAL_ENV/bin/postactivate
echo "Hook created to move to current directory"
@alfredo
alfredo / gist:1219704
Created September 15, 2011 16:20
Nyan Nyan Nyan
class NyanCatMiddleware(object):
"""Nyan Nyan Nyan"""
def process_response(self, request, response):
response['X-Cat'] = "Nyan nyan nyan nyan"
return response
@alfredo
alfredo / gist:1226378
Created September 19, 2011 12:11
JavaScript with Google Closure on emacs
Get google closure compiler to evaluate your javascript code on your emacs
--------------------------------------------------------------------------
- Download the google closure compiler, unzip it and place ``compiler.jar`` somewhere accessible
(e.g. ``~/bin/compiler.jar``)::
http://closure-compiler.googlecode.com/files/compiler-latest.zip
- Create a shell script to run closure on a file (e.g. closure.sh)::
@alfredo
alfredo / gist:1229022
Created September 20, 2011 13:03
Secured downloads with DEBUG fallback
def secured_download(func):
"""Wraps a view to server files securely ONLY when a
``FileField`` is returned
Uses Django when ``DEBUG`` is on else NGINX
"""
def wrapper(request, *args, **kwargs):
response = func(request, *args, **kwargs)
if isinstance(response, FieldFile):
# response is a ``FieldFile`` we should serve it
if settings.DEBUG:
@alfredo
alfredo / field.html
Created November 9, 2011 11:32 — forked from pydanny/field.html
django-uni-form + Twitter Bootstrap
<!-- this usually goes in <project-root>/templates/uni_form/field.html -->
{% if field.is_hidden %}
{{ field }}
{% else %}
<div class="clearfix {% if field.errors %}error{% endif %}">
<label for="{{ field.auto_id }}" {% if field.field.required %}class="requiredField"{% endif %}>
{{ field.label|safe }}{% if field.field.required %}<span class="asteriskField">*</span>{% endif %}
</label>
<div class="input">
@alfredo
alfredo / gist:1364005
Created November 14, 2011 14:03
Using events driven JS
https://github.com/theteam/ubuntu-start/blob/develop/www/script/uStart.ParallaxCarousel.js#L120
https://github.com/theteam/ubuntu-start/blob/develop/www/script/uStart.ParallaxCarousel.js#L123
https://github.com/theteam/ubuntu-start/blob/develop/www/script/base.js#L52
@alfredo
alfredo / test_file.py
Created November 30, 2011 14:01
Create temporal file to test
from django.test import TestCase
from django.core.files.base import ContentFile
from django.core.files.storage import default_storage
class ToolsTest(TestCase):
def test_file(self):
"""Creates a tmp file"""
tmp_file = default_storage.save('tools/default.txt',
ContentFile('default content'))
@alfredo
alfredo / redis_session_backend.py
Created December 6, 2011 19:32 — forked from mikeyk/redis_session_backend.py
A redis backend for Django Sessions, tested on Django 1.3+
from django.contrib.sessions.backends.base import SessionBase, CreateError
from django.conf import settings
from django.utils.encoding import force_unicode
import redis
class SessionStore(SessionBase):
""" Redis store for sessions"""
def __init__(self, session_key=None):
self.redis = redis.Redis(
@alfredo
alfredo / gist:1542184
Created December 31, 2011 00:24
Install node.js and coffee-script inside a python virtualenv
To install node.js and coffee-script inside a virtualenv and keep it self-contained:
1. Activate the virtualenv::
$ workon test
2. Move inside the virtualenv directory::
(test)$ cdvirtualenv
@alfredo
alfredo / gist:1548139
Created January 1, 2012 19:38
Installing redis from debian backports
To install redis from debian backports we need to add the backports sources.
1. Add to /etc/apt/sources.list:
deb http://backports.debian.org/debian-backports squeeze-backports main
2. Retrieve debian key
$ gpg --keyserver pgp.mit.edu --recv-keys AED4B06F473041FA