Skip to content

Instantly share code, notes, and snippets.

@jacobian
jacobian / loginrequired.py
Created September 20, 2011 15:58
Login required mixin
class LoginRequired(object):
@method_decorator(login_required)
def dispatch(self, *args, **kwargs):
return super(LoginRequired, self).dispatch(*args, **kwargs)
# Nginx conf (/etc/nginx/nginx.conf).
user www-data;
error_log /var/log/nginx/error.log;
pid /var/run/nginx.pid;
worker_processes 4;
events {
worker_connections 100;
[celery]
base = .
djcelery = true
append_settings = false
@jacobian
jacobian / login.html
Created October 12, 2011 18:49
Simple login template - registration/login.html
<form method="post" action="{% url login %}">
{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="login" />
<input type="hidden" name="next" value="{{ next|default:"/entries/" }}" />
</form>
@jacobian
jacobian / randomreviews.py
Created October 20, 2011 21:51 — forked from lambacck/gist:1299684
PyCon random review script
"""
Review you some talks!
Usage:
First::
$ pip install lxml
$ pip install -e git://github.com/jacobian/remotetable.git#egg=remotetable
@jacobian
jacobian / models.py
Created November 3, 2011 20:18
Creating models on-the-fly
"""
NB: this is works-for-me quality code, completely not suitable for production.
Please use it as inspiration, but please test it better than I have before
you use it in your own projects!
"""
from django import db
from django.conf import settings
from django.db import models
@jacobian
jacobian / pullhook.py
Created December 7, 2011 21:53
Github pull request hook
data = {
"active": true,
"config": {"url": "http://postbin.ep.io/33/"},
"name": "trac",
"events": ['pull_request', 'issue_comment', 'issues'],
}
requests.post('https://api.github.com/repos/django/django/hooks',
auth = (USERNAME, PASSWORD),
headers = {'Content-Type':'application/json'},
@jacobian
jacobian / save_only_changes.py
Created February 16, 2012 18:17
Save only changed fields when calling Model.save()
def saves_only_changes(cls):
"""
When calling save(), only save changed model fields.
This is a class decorator, so use it thusly::
@saves_only_changes
class Person(models.Model):
first_name = models.CharField(max_length=200)
last_name = models.CharField(max_length=200)
# stuff = {'term': (title, url)}
# e.g. {'RequestFactory',: ('django.test.client.RequestFactory/', 'http://...'), ...}
def find(term):
score, found = 0, None
for candidate in stuff:
cand_score = stringslipper.score(candidate.lower(), term)
if cand_score > score:
score = cand_score
found = (candidate, stuff[candidate])
@jacobian
jacobian / authuser.md
Created March 30, 2012 00:35
Upgrading auth.User - the profile approach

Upgrading auth.User - the profile approach

This proposal presents a "middle ground" approach to improving and refactoring auth.User, based around a new concept of "profiles". These profiles provide the main customization hook for the user model, but the user model itself stays concrete and cannot be replaced.

I call it a middle ground because it doesn't go as far as refactoring the whole auth app -- a laudable goal, but one that I believe will ultimately take far too long -- but goes a bit further than just fixing the most egregious errors (username length, for example).

This proposal includes a fair number of design decisions -- you're reading the fifth or sixth draft. To keep things clear, the options have been pruned out and on the one I think is the "winner" is still there. But see the FAQ at the end for some discussion and justification of various choices.

The User model