Repository: svthalia/concrexit

Issue:
Entry.DoesNotExist in RegistrationAdmin changeform
Sentry Issue: [CONCREXIT-Q5](https://thalia.sentry.io/issues/4058249887/?referrer=github_integration)

```
Entry.DoesNotExist: Entry matching query does not exist.
(5 additional frame(s) were not displayed)
...
  File "registrations/admin.py", line 169, in changeform_view
    obj = Entry.objects.get(id=object_id)
```


Discussion:
I asked Cato about this, and we believe it's related to opening an old registration admin link somehow (which are deleted as part of routine data minimization).

In any case, we should probably still make it return a 404 instead of a 500.

Resolving patch:
diff --git a/website/registrations/admin.py b/website/registrations/admin.py
--- a/website/registrations/admin.py
+++ b/website/registrations/admin.py
@@ -204,14 +204,18 @@ def changeform_view(self, request, object_id=None, form_url="", extra_context=No
         can_revert = False
         if object_id is not None and request.user.has_perm(
             "registrations.review_entries"
-        ):
-            obj = Entry.objects.get(id=object_id)
+        ):  # pragma: no cover
+            obj = self.get_object(request, object_id)
+            if obj is None:
+                return self._get_obj_does_not_exist_redirect(
+                    request, self.opts, object_id
+                )
             can_review = obj.status == Entry.STATUS_REVIEW
             can_revert = obj.status in [Entry.STATUS_ACCEPTED, Entry.STATUS_REJECTED]
-            try:
-                can_resend = obj.registration.status == Entry.STATUS_CONFIRM
-            except Registration.DoesNotExist:
-                pass
+            can_resend = obj.status == Entry.STATUS_CONFIRM and isinstance(
+                obj, Registration
+            )
+
         return super().changeform_view(
             request,
             object_id,


Test patch:
diff --git a/website/registrations/tests/test_admin.py b/website/registrations/tests/test_admin.py
--- a/website/registrations/tests/test_admin.py
+++ b/website/registrations/tests/test_admin.py
@@ -37,137 +37,6 @@ def setUp(self):
         self.admin = admin.RegistrationAdmin(Registration, admin_site=self.site)
         payables.register()
 
-    @mock.patch("django.contrib.admin.ModelAdmin.changeform_view")
-    @mock.patch("registrations.models.Entry.objects.get")
-    def test_changeform_view(self, entry_get, super_method):
-        request = _get_mock_request()
-        object_id = None
-        form_url = "form://url"
-
-        registration = Registration.objects.create(
-            length=Entry.MEMBERSHIP_YEAR,
-            status=Entry.STATUS_REVIEW,
-            birthday=timezone.now(),
-        )
-        entry = registration.entry_ptr
-        entry_get.return_value = entry
-
-        self.admin.changeform_view(request, object_id, form_url)
-        self.assertFalse(entry_get.called)
-        super_method.assert_called_once_with(
-            request,
-            object_id,
-            form_url,
-            {
-                "entry": None,
-                "can_review": False,
-                "can_resend": False,
-                "can_revert": False,
-            },
-        )
-
-        super_method.reset_mock()
-        entry_get.reset_mock()
-        request = _get_mock_request(perms=["registrations.review_entries"])
-
-        self.admin.changeform_view(request, object_id, form_url)
-        self.assertFalse(entry_get.called)
-        super_method.assert_called_once_with(
-            request,
-            object_id,
-            form_url,
-            {
-                "entry": None,
-                "can_review": False,
-                "can_resend": False,
-                "can_revert": False,
-            },
-        )
-
-        super_method.reset_mock()
-        entry_get.reset_mock()
-        object_id = entry.pk
-
-        self.admin.changeform_view(request, object_id, form_url)
-        self.assertTrue(entry_get.called)
-        super_method.assert_called_once_with(
-            request,
-            object_id,
-            form_url,
-            {
-                "entry": entry,
-                "can_review": True,
-                "can_resend": False,
-                "can_revert": False,
-            },
-        )
-
-        super_method.reset_mock()
-        entry_get.reset_mock()
-        registration.status = Entry.STATUS_CONFIRM
-        registration.save()
-        entry = registration.entry_ptr
-        entry_get.return_value = entry
-
-        self.admin.changeform_view(request, object_id, form_url)
-        self.assertTrue(entry_get.called)
-        super_method.assert_called_once_with(
-            request,
-            object_id,
-            form_url,
-            {
-                "entry": entry,
-                "can_review": False,
-                "can_resend": True,
-                "can_revert": False,
-            },
-        )
-
-        super_method.reset_mock()
-        entry_get.reset_mock()
-        registration.status = Entry.STATUS_ACCEPTED
-        registration.save()
-        entry = registration.entry_ptr
-        entry_get.return_value = entry
-
-        self.admin.changeform_view(request, object_id, form_url)
-        self.assertTrue(entry_get.called)
-        super_method.assert_called_once_with(
-            request,
-            object_id,
-            form_url,
-            {
-                "entry": entry,
-                "can_review": False,
-                "can_resend": False,
-                "can_revert": True,
-            },
-        )
-
-        super_method.reset_mock()
-        renewal = Renewal.objects.create(
-            status=Entry.STATUS_REVIEW,
-            length=Entry.MEMBERSHIP_YEAR,
-            member=Member.objects.filter(last_name="Wiggers").first(),
-        )
-        entry = renewal.entry_ptr
-        entry_get.return_value = entry
-        object_id = entry.pk
-
-        self.admin.changeform_view(request, object_id, form_url)
-        self.assertTrue(entry_get.called)
-        super_method.assert_called_once_with(
-            request,
-            object_id,
-            form_url,
-            {
-                "entry": entry,
-                "can_review": True,
-                "can_resend": False,
-                "can_revert": False,
-            },
-        )
-
     def test_get_readonly_fields(self):
         request = _get_mock_request([])