Skip to content

Instantly share code, notes, and snippets.

View bohde's full-sized avatar

Rowan Bohde bohde

View GitHub Profile
@bohde
bohde / resources.py
Created July 23, 2011 02:54
Return the authenticated user if the pk is 'self'
class UserResource(ModelResource):
def cached_obj_get(self, request=None, **kwargs):
if request and 'id' in kwargs and kwargs['id'] == 'self':
return request.user
return super(UserResource, self).cached_obj_get(request, **kwargs)
@bohde
bohde / results.json
Created August 21, 2011 20:57
Current results of boomerang-based performance testing of various ways to load scripts.
{
"defer": {
"quantiles": {
"0.75": 6100,
"0.95": 24900
},
"total": 201
},
"revised_cache": {
"quantiles": {
@bohde
bohde / Adding a property to a model
Created August 23, 2011 15:23
Adding a property to a model
class MyUserModel(User):
@property
def room(self):
# Don't run the query twice
if not hasattr(self, '_room'):
try:
self._room = self.lease_set.select_related('room').order_by('-date')[0].room
except Lease.DoesNotExist:
self._room = None
@bohde
bohde / views.py
Created October 11, 2011 02:21
Example of using obj_get_list in a standard django view
from annoying.decorators import render_to
from snipts.api import PublicTagResource
@render_to('home.html')
def home(request):
tr = PublicTagResource()
tags = tr.cached_obj_get_list()
bundles = (tr.build_bundle(request=request, obj=tag) for tag in tags)
dehydrated = [tr.full_dehydrate(bundle) for bundle in bundles]
return {
@bohde
bohde / authentication.py
Created November 15, 2011 04:47
Some alternative Tastypie ApiKeyAuths
from tastypie.authentication import ApiKeyAuthentication
class ConfigurableApiKeyAuthentication(ApiKeyAuthentication):
"""
Just like standard APIKeyAuthentication,
but with configurable parameters in case the parameters would be ambiguous.
"""
def __init__(self, username_param='username', api_key_param='api_key'):
self.username_param = username_param
from tastypie import fields
from tastypie.resources import Resource
class TaggedResource(Resource):
tags = fields.ListField() # no attribute declaration on purpose
def dehydrate_tags(self, bundle):
return map(str, bundle.obj.tags.all())
joshbohde@mu:~ $ echo '{"name":"foo"}' | branch: (master)
curl -H 'Content-Type: application/json' -X PUT --data @- "http://localhost:8000/api/v1/artists/1/"
joshbohde@mu:~ $ echo '{"album": {"id": "1", "title": "Physical Graffiti", "artist":"/api/v1/artists/1/"}, "id": "1", "resource_uri": "/playlist/api/v1/tracks/1/", "title": "Custard Pie", "track_number": 1}' |
curl -H 'Content-Type: application/json' -X PUT --data @- "http://localhost:8000/api/v1/tracks/1/"
joshbohde@mu:~ $ curl -H 'Accept: application/json' "http://localhost:8000/api/v1/albums/1/" branch: (master)
{"artist": "/api/v1/artists/1/", "id": "1", "resource_uri": "/api/v1/albums/1/", "title": "Physical Graffiti"}
joshbohde@mu:~ $ echo '{"album": {"id": "1", "
@bohde
bohde / graphite_pip_uwsgi_recipe.sh
Created November 28, 2011 18:08 — forked from bfritz/graphite_pip_uwsgi_recipe.sh
installing graphite-web with pip and running under uwsgi
# graphite-web install is hardcoded in setup.cfg to /opt/graphite
sudo mkdir /opt/graphite
sudo chown brad.users /opt/graphite
# run under python2.7 virtualenv
virtualenv --python=python2.7 ~/ve/graphite
source ~/ve/graphite/bin/activate
# install the necessary python packages (simplejson is for flot graphs)
pip install graphite-web carbon whisper django django-tagging uwsgi simplejson
class DebugModelResource(ModelResource):
def obj_get(self, request=None, **kwargs):
print kwargs
return super(DebugModelResource, self).obj_get(request=request, **kwargs)
class PackageResource(ModelResource):
# Stuff already there
def obj_get(self, request=None, **kwargs):
kwargs.pop("available_options")
return super(PackageResource, self).obj_get(request=request, **kwargs)