Skip to content

Instantly share code, notes, and snippets.

@mitchtabian
Created September 16, 2019 20:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mitchtabian/c9d2bf1fc0088fe46ab24bee72f5b35f to your computer and use it in GitHub Desktop.
Save mitchtabian/c9d2bf1fc0088fe46ab24bee72f5b35f to your computer and use it in GitHub Desktop.
@api_view(['GET',])
@permission_classes((IsAuthenticated,))
def api_is_author_of_blogpost(request, slug):
try:
blog_post = BlogPost.objects.get(slug=slug)
except BlogPost.DoesNotExist:
return Response(status=status.HTTP_404_NOT_FOUND)
data = {}
user = request.user
if blog_post.author != user:
data['response'] = "You don't have permission to edit that."
return Response(data=data)
data['response'] = "You have permission to edit that."
return Response(data=data)
from django.urls import path
from blog.api.views import(
api_detail_blog_view,
api_update_blog_view,
api_delete_blog_view,
api_create_blog_view,
api_is_author_of_blogpost,
ApiBlogListView
)
app_name = 'blog'
urlpatterns = [
path('<slug>/', api_detail_blog_view, name="detail"),
path('<slug>/update', api_update_blog_view, name="update"),
path('<slug>/delete', api_delete_blog_view, name="delete"),
path('create', api_create_blog_view, name="create"),
path('list', ApiBlogListView.as_view(), name="list"),
path('<slug>/is_author', api_is_author_of_blogpost, name="is_author"),
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment