Skip to content

Commit

Permalink
Various fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
charludo committed Dec 12, 2024
1 parent 77707d7 commit 1e51ce7
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 132 deletions.
85 changes: 18 additions & 67 deletions integreat_cms/cms/models/contact/contact.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from __future__ import annotations

from typing import TYPE_CHECKING
from typing import Generator, TYPE_CHECKING

from django.conf import settings
from django.contrib.postgres.search import SearchQuery, SearchRank, SearchVector
Expand All @@ -23,6 +23,8 @@
if TYPE_CHECKING:
from django.db.models.query import QuerySet

from ..abstract_content_translation import AbstractContentTranslation


class Contact(AbstractBaseModel):
"""
Expand Down Expand Up @@ -182,7 +184,7 @@ def referring_page_translations(self) -> QuerySet[PageTranslation]:
Link.objects.filter(
url__url=self.full_url,
content_type=PageTranslationLinklist.content_type(),
).values_list("object_id", flat=True)
).values("object_id")
),
)

Expand All @@ -200,7 +202,7 @@ def referring_poi_translations(self) -> QuerySet[POITranslation]:
Link.objects.filter(
url__url=self.full_url,
content_type=POITranslationLinklist.content_type(),
).values_list("object_id", flat=True)
).values("object_id")
),
)

Expand All @@ -218,10 +220,21 @@ def referring_event_translations(self) -> QuerySet[EventTranslation]:
Link.objects.filter(
url__url=self.full_url,
content_type=EventTranslationLinklist.content_type(),
).values_list("object_id", flat=True)
).values("object_id")
),
)

@cached_property
def referring_objects(self) -> Generator[AbstractContentTranslation]:
"""
Returns a list of all objects linking to this contact.
:return: all objects referring to this contact
"""
return (
link.content_object for link in Link.objects.filter(url__url=self.full_url)
)

def archive(self) -> None:
"""
Archives the contact
Expand All @@ -244,76 +257,14 @@ def copy(self) -> None:
self.point_of_contact_for = self.point_of_contact_for + " " + _("(Copy)")
self.save()

@cached_property
def url_prefix(self) -> str:
"""
Generates the prefix of the url of the contact
For information about the components of such an url,
see :meth:`~integreat_cms.cms.models.contact.contact.Contact.get_absolute_url`
:return: The prefix to the url
"""
return "/" + "/".join(
filter(
None,
[
self.location.region.slug,
self.url_infix,
],
)
)

@cached_property
def url_infix(self) -> str:
"""
Generates the infix of the url of the contact
For information about the components of such an url,
see :meth:`~integreat_cms.cms.models.contact.contact.Contact.get_absolute_url`
"""
return "contact/"

@cached_property
def base_link(self) -> str:
"""
Generates the base link which is the whole url without id
For information about the components of such an url,
see :meth:`~integreat_cms.cms.models.contact.contact.Contact.get_absolute_url`
:return: the base link of the content
"""
if not self.id:
return settings.BASE_URL + "/"
return settings.BASE_URL + self.url_prefix

def get_absolute_url(self) -> str:
"""
Generates the absolute url of the contact
Here is an example for demonstrating the components::
https://integreat.app/augsburg/contact/42/
|----------------------------------------| full_url
|-------------------| get_absolute_url()
|-------------------------------------| base_link
|----------------| url_prefix
|-------| url_infix
|--| id
:return: The absolute url
"""
return self.url_prefix + str(self.id) + "/"

@cached_property
def full_url(self) -> str:
"""
This property returns the full url of this contact
:return: The full url
"""
return settings.BASE_URL + self.get_absolute_url()
return f"{settings.BASE_URL}/{self.location.region.slug}/contact/{self.id}/"

class Meta:
verbose_name = _("contact")
Expand Down
77 changes: 23 additions & 54 deletions integreat_cms/cms/templates/contacts/contact_card.html
Original file line number Diff line number Diff line change
@@ -1,89 +1,58 @@
{% load settings_tags %}
{% load svg_tags %}
{% load static %}
{% spaceless %}
<div contenteditable="false"
data-contact-id="{{ contact.pk }}"
data-contact-url="{{ contact.full_url }}"
class="contact-card">
class="contact-card inline-block box-border p-4 rounded bg-no-repeat cursor-default text-[initial] shadow-md shadow-black decoration-[initial] min-w-[50%]">
<a href={{ contact.full_url }} class="marker-link">Contact</a>
{% if contact %}
{% if contact.location %}
{% if contact.name or contact.point_of_contact_for %}
<h4>
{% if contact.name and contact.name.strip %}
<span class="notranslate" dir="ltr" translate="no">{{ contact.name.strip }}</span>
{% if contact.name %}
<span class="notranslate" dir="ltr" translate="no">{{ contact.name }}</span>
{% endif %}
{% if contact.name and contact.point_of_contact_for %}
|
{% endif %}
{% if contact.point_of_contact_for and contact.point_of_contact_for.strip %}
{{ contact.point_of_contact_for.strip }}
{% if contact.point_of_contact_for %}
{{ contact.point_of_contact_for }}
{% endif %}
</h4>
{% endif %}
{% if contact.location %}
<p>
<picture>
<source srcset="{% get_base_url %}{% static "svg/pin.svg" %}" type="image/svg+xml">
<img src="{% get_static_prefix %}svg/pin.svg"
alt="Address: "
width="15"
height="15" />
</picture>
&nbsp;
<a href="{{ contact.location.map_url }}">
<span class="notranslate" dir="ltr" translate="no">
{{ contact.location.short_address }}
</span>
</a>
<img src="{% get_svg_icon "pin" %}" alt="Address: " width="15" height="15" />
&nbsp; <a href="{{ contact.location.map_url }}"> <span class="notranslate" dir="ltr" translate="no"> {{ contact.location.short_address }} </span> </a>
</p>
{% endif %}
{% if contact.email and contact.email.strip %}
{% if contact.email %}
<p>
<picture>
<source srcset="{% get_base_url %}{% static "svg/email.svg" %}" type="image/svg+xml">
<img src="{% get_static_prefix %}svg/email.svg"
alt="Email: "
width="15"
height="15" />
</picture>
<img src="{% get_svg_icon "email" %}" alt="Email: " width="15" height="15" />
&nbsp;
<a href="mailto:{{ contact.email.strip }}">
<a href="mailto:{{ contact.email }}">
<span class="notranslate" dir="ltr" translate="no">
{{ contact.email.strip }}
{{ contact.email }}
</span>
</a>
</p>
{% endif %}
{% if contact.phone_number and contact.phone_number.strip %}
{% if contact.phone_number %}
<p>
<picture>
<source srcset="{% get_base_url %}{% static "svg/call.svg" %}" type="image/svg+xml">
<img src="{% get_static_prefix %}svg/call.svg"
alt="Phone Number: "
width="15"
height="15" />
</picture>
&nbsp;
<a href="tel:{{ contact.phone_number.strip }}">
<span class="notranslate" dir="ltr" translate="no">
{{ contact.phone_number.strip }}
</span>
</a>
<img src="{% get_svg_icon "call" %}"
alt="Phone Number: "
width="15"
height="15" />
&nbsp; <a href="tel:{{ contact.phone_number }}"> <span class="notranslate" dir="ltr" translate="no"> {{ contact.phone_number }} </span> </a>
</p>
{% endif %}
{% if contact.website and contact.website.strip %}
{% if contact.website %}
<p>
<picture>
<source srcset="{% get_base_url %}{% static "svg/www.svg" %}" type="image/svg+xml">
<img src="{% get_static_prefix %}svg/www.svg"
alt="Website: "
width="15"
height="15" />
</picture>
<img src="{% get_svg_icon "www" %}" alt="Website: " width="15" height="15" />
&nbsp;
<a href="{{ contact.website.strip }}">
<a href="{{ contact.website }}">
<span class="notranslate" dir="ltr" translate="no">
{{ contact.website.strip }}
{{ contact.website }}
</span>
</a>
</p>
Expand Down
23 changes: 23 additions & 0 deletions integreat_cms/cms/templatetags/svg_tags.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
"""
Contains a collection of tags for working with svg icons.
"""

from __future__ import annotations

from django import template

from ..utils.tinymce_icon_utils import get_icon_url

register = template.Library()


@register.simple_tag
def get_svg_icon(svg: str) -> str:
"""
This tags inserts the link to an svg icon
:param svg: The svg icon to insert
:return: The full url of the svg
"""
return get_icon_url(svg)
2 changes: 1 addition & 1 deletion integreat_cms/cms/utils/content_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ def render_contact_card(contact_id: int) -> HtmlElement:
return fromstring(raw_element)
except Contact.DoesNotExist:
logger.warning("Contact with id=%i does not exist!", contact_id)
return Element("p", contact_id)
return Element("p")
except LxmlError as e:
logger.debug(
"Failed to parse rendered HTML for contact card: %r\n→ %s\nEOF",
Expand Down
7 changes: 4 additions & 3 deletions integreat_cms/cms/views/utils/contact_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@
from django.shortcuts import get_object_or_404, render
from django.views.decorators.http import require_POST

from ...models import (
Contact,
)
from ...decorators import permission_required
from ...models import Contact

if TYPE_CHECKING:
from typing import Any, Literal
Expand Down Expand Up @@ -64,6 +63,7 @@ def search_contact_ajax(
)


@permission_required("cms.view_contact")
def get_contact(
request: HttpRequest, contact_id: int, region_slug: str | None = None
) -> str:
Expand All @@ -80,6 +80,7 @@ def get_contact(
return render(request, "contacts/contact_card.html", {"contact": contact})


@permission_required("cms.view_contact")
def get_contact_raw(
request: HttpRequest, contact_id: int, region_slug: str | None = None
) -> str:
Expand Down
7 changes: 1 addition & 6 deletions integreat_cms/core/signals/contact_signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,7 @@ def contact_save_handler(instance: Contact, **kwargs: Any) -> None:
:param instance: The page translation that gets saved
:param \**kwargs: The supplied keyword arguments
"""
referring_objects = (
list(instance.referring_page_translations)
+ list(instance.referring_poi_translations)
+ list(instance.referring_event_translations)
)
for referrer in referring_objects:
for referrer in instance.referring_objects:
if getattr(referrer.foreign_object, "archived", False):
continue
logger.debug("Updating %r, since it references %r.", referrer, instance)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,11 +153,12 @@ import { getCsrfToken } from "../../utils/csrf-token";
return editor.windowManager.open(dialogConfig);
};

editor.addShortcut("Meta+P", tinymceConfig.getAttribute("data-contact-menu-text"), openDialog);
editor.addShortcut("Meta+L", tinymceConfig.getAttribute("data-contact-menu-text"), openDialog);

editor.ui.registry.addMenuItem("add_contact", {
text: tinymceConfig.getAttribute("data-contact-menu-text"),
icon: "contact",
shortcut: "Meta+L",
onAction: openDialog,
});

Expand Down

0 comments on commit 1e51ce7

Please sign in to comment.