Skip to content

Commit

Permalink
fixed participant comment form
Browse files Browse the repository at this point in the history
  • Loading branch information
bbonf committed Nov 28, 2023
1 parent 0376cd2 commit 3dbb102
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 7 deletions.
6 changes: 2 additions & 4 deletions lab/comments/forms.py
Original file line number Diff line number Diff line change
@@ -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()
16 changes: 14 additions & 2 deletions lab/comments/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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):
Expand Down
3 changes: 2 additions & 1 deletion lab/integration_tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -46,6 +46,7 @@ def sample_participant(db):
participant = Participant.objects.create(
email="[email protected]",
name="Baby McBaby",
sex=Participant.Sex.UNKNOWN,
parent_first_name="Parent",
parent_last_name="McParent",
birth_date=date(2020, 1, 1),
Expand Down
12 changes: 12 additions & 0 deletions lab/integration_tests/test_participants.py
Original file line number Diff line number Diff line change
@@ -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")

0 comments on commit 3dbb102

Please sign in to comment.