Skip to content

Commit

Permalink
Added a comment system
Browse files Browse the repository at this point in the history
  • Loading branch information
yahyaaly151989 committed Aug 20, 2023
1 parent eb6a0fe commit 921fbb0
Show file tree
Hide file tree
Showing 16 changed files with 115 additions and 6 deletions.
Binary file modified Module02/mysite/blog/__pycache__/admin.cpython-311.pyc
Binary file not shown.
Binary file modified Module02/mysite/blog/__pycache__/forms.cpython-311.pyc
Binary file not shown.
Binary file modified Module02/mysite/blog/__pycache__/models.cpython-311.pyc
Binary file not shown.
Binary file modified Module02/mysite/blog/__pycache__/urls.cpython-311.pyc
Binary file not shown.
Binary file modified Module02/mysite/blog/__pycache__/views.cpython-311.pyc
Binary file not shown.
9 changes: 7 additions & 2 deletions Module02/mysite/blog/admin.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from django.contrib import admin
from .models import Post

from .models import Post, Comment

@admin.register(Post)
class PostAdmin(admin.ModelAdmin):
Expand All @@ -11,3 +10,9 @@ class PostAdmin(admin.ModelAdmin):
raw_id_fields = ['author']
date_hierarchy = 'publish'
ordering = ['status', 'publish']

@admin.register(Comment)
class CommentAdmin(admin.ModelAdmin):
list_display = ['name', 'email', 'post', 'created', 'active']
list_filter = ['active', 'created', 'updated']
search_fields = ['name', 'email', 'body']
6 changes: 6 additions & 0 deletions Module02/mysite/blog/forms.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
from django import forms
from .models import Comment

class EmailPostForm(forms.Form):
name = forms.CharField(max_length=25)
email = forms.EmailField()
to = forms.EmailField()
comments = forms.CharField(required=False, widget=forms.Textarea)

class CommentForm(forms.ModelForm):
class Meta:
model = Comment
fields = ['name', 'email', 'body']
31 changes: 31 additions & 0 deletions Module02/mysite/blog/migrations/0003_comment.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Generated by Django 4.2.4 on 2023-08-20 12:32

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


class Migration(migrations.Migration):

dependencies = [
('blog', '0002_alter_post_slug'),
]

operations = [
migrations.CreateModel(
name='Comment',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(max_length=80)),
('email', models.EmailField(max_length=254)),
('body', models.TextField()),
('created', models.DateTimeField(auto_now_add=True)),
('updated', models.DateTimeField(auto_now=True)),
('active', models.BooleanField(default=True)),
('post', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='comments', to='blog.post')),
],
options={
'ordering': ['created'],
'indexes': [models.Index(fields=['created'], name='blog_commen_created_0e6ed4_idx')],
},
),
]
Binary file not shown.
16 changes: 15 additions & 1 deletion Module02/mysite/blog/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,19 @@ def get_absolute_url(self):
self.slug,
])


class Comment(models.Model):
post = models.ForeignKey(Post, on_delete=models.CASCADE, related_name='comments')
name = models.CharField(max_length=80)
email = models.EmailField()
body = models.TextField()
created = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)
active = models.BooleanField(default=True)

class Meta:
ordering = ['created']
indexes = [
models.Index(fields=['created']),
]
def __str__(self):
return f'Comment by {self.name} on {self.post}'
10 changes: 10 additions & 0 deletions Module02/mysite/blog/templates/blog/post/comment.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{% extends "blog/base.html" %}
{% block title %}Add a comment{% endblock %}
{% block content %}
{% if comment %}
<h2>Your comment has been added.</h2>
<p><a href="{{ post.get_absolute_url }}">Back to the post</a></p>
{% else %}
{% include "blog/post/includes/comment_form.html" %}
{% endif %}
{% endblock %}
17 changes: 17 additions & 0 deletions Module02/mysite/blog/templates/blog/post/detail.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,21 @@ <h1>{{ post.title }}</h1>
Share this post
</a>
</p>
{% with comments.count as total_comments %}
<h2>
{{ total_comments }} comment{{ total_comments|pluralize }}
</h2>
{% endwith %}
{% for comment in comments %}
<div class="comment">
<p class="info">
Comment {{ forloop.counter }} by {{ comment.name }}
{{ comment.created }}
</p>
{{ comment.body|linebreaks }}
</div>
{% empty %}
<p>There are no comments.</p>
{% endfor %}
{% include "blog/post/includes/comment_form.html" %}
{% endblock %}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<h2>Add a new comment</h2>
<form action="{% url "blog:post_comment" post.id %}" method="post">
{{ form.as_p }}
{% csrf_token %}
<p><input type="submit" value="Add comment"></p>
</form>
1 change: 1 addition & 0 deletions Module02/mysite/blog/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@
path('', views.PostListView.as_view(), name='post_list'),
path('<int:year>/<int:month>/<int:day>/<slug:post>/', views.post_detail, name='post_detail'),
path('<int:post_id>/share/', views.post_share, name='post_share'),
path('<int:post_id>/comment/', views.post_comment, name='post_comment'),
]
25 changes: 22 additions & 3 deletions Module02/mysite/blog/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
from .models import Post
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
from django.views.generic import ListView
from .forms import EmailPostForm
from .forms import EmailPostForm, CommentForm
from django.core.mail import send_mail

from django.views.decorators.http import require_POST

# def post_list(request):
# post_list = Post.objects.all()
Expand Down Expand Up @@ -37,7 +37,11 @@ def post_detail(request, year, month, day, post):
publish__month=month,
publish__day=day
)
return render(request, 'blog/post/detail.html', {'post': post})
# List of active comments for this post
comments = post.comments.filter(active=True)
# Form for users to comment
form = CommentForm()
return render(request, 'blog/post/detail.html', {'post': post, 'comments': comments, 'form': form})

def post_share(request, post_id):
# Retrieve post by id
Expand All @@ -57,3 +61,18 @@ def post_share(request, post_id):
else:
form = EmailPostForm()
return render(request, 'blog/post/share.html', {'post': post, 'form': form, 'sent': sent})

@require_POST
def post_comment(request, post_id):
post = get_object_or_404(Post, id=post_id, status=Post.Status.PUBLISHED)
comment = None
# A comment was posted
form = CommentForm(data=request.POST)
if form.is_valid():
# Create a Comment object without saving it to the database
comment = form.save(commit=False)
# Assign the post to the comment
comment.post = post
# Save the comment to the database
comment.save()
return render(request, 'blog/post/comment.html', {'post': post, 'form': form, 'comment': comment})
Binary file modified Module02/mysite/db.sqlite3
Binary file not shown.

0 comments on commit 921fbb0

Please sign in to comment.