From e2a28c7673ca1cf4e3a7d2b3ba149925fa0dc03c Mon Sep 17 00:00:00 2001 From: Can Date: Fri, 10 May 2024 16:22:27 +0300 Subject: [PATCH] Comment Added Comment Added to App --- .env.example | 8 ----- backend/.env.example | 6 ---- backend/nba_app/migrations/0003_comment.py | 25 ++++++++++++++ backend/nba_app/models.py | 15 +++++++- backend/nba_app/urls.py | 4 ++- backend/nba_app/views.py | 40 ++++++++++++++++++++-- backend/nba_project/settings.py | 3 ++ backend/templates/comment.html | 16 +++++++++ backend/templates/post_detail.html | 26 ++++++++++++++ 9 files changed, 124 insertions(+), 19 deletions(-) delete mode 100644 .env.example delete mode 100644 backend/.env.example create mode 100644 backend/nba_app/migrations/0003_comment.py create mode 100644 backend/templates/comment.html create mode 100644 backend/templates/post_detail.html diff --git a/.env.example b/.env.example deleted file mode 100644 index 6cff664c..00000000 --- a/.env.example +++ /dev/null @@ -1,8 +0,0 @@ -DB_HOST=db -DB_NAME=nba_database -DB_USER=group4 -DB_PASSWORD=group4_dbnba -DB_PORT=3306 -DB_ROOT_PASSWORD=admin_group4_dbnba -DEPLOY_MACHINE_IP=127.0.0.1 -REACT_APP_DEPLOY_MACHINE_IP=127.0.0.1 \ No newline at end of file diff --git a/backend/.env.example b/backend/.env.example deleted file mode 100644 index a7b8c3fe..00000000 --- a/backend/.env.example +++ /dev/null @@ -1,6 +0,0 @@ -DB_NAME=mydatabase -DB_USER=myuser -DB_PASSWORD=mypassword -DB_HOST=127.0.0.1 -DB_PORT=3306 -DEPLOY_MACHINE_IP=127.0.0.1 \ No newline at end of file diff --git a/backend/nba_app/migrations/0003_comment.py b/backend/nba_app/migrations/0003_comment.py new file mode 100644 index 00000000..ce4a102f --- /dev/null +++ b/backend/nba_app/migrations/0003_comment.py @@ -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)), + ], + ), + ] diff --git a/backend/nba_app/models.py b/backend/nba_app/models.py index e37ba3a3..ff8e71ef 100644 --- a/backend/nba_app/models.py +++ b/backend/nba_app/models.py @@ -52,4 +52,17 @@ class Post(models.Model): created_at = models.DateTimeField(auto_now_add=True) def __str__(self): - return self.content \ No newline at end of file + 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}' + + + \ No newline at end of file diff --git a/backend/nba_app/urls.py b/backend/nba_app/urls.py index 1fae000f..f660bfda 100644 --- a/backend/nba_app/urls.py +++ b/backend/nba_app/urls.py @@ -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//', views.post_detail, name='post_detail'), + path('post//comment/', views.create_comment, name='create_comment') ] \ No newline at end of file diff --git a/backend/nba_app/views.py b/backend/nba_app/views.py index 1a9225cc..9a615053 100644 --- a/backend/nba_app/views.py +++ b/backend/nba_app/views.py @@ -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): @@ -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": @@ -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): diff --git a/backend/nba_project/settings.py b/backend/nba_project/settings.py index 9258703b..b8872103 100644 --- a/backend/nba_project/settings.py +++ b/backend/nba_project/settings.py @@ -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 diff --git a/backend/templates/comment.html b/backend/templates/comment.html new file mode 100644 index 00000000..892e811d --- /dev/null +++ b/backend/templates/comment.html @@ -0,0 +1,16 @@ + + + + + + Comment + + +

Comment on Post {{ post_id }}

+
+ {% csrf_token %} +
+ +
+ + diff --git a/backend/templates/post_detail.html b/backend/templates/post_detail.html new file mode 100644 index 00000000..95b7a8ac --- /dev/null +++ b/backend/templates/post_detail.html @@ -0,0 +1,26 @@ + + + + + + Post Detail + + +

Post Detail

+

{{ post.content }}

+

Comments

+
    + {% for comment in comments %} +
  • {{ comment.user.username }}: {{ comment.content }}
  • + {% empty %} +
  • No comments yet.
  • + {% endfor %} +
+

Add a Comment

+
+ {% csrf_token %} +
+ +
+ +