This repository has been archived on 2022-09-23. You can view files and clone it, but cannot push or open issues or pull requests.
webdev/website/posts/views.py

30 lines
1016 B
Python

from django.contrib.auth.models import User
from django.shortcuts import render
from snippets.serializers import UserSerializer
from .models import Post
from .serializers import PostSerializer
from posts.permissions import IsOwnerOrReadOnly
from rest_framework import permissions, renderers
from rest_framework.reverse import reverse
from rest_framework import viewsets
from rest_framework.decorators import action
from rest_framework.response import Response
class PostsViewSet(viewsets.ModelViewSet):
"""
docstring
"""
queryset = Post.objects.all()
serializer_class = PostSerializer
permission_classes = [permissions.IsAuthenticatedOrReadOnly,
IsOwnerOrReadOnly]
def perform_create(self, serializer):
serializer.save(owner=self.request.user)
class UserViewSet(viewsets.ReadOnlyModelViewSet):
"""
This viewset automatically provides `list` and `retrieve` actions.
"""
queryset = User.objects.all()
serializer_class = UserSerializer