This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"defer": { | |
"quantiles": { | |
"0.75": 6100, | |
"0.95": 24900 | |
}, | |
"total": 201 | |
}, | |
"revised_cache": { | |
"quantiles": { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", " |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class DebugModelResource(ModelResource): | |
def obj_get(self, request=None, **kwargs): | |
print kwargs | |
return super(DebugModelResource, self).obj_get(request=request, **kwargs) | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |