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 Dec 9, 2024
1 parent c391eaa commit 4967b5c
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 14 deletions.
31 changes: 22 additions & 9 deletions integreat_cms/cms/views/contacts/contact_actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,16 +57,29 @@ 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_page_translations
or to_be_deleted_contact.referring_poi_translations
or to_be_deleted_contact.referring_event_translations
):
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
20 changes: 17 additions & 3 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 ngettext_lazy, gettext_lazy

from ...models import Contact
from ...utils.stringify_list import iter_to_string
Expand Down Expand Up @@ -132,8 +132,22 @@ 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_page_translations
or content_object.referring_poi_translations
or content_object.referring_event_translations
):
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
12 changes: 10 additions & 2 deletions integreat_cms/locale/de/LC_MESSAGES/django.po
Original file line number Diff line number Diff line change
Expand Up @@ -9147,6 +9147,14 @@ msgstr "Kontakt {0} wurde erfolgreich archiviert"
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 folgende Inhalte auf ihn "
"verweisen."

#: cms/views/contacts/contact_actions.py
#, python-brace-format
msgid "Contact {0} was successfully restored"
Expand Down Expand Up @@ -9237,8 +9245,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 4967b5c

Please sign in to comment.