Skip to content

Commit

Permalink
Use ModelAdmin.opts for correct reverse in admin
Browse files Browse the repository at this point in the history
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
  • Loading branch information
frennkie committed Nov 6, 2020
1 parent 57ac58d commit 338404b
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions newsletter/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,16 +168,17 @@ 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
submission.save()

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 """
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -439,7 +441,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:
Expand All @@ -453,9 +455,8 @@ 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']
Expand Down Expand Up @@ -488,7 +489,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:
Expand Down

0 comments on commit 338404b

Please sign in to comment.