Skip to content

Instantly share code, notes, and snippets.

View tomchristie's full-sized avatar
🌿

Tom Christie tomchristie

🌿
View GitHub Profile
from django.conf.urls import patterns, url, include
from rest_framework.routers import AutoRouter
urlpatterns = patterns('',
url(r'^', include(AutoRouter().urls)),
url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework'))
)
@tomchristie
tomchristie / gist:5705771
Created June 4, 2013 13:11
ObjectSerializer
class ObjectSerializer(Serializer):
def get_default_fields(self, obj, nested):
"""
Given an object, return the default set of fields to serialize.
"""
ret = SortedDict()
attrs = [key for key in obj.__dict__.keys() if not(key.startswith('_'))]
for attr in sorted(attrs):
if nested:
ret[attr] = self.__class__()
class ExtendedRouter(SimpleRouter):
routes = SimpleRouter.routes + [
Route(
url=r'^{prefix}/{lookup}/{methodname}/(?P<slug>[^/]+){trailing_slash}$',
mapping={
'{httpmethod}': '{methodname}',
},
name='{basename}-{methodnamehyphen}-slug',
initkwargs={}
),
@link()
def children(self, request, *args, **kwargs):
parent = self.get_object()
children = parent.children.all()
serializer = ChildSerializer(children, many=True)
return Response(serializer.data)
def get_upload_to(instance, filename):
user = instance.user
return 'uploads/%s/%s' % (user.username, filename)
class MyModel(models.Model):
upload = models.FileField(upload_to=get_upload_to)
...
<html>
<body><div><label><input type="checkbox" checked="checked"> Capture</label></div>
<div><input type="button" value="[1, 2]" /> <input type="button" value="Array(1, 2);" /> <input type="button" value="{foo: 'bar'}" /> <input type="button" value="({}).foo = 'bar';" /></div>
<div><textarea></textarea></div></body>
<script>
alert(1);
var capture = function() {
var ta = document.querySelector('textarea')
ta.innerHTML = '';
ta.appendChild(document.createTextNode("Captured: "+JSON.stringify(arguments)));
{
"users": [
{
"organisations": [
{
"id": 456,
"role": "OWNER",
"editable": false,
"name": "Acme Org",
"permissions": ["SDFDS::SDFDS", "SDFDFS::SASDA"]
permissions = [
Permission(permission=permission, user=user, organisation=self)
for permission in ACCOUNT_PERMISSIONS
]
@tomchristie
tomchristie / put_as_create.py
Created November 3, 2014 11:55
PUT-as-create mixin class for Django REST framework.
class AllowPUTAsCreateMixin(object):
"""
The following mixin class may be used in order to support PUT-as-create
behavior for incoming requests.
"""
def update(self, request, *args, **kwargs):
partial = kwargs.pop('partial', False)
instance = self.get_object_or_none()
serializer = self.get_serializer(instance, data=request.data, partial=partial)
serializer.is_valid(raise_exception=True)

This is a test of stuff.