This is a test of stuff.
This file contains 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 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')) | |
) |
This file contains 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 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__() |
This file contains 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 ExtendedRouter(SimpleRouter): | |
routes = SimpleRouter.routes + [ | |
Route( | |
url=r'^{prefix}/{lookup}/{methodname}/(?P<slug>[^/]+){trailing_slash}$', | |
mapping={ | |
'{httpmethod}': '{methodname}', | |
}, | |
name='{basename}-{methodnamehyphen}-slug', | |
initkwargs={} | |
), |
This file contains 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
@link() | |
def children(self, request, *args, **kwargs): | |
parent = self.get_object() | |
children = parent.children.all() | |
serializer = ChildSerializer(children, many=True) | |
return Response(serializer.data) |
This file contains 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
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) | |
... |
This file contains 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
<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))); |
This file contains 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
{ | |
"users": [ | |
{ | |
"organisations": [ | |
{ | |
"id": 456, | |
"role": "OWNER", | |
"editable": false, | |
"name": "Acme Org", | |
"permissions": ["SDFDS::SDFDS", "SDFDFS::SASDA"] |
This file contains 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
permissions = [ | |
Permission(permission=permission, user=user, organisation=self) | |
for permission in ACCOUNT_PERMISSIONS | |
] |
This file contains 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 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) |