Skip to content

Instantly share code, notes, and snippets.

View bohde's full-sized avatar

Rowan Bohde bohde

View GitHub Profile
curl civic-voice.appspot.com/meeting/submit_input -d meetingId=t2 -d user=commandlineapi -d value="gist.github.com/370617" -d inputType=comment
class EntryResource(ModelResource):
user = fields.ForeignKey(UserResource, 'user')
class Meta:
queryset = Entry.objects.all()
resource_name = 'entry'
def dispatch(self, request_type, request, **kwargs):
username = kwargs.pop('username')
kwargs['user'] = get_object_or_404(User, username=username)
from django.db import models
class DependentCharField(models.CharField):
description = "A Field that can be null or blank if other returns a truthy value"
def __init__(self, dependentName, *args, **kwargs):
kwargs['blank'] = True
kwargs['null'] = True
self.dependentName = dependentName
@bohde
bohde / gist:754323
Created December 24, 2010 15:10
Running logic on update in tastypie
from tastypie.resources import ModelResource
class FlaggedResource(ModelResource):
def obj_update(self, bundle, request=None, **kwargs):
processFlag = bundle.obj and not bundle.obj.flagged
bundle = super(FlaggedResource, self).obj_update(bundle, request, **kwargs)
if processFlag:
bundle.obj.flagLogic()
return bundle
@bohde
bohde / authentication.py
Created December 24, 2010 15:17
Django based auth for Tastypie
from tastypie.authentication import Authentication
class DjangoAuthentication(Authentication):
"""Authenticate based upon Django session"""
def is_authenticated(self, request, **kwargs):
return request.user.is_authenticated()
@bohde
bohde / api.py
Created December 24, 2010 15:48
def post_list(self, request, **kwargs):
deserialized = self.deserialize(request,
request.raw_post_data,
format=request.META.get('CONTENT_TYPE',
'application/json'))
bundle = self.build_bundle(data=dict_strip_unicode_keys(deserialized))
self.is_valid(bundle, request)
updated_bundle = self.obj_create(bundle, request=request)
resp = self.create_response(request,
self.full_dehydrate(updated_bundle.obj))
class SiteResource(ModelResource):
""" Thing that are meant to be accessed with a cookie """
def dispatch(self, request_type, request, **kwargs):
kwargs['site'] = Site.objects.get_current()
return super(SiteResource, self).dispatch(request_type, request, **kwargs)
@bohde
bohde / client.py
Created January 13, 2011 00:27
An implementation vows.js style macros in Python
class Response(object):
def __init__(self, status=200):
self.status = status
class Client:
def get(self, path):
return Response(200)
def post(self, path):
return Response(405)
@bohde
bohde / out
Created February 2, 2011 18:45
bundle.data['max_responses'] -> None
bundle.obj.max_responses -> 22
@bohde
bohde / async_manage.py
Created February 11, 2011 02:56
Patch Django with eventlet before it loads any connections
#!/usr/bin/env python
import os
import eventlet
import eventlet.debug
os.environ["EVENTLET_NOPATCH"] = 'True'
eventlet.monkey_patch()
eventlet.debug.hub_prevent_multiple_readers(False)
from django.core.management import execute_manager