diff --git a/.idea/sonarlint.xml b/.idea/sonarlint.xml new file mode 100644 index 0000000..5b670f2 --- /dev/null +++ b/.idea/sonarlint.xml @@ -0,0 +1,6 @@ + + + + + \ No newline at end of file diff --git a/src/party/forms.py b/src/party/forms.py index 588bfce..553845f 100644 --- a/src/party/forms.py +++ b/src/party/forms.py @@ -1,5 +1,6 @@ from django import forms from django.core.exceptions import ValidationError +from django.db.models import Q from character.models import Character from party.models import Party @@ -9,9 +10,16 @@ class PartyForm(forms.ModelForm): def __init__(self, *args, **kwargs): self.original_instance = kwargs.get("instance") super().__init__(*args, **kwargs) - qs = Character.objects.filter(private=False) + qs = Character.objects.all() if self.original_instance: - qs = qs.union(self.original_instance.invited_characters.all()) + qs = qs.filter( + Q(private=False) + | Q( + pk__in=self.original_instance.invited_characters.all().values_list( + "pk", flat=True + ) + ) + ) self.fields["invited_characters"].queryset = qs class Meta: diff --git a/src/party/templates/party/parties_list.html b/src/party/templates/party/parties_list.html index 3ffc4c6..c1921bd 100644 --- a/src/party/templates/party/parties_list.html +++ b/src/party/templates/party/parties_list.html @@ -12,19 +12,19 @@
{% for party in managed_parties %}
-
+
{{ party.name }}
diff --git a/src/party/tests/test_interactions.py b/src/party/tests/test_interactions.py new file mode 100644 index 0000000..c891e9c --- /dev/null +++ b/src/party/tests/test_interactions.py @@ -0,0 +1,39 @@ +import pytest +from django.core.management import call_command +from django.urls import reverse +from model_bakery import baker +from pytest_django.live_server_helper import LiveServer +from selenium.webdriver.common.by import By +from selenium.webdriver.firefox.webdriver import WebDriver +from selenium.webdriver.support.select import Select + +from character.models import Character +from character.tests.test_interactions import login +from common.models import User +from party.models import Party + + +@pytest.mark.django_db +def test_add_character_to_existing_group(selenium: WebDriver, live_server: LiveServer): + call_command("loaddata", "initial_data") + + username, password = "gm", "password" + gm = User.objects.create_user(username, password=password) + player = User.objects.create_user("player") + character = baker.make(Character, player=player) + party = baker.make(Party, game_master=gm) + + selenium.get(live_server.url) + login(selenium, username, password) + + selenium.get(live_server.url + reverse("party:list")) + selenium.find_element( + By.CSS_SELECTOR, f".party[data-id='{party.pk}'] .edit" + ).click() + invited = Select(selenium.find_element(By.ID, "id_invited_characters")) + invited.select_by_index(0) + selenium.find_element(By.CSS_SELECTOR, "[type=submit]").click() + + assert selenium.current_url == live_server.url + reverse("party:list") + party.refresh_from_db() + assert set(party.invited_characters.all()) == {character}