admin.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. from __future__ import unicode_literals
  2. from future.builtins import str
  3. from copy import copy
  4. from django.contrib import admin
  5. from django.contrib.messages import info
  6. from django.http import HttpResponseRedirect
  7. from django.utils.translation import ugettext_lazy as _
  8. from django.utils.encoding import force_text
  9. from mezzanine.core.admin import BaseTranslationModelAdmin
  10. from mezzanine.conf import settings
  11. from mezzanine.conf.models import Setting
  12. from mezzanine.conf.forms import SettingsForm
  13. from mezzanine.utils.static import static_lazy as static
  14. from mezzanine.utils.urls import admin_url
  15. class SettingsAdmin(admin.ModelAdmin):
  16. """
  17. Admin class for settings model. Redirect add/change views to the
  18. list view where a single form is rendered for editing all settings.
  19. """
  20. class Media(BaseTranslationModelAdmin.Media):
  21. css = copy(BaseTranslationModelAdmin.Media.css)
  22. css["all"] += (static("mezzanine/css/admin/settings.css"),)
  23. js = [js.replace(str(static("tabbed_translation_fields.js")),
  24. str(static("tabbed_translatable_settings.js")))
  25. for js in BaseTranslationModelAdmin.Media.js]
  26. def changelist_redirect(self):
  27. changelist_url = admin_url(Setting, "changelist")
  28. return HttpResponseRedirect(changelist_url)
  29. def add_view(self, *args, **kwargs):
  30. return self.changelist_redirect()
  31. def change_view(self, *args, **kwargs):
  32. return self.changelist_redirect()
  33. def changelist_view(self, request, extra_context=None):
  34. if extra_context is None:
  35. extra_context = {}
  36. settings_form = SettingsForm(request.POST or None)
  37. if settings_form.is_valid():
  38. settings_form.save()
  39. settings.clear_cache()
  40. info(request, _("Settings were successfully updated."))
  41. return self.changelist_redirect()
  42. extra_context["settings_form"] = settings_form
  43. extra_context["title"] = u"%s %s" % (
  44. _("Change"), force_text(Setting._meta.verbose_name_plural))
  45. return super(SettingsAdmin, self).changelist_view(request,
  46. extra_context)
  47. admin.site.register(Setting, SettingsAdmin)