Skip to content

Instantly share code, notes, and snippets.

@rozza
rozza / auth_backend.py
Created May 11, 2010 20:24
Custom auth backend with an extended AnonymousUser
from django.contrib.auth import SESSION_KEY, BACKEND_SESSION_KEY, load_backend
from users.models import AnonymousUser
def get_user(request):
try:
user_id = request.session[SESSION_KEY]
backend_path = request.session[BACKEND_SESSION_KEY]
backend = load_backend(backend_path)
user = backend.get_user(user_id) or AnonymousUser()
except KeyError:
@rozza
rozza / django_jinja.py
Created July 12, 2010 09:49
Django 1.2 and Jinja2 loader
"""
Using Jinja2 with Django 1.2
Based on: http://djangosnippets.org/snippets/2063/
To use:
* Add this template loader to settings: `TEMPLATE_LOADERS`
* Add template dirs to settings: `JINJA2_TEMPLATE_DIRS`
If in template debug mode - we fire the template rendered signal, which allows
debugging the context with the debug toolbar. Viewing source currently doesnt
diff --git a/README.md b/README.md
index 713bf92..6747aaa 100644
--- a/README.md
+++ b/README.md
@@ -5,7 +5,7 @@ monocle straightens out event-driven code using Python's generators.
It aims to be portable between event-driven I/O frameworks, and
currently supports Twisted and Tornado.
-It's for Python 2.5 and up; the syntax it uses isn't supported
+It's for Python 2.5 and up (pre 2.7 builds require orderedict); the syntax it uses isn't supported
@rozza
rozza / runner.py
Created July 30, 2010 14:25
saves and restores sys.stdout
import sys
class ResultPlugin(object):
"""
Captures the TestResult object for later inspection.
nose doesn't return the full test result object from any of its runner
methods. Pass an instance of this plugin to the TestProgram and use
``result`` after running the tests to get the TestResult object.
"""
from django.conf import settings
from django.http import HttpResponseRedirect
def secure_required(func):
"""
Decorator makes sure URL is accessed over https.
Use with `SecureRequiredMiddleware` to ensure only decorated urls are
accessed via https
"""
def wrap(request, *args, **kwargs):
from django.http import HttpResponseRedirect
class SecureRequiredMiddleware(object):
def process_response(self, request, response):
"""
Ensures that any non decorated secure_required urls redirect if accessed
via https (only GET requests returning 200's are redirected to HTTP urls)
"""
@rozza
rozza / (ab)using fixtures in migrations
Created October 13, 2010 09:10
An example of using fixtures in south migrations
from django.core import serializers
widgets = serializers.deserialize('json', open('widget/fixtures/widgets.json'))
for widget_data in widgets:
widget = orm.Widget()
for k,v in widget_data.object.__dict__.items():
if k.startswith('_'):
continue
setattr(widget, k, v)
widget.save()
@rozza
rozza / decorator.py
Created October 18, 2010 09:17
A decorator for django management commands that ensures only one process is running at any one time.
"""
A decorator for management commands (or any class method) to ensure that there is
only ever one process running the method at any one time.
Requires lockfile - (pip install lockfile)
Author: Ross Lawley
"""
import time
@rozza
rozza / gist:968114
Created May 12, 2011 07:44
current_user proxy for flask example
from flask import session, g
current_user = LocalProxy(get_user) # Global access to the current user
def get_user():
"""
A proxy for the User model
Is used as a `LocalProxy` so is context local.
Uses the `g` object to cache the `User` instance which prevents
@rozza
rozza / django_template_test.py
Created May 18, 2011 07:50
mongoengine/issues/166
# -*- coding: utf-8 -*-
from django.conf import settings
import unittest
from mongoengine import *
from django.template.defaultfilters import length
from django.template import Context, Template