summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPratyush Desai2021-12-24 21:34:35 +0530
committerPratyush Desai2021-12-24 21:34:35 +0530
commit0d88ce9d3547b648bb28a0d3c7de7bf645731948 (patch)
tree6af99c456086f98e7f3d745d076ebedb8813cc06
parent8540555f6b25fbfd904a31e51b75c28595bc5af2 (diff)
downloadwebsite-0d88ce9d3547b648bb28a0d3c7de7bf645731948.tar.gz
website-0d88ce9d3547b648bb28a0d3c7de7bf645731948.tar.bz2
website-0d88ce9d3547b648bb28a0d3c7de7bf645731948.zip
add blog app, config models and viewsdjango-devel
-rw-r--r--api/api/blogs/const.py5
-rw-r--r--api/api/blogs/models.py14
-rw-r--r--api/api/blogs/permissions.py6
-rw-r--r--api/api/blogs/serializers.py10
-rw-r--r--api/api/blogs/views.py14
5 files changed, 48 insertions, 1 deletions
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]