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
import java.util.*; | |
public class Permutations { | |
static void swap(char[] a, int l, int r) { | |
char temp = a[l]; | |
a[l] = a[r]; | |
a[r] = temp; | |
} |
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 CustomRootAPIClient(APIClient): | |
root = 'data' | |
def post(self, *args, **kwargs): | |
response = super(CustomRootAPIClient, self).post(*args, **kwargs) | |
if (response.status_code == status.HTTP_201_CREATED and | |
self.root in response.data): | |
response.data = response.data[self.root] | |
return response |
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 MyView(APIView): | |
renderer_classes = (CustomRootJSONRenderer, ) | |
... |
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 CustomRootJSONRenderer(JSONRenderer): | |
root = 'data' | |
def render(self, data, accepted_media_type=None, renderer_context=None): | |
if data: | |
if (not isinstance(data, list) and | |
data.get('results')): # check if the results have been paginated | |
data[self.root] = data['results'] | |
del data['results'] | |
else: |
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 CustomRootJSONRenderer(JSONRenderer): | |
root = 'data' | |
def render(self, data, accepted_media_type=None, renderer_context=None): | |
if data: | |
if (not isinstance(data, list) and # check if it is not the ListView result without pagination | |
data.get('results')): # check if the results have been paginated | |
data[self.root] = data['results'] | |
del data['results'] | |
else: |
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 rest_framework.test import APITestCase | |
from django.contrib.auth.models import User | |
class MyAPICase(APITestCase): | |
def setUp(self): | |
user = User.objects.create_user('test_user', | |
'[email protected]', '12345') | |
self.client.force_authenticate(user=user) |
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 CreateUser(forms.Form): | |
username = forms.CharField() | |
password = forms.CharField(min_length=8) | |
re_password = forms.CharField(min_length=8) | |
email = forms.EmailField() | |
def clean_password(self): | |
print "CLEANED DATA: ", self.cleaned_data | |
password = self.cleaned_data['password'] |