Skip to content

Commit

Permalink
Comment Added
Browse files Browse the repository at this point in the history
Comment Added to App
  • Loading branch information
cankocakulak committed May 10, 2024
1 parent 63c0404 commit e2a28c7
Show file tree
Hide file tree
Showing 9 changed files with 124 additions and 19 deletions.
8 changes: 0 additions & 8 deletions .env.example

This file was deleted.

6 changes: 0 additions & 6 deletions backend/.env.example

This file was deleted.

25 changes: 25 additions & 0 deletions backend/nba_app/migrations/0003_comment.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Generated by Django 4.2.13 on 2024-05-10 13:04

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


class Migration(migrations.Migration):

dependencies = [
('nba_app', '0002_post'),
]

operations = [
migrations.CreateModel(
name='Comment',
fields=[
('comment_id', models.AutoField(primary_key=True, serialize=False)),
('content', models.TextField(max_length=300)),
('created_at', models.DateTimeField(auto_now_add=True)),
('post', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='comments', to='nba_app.post')),
('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
],
),
]
15 changes: 14 additions & 1 deletion backend/nba_app/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,17 @@ class Post(models.Model):
created_at = models.DateTimeField(auto_now_add=True)

def __str__(self):
return self.content
return self.content

class Comment(models.Model):
comment_id = models.AutoField(primary_key=True)
post = models.ForeignKey(Post, on_delete=models.CASCADE, related_name='comments')
user = models.ForeignKey(User, on_delete=models.CASCADE)
content = models.TextField(max_length=300, blank=False)
created_at = models.DateTimeField(auto_now_add=True)

def __str__(self):
return f'{self.user.username} on {self.post.post_id}: {self.content}'



4 changes: 3 additions & 1 deletion backend/nba_app/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,7 @@
path('player/', views.player, name='player'),
path('csrf_token/', views.csrf_token, name='csrf_token'),
path('session/', views.session, name='session'),
path('log_out/', views.log_out, name='log_out')
path('log_out/', views.log_out, name='log_out'),
path('post/<int:post_id>/', views.post_detail, name='post_detail'),
path('post/<int:post_id>/comment/', views.create_comment, name='create_comment')
]
40 changes: 37 additions & 3 deletions backend/nba_app/views.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
from django.contrib.auth.decorators import login_required
from django.http import HttpResponse
from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import render, redirect
from django.contrib import messages
from django.contrib.auth import authenticate, login, logout
from django.middleware.csrf import get_token
from django.http import JsonResponse, HttpResponse
from django.urls import reverse
from .models import User, Post
from .models import User, Post, Comment
import requests

def sign_up(request):
Expand Down Expand Up @@ -61,7 +61,7 @@ def log_out(request):
request.session.flush()
return HttpResponse("Logged out successfully", status=200)


"""
@login_required
def post(request):
if request.method == "POST":
Expand All @@ -76,6 +76,40 @@ def post(request):
print(text)
return render(request, 'post.html')
"""
@login_required
def post(request):
if request.method == "POST":
user = request.user
content = request.POST.get("content")
post = Post.objects.create(user=user, content=content)
#if username == "":
# # handle if the user is not logged in
# print("not logged in")
# # return redirect('signup')
return HttpResponseRedirect(f'/post/{post.post_id}/')
return render(request, 'post.html')

@login_required
def create_comment(request, post_id):
if request.method == "POST":
user = request.user
content = request.POST.get("content")
post = Post.objects.get(post_id=post_id)

if post:
Comment.objects.create(user=user, content=content, post=post)
return HttpResponseRedirect(f'/post/{post_id}/')
else:
return HttpResponse("Post not found", status=404)

return render(request, 'comment.html', {'post_id': post_id})


def post_detail(request, post_id):
post = Post.objects.get(post_id=post_id)
comments = post.comments.all()
return render(request, 'post_detail.html', {'post': post, 'comments': comments})

@login_required
def feed(request):
Expand Down
3 changes: 3 additions & 0 deletions backend/nba_project/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
# Load environment variables from .env file
load_dotenv('././.env')

print('DB_HOST:', os.getenv('DB_HOST'))
print('DB_USER:', os.getenv('DB_USER'))

# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent

Expand Down
16 changes: 16 additions & 0 deletions backend/templates/comment.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<!-- comment.html -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Comment</title>
</head>
<body>
<h1>Comment on Post {{ post_id }}</h1>
<form action="{% url 'create_comment' post_id %}" method="post">
{% csrf_token %}
<textarea name="content" placeholder="Write your comment..."></textarea><br>
<button type="submit">Comment</button>
</form>
</body>
</html>
26 changes: 26 additions & 0 deletions backend/templates/post_detail.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<!-- post_detail.html -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Post Detail</title>
</head>
<body>
<h1>Post Detail</h1>
<p>{{ post.content }}</p>
<h2>Comments</h2>
<ul>
{% for comment in comments %}
<li>{{ comment.user.username }}: {{ comment.content }}</li>
{% empty %}
<li>No comments yet.</li>
{% endfor %}
</ul>
<h3>Add a Comment</h3>
<form action="{% url 'create_comment' post.post_id %}" method="post">
{% csrf_token %}
<textarea name="content" placeholder="Write your comment..."></textarea><br>
<button type="submit">Comment</button>
</form>
</body>
</html>

0 comments on commit e2a28c7

Please sign in to comment.