diff --git a/blog/forms.py b/blog/forms.py index b31cf4b..3067983 100755 --- a/blog/forms.py +++ b/blog/forms.py @@ -6,4 +6,7 @@ class CommentForm(forms.ModelForm): class Meta: model = Comment - fields = ("name", "email", "body") + fields = ( "name","email", "body") + + name = forms.CharField(widget=forms.TextInput(attrs={'class':'form-control','placeholder': "enter your first name"})) + email = forms.CharField(widget=forms.TextInput(attrs={'class':'form-control','placeholder': "Email"})) diff --git a/blog/models.py b/blog/models.py index bd5695d..a9ac8f7 100755 --- a/blog/models.py +++ b/blog/models.py @@ -1,5 +1,6 @@ from django.contrib.auth.models import User from django.db import models +from django.conf import settings STATUS = ((0, "Draft"), (1, "Publish")) @@ -8,7 +9,7 @@ class Post(models.Model): title = models.CharField(max_length=200, unique=True) slug = models.SlugField(max_length=200, unique=True) author = models.ForeignKey( - User, on_delete=models.CASCADE, related_name="blog_posts" + settings.AUTH_USER_MODEL, on_delete=models.CASCADE, related_name="blog_posts" ) updated_on = models.DateTimeField(auto_now=True) content = models.TextField() diff --git a/blog/urls.py b/blog/urls.py index 9bc4613..d19ea0f 100755 --- a/blog/urls.py +++ b/blog/urls.py @@ -3,6 +3,8 @@ from . import views from .feeds import AtomSiteNewsFeed, LatestPostsFeed +app_name = 'blog' + urlpatterns = [ path("feed/rss", LatestPostsFeed(), name="post_feed"), path("feed/atom", AtomSiteNewsFeed()), diff --git a/blog/views.py b/blog/views.py index 863d3b6..f5f172e 100755 --- a/blog/views.py +++ b/blog/views.py @@ -1,10 +1,12 @@ -from django.shortcuts import get_object_or_404, render +from django.shortcuts import get_object_or_404, render, redirect from django.views import generic +from django.contrib.auth.decorators import login_required from .forms import CommentForm from .models import Post + class PostList(generic.ListView): queryset = Post.objects.filter(status=1).order_by("-created_on") template_name = "index.html" @@ -21,17 +23,26 @@ def post_detail(request, slug): post = get_object_or_404(Post, slug=slug) comments = post.comments.filter(active=True).order_by("-created_on") new_comment = None - # Comment posted + if request.method == "POST": comment_form = CommentForm(data=request.POST) + if comment_form.is_valid(): + user = request.user # Get the logged-in user + + # Check if the provided first_name and email match the logged-in user's data + if ( + user.first_name == comment_form.cleaned_data['name'] + and user.email == comment_form.cleaned_data['email'] + ): + new_comment = 'success' # Comment successfully created + comment = comment_form.save(commit=False) + comment.post = post + comment.save() + else: + # Provided first_name or email do not match the logged-in user's data + new_comment = 'mismatch' - # Create Comment object but don't save to database yet - new_comment = comment_form.save(commit=False) - # Assign the current post to the comment - new_comment.post = post - # Save the comment to the database - new_comment.save() else: comment_form = CommentForm() @@ -45,3 +56,4 @@ def post_detail(request, slug): "comment_form": comment_form, }, ) + diff --git a/mysite/settings.py b/mysite/settings.py index 7e6330e..92a0a61 100755 --- a/mysite/settings.py +++ b/mysite/settings.py @@ -43,6 +43,7 @@ "blog", "crispy_forms", "django_summernote", + 'users', ] @@ -158,3 +159,8 @@ DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField" X_FRAME_OPTIONS = "SAMEORIGIN" + +AUTH_USER_MODEL = "users.CustomUser" + +LOGIN_REDIRECT_URL = "blog:home" +LOGOUT_REDIRECT_URL = "blog:home" \ No newline at end of file diff --git a/mysite/urls.py b/mysite/urls.py index 4c3bd0e..13f88ba 100755 --- a/mysite/urls.py +++ b/mysite/urls.py @@ -32,6 +32,10 @@ path("summernote/", include("django_summernote.urls")), path('__debug__/', include(debug_toolbar.urls)), path("sitemap.xml", sitemap, {"sitemaps": sitemaps}, name="sitemap"), + + #redirect user to logins system + path("users/", include("users.urls")), + path("users/", include("django.contrib.auth.urls")), ] if settings.DEBUG: diff --git a/templates/base.html b/templates/base.html index 1213d83..a7c7462 100755 --- a/templates/base.html +++ b/templates/base.html @@ -24,7 +24,7 @@