Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Suggest users potential contact sources #3314

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
{% extends "_base.html" %}
{% load i18n %}
{% load static %}
{% block content %}
<div class="flex justify-between mb-4">
<h1 class="heading">
{% translate "Potential contact data" %}
</h1>
</div>
<div class="pb-4">
<span class="pb-4">
{% translate "Here is the list of E-mail addresses and phone numbers that are embedded in contents. We recommend to replace them with a contact card so they can be centrally managed and updated in future." %}
</span>
</div>
<div class="pb-4">
<div class="pb-4">
<h2 class="font-bold">
{% translate "Pages" %}
</h2>
{% if links_per_page %}
{{ links_per_page|length }}{% translate " pages have potential contacts." %}
{% endif %}
</div>
{% for content, links, contacts in links_per_page %}
{% include "contacts/contact_from_email_and_phone_row.html" with collapsed=True %}
{% empty %}
{% translate "No E-mail address and phone number detected" %}
{% endfor %}
</div>
<div class="pb-4">
<div class="pb-4">
<h2 class="font-bold">
{% translate "Events" %}
</h2>
{% if links_per_event %}
{{ links_per_event|length }}{% translate " events have potential contacts." %}
{% endif %}
</div>
{% for content, links, contacts in links_per_event %}
{% include "contacts/contact_from_email_and_phone_row.html" with collapsed=True %}
{% empty %}
{% translate "No E-mail address and phone number detected" %}
{% endfor %}
</div>
<div class="pb-4">
<div class="pb-4">
<h2 class="font-bold">
{% translate "Locations" %}
</h2>
{% if links_per_poi %}
{{ links_per_poi|length }}{% translate " events have potential contacts." %}
{% endif %}
</div>
{% for content, links, contacts in links_per_poi %}
{% include "contacts/contact_from_email_and_phone_row.html" with collapsed=True %}
{% empty %}
{% translate "No E-mail address and phone number detected" %}
{% endfor %}
</div>
{% include "pagination.html" with chunk=contacts %}
{% endblock content %}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{% extends "../_collapsible_box.html" %}
{% load i18n %}
{% load content_filters %}
{% load page_filters %}
{% load tree_filters %}
{% load static %}
{% load rules %}
{% block collapsible_box_icon %}
{% endblock collapsible_box_icon %}
{% block collapsible_box_title %}
{{ content.best_translation.title }}
<i icon-name="eye"></i> <a href="{{ content.backend_edit_link }}"><i icon-name="pencil"></i></a>
{% endblock collapsible_box_title %}
{% block collapsible_box_content %}
<div class="grid xl:grid-cols-2 gap-4">
<div>
{% for link in links %}
<div>
{{ link }}
</div>
{% endfor %}
</div>
<div>
{% for contact in contacts %}
<div>
{{ contact }}
</div>
{% empty %}
{% trans "There is no contact suggested." %}
{% endfor %}
</div>
</div>
{% endblock collapsible_box_content %}
4 changes: 4 additions & 0 deletions integreat_cms/cms/templates/contacts/contact_list.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ <h1 class="heading">
{% else %}
{% translate "Contacts" %}
{% endif %}
<a href="{% url 'potential_targets' region_slug=request.region.slug %}"
class="font-bold text-sm text-gray-800 flex items-center gap-1 mb-2 hover:underline">
<i icon-name="lightbulb" class="align-baseline"></i>{% trans "Analysis of content for existing contacts" %}
</a>
</h1>
{% if is_archive %}
<a href="{% url 'contacts' region_slug=request.region.slug %}"
Expand Down
5 changes: 5 additions & 0 deletions integreat_cms/cms/urls/protected.py
Original file line number Diff line number Diff line change
Expand Up @@ -1504,6 +1504,11 @@
]
),
),
path(
"potential_targets",
contacts.PotentialContactSourcesView.as_view(),
name="potential_targets",
),
]
),
),
Expand Down
19 changes: 19 additions & 0 deletions integreat_cms/cms/utils/link_utils.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
from __future__ import annotations

import re
from typing import TYPE_CHECKING
from urllib.parse import ParseResult, unquote, urlparse

if TYPE_CHECKING:
from linkcheck.models import Url


def fix_domain_encoding(url: re.Match[str]) -> str:
"""
Expand All @@ -24,3 +28,18 @@ def fix_content_link_encoding(content: str) -> str:
:return: The fixed content
"""
return re.sub(r"(?<=[\"'])(https?://.+?)(?=[\"'])", fix_domain_encoding, content)


def clean_url(url: Url) -> str:
"""
Remove the prefix tel: and mailto:

:param url: The URL object
:return: URL string without prefix
"""

if url.type == "phone":
return url.url.lstrip("tel:")
if url.type == "mailto":
return url.url[7:]
return url.url
1 change: 1 addition & 0 deletions integreat_cms/cms/views/contacts/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@
RestoreContactBulkAction,
)
from .contact_form_view import ContactFormView
from .contact_from_existing_data import PotentialContactSourcesView
from .contact_list_view import ContactListView
134 changes: 134 additions & 0 deletions integreat_cms/cms/views/contacts/contact_from_existing_data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
from __future__ import annotations

from typing import TYPE_CHECKING

from django.contrib.contenttypes.models import ContentType
from django.db.models import Q
from django.shortcuts import render
from django.views.generic.base import TemplateView

from integreat_cms.cms.utils.link_utils import clean_url

from ...models import Contact, Event, Page, POI
from ...utils.linkcheck_utils import get_region_links

if TYPE_CHECKING:
from typing import Any

from django.http import HttpRequest, HttpResponse
from django.template.response import TemplateResponse


class PotentialContactSourcesView(TemplateView):
# pylint: disable=too-many-locals
"""
View for the contact suggestion
"""

template_name = "contacts/contact_from_email_and_phone.html"
extra_context = {
"current_menu_item": "contacts",
}

def get(self, request: HttpRequest, *args: Any, **kwargs: Any) -> TemplateResponse:
r"""
Render the contact suggestion

:param request: Object representing the user call
:param \*args: The supplied arguments
:param \**kwargs: The supplied keyword arguments
:return: The rendered template response
"""

region_links = get_region_links(request.region)
email_or_phone_links = region_links.filter(
Q(url__url__startswith="mailto") | Q(url__url__startswith="tel")
)

page_translation_content_type_id = ContentType.objects.get(
model="pagetranslation"
).id
event_translation_content_type_id = ContentType.objects.get(
model="eventtranslation"
).id
poi_translation_content_type_id = ContentType.objects.get(
model="poitranslation"
).id

links_in_pages = email_or_phone_links.filter(
content_type=page_translation_content_type_id
)
links_in_events = email_or_phone_links.filter(
content_type=event_translation_content_type_id
)
links_in_pois = email_or_phone_links.filter(
content_type=poi_translation_content_type_id
)

page_ids = (
links_in_pages.order_by()
.values_list("page_translation__page", flat=True)
.distinct()
)
links_per_page = []
for page_id in page_ids:
page = Page.objects.filter(id=page_id).first()
links = [
clean_url(link.url)
for link in links_in_pages.filter(page_translation__page=page_id)
.order_by()
.distinct("url__url")
]
contacts = Contact.objects.filter(
Q(email__in=links) | Q(phone_number__in=links)
)
links_per_page += [(page, links, contacts)]

event_ids = (
links_in_events.order_by()
.values_list("event_translation__event", flat=True)
.distinct()
)
links_per_event = []
for event_id in event_ids:
event = Event.objects.filter(id=event_id).first()
links = [
clean_url(link.url)
for link in links_in_events.filter(event_translation__event=event_id)
.order_by()
.distinct("url__url")
]
contacts = Contact.objects.filter(
Q(email__in=links) | Q(phone_number__in=links)
)
links_per_event += [(event, links, contacts)]

poi_ids = (
links_in_pois.order_by()
.values_list("poi_translation__poi", flat=True)
.distinct()
)
links_per_poi = []
for poi_id in poi_ids:
poi = POI.objects.filter(id=poi_id).first()
links = [
clean_url(link.url)
for link in links_in_pois.filter(poi_translation__poi=poi_id)
.order_by()
.distinct("url__url")
]
contacts = Contact.objects.filter(
Q(email__in=links) | Q(phone_number__in=links)
)
links_per_poi += [(poi, links, contacts)]

return render(
request,
self.template_name,
{
**self.get_context_data(**kwargs),
"links_per_page": links_per_page,
"links_per_event": links_per_event,
"links_per_poi": links_per_poi,
},
)
43 changes: 40 additions & 3 deletions integreat_cms/locale/de/LC_MESSAGES/django.po
Original file line number Diff line number Diff line change
Expand Up @@ -3081,6 +3081,7 @@ msgstr "Impressums-Feedback"

#: cms/models/feedback/map_feedback.py cms/models/regions/region.py
#: cms/templates/_base.html cms/templates/contacts/contact_form.html
#: cms/templates/contacts/contact_from_email_and_phone.html
#: cms/templates/organizations/organization_form.html
#: cms/templates/pois/poi_list.html
msgid "Locations"
Expand Down Expand Up @@ -4221,12 +4222,14 @@ msgstr "verbrauchtes Budget"

#: cms/models/regions/region.py cms/templates/_base.html
#: cms/templates/contacts/contact_form.html
#: cms/templates/contacts/contact_from_email_and_phone.html
#: cms/templates/organizations/organization_form.html
msgid "Pages"
msgstr "Seiten"

#: cms/models/regions/region.py cms/templates/_base.html
#: cms/templates/contacts/contact_form.html
#: cms/templates/contacts/contact_from_email_and_phone.html
#: cms/templates/events/event_list.html
msgid "Events"
msgstr "Veranstaltungen"
Expand Down Expand Up @@ -5385,10 +5388,44 @@ msgstr "Kontakt wiederherstellen"
msgid "Delete contact"
msgstr "Kontakt löschen"

#: cms/templates/contacts/contact_from_email_and_phone.html
msgid "Potential contact data"
msgstr "Mögliche Kontaktdaten"

#: cms/templates/contacts/contact_from_email_and_phone.html
msgid ""
"Here is the list of E-mail addresses and phone numbers that are embedded in "
"contents. We recommend to replace them with a contact card so they can be "
"centrally managed and updated in future."
msgstr ""
"Hier ist die Liste der E-Mail-Adressen und Telefonnummern, die in Inhalte "
"eingebettet sind. Wir empfehlen, diese durch eine Kontaktkarte zu ersetzen, "
"damit sie künftig zentral verwaltet und aktualisiert werden können."

#: cms/templates/contacts/contact_from_email_and_phone.html
msgid " pages have potential contacts."
msgstr " Seiten haben mögliche Kontakte."

#: cms/templates/contacts/contact_from_email_and_phone.html
msgid "No E-mail address and phone number detected"
msgstr "Keine E-Mail-Ad­res­se und Te­le­fon­num­mer gefunden."

#: cms/templates/contacts/contact_from_email_and_phone.html
msgid " events have potential contacts."
msgstr " Veranstaltungen haben mögliche Kontakte."

#: cms/templates/contacts/contact_from_email_and_phone_row.html
msgid "There is no contact suggested."
msgstr "Es wird kein Kontakt vorgeschlagen."

#: cms/templates/contacts/contact_list.html
msgid "Archived Contacts"
msgstr "Archivierte Kontakte"

#: cms/templates/contacts/contact_list.html
msgid "Analysis of content for existing contacts"
msgstr "Inhaltsanalyse für bestehende Kontaktdaten"

#: cms/templates/contacts/contact_list.html
msgid "Back to contacts"
msgstr "Zurück zu Kontakte"
Expand Down Expand Up @@ -11188,6 +11225,9 @@ msgstr ""
#~ msgid "An error occurred while importing events from this external calendar"
#~ msgstr "Ein Fehler mit dem Import von Veranstaltungen ist aufgetreten"

#~ msgid "There is no page yet."
#~ msgstr "Es gibt keinen Seiteninhalt."

#~ msgid "Invalid links"
#~ msgstr "Ungültige Links"

Expand Down Expand Up @@ -11714,9 +11754,6 @@ msgstr ""
#~ msgid "Status message"
#~ msgstr "Status-Nachricht"

#~ msgid "There is no page yet."
#~ msgstr "Es gibt keinen Seiteninhalt."

#~ msgid "Current machine translation"
#~ msgstr "Die Übersetzung ist aktuell und wurde maschinell erzeugt"

Expand Down
Loading