Skip to content

Commit

Permalink
added blogs
Browse files Browse the repository at this point in the history
  • Loading branch information
Anupamaraie committed Nov 4, 2023
1 parent fb83056 commit e5302da
Show file tree
Hide file tree
Showing 9 changed files with 520 additions and 9 deletions.
8 changes: 0 additions & 8 deletions .env.example

This file was deleted.

1 change: 1 addition & 0 deletions backend/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
urlpatterns = [
path("admin/", admin.site.urls),
path("api/auth/", include("authentication.urls")),
path("api/blogs/", include("blogs.urls")),
]

if settings.DEBUG:
Expand Down
5 changes: 5 additions & 0 deletions blogs/admin.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
from django.contrib import admin

from .models import *

# Register your models here.
admin.site.register(Posts)
admin.site.register(Comments)
admin.site.register(Likes)
93 changes: 93 additions & 0 deletions blogs/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
# Generated by Django 4.2.6 on 2023-10-28 08:21

from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):
initial = True

dependencies = []

operations = [
migrations.CreateModel(
name="Users",
fields=[
("user_id", models.AutoField(primary_key=True, serialize=False)),
("username", models.CharField(max_length=50)),
("email", models.EmailField(max_length=254, unique=True)),
("password", models.CharField(max_length=10)),
],
),
migrations.CreateModel(
name="Posts",
fields=[
("post_id", models.AutoField(primary_key=True, serialize=False)),
("title", models.CharField(max_length=50)),
("content", models.TextField()),
("created_at", models.DateTimeField(auto_now_add=True)),
("updated_at", models.DateTimeField(auto_now=True)),
(
"user",
models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE, to="blogs.users"
),
),
],
),
migrations.CreateModel(
name="Likes",
fields=[
(
"id",
models.BigAutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
("created_at", models.DateTimeField(auto_now=True)),
(
"post",
models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE, to="blogs.posts"
),
),
(
"user",
models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE, to="blogs.users"
),
),
],
),
migrations.CreateModel(
name="Comments",
fields=[
(
"id",
models.BigAutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
("c_content", models.TextField()),
("created_at", models.DateTimeField(auto_now=True)),
(
"post",
models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE, to="blogs.posts"
),
),
(
"user",
models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE, to="blogs.users"
),
),
],
),
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# Generated by Django 4.2.6 on 2023-10-28 15:52

from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):
dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
("blogs", "0001_initial"),
]

operations = [
migrations.AlterField(
model_name="comments",
name="post",
field=models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE,
related_name="comment_post",
to="blogs.posts",
),
),
migrations.AlterField(
model_name="comments",
name="user",
field=models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE,
related_name="comment_user",
to=settings.AUTH_USER_MODEL,
),
),
migrations.AlterField(
model_name="likes",
name="post",
field=models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE,
related_name="like_post",
to="blogs.posts",
),
),
migrations.AlterField(
model_name="likes",
name="user",
field=models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE,
related_name="like_user",
to=settings.AUTH_USER_MODEL,
),
),
migrations.AlterField(
model_name="posts",
name="user",
field=models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE,
related_name="post",
to=settings.AUTH_USER_MODEL,
),
),
migrations.DeleteModel(
name="Users",
),
]
32 changes: 32 additions & 0 deletions blogs/models.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,35 @@
from datetime import datetime

from django.conf import settings
from django.contrib.auth.models import User
from django.db import models

# Create your models here.


class Posts(models.Model):
post_id = models.AutoField(primary_key=True)
title = models.CharField(max_length=50, blank=False)
content = models.TextField(blank=False)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, related_name='post') #reference from Users

def __str__(self):
return self.title


class Comments(models.Model):
post = models.ForeignKey(Posts, on_delete= models.CASCADE,related_name='comment_post') #reference from Posts
user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete= models.CASCADE, related_name='comment_user') #reference from Users
c_content = models.TextField()
created_at = models.DateTimeField(auto_now=True)

def __str__(self):
return f"{self.user}/{self.post}"


class Likes(models.Model):
post = models.ForeignKey(Posts, on_delete= models.CASCADE, related_name='like_post') #refrence from Posts
user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete= models.CASCADE, related_name='like_user') #reference from Users
created_at = models.DateTimeField(auto_now=True)
39 changes: 39 additions & 0 deletions blogs/serializers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
from django.contrib.auth.models import User
from rest_framework import serializers

from authentication.serializers import *

from .models import *


class LikeSerializer(serializers.ModelSerializer):
class Meta:
model = Likes
fields = '__all__'
read_only_fields = ['user','post']

class CommentSerializer(serializers.ModelSerializer):
class Meta:
model = Comments
fields = '__all__'
read_only_fields = ['user']

class CommentEditSerializer(serializers.ModelSerializer):
class Meta:
model = Comments
fields = '__all__'
read_only_fields = ['user','post']

class PostSerializer(serializers.ModelSerializer):
comment_post = CommentSerializer(many=True)
like_post = LikeSerializer(many=True)
user = serializers.CharField()
class Meta:
model = Posts
fields = ('user','post_id','title','content','created_at','updated_at','comment_post','like_post')

class CreatePostSerializer(serializers.ModelSerializer):
class Meta:
model = Posts
fields = ('user','title','content')
read_only_fields = ['user']
19 changes: 19 additions & 0 deletions blogs/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from django.urls import include, path

from blogs.views import (CreateBlogView, CreateCommentView, EditCommentView,
GetBlogView, LikeCreateView, ReadCommentView,
ReadPostView)

urlpatterns = [
path('getblog',GetBlogView.as_view()),
path('readpost/<int:post_id>',ReadPostView.as_view()),
path('createblog',CreateBlogView.as_view()),
path('createblog/<int:post_id>/',CreateBlogView.as_view()),
path('deleteblog/<int:post_id>',CreateBlogView.as_view()),
path('createcomment',CreateCommentView.as_view()),
path('readcomment/<int:id>', ReadCommentView.as_view()),
path('editcomment/<int:id>/',EditCommentView.as_view()),
path('deletecomment/<int:id>',CreateCommentView.as_view()),
path('likepost/<int:post_id>',LikeCreateView.as_view()),
path('unlikepost/<int:post_id>',LikeCreateView.as_view()),
]
Loading

0 comments on commit e5302da

Please sign in to comment.