Ajout de la possibilité de commenter les événements

This commit is contained in:
Jean-Marie Favreau 2024-12-12 00:24:53 +01:00
parent bc06b6205d
commit 0526854d6b
5 changed files with 410 additions and 342 deletions

View File

@ -798,3 +798,13 @@ class MessageForm(ModelForm):
if self.internal: if self.internal:
self.fields.pop("name") self.fields.pop("name")
self.fields.pop("email") self.fields.pop("email")
class MessageEventForm(ModelForm):
class Meta:
model = Message
fields = ["message"]
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.fields["message"].label = _("Add a comment")

File diff suppressed because it is too large Load Diff

View File

@ -1574,7 +1574,7 @@ label.required::after {
} }
.chronology { #chronology {
.entree { .entree {
display: grid; display: grid;
grid-template-columns: fit-content(2em) auto; grid-template-columns: fit-content(2em) auto;

View File

@ -28,7 +28,7 @@
{% endwith %} {% endwith %}
{% if user.is_authenticated %} {% if user.is_authenticated %}
<article class="chronology"> <article id="chronology">
<header> <header>
<h2>Chronologie</h2> <h2>Chronologie</h2>
</header> </header>
@ -37,10 +37,10 @@
<div class="entree dateline"> <div class="entree dateline">
<div><span class="ts">{{ step.timestamp }}</span></div> <div><span class="ts">{{ step.timestamp }}</span></div>
<div> <div>
{% if step.data == "created_date" %}<em>création</em>{% endif %} {% if step.data == "created_date" %}<em>création</em>{% if event.created_by_user %} par {{ event.created_by_user.username }}{% endif %}{% endif %}
{% if step.data == "modified_date" %}<em>dernière modification</em>{% endif %} {% if step.data == "modified_date" %}<em>dernière modification</em>{% if event.modified_by_user %} par {{ event.modified_by_user.username }}{% endif %}{% endif %}
{% if step.data == "moderated_date" %}<em>dernière modération</em>{% endif %} {% if step.data == "moderated_date" %}<em>dernière modération</em>{% if event.moderated_by_user %} par {{ event.moderated_by_user.username }}{% endif %}{% endif %}
{% if step.data == "imported_date" %}<em>dernière importation</em>{% endif %} {% if step.data == "imported_date" %}<em>dernière importation</em>{% if event.imported_by_user %} par {{ event.imported_by_user.username }}{% endif %}{% endif %}
</div> </div>
</div> </div>
{% else %} {% else %}
@ -56,6 +56,11 @@
</div> </div>
{% endif %} {% endif %}
{% endfor %} {% endfor %}
<form method="post">{% csrf_token %}
{{ form.media }}
{{ form.as_p }}
<input type="submit" value="Commenter">
</form>
{% include "agenda_culturel/event-info-inc.html" with allbutdates=1 %} {% include "agenda_culturel/event-info-inc.html" with allbutdates=1 %}
</article> </article>

View File

@ -1,6 +1,6 @@
from django.shortcuts import render, get_object_or_404 from django.shortcuts import render, get_object_or_404
from django.views.generic import ListView, DetailView from django.views.generic import ListView, DetailView
from django.views.generic.edit import CreateView, UpdateView, DeleteView from django.views.generic.edit import CreateView, UpdateView, DeleteView, ModelFormMixin
from django.contrib.auth.mixins import ( from django.contrib.auth.mixins import (
LoginRequiredMixin, LoginRequiredMixin,
UserPassesTestMixin, UserPassesTestMixin,
@ -44,6 +44,7 @@ from .forms import (
TagForm, TagForm,
TagRenameForm, TagRenameForm,
MessageForm, MessageForm,
MessageEventForm,
) )
from .filters import ( from .filters import (
@ -469,8 +470,9 @@ class EventDeleteView(
success_message = _("The event has been successfully deleted.") success_message = _("The event has been successfully deleted.")
class EventDetailView(UserPassesTestMixin, DetailView): class EventDetailView(UserPassesTestMixin, DetailView, ModelFormMixin):
model = Event model = Event
form_class = MessageEventForm
template_name = "agenda_culturel/page-event.html" template_name = "agenda_culturel/page-event.html"
queryset = Event.objects.select_related("exact_location").select_related("category").select_related("other_versions").select_related("other_versions__representative").prefetch_related("message_set") queryset = Event.objects.select_related("exact_location").select_related("category").select_related("other_versions").select_related("other_versions__representative").prefetch_related("message_set")
@ -490,6 +492,32 @@ class EventDetailView(UserPassesTestMixin, DetailView):
obj.set_current_date(date(y, m, d)) obj.set_current_date(date(y, m, d))
return obj return obj
def get_success_url(self):
return self.get_object().get_absolute_url() + "#chronology"
def post(self, request, *args, **kwargs):
if not request.user.is_authenticated:
return HttpResponseForbidden()
form = self.get_form()
if form.is_valid():
return self.form_valid(form)
else:
return self.form_invalid(form)
def form_valid(self, form):
logger.warning("on form valide")
message = form.save(commit=False)
message.user = self.request.user
message.related_event = self.get_object()
message.subject = _("Comment")
message.spam = False
message.closed = True
message.save()
logger.warning("on save " + str(message))
return super().form_valid(form)
@login_required(login_url="/accounts/login/") @login_required(login_url="/accounts/login/")