Skip to content

Commit

Permalink
Mostly 3.0 ready
Browse files Browse the repository at this point in the history
  • Loading branch information
Carlton Gibson committed Dec 15, 2014
1 parent 8ab529c commit dc7d831
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 11 deletions.
2 changes: 2 additions & 0 deletions snippets/permissions.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,7 @@ def has_object_permission(self, request, view, obj):
if request.method in permissions.SAFE_METHODS:
return True

method = request.method

# Write permissions are only allowed to the owner of the snippet
return obj.owner == request.user
7 changes: 3 additions & 4 deletions snippets/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,16 @@


class SnippetSerializer(serializers.HyperlinkedModelSerializer):
owner = serializers.Field(source='owner.username')
owner = serializers.ReadOnlyField(source='owner.username')
highlight = serializers.HyperlinkedIdentityField(view_name='snippet-highlight', format='html')

class Meta:
model = Snippet
fields = ('url', 'highlight', 'owner',
'title', 'code', 'linenos', 'language', 'style')



class UserSerializer(serializers.HyperlinkedModelSerializer):
snippets = serializers.HyperlinkedRelatedField(view_name='snippet-detail', many=True)
snippets = serializers.HyperlinkedRelatedField(queryset=Snippet.objects.all(), view_name='snippet-detail', many=True)

class Meta:
model = User
Expand Down
14 changes: 7 additions & 7 deletions snippets/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from rest_framework import permissions
from rest_framework import renderers
from rest_framework import viewsets
from rest_framework.decorators import link
from rest_framework.decorators import detail_route
from rest_framework.response import Response
from snippets.models import Snippet
from snippets.permissions import IsOwnerOrReadOnly
Expand All @@ -24,17 +24,17 @@ class SnippetViewSet(viewsets.ModelViewSet):
"""
queryset = Snippet.objects.all()
serializer_class = SnippetSerializer
permission_classes = (permissions.IsAuthenticatedOrReadOnly,
IsOwnerOrReadOnly,)
permission_classes = (permissions.IsAuthenticatedOrReadOnly,)
# IsOwnerOrReadOnly,)

@link(renderer_classes=(renderers.StaticHTMLRenderer,))
@detail_route(renderer_classes=(renderers.StaticHTMLRenderer,))
def highlight(self, request, *args, **kwargs):
snippet = self.get_object()
return Response(snippet.highlighted)

def pre_save(self, obj):
obj.owner = self.request.user

def perform_create(self, serializer):
serializer.save(owner=self.request.user)

class UserViewSet(viewsets.ReadOnlyModelViewSet):
"""
Expand Down

0 comments on commit dc7d831

Please sign in to comment.