Skip to content

Commit

Permalink
added button to admin page for reseting
Browse files Browse the repository at this point in the history
renamed method, added unit tests

added button to admin page for reseting
  • Loading branch information
ar0ne committed Apr 15, 2024
1 parent 7608c7e commit 8c2cb76
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 11 deletions.
21 changes: 21 additions & 0 deletions extra_settings/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
from django.contrib.admin.sites import NotRegistered
from django.core.exceptions import ImproperlyConfigured
from django.utils.translation import gettext_lazy as _
from django.urls import path, reverse
from django.shortcuts import redirect

from extra_settings.forms import SettingForm
from extra_settings.models import Setting
Expand Down Expand Up @@ -55,6 +57,7 @@ def queryset(self, request, queryset):

class SettingAdmin(admin.ModelAdmin):
form = SettingForm
change_list_template = "extra_settings/changelist.html"
value_fields_names = (
"value_bool",
"value_date",
Expand Down Expand Up @@ -123,6 +126,24 @@ def get_fieldsets(self, request, obj=None):
def get_readonly_fields(self, request, obj=None):
return ("value_type",) if obj else ()

def get_urls(self):
urls = super().get_urls()
my_urls = [
path(
"reset/",
self.reset_settings,
name=f"{settings.EXTRA_SETTINGS_ADMIN_APP}_setting_reset",
),
]
return my_urls + urls

def reset_settings(self, request):
"""Reset existing settings to default values"""
Setting.reset_to_default()
return redirect(
reverse(f"admin:{settings.EXTRA_SETTINGS_ADMIN_APP}_setting_changelist")
)

class Media:
css = {
"all": ("extra_settings/css/extra_settings.css",),
Expand Down
11 changes: 11 additions & 0 deletions extra_settings/templates/extra_settings/changelist.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{% extends 'admin/change_list.html' %}
{% load i18n admin_urls %}

{% block object-tools-items %}
{% if has_add_permission %}
<li>
<a href="{% url opts|admin_urlname:'reset' %}" class="historylink">{% translate "Reset settings" %}</a>
</li>
{% endif %}
{{ block.super }}
{% endblock %}
55 changes: 44 additions & 11 deletions tests/test_admin.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from django.contrib.admin.sites import AdminSite
from django.test import TestCase
from django.test import TestCase, override_settings

from unittest.mock import patch

from extra_settings.admin import SettingAdmin
from extra_settings.forms import SettingForm
Expand All @@ -20,18 +22,15 @@ def has_perm(self, perm):


class ExtraSettingsAdminTestCase(TestCase):
def setUp(self):
self._setting_obj, setting_created = Setting.objects.get_or_create(
@classmethod
def setUpClass(cls):
super().setUpClass()
cls._setting_obj = Setting.objects.create(
name="PACKAGE_NAME",
defaults={
"value_type": Setting.TYPE_STRING,
"value_string": "django-extra-settings",
},
value_type=Setting.TYPE_STRING,
value_string="django-extra-settings",
)
self._site = AdminSite()

def tearDown(self):
pass
cls._site = AdminSite()

def test_changelist_form(self):
ma = SettingAdmin(model=Setting, admin_site=AdminSite())
Expand Down Expand Up @@ -84,3 +83,37 @@ def test_readonly_fields(self):
self.assertEqual(
ma.get_readonly_fields(request, self._setting_obj), ("value_type",)
)

@override_settings(
EXTRA_SETTINGS_DEFAULTS=[
{
"name": "foo",
"type": "string",
"value": "bar",
},
]
)
@patch("extra_settings.admin.redirect")
@patch("extra_settings.admin.reverse")
def test_modeladmin_reset(self, mock_redirect, mock_reverse):
self.assertEqual(Setting.objects.count(), 1)
self._setting_obj.value_string = "foo"
self._setting_obj.save()
Setting.objects.create(
name="bar",
value_type=Setting.TYPE_BOOL,
value_bool=True,
)
ma = SettingAdmin(model=Setting, admin_site=AdminSite())
ma.reset_settings(request)
self.assertEqual(Setting.objects.count(), 1)
obj = Setting.objects.get(name="FOO")
self.assertEqual(obj.value_type, Setting.TYPE_STRING)
self.assertEqual(obj.value, "bar")

@override_settings(EXTRA_SETTINGS_ADMIN_APP="app")
def test_get_urls(self) -> None:
reset_url = "app_setting_reset"
ma = SettingAdmin(model=Setting, admin_site=AdminSite())
urls = [url.name for url in ma.get_urls()]
self.assertTrue(reset_url in urls)

0 comments on commit 8c2cb76

Please sign in to comment.