Skip to content

Commit

Permalink
Question: allow admin to add a comment
Browse files Browse the repository at this point in the history
  • Loading branch information
raphodn committed May 29, 2024
1 parent 323abd3 commit 19a05dd
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 7 deletions.
20 changes: 18 additions & 2 deletions contributions/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,35 @@
from core import constants


COMMENT_EDIT_FORM_FIELDS = [
COMMENT_CREATE_FORM_FIELDS = [
field.name for field in Comment._meta.fields if field.name not in Comment.COMMENT_READONLY_FIELDS
]
COMMENT_REPLY_CREATE_FORM_FIELDS = ["type", "text", "author", "parent", "status"]
COMMENT_REPLY_HIDDEN_FORM_FIELDS = ["parent", "status"]


class CommentCreateForm(forms.ModelForm):
class Meta:
model = Comment
fields = COMMENT_CREATE_FORM_FIELDS
widgets = {
"text": forms.Textarea(attrs={"rows": 3}),
"description": forms.Textarea(attrs={"rows": 3}),
}

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
for field_name in ["type", "question", "quiz", "author"]:
self.fields[field_name].disabled = True
# see views.py for exceptions


class CommentEditForm(forms.ModelForm):
# publish = forms.BooleanField(required=False)

class Meta:
model = Comment
fields = COMMENT_EDIT_FORM_FIELDS
fields = COMMENT_CREATE_FORM_FIELDS
widgets = {
"text": forms.Textarea(attrs={"rows": 3}),
"description": forms.Textarea(attrs={"rows": 3}),
Expand Down
32 changes: 31 additions & 1 deletion templates/questions/detail_comments.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{% extends "questions/detail_base.html" %}
{% load render_table from django_tables2 %}
{% load i18n %}
{% load i18n django_bootstrap5 %}

{% block question_detail_content %}
<div class="row">
Expand All @@ -12,4 +12,34 @@
{% endif %}
</div>
</div>

<br />

<!-- Comment form -->
<div class="row">
<div class="col-12">
<div class="card">
<div class="card-header">
<strong>{% translate "Add a comment" %}</strong>
</div>
<div class="card-body">
<form id="comment_create_form" method="POST" action="">
{% csrf_token %}

<div class="row">
<div class="col-md-8">
{% bootstrap_form form alert_error_type="all" %}
</div>
</div>

<div class="row">
<div class="col">
{% bootstrap_button button_type="submit" button_class="btn-primary" content=_("Add") %}
</div>
</div>
</form>
</div>
</div>
</div>
</div>
{% endblock %}
4 changes: 2 additions & 2 deletions www/contributions/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

from api.contributions.serializers import CommentFullSerializer
from contributions.filters import CommentFilter
from contributions.forms import COMMENT_EDIT_FORM_FIELDS, CommentEditForm, CommentReplyCreateForm
from contributions.forms import COMMENT_CREATE_FORM_FIELDS, CommentEditForm, CommentReplyCreateForm
from contributions.models import Comment
from contributions.tables import CommentTable
from core import constants
Expand Down Expand Up @@ -135,7 +135,7 @@ def get_context_data(self, **kwargs):
else:
# probably a create action
# we create the diff ourselves because there isn't any previous record
delta_fields = COMMENT_EDIT_FORM_FIELDS
delta_fields = COMMENT_CREATE_FORM_FIELDS
delta_new = [{"field": k, "new": v} for k, v in record.__dict__.items() if k in delta_fields if v]
context["comment_history_delta"].append(delta_new)
return context
43 changes: 41 additions & 2 deletions www/questions/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,16 @@
from django.utils.safestring import mark_safe
from django.utils.translation import gettext_lazy as _
from django.views.generic import CreateView, DetailView, ListView, UpdateView
from django.views.generic.edit import FormMixin
from django_filters.views import FilterView
from django_tables2.views import SingleTableMixin, SingleTableView
from django_tables2.views import SingleTableMixin

from activity.utilities import create_event
from api.questions.serializers import QuestionFullStringSerializer
from contributions.forms import CommentCreateForm
from contributions.models import Comment
from contributions.tables import CommentTable
from core import constants
from core.forms import form_filters_cleaned_dict, form_filters_to_list
from core.mixins import ContributorUserRequiredMixin
from core.utils.s3 import S3Upload
Expand Down Expand Up @@ -144,11 +147,12 @@ def get_context_data(self, **kwargs):
return context


class QuestionDetailCommentListView(ContributorUserRequiredMixin, SingleTableView):
class QuestionDetailCommentListView(ContributorUserRequiredMixin, SingleTableMixin, FormMixin, ListView):
model = Comment
template_name = "questions/detail_comments.html"
context_object_name = "question_contributions"
table_class = CommentTable
form_class = CommentCreateForm

def get_queryset(self):
qs = super().get_queryset()
Expand All @@ -161,6 +165,41 @@ def get_context_data(self, **kwargs):
context["question"] = Question.objects.get(id=self.kwargs.get("pk"))
return context

def get_initial(self):
return {
"type": constants.COMMENT_TYPE_COMMENT_QUESTION,
"question": self.kwargs.get("pk"),
"author": self.request.user,
"parent": None,
}

def post(self, request, *args, **kwargs):
form_class = self.get_form_class()
form = self.get_form(form_class)
if form.is_valid():
return self.form_valid(
form,
)
else:
return self.form_invalid(
form,
)

def form_valid(self, form):
form.save()
# response
messages.add_message(self.request, messages.SUCCESS, self.get_success_message(form.cleaned_data))
return HttpResponseRedirect(self.get_success_url())

def form_invalid(self, form):
return self.render_to_response(self.get_context_data())

def get_success_message(self, cleaned_data):
return _("Your message was created.")

def get_success_url(self):
return reverse_lazy("questions:detail_comments", args=[self.kwargs.get("pk")])


class QuestionDetailStatsView(ContributorUserRequiredMixin, DetailView):
model = QuestionAggStat
Expand Down

0 comments on commit 19a05dd

Please sign in to comment.