Les personnes connextées ont un formulaire simplifié

This commit is contained in:
Jean-Marie Favreau 2024-12-11 13:50:46 +01:00
parent c1f7bfd8c4
commit a94b9a53f2
6 changed files with 43 additions and 3 deletions

View File

@ -788,9 +788,13 @@ class MessageForm(ModelForm):
class Meta:
model = Message
fields = ["subject", "name", "email", "message", "related_event"]
widgets = {"related_event": HiddenInput()}
widgets = {"related_event": HiddenInput(), "user": HiddenInput() }
def __init__(self, *args, **kwargs):
self.event = kwargs.pop("event", False)
self.internal = kwargs.pop("internal", False)
super().__init__(*args, **kwargs)
self.fields['related_event'].required = False
if self.internal:
self.fields.pop("name")
self.fields.pop("email")

View File

@ -0,0 +1,21 @@
# Generated by Django 4.2.9 on 2024-12-11 11:56
from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
('agenda_culturel', '0125_rename_contactmessage_message_alter_message_options'),
]
operations = [
migrations.AddField(
model_name='message',
name='user',
field=models.ForeignKey(default=None, null=True, on_delete=django.db.models.deletion.SET_DEFAULT, to=settings.AUTH_USER_MODEL, verbose_name='Author of the message'),
),
]

View File

@ -1761,6 +1761,14 @@ class Message(models.Model):
on_delete=models.SET_DEFAULT,
)
user = models.ForeignKey(
User,
verbose_name=_("Author of the message"),
null=True,
default=None,
on_delete=models.SET_DEFAULT,
)
name = models.CharField(
verbose_name=_("Name"),
help_text=_("Your name"),

View File

@ -30,7 +30,7 @@
<h1>Modération du message «&nbsp;{{ object.subject }}&nbsp;»</h1>
<ul>
<li>Date&nbsp;: {{ object.date.date }} à {{ object.date.time }}</li>
<li>Auteur&nbsp;: {{ object.name }} {% if object.email %}<a href="mailto:{{ object.email }}">{{ object.email }}</a>{% endif %}</li>
<li>Auteur&nbsp;: {% if object.user %}<em>{{ object.user }}</em>{% else %}{{ object.name }}{% endif %} {% if object.email %}<a href="mailto:{{ object.email }}">{{ object.email }}</a>{% endif %}</li>
{% if object.related_event %}<li>Événement associé&nbsp;: <a href="{{ object.related_event.get_absolute_url }}">{{ object.related_event.title }}</a> du {{ object.related_event.start_day }}</li>{% endif %}
</ul>
</header>

View File

@ -46,7 +46,7 @@
<tr>
<td>{{ obj.date }}</td>
<td><a href="{% url 'message' obj.pk %}">{{ obj.subject }}</a></td>
<td>{{ obj.name }}</td>
<td>{% if obj.user %}<em>{{ obj.user }}</em>{% else %}{{ obj.name }}{% endif %}</td>
<td>{% if obj.related_event %}<a href="{{ obj.related_event.get_absolute_url }}">{{ obj.related_event.pk }}</a>{% else %}/{% endif %}</td>
<td>{% if obj.closed %}{% picto_from_name "check-square" "fermé" %}{% else %}{% picto_from_name "square" "ouvert" %}{% endif %}</td>
<td>{% if obj.spam %}{% picto_from_name "check-square" "spam" %}{% else %}{% picto_from_name "square" "non spam" %}{% endif %}</td>

View File

@ -765,8 +765,15 @@ class MessageCreateView(SuccessMessageMixin, CreateView):
def get_form_kwargs(self):
kwargs = super().get_form_kwargs()
kwargs["event"] = self.event
if self.request.user.is_authenticated:
kwargs["internal"] = True
return kwargs
def form_valid(self, form):
form.instance.user = self.request.user
return super().form_valid(form)
def get_initial(self):
result = super().get_initial()
if "pk" in self.kwargs: