diff --git a/src/agenda_culturel/forms.py b/src/agenda_culturel/forms.py index 4570aaf..f787150 100644 --- a/src/agenda_culturel/forms.py +++ b/src/agenda_culturel/forms.py @@ -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") diff --git a/src/agenda_culturel/migrations/0126_message_user.py b/src/agenda_culturel/migrations/0126_message_user.py new file mode 100644 index 0000000..42caa87 --- /dev/null +++ b/src/agenda_culturel/migrations/0126_message_user.py @@ -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'), + ), + ] diff --git a/src/agenda_culturel/models.py b/src/agenda_culturel/models.py index b404527..65eea51 100644 --- a/src/agenda_culturel/models.py +++ b/src/agenda_culturel/models.py @@ -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"), diff --git a/src/agenda_culturel/templates/agenda_culturel/message_moderation_form.html b/src/agenda_culturel/templates/agenda_culturel/message_moderation_form.html index 6e2d656..9ed886a 100644 --- a/src/agenda_culturel/templates/agenda_culturel/message_moderation_form.html +++ b/src/agenda_culturel/templates/agenda_culturel/message_moderation_form.html @@ -30,7 +30,7 @@

Modération du message « {{ object.subject }} »

diff --git a/src/agenda_culturel/templates/agenda_culturel/messages.html b/src/agenda_culturel/templates/agenda_culturel/messages.html index b9a587b..257fcbf 100644 --- a/src/agenda_culturel/templates/agenda_culturel/messages.html +++ b/src/agenda_culturel/templates/agenda_culturel/messages.html @@ -46,7 +46,7 @@ {{ obj.date }} {{ obj.subject }} - {{ obj.name }} + {% if obj.user %}{{ obj.user }}{% else %}{{ obj.name }}{% endif %} {% if obj.related_event %}{{ obj.related_event.pk }}{% else %}/{% endif %} {% if obj.closed %}{% picto_from_name "check-square" "fermé" %}{% else %}{% picto_from_name "square" "ouvert" %}{% endif %} {% if obj.spam %}{% picto_from_name "check-square" "spam" %}{% else %}{% picto_from_name "square" "non spam" %}{% endif %} diff --git a/src/agenda_culturel/views.py b/src/agenda_culturel/views.py index 3cf3c37..8ef955c 100644 --- a/src/agenda_culturel/views.py +++ b/src/agenda_culturel/views.py @@ -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: