From 3dbb10235bd4d1faccb3123ae3063bcaca5b8c14 Mon Sep 17 00:00:00 2001 From: bbonf Date: Tue, 28 Nov 2023 14:29:49 +0100 Subject: [PATCH] fixed participant comment form --- lab/comments/forms.py | 6 ++---- lab/comments/views.py | 16 ++++++++++++++-- lab/integration_tests/conftest.py | 3 ++- lab/integration_tests/test_participants.py | 12 ++++++++++++ 4 files changed, 30 insertions(+), 7 deletions(-) create mode 100644 lab/integration_tests/test_participants.py diff --git a/lab/comments/forms.py b/lab/comments/forms.py index fd9ece66..7d21abc8 100644 --- a/lab/comments/forms.py +++ b/lab/comments/forms.py @@ -1,15 +1,13 @@ from django import forms -from experiments.models import Experiment -from participants.models import Participant from .models import Comment class CommentForm(forms.ModelForm): class Meta: model = Comment - fields = ['participant', 'comment'] + fields = ["participant", "comment"] def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) - self.fields['participant'].widget = forms.HiddenInput() + self.fields["participant"].widget = forms.HiddenInput() diff --git a/lab/comments/views.py b/lab/comments/views.py index c0ed4772..bbe617af 100644 --- a/lab/comments/views.py +++ b/lab/comments/views.py @@ -6,6 +6,7 @@ from django.views import generic from main.auth.util import RandomLeaderMixin +from participants.models import Participant from participants.permissions import can_leader_access_participant from .forms import CommentForm @@ -15,13 +16,24 @@ class CommentCreateView(RandomLeaderMixin, SuccessMessageMixin, generic.CreateView): template_name = "comments/new.html" form_class = CommentForm + model = Comment success_message = _("comments:messages:created") + @property + def participant(self): + pk = int(self.request.POST["participant"]) + return Participant.objects.get(pk=pk) + def test_func(self, user): - return can_leader_access_participant(user, self.object.participant) + return can_leader_access_participant(user, self.participant) def get_success_url(self): - return reverse("participants:detail", args=(self.object.participant.pk,)) + return reverse("participants:detail", args=(self.participant.pk,)) + + def form_valid(self, form): + # force the leader field to match current user + form.instance.leader = self.request.user + return super().form_valid(form) class CommentsDeleteView(braces.UserPassesTestMixin, DeleteSuccessMessageMixin, generic.DeleteView): diff --git a/lab/integration_tests/conftest.py b/lab/integration_tests/conftest.py index e9427f9c..a1e11586 100644 --- a/lab/integration_tests/conftest.py +++ b/lab/integration_tests/conftest.py @@ -24,7 +24,7 @@ def as_admin(sb, admin_user, live_server): def as_leader(sb, django_user_model, live_server): username = "test_user" password = "test_user" - user = django_user_model.objects.create_user(username=username, password=password) + user = django_user_model.objects.create_user(username=username, password=password, name='Test Leader') sb.open(live_server.url + "/login") # sb.click('#djHideToolBarButton') sb.type("#id_username", username) @@ -46,6 +46,7 @@ def sample_participant(db): participant = Participant.objects.create( email="baby@baby.com", name="Baby McBaby", + sex=Participant.Sex.UNKNOWN, parent_first_name="Parent", parent_last_name="McParent", birth_date=date(2020, 1, 1), diff --git a/lab/integration_tests/test_participants.py b/lab/integration_tests/test_participants.py new file mode 100644 index 00000000..96534d70 --- /dev/null +++ b/lab/integration_tests/test_participants.py @@ -0,0 +1,12 @@ +def test_participant_add_comment(sb, live_server, as_leader, sample_experiment, sample_participant): + sample_experiment.leaders.add(as_leader) + sb.open(live_server.url + "/participants") + sb.click(f"a:contains('{sample_participant.name}')") + sb.click("a:contains('new comment')") + sb.click("textarea") + sb.type("textarea", "hello this is a comment") + sb.click("button:contains(Send)") + + sb.assert_text_not_visible("button:contains(Send)") + sb.assert_text_visible("hello this is a comment") + sb.assert_text(as_leader.name, "#participant-comments") \ No newline at end of file