From 7132e99fd8ba4c4df645a1bc1f98428f93f4e062 Mon Sep 17 00:00:00 2001 From: Robert Habermann Date: Fri, 6 Nov 2020 22:50:58 +0100 Subject: [PATCH] Use ModelAdmin.opts for correct reverse in admin Make sure to use ModelAdmin.opts in admin.py in order to get the correct app_label and model_name. This is important when a subclass (e.g. of Newsletter) is used --- newsletter/admin.py | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/newsletter/admin.py b/newsletter/admin.py index 20157f36..4c6dde61 100644 --- a/newsletter/admin.py +++ b/newsletter/admin.py @@ -168,8 +168,9 @@ def submit(self, request, object_id): if submission.sent or submission.prepared: messages.info(request, _("Submission already sent.")) change_url = reverse( - 'admin:newsletter_submission_change', args=[object_id] + 'admin:%s_%s_change' % (self.opts.app_label, self.opts.model_name), args=[object_id] ) + return HttpResponseRedirect(change_url) submission.prepared = True @@ -177,7 +178,7 @@ def submit(self, request, object_id): messages.info(request, _("Your submission is being sent.")) - changelist_url = reverse('admin:newsletter_submission_changelist') + changelist_url = reverse('admin:%s_%s_changelist' % (self.opts.app_label, self.opts.model_name)) return HttpResponseRedirect(changelist_url) """ URLs """ @@ -305,9 +306,10 @@ def preview_text(self, request, object_id): def submit(self, request, object_id): submission = Submission.from_message(self._getobj(request, object_id)) + opts = submission._meta change_url = reverse( - 'admin:newsletter_submission_change', args=[submission.id]) + 'admin:%s_%s_change' % (opts.app_label, opts.model_name), args=[submission.id]) return HttpResponseRedirect(change_url) @@ -346,6 +348,7 @@ def get_urls(self): class SubscriptionAdmin(NewsletterAdminLinkMixin, ExtendibleModelAdminMixin, admin.ModelAdmin): + newsletter_class = Newsletter form = SubscriptionAdminForm list_display = ( 'name', 'email', 'admin_newsletter', 'admin_subscribe_date', @@ -439,7 +442,7 @@ def subscribers_import(self, request): form.cleaned_data['newsletter'].pk confirm_url = reverse( - 'admin:newsletter_subscription_import_confirm' + 'admin:%s_%s_import_confirm' % (self.opts.app_label, self.opts.model_name) ) return HttpResponseRedirect(confirm_url) else: @@ -453,13 +456,12 @@ def subscribers_import(self, request): def subscribers_import_confirm(self, request): # If no addresses are in the session, start all over. - if 'addresses' not in request.session: - import_url = reverse('admin:newsletter_subscription_import') + import_url = reverse('admin:%s_%s_import' % (self.opts.app_label, self.opts.model_name)) return HttpResponseRedirect(import_url) addresses = request.session['addresses'] - newsletter = Newsletter.objects.get( + newsletter = self.newsletter_class.objects.get( pk=request.session['newsletter_pk'] ) @@ -488,7 +490,7 @@ def subscribers_import_confirm(self, request): ) changelist_url = reverse( - 'admin:newsletter_subscription_changelist' + 'admin:%s_%s_changelist' % (self.opts.app_label, self.opts.model_name) ) return HttpResponseRedirect(changelist_url) else: