Skip to content

Instantly share code, notes, and snippets.

Avatar
🌿

Tom Christie tomchristie

🌿
View GitHub Profile
@tomchristie
tomchristie / unicode_json_renderer.py
Created Sep 20, 2011
JSONRenderer that doesn't force ascii
View unicode_json_renderer.py
class UnicodeJSONRenderer(BaseRenderer):
"""
Renderer which serializes to JSON
"""
media_type = 'application/json'
format = 'json'
def render(self, obj=None, media_type=None):
"""
@tomchristie
tomchristie / renderers.py
Created Sep 21, 2011
Better API on DocumentingTemplateRenderer for escaping content.
View renderers.py
class DocumentingTemplateRenderer(BaseRenderer):
...
escape_binary = True
def _escape_binary(content):
if self.escape_binary and not all(char in string.printable for char in content):
return '[%d bytes of binary content]' % len(content)
return content
...
@tomchristie
tomchristie / gist:1372740
Created Nov 17, 2011
Don't attempt to connect to the Selenium server at import; Wait until the first test is run.
View gist:1372740
class SeleniumTestCase(LiveServerTestCase):
_selenium_server_running = None
@property
def selenium_server_running(self):
"""
Determine if we can connect to the Selenium RC server.
Only evaluated once for performance reasons.
"""
@tomchristie
tomchristie / gist:1372745
Created Nov 17, 2011
Dont attempt to connect to the Selenium server at import; Check at the start of every test.
View gist:1372745
class SeleniumTestCase(LiveServerTestCase):
@property
def selenium_server_running(self):
"""
Determine if we can connect to the Selenium RC server.
"""
try:
conn = httplib.HTTPConnection(settings.SELENIUM_SERVER_HOST,
settings.SELENIUM_SERVER_PORT)
@tomchristie
tomchristie / gist:3499147
Created Aug 28, 2012
Example of writing views with django-serializers
View gist:3499147
# serializers.py
class CommentSerializer(serializers.Serializer):
email = serializers.EmailField()
content = serializers.CharField(max_length=200)
created = serializers.DateTimeField()
def restore_object(self, attrs, instance=None):
"""
Create or update a new comment instance.
View gist:3667231
class GetAccessToken(APIView):
authentication_classes = [authentication.UserBasicAuthentication]
permission_classes = [permissions.UserIsAuthenticated]
model = djangorestframework.basictoken.models.BasicToken
def post(self, request, *args, **kwargs):
token = self.model.objects.get_or_create(user=request.user)
return Response({'key': token.key})
View gist:3667533
class TokenAuthentication(BaseAuthentication):
model = djangorestframework.tokenauth.models.Token
def authenticate(self, request):
key = request.META.get('HTTP_AUTHORIZATION', '').strip()
if not key:
return None
try:
token = self.model.objects.get(key=key)
View gist:3851739
class MultipleObjectBaseView(MultipleObjectMixin, BaseView):
"""
Base class for generic views onto a queryset.
"""
pagination_serializer_class = api_settings.PAGINATION_SERIALIZER
paginate_by = api_settings.PAGINATE_BY
filter_class = None
filter_fields = None
View gist:3911076
# Currently in initial()
if not self.has_permission(request):
self.permission_denied(request)
self.check_throttles(request)
# May be this...
if not self.has_permission(request):
if request._authenticated:
self.permission_denied(request)
else:
@tomchristie
tomchristie / models.py
Created Oct 26, 2012
SnippetSerializer
View models.py
class SnippetSerializer(serializers.Serializer):
title = serializers.CharField(required=False,
max_length=100)
code = serializers.CharField(widget=widgets.Textarea,
max_length=100000)
linenos = serializers.BooleanField(required=False)
lexer = serializers.ChoiceField(choices=models.LEXER_CHOICES,
default='python')
style = serializers.ChoiceField(choices=models.STYLE_CHOICES,
default='friendly')