From aebbe0f6c69e676169d9087df371ed7e2eab2454 Mon Sep 17 00:00:00 2001 From: moanos Date: Thu, 30 May 2024 14:37:15 +0200 Subject: [PATCH] feat: Add basic comment form --- src/fellchensammlung/forms.py | 6 +++++- .../fellchensammlung/forms/form-comment.html | 7 +++++++ .../partials/partial-comment-section.html | 6 ++++++ src/fellchensammlung/views.py | 20 ++++++++++++++++--- 4 files changed, 35 insertions(+), 4 deletions(-) create mode 100644 src/fellchensammlung/templates/fellchensammlung/forms/form-comment.html diff --git a/src/fellchensammlung/forms.py b/src/fellchensammlung/forms.py index f7b0f63..70ecb92 100644 --- a/src/fellchensammlung/forms.py +++ b/src/fellchensammlung/forms.py @@ -2,7 +2,7 @@ import datetime from django import forms -from .models import AdoptionNotice, Animal, Image, Report, ModerationAction, User, Species +from .models import AdoptionNotice, Animal, Image, Report, ModerationAction, User, Species, Comment from django_registration.forms import RegistrationForm from crispy_forms.helper import FormHelper from crispy_forms.layout import Submit, Layout, Fieldset, HTML, Row, Column, Field @@ -59,6 +59,10 @@ class ReportForm(forms.ModelForm): model = Report fields = ('reported_broken_rules', 'comment') +class CommentForm(forms.ModelForm): + class Meta: + model = Comment + fields = ('text',) class ModerationActionForm(forms.ModelForm): class Meta: diff --git a/src/fellchensammlung/templates/fellchensammlung/forms/form-comment.html b/src/fellchensammlung/templates/fellchensammlung/forms/form-comment.html new file mode 100644 index 0000000..7c06780 --- /dev/null +++ b/src/fellchensammlung/templates/fellchensammlung/forms/form-comment.html @@ -0,0 +1,7 @@ +{% load i18n %} + +
+ {% csrf_token %} + {{ comment_form.as_p }} + +
\ No newline at end of file diff --git a/src/fellchensammlung/templates/fellchensammlung/partials/partial-comment-section.html b/src/fellchensammlung/templates/fellchensammlung/partials/partial-comment-section.html index 8823b14..c424e0a 100644 --- a/src/fellchensammlung/templates/fellchensammlung/partials/partial-comment-section.html +++ b/src/fellchensammlung/templates/fellchensammlung/partials/partial-comment-section.html @@ -12,4 +12,10 @@ {% endif %} + {% if user.is_authenticated %} + {% include "fellchensammlung/forms/form-comment.html" %} + {% else %} + {% translate 'Du musst dich einloggen um Kommentare zu hinterlassen' %} + {% endif %} + \ No newline at end of file diff --git a/src/fellchensammlung/views.py b/src/fellchensammlung/views.py index e0faa5d..8de6f24 100644 --- a/src/fellchensammlung/views.py +++ b/src/fellchensammlung/views.py @@ -4,15 +4,16 @@ from django.http import HttpResponseRedirect from django.shortcuts import render, redirect from django.urls import reverse from django.contrib.auth.decorators import login_required - from django.utils import translation +from django.core.exceptions import PermissionDenied + from .mail import mail_admins_new_report from notfellchen import settings from fellchensammlung import logger from fellchensammlung.models import AdoptionNotice, Text, Animal, Rule, Image, Report, ModerationAction, \ Member -from .forms import AdoptionNoticeForm, ImageForm, ReportForm +from .forms import AdoptionNoticeForm, ImageForm, ReportForm, CommentForm from .models import Language @@ -43,7 +44,20 @@ def change_language(request): def adoption_notice_detail(request, adoption_notice_id): adoption_notice = AdoptionNotice.objects.get(id=adoption_notice_id) - context = {"adoption_notice": adoption_notice} + if request.method == 'POST': + if request.user.is_authenticated: + comment_form = CommentForm(request.POST) + + if comment_form.is_valid(): + comment_instance = comment_form.save(commit=False) + comment_instance.adoption_notice_id = adoption_notice_id + comment_instance.user = request.user.member + comment_instance.save() + else: + raise PermissionDenied + else: + comment_form = CommentForm(instance=adoption_notice) + context = {"adoption_notice": adoption_notice, "comment_form": comment_form} return render(request, 'fellchensammlung/details/detail_adoption_notice.html', context=context)