Skip to content

Commit

Permalink
Show related contacts in the POI form
Browse files Browse the repository at this point in the history
Co-authored-by: David Venhoff <[email protected]>
Co-authored-by: jarlhengstmengel <[email protected]>
  • Loading branch information
3 people committed Nov 30, 2024
1 parent 8490250 commit bca251a
Show file tree
Hide file tree
Showing 6 changed files with 172 additions and 5 deletions.
16 changes: 16 additions & 0 deletions integreat_cms/cms/models/contact/contact.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from django.db.utils import DataError
from django.utils import timezone
from django.utils.functional import cached_property
from django.utils.translation import gettext
from django.utils.translation import gettext_lazy as _

from ..abstract_base_model import AbstractBaseModel
Expand Down Expand Up @@ -91,6 +92,21 @@ def get_additional_attribute(self) -> str:

return ""

def label_in_reference_list(self) -> str:
"""
This function returns a display name of this contact for the poi contacts list
"""

label = (
_("General contact information")
if not self.point_of_contact_for
else f"{self.point_of_contact_for} {self.name}"
)
if self.archived:
label += " (⚠ " + gettext("Archived") + ")"

return label

def get_repr(self) -> str:
"""
This overwrites the default Django ``__repr__()`` method which would return ``<Contact: Contact object (id)>``.
Expand Down
3 changes: 3 additions & 0 deletions integreat_cms/cms/templates/pois/poi_form.html
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,9 @@ <h3 class="font-bold text-black heading">
<div id="right-sidebar-column"
class="flex flex-col flex-wrap 3xl:col-end-3 4xl:col-end-auto md:w-full">
{% include "./poi_form_sidebar/contact_box.html" with box_id="poi-contact" %}
{% if poi_form.instance.id and perms.cms.view_contact %}
{% include "./poi_form_sidebar/related_contacts_box.html" with box_id="poi-related-contacts" %}
{% endif %}
{% include "./poi_form_sidebar/opening_hours_box.html" with box_id="poi-opening-hours" no_padding=True %}
{% include "./poi_form_sidebar/category_box.html" with box_id="poi-category" %}
{% include "./poi_form_sidebar/icon_box.html" with box_id="poi-icon" %}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
{% for contact in poi_form.instance.contacts.all %}
<a href="{% url 'edit_contact' contact_id=contact.id region_slug=request.region.slug %}"
class="block pt-2 hover:underline">
<i icon-name="pen-square" class="mr-2"></i> {{ contact.point_of_contact_for }} {{ contact.name }}
<i icon-name="pen-square" class="mr-2"></i> {{ contact.label_in_reference_list }}
</a>
{% endfor %}
</div>
Expand Down Expand Up @@ -130,7 +130,7 @@
{% for contact in poi_form.instance.contacts.all %}
<a href="{% url 'edit_contact' contact_id=contact.id region_slug=request.region.slug %}"
class="block pt-2 hover:underline">
<i icon-name="pen-square" class="mr-2"></i> {{ contact.point_of_contact_for }} {{ contact.name }}
<i icon-name="pen-square" class="mr-2"></i> {{ contact.label_in_reference_list }}
</a>
{% endfor %}
</div>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{% extends "../../_collapsible_box.html" %}
{% load i18n %}
{% load widget_tweaks %}
{% block collapsible_box_icon %}
message-square
{% endblock collapsible_box_icon %}
{% block collapsible_box_title %}
{% trans "Related contacts" %}
{% endblock collapsible_box_title %}
{% block collapsible_box_content %}
{% with poi_form.instance.contacts.all as contacts %}
<div>
<div class="help-text">
{% if contacts %}
{% trans "This location is currently referred to by the following contacts." %}
{% else %}
{% trans "This location is not currently referred to in any contact." %}
{% endif %}
</div>
{% for contact in contacts %}
<a href="{% url 'edit_contact' contact_id=contact.id region_slug=request.region.slug %}"
class="block pt-2 hover:underline">
<i icon-name="pen-square" class="mr-2"></i> {{ contact.label_in_reference_list }}
</a>
{% endfor %}
</div>
{% endwith %}
{% endblock collapsible_box_content %}
22 changes: 19 additions & 3 deletions integreat_cms/locale/de/LC_MESSAGES/django.po
Original file line number Diff line number Diff line change
Expand Up @@ -1680,9 +1680,9 @@ msgstr "Aktiv"
msgid "Hidden"
msgstr "Versteckt"

#: cms/constants/region_status.py cms/models/pages/page_translation.py
#: cms/models/regions/region.py cms/templates/events/event_form.html
#: cms/templates/pages/page_form.html
#: cms/constants/region_status.py cms/models/contact/contact.py
#: cms/models/pages/page_translation.py cms/models/regions/region.py
#: cms/templates/events/event_form.html cms/templates/pages/page_form.html
#: cms/templates/pages/page_tree_archived_node.html
#: cms/templates/pois/poi_form.html
msgid "Archived"
Expand Down Expand Up @@ -2812,6 +2812,10 @@ msgstr "mit Telefonnummer: {}"
msgid "with website: {}"
msgstr "mit Website: {}"

#: cms/models/contact/contact.py
msgid "General contact information"
msgstr "Allgemeine Kontaktinformationen"

#: cms/models/contact/contact.py
msgid "(Copy)"
msgstr "(Kopie)"
Expand Down Expand Up @@ -7760,6 +7764,18 @@ msgstr "Nur Koordinaten übernehmen"
msgid "Please choose one option or cancel"
msgstr "Bitte eine Option auswählen oder abbrechen"

#: cms/templates/pois/poi_form_sidebar/related_contacts_box.html
msgid "Related contacts"
msgstr "Zugehörige Kontakte"

#: cms/templates/pois/poi_form_sidebar/related_contacts_box.html
msgid "This location is currently referred to by the following contacts."
msgstr "Dieser Ort wird in folgenden Kontakten verwendet."

#: cms/templates/pois/poi_form_sidebar/related_contacts_box.html
msgid "This location is not currently referred to in any contact."
msgstr "Zur Zeit gibt es keine Kontakte zu diesem Ort."

#: cms/templates/pois/poi_list.html cms/templates/pois/poi_list_archived.html
msgid "Archived locations"
msgstr "Archivierte Orte"
Expand Down
104 changes: 104 additions & 0 deletions tests/cms/views/poi/test_poi_form.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,3 +218,107 @@ def test_poi_in_use_not_bulk_archived(

# Check the POI is not archived
assert not POI.objects.filter(id=poi_id).first().archived


@pytest.mark.django_db
def test_poi_form_shows_associated_contacts(
load_test_data: None,
login_role_user: tuple[Client, str],
settings: SettingsWrapper,
) -> None:
"""
POI "Draft location" (id=6) has three related contacts. Test whether they are shown in the POI form.
"""
client, role = login_role_user

# Choose a POI which has related contacts
POI_ID = 6

# Set the language setting to English so assertion does not fail because of corresponding German sentence appearing instead the english one.
settings.LANGUAGE_CODE = "en"

poi = POI.objects.filter(id=POI_ID).first()
related_contacts = list(poi.contacts.all())

assert len(related_contacts) > 0

edit_poi = reverse(
"edit_poi",
kwargs={
"poi_id": poi.id,
"region_slug": poi.region.slug,
"language_slug": poi.region.default_language.slug,
},
)
response = client.get(edit_poi)

if role == ANONYMOUS:
assert response.status_code == 302
assert (
response.headers.get("location") == f"{settings.LOGIN_URL}?next={edit_poi}"
)
# probably needs adjustment after #2958
elif role in HIGH_PRIV_STAFF_ROLES:
for contact in related_contacts:
if contact.point_of_contact_for:
assert (
f"{contact.point_of_contact_for} {contact.name}"
in response.content.decode("utf-8")
)
else:
assert "General contact information" in response.content.decode("utf-8")
else:
assert (
"This location is currently referred to in those contacts."
not in response.content.decode("utf-8")
)


@pytest.mark.django_db
def test_poi_form_shows_no_associated_contacts(
load_test_data: None,
login_role_user: tuple[Client, str],
settings: SettingsWrapper,
) -> None:
"""
POI "Test location" (id=4) has no related contacts. Test whether the correct message is shown in the POi form.
"""
client, role = login_role_user

# Choose a POI which has related contacts
POI_ID = 4

# Set the language setting to English so assertion does not fail because of corresponding German sentence appearing instead the english one.
settings.LANGUAGE_CODE = "en"

poi = POI.objects.filter(id=POI_ID).first()
related_contacts = list(poi.contacts.all())

assert len(related_contacts) == 0

edit_poi = reverse(
"edit_poi",
kwargs={
"poi_id": poi.id,
"region_slug": poi.region.slug,
"language_slug": poi.region.default_language.slug,
},
)
response = client.get(edit_poi)

if role == ANONYMOUS:
assert response.status_code == 302
assert (
response.headers.get("location") == f"{settings.LOGIN_URL}?next={edit_poi}"
)
# probably needs adjustment after #2958
elif role in HIGH_PRIV_STAFF_ROLES:
assert (
"This location is not currently referred to in any contact."
in response.content.decode("utf-8")
)
else:
assert (
"This location is not currently referred to in any contact."
not in response.content.decode("utf-8")
)

0 comments on commit bca251a

Please sign in to comment.