Skip to content

Commit

Permalink
Prevent orphaned contact cards
Browse files Browse the repository at this point in the history
  • Loading branch information
charludo committed Jan 7, 2025
1 parent 45a2dd1 commit 78a40b8
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 30 deletions.
8 changes: 4 additions & 4 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 Generator, TYPE_CHECKING
from typing import List, TYPE_CHECKING

from django.conf import settings
from django.contrib.postgres.search import SearchQuery, SearchRank, SearchVector
Expand Down Expand Up @@ -225,15 +225,15 @@ def referring_event_translations(self) -> QuerySet[EventTranslation]:
)

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

def archive(self) -> None:
"""
Expand Down
54 changes: 35 additions & 19 deletions integreat_cms/cms/views/contacts/contact_actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,25 @@ def archive_contact(
to_be_archived_contact = get_object_or_404(
Contact, id=contact_id, location__region=request.region
)
to_be_archived_contact.archive()

messages.success(
if not to_be_archived_contact.referring_objects:
to_be_archived_contact.archive()
messages.success(
request,
_("Contact {0} was successfully archived").format(to_be_archived_contact),
)
return redirect(
"contacts",
**{
"region_slug": region_slug,
},
)
messages.error(
request,
_("Contact {0} was successfully archived").format(to_be_archived_contact),
)
return redirect(
"contacts",
**{
"region_slug": region_slug,
},
_('Cannot archive contact "{0}" while content objects refer to it.').format(
to_be_archived_contact,
),
)
return redirect("edit_contact", region_slug=region_slug, contact_id=contact_id)


@permission_required("cms.delete_contact")
Expand All @@ -57,16 +64,25 @@ def delete_contact(
to_be_deleted_contact = get_object_or_404(
Contact, id=contact_id, location__region=request.region
)
to_be_deleted_contact.delete()
messages.success(
request, _("Contact {0} was successfully deleted").format(to_be_deleted_contact)
)
return redirect(
"contacts",
**{
"region_slug": region_slug,
},
if not to_be_deleted_contact.referring_objects:
to_be_deleted_contact.delete()
messages.success(
request,
_("Contact {0} was successfully deleted").format(to_be_deleted_contact),
)
return redirect(
"contacts",
**{
"region_slug": region_slug,
},
)
messages.error(
request,
_('Cannot delete contact "{0}" while content objects refer to it.').format(
to_be_deleted_contact,
),
)
return redirect("edit_contact", region_slug=region_slug, contact_id=contact_id)


@permission_required("cms.change_contact")
Expand Down
30 changes: 25 additions & 5 deletions integreat_cms/cms/views/contacts/contact_bulk_actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from django.contrib import messages
from django.db.models import Q
from django.utils.translation import ngettext_lazy
from django.utils.translation import gettext_lazy, ngettext_lazy

from ...models import Contact
from ...utils.stringify_list import iter_to_string
Expand Down Expand Up @@ -52,8 +52,18 @@ def post(self, request: HttpRequest, *args: Any, **kwargs: Any) -> HttpResponse:
archive_successful = []

for content_object in self.get_queryset():
content_object.archive()
archive_successful.append(content_object)
if not content_object.referring_objects:
content_object.archive()
archive_successful.append(content_object)
else:
messages.error(
request,
gettext_lazy(
'Cannot archive contact "{0}" while content objects refer to it.'
).format(
content_object,
),
)

if archive_successful:
messages.success(
Expand Down Expand Up @@ -132,8 +142,18 @@ def post(self, request: HttpRequest, *args: Any, **kwargs: Any) -> HttpResponse:
"""
delete_sucessful = []
for content_object in self.get_queryset():
content_object.delete()
delete_sucessful.append(content_object)
if not content_object.referring_objects:
content_object.delete()
delete_sucessful.append(content_object)
else:
messages.error(
request,
gettext_lazy(
'Cannot delete contact "{0}" while content objects refer to it.'
).format(
content_object,
),
)

if delete_sucessful:
messages.success(
Expand Down
18 changes: 16 additions & 2 deletions integreat_cms/locale/de/LC_MESSAGES/django.po
Original file line number Diff line number Diff line change
Expand Up @@ -9159,11 +9159,25 @@ msgstr[1] ""
msgid "Contact {0} was successfully archived"
msgstr "Kontakt {0} wurde erfolgreich archiviert"

#: cms/views/contacts/contact_actions.py
#: cms/views/contacts/contact_bulk_actions.py
#, python-brace-format
msgid "Cannot archive contact \"{0}\" while content objects refer to it."
msgstr ""
"Kontakt {0} kann nicht archiviert werden solange Inhalte auf ihn verweisen."

#: cms/views/contacts/contact_actions.py
#, python-brace-format
msgid "Contact {0} was successfully deleted"
msgstr "Kontakt {0} wurde erfolgreich gelöscht."

#: cms/views/contacts/contact_actions.py
#: cms/views/contacts/contact_bulk_actions.py
#, python-brace-format
msgid "Cannot delete contact \"{0}\" while content objects refer to it."
msgstr ""
"Kontakt {0} kann nicht gelöscht werden solange Inhalte auf ihn verweisen."

#: cms/views/contacts/contact_actions.py
#, python-brace-format
msgid "Contact {0} was successfully restored"
Expand Down Expand Up @@ -9254,8 +9268,8 @@ msgstr "Die Version {} existiert nicht."
msgid ""
"%s %s can not change its status as it was imported from an external calendar"
msgstr ""
"Der Status von %s %s kann nicht geändert "
"werden, da diese Veranstaltung aus einem externen Kalender importiert wurde"
"Der Status von %s %s kann nicht geändert werden, da diese Veranstaltung aus "
"einem externen Kalender importiert wurde"

#: cms/views/content_version_view.py
msgid "You cannot reject changes if there is no version to return to."
Expand Down

0 comments on commit 78a40b8

Please sign in to comment.