add blog app, config models and views

This commit is contained in:
Pratyush Desai 2021-12-24 21:34:35 +05:30
parent 8540555f6b
commit 0d88ce9d35
Signed by: pratyush
GPG Key ID: DBA5BB7505946FAD
5 changed files with 48 additions and 1 deletions

5
api/api/blogs/const.py Normal file
View File

@ -0,0 +1,5 @@
# STATUS = (
# (0, "Draft"),
# (1, "Published")
# )

View File

@ -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()

View File

@ -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.
"""

View File

@ -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__'

View File

@ -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]