From 0d88ce9d3547b648bb28a0d3c7de7bf645731948 Mon Sep 17 00:00:00 2001 From: Pratyush Desai Date: Fri, 24 Dec 2021 21:34:35 +0530 Subject: add blog app, config models and views --- api/api/blogs/const.py | 5 +++++ api/api/blogs/models.py | 14 ++++++++++++++ api/api/blogs/permissions.py | 6 ++++++ api/api/blogs/serializers.py | 10 ++++++++++ api/api/blogs/views.py | 14 +++++++++++++- 5 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 api/api/blogs/const.py create mode 100644 api/api/blogs/permissions.py create mode 100644 api/api/blogs/serializers.py diff --git a/api/api/blogs/const.py b/api/api/blogs/const.py new file mode 100644 index 0000000..c8c1938 --- /dev/null +++ b/api/api/blogs/const.py @@ -0,0 +1,5 @@ + +# STATUS = ( +# (0, "Draft"), +# (1, "Published") +# ) \ No newline at end of file diff --git a/api/api/blogs/models.py b/api/api/blogs/models.py index b096caa..43bb8d1 100644 --- a/api/api/blogs/models.py +++ b/api/api/blogs/models.py @@ -1,4 +1,18 @@ from django.db import models + +# move to `const.py` + +STATUS = ( + (0,"Draft"), + (1,"Publish") +) # Create your models here. +class Post(models.Model): + title = models.CharField(max_length=200, unique=True) + author = models.ForeignKey('user.auth',on_delete=models.CASCADE) + updated_on =models.DateTimeField(auto_now= True) + created_on = models.DateTimeField(auto_now_add=True) + content = models.TextField() + status = models.IntegerChoices() \ No newline at end of file diff --git a/api/api/blogs/permissions.py b/api/api/blogs/permissions.py new file mode 100644 index 0000000..2ec78ea --- /dev/null +++ b/api/api/blogs/permissions.py @@ -0,0 +1,6 @@ +from rest_framework import permissions + +class IsOwnerOrReadOnly(permissions.BasePermission): + """ + Custom permission to only allow owners/authors of the object to edit it. + """ \ No newline at end of file diff --git a/api/api/blogs/serializers.py b/api/api/blogs/serializers.py new file mode 100644 index 0000000..e05ccc3 --- /dev/null +++ b/api/api/blogs/serializers.py @@ -0,0 +1,10 @@ +from django.contrib.auth.models import User +from .models import * + +from rest_framework import serializers + +class PostSerializer(serializers.ModelSerializer): + class Meta: + model = Post + # better to explicitly mention the fields + fields = '__all__' \ No newline at end of file diff --git a/api/api/blogs/views.py b/api/api/blogs/views.py index 69a6f29..b6d8f6f 100644 --- a/api/api/blogs/views.py +++ b/api/api/blogs/views.py @@ -1,7 +1,19 @@ from django.shortcuts import render from django.http import HttpResponse +from rest_framework import permissions +from rest_framework import viewsets + +from .models import * +from .serializers import * + + +# Create your views here. + def index(request): return HttpResponse("You're at the Blogs index") -# Create your views here. +class PostViewSet(viewsets.ModelViewSet): + queryset = Post.objects.all() + serializer_class = PostSerializer + permission_classes = [permissions.IsAuthenticatedOrReadOnly] -- cgit v1.2.3