Skip to content

Instantly share code, notes, and snippets.

View tomchristie's full-sized avatar
🌿

Tom Christie tomchristie

🌿
View GitHub Profile
@tomchristie
tomchristie / unicode_json_renderer.py
Created September 20, 2011 16:58
JSONRenderer that doesn't force ascii
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 September 21, 2011 07:52
Better API on DocumentingTemplateRenderer for escaping content.
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 November 17, 2011 08:59
Don't attempt to connect to the Selenium server at import; Wait until the first test is run.
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 November 17, 2011 09:02
Dont attempt to connect to the Selenium server at import; Check at the start of every test.
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 August 28, 2012 15:31
Example of writing views with django-serializers
# 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.
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})
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)
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
# 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 October 26, 2012 11:19
SnippetSerializer
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')