mirror of
				https://github.com/inventree/InvenTree.git
				synced 2025-10-31 13:15:43 +00:00 
			
		
		
		
	[BUG] Duplication of "base URL" setting (#3263)
* [BUG] Duplication of "base URL" setting Fixes #3196 Adds after_safe key to settings: this action is executed after the setting is saved. The current setting object is supplied to the function as the first argument. * Add unittests * fix style * ammned allowed keys
This commit is contained in:
		| @@ -9,6 +9,7 @@ from unittest import mock | |||||||
| import django.core.exceptions as django_exceptions | import django.core.exceptions as django_exceptions | ||||||
| from django.conf import settings | from django.conf import settings | ||||||
| from django.contrib.auth import get_user_model | from django.contrib.auth import get_user_model | ||||||
|  | from django.contrib.sites.models import Site | ||||||
| from django.core.exceptions import ValidationError | from django.core.exceptions import ValidationError | ||||||
| from django.test import TestCase, override_settings | from django.test import TestCase, override_settings | ||||||
|  |  | ||||||
| @@ -604,3 +605,16 @@ class TestInstanceName(helpers.InvenTreeTestCase): | |||||||
|         InvenTreeSetting.set_setting("INVENTREE_INSTANCE", "Testing title", self.user) |         InvenTreeSetting.set_setting("INVENTREE_INSTANCE", "Testing title", self.user) | ||||||
|  |  | ||||||
|         self.assertEqual(version.inventreeInstanceTitle(), 'Testing title') |         self.assertEqual(version.inventreeInstanceTitle(), 'Testing title') | ||||||
|  |  | ||||||
|  |         # The site should also be changed | ||||||
|  |         site_obj = Site.objects.all().order_by('id').first() | ||||||
|  |         self.assertEqual(site_obj.name, 'Testing title') | ||||||
|  |  | ||||||
|  |     def test_instance_url(self): | ||||||
|  |         """Test instance url settings.""" | ||||||
|  |         # Set up required setting | ||||||
|  |         InvenTreeSetting.set_setting("INVENTREE_BASE_URL", "http://127.1.2.3", self.user) | ||||||
|  |  | ||||||
|  |         # The site should also be changed | ||||||
|  |         site_obj = Site.objects.all().order_by('id').first() | ||||||
|  |         self.assertEqual(site_obj.domain, 'http://127.1.2.3') | ||||||
|   | |||||||
| @@ -21,6 +21,7 @@ from django.contrib.auth.models import Group, User | |||||||
| from django.contrib.contenttypes.fields import GenericForeignKey | from django.contrib.contenttypes.fields import GenericForeignKey | ||||||
| from django.contrib.contenttypes.models import ContentType | from django.contrib.contenttypes.models import ContentType | ||||||
| from django.contrib.humanize.templatetags.humanize import naturaltime | from django.contrib.humanize.templatetags.humanize import naturaltime | ||||||
|  | from django.contrib.sites.models import Site | ||||||
| from django.core.cache import cache | from django.core.cache import cache | ||||||
| from django.core.exceptions import AppRegistryNotReady, ValidationError | from django.core.exceptions import AppRegistryNotReady, ValidationError | ||||||
| from django.core.validators import MinValueValidator, URLValidator | from django.core.validators import MinValueValidator, URLValidator | ||||||
| @@ -82,6 +83,14 @@ class BaseInvenTreeSetting(models.Model): | |||||||
|  |  | ||||||
|         super().save() |         super().save() | ||||||
|  |  | ||||||
|  |         # Get after_save action | ||||||
|  |         setting = self.get_setting_definition(self.key, *args, **kwargs) | ||||||
|  |         after_save = setting.get('after_save', None) | ||||||
|  |  | ||||||
|  |         # Execute if callable | ||||||
|  |         if callable(after_save): | ||||||
|  |             after_save(self) | ||||||
|  |  | ||||||
|     @property |     @property | ||||||
|     def cache_key(self): |     def cache_key(self): | ||||||
|         """Generate a unique cache key for this settings object""" |         """Generate a unique cache key for this settings object""" | ||||||
| @@ -735,6 +744,20 @@ def settings_group_options(): | |||||||
|     return [('', _('No group')), *[(str(a.id), str(a)) for a in Group.objects.all()]] |     return [('', _('No group')), *[(str(a.id), str(a)) for a in Group.objects.all()]] | ||||||
|  |  | ||||||
|  |  | ||||||
|  | def update_instance_url(setting): | ||||||
|  |     """Update the first site objects domain to url.""" | ||||||
|  |     site_obj = Site.objects.all().order_by('id').first() | ||||||
|  |     site_obj.domain = setting.value | ||||||
|  |     site_obj.save() | ||||||
|  |  | ||||||
|  |  | ||||||
|  | def update_instance_name(setting): | ||||||
|  |     """Update the first site objects name to instance name.""" | ||||||
|  |     site_obj = Site.objects.all().order_by('id').first() | ||||||
|  |     site_obj.name = setting.value | ||||||
|  |     site_obj.save() | ||||||
|  |  | ||||||
|  |  | ||||||
| class InvenTreeSetting(BaseInvenTreeSetting): | class InvenTreeSetting(BaseInvenTreeSetting): | ||||||
|     """An InvenTreeSetting object is a key:value pair used for storing single values (e.g. one-off settings values). |     """An InvenTreeSetting object is a key:value pair used for storing single values (e.g. one-off settings values). | ||||||
|  |  | ||||||
| @@ -782,6 +805,7 @@ class InvenTreeSetting(BaseInvenTreeSetting): | |||||||
|             'name': _('Server Instance Name'), |             'name': _('Server Instance Name'), | ||||||
|             'default': 'InvenTree', |             'default': 'InvenTree', | ||||||
|             'description': _('String descriptor for the server instance'), |             'description': _('String descriptor for the server instance'), | ||||||
|  |             'after_save': update_instance_name, | ||||||
|         }, |         }, | ||||||
|  |  | ||||||
|         'INVENTREE_INSTANCE_TITLE': { |         'INVENTREE_INSTANCE_TITLE': { | ||||||
| @@ -809,6 +833,7 @@ class InvenTreeSetting(BaseInvenTreeSetting): | |||||||
|             'description': _('Base URL for server instance'), |             'description': _('Base URL for server instance'), | ||||||
|             'validator': EmptyURLValidator(), |             'validator': EmptyURLValidator(), | ||||||
|             'default': '', |             'default': '', | ||||||
|  |             'after_save': update_instance_url, | ||||||
|         }, |         }, | ||||||
|  |  | ||||||
|         'INVENTREE_DEFAULT_CURRENCY': { |         'INVENTREE_DEFAULT_CURRENCY': { | ||||||
|   | |||||||
| @@ -133,6 +133,7 @@ class SettingsTest(InvenTreeTestCase): | |||||||
|             'choices', |             'choices', | ||||||
|             'units', |             'units', | ||||||
|             'requires_restart', |             'requires_restart', | ||||||
|  |             'after_save', | ||||||
|         ] |         ] | ||||||
|  |  | ||||||
|         for k in setting.keys(): |         for k in setting.keys(): | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user