Skip to content

Commit

Permalink
Setting refactor (inventree#7404)
Browse files Browse the repository at this point in the history
* Add helper functions to set/get settings

* Refactor instances of get_setting

* UPdates

* Fix for task

* Add debug messages

- Work out what is going on in CI

* add more debug

- Cannot reproduce locally?

* More debug...

* Remove debug prints

* Add better debug msg

* Simplify unit test

* Increase timeout for plugin tests

* Update validator code
  • Loading branch information
SchrodingersGat authored Jun 13, 2024
1 parent 4c7a74e commit 129975a
Show file tree
Hide file tree
Showing 36 changed files with 236 additions and 317 deletions.
12 changes: 3 additions & 9 deletions src/backend/InvenTree/InvenTree/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import InvenTree.conversion
import InvenTree.ready
import InvenTree.tasks
from common.settings import get_global_setting, set_global_setting
from InvenTree.config import get_setting

logger = logging.getLogger('inventree')
Expand Down Expand Up @@ -238,8 +239,6 @@ def update_site_url(self):
- If a fixed SITE_URL is specified (via configuration), it should override the INVENTREE_BASE_URL setting
- If multi-site support is enabled, update the site URL for the current site
"""
import common.models

if not InvenTree.ready.canAppAccessDatabase():
return

Expand All @@ -248,13 +247,8 @@ def update_site_url(self):

if settings.SITE_URL:
try:
if (
common.models.InvenTreeSetting.get_setting('INVENTREE_BASE_URL')
!= settings.SITE_URL
):
common.models.InvenTreeSetting.set_setting(
'INVENTREE_BASE_URL', settings.SITE_URL
)
if get_global_setting('INVENTREE_BASE_URL') != settings.SITE_URL:
set_global_setting('INVENTREE_BASE_URL', settings.SITE_URL)
logger.info('Updated INVENTREE_SITE_URL to %s', settings.SITE_URL)
except Exception:
pass
Expand Down
7 changes: 2 additions & 5 deletions src/backend/InvenTree/InvenTree/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from rest_framework.fields import empty

import InvenTree.helpers
from common.settings import get_global_setting

from .validators import AllowedURLValidator, allowable_url_schemes

Expand All @@ -32,11 +33,7 @@ def __init__(self, **kwargs):

def run_validation(self, data=empty):
"""Override default validation behaviour for this field type."""
import common.models

strict_urls = common.models.InvenTreeSetting.get_setting(
'INVENTREE_STRICT_URLS', True, cache=False
)
strict_urls = get_global_setting('INVENTREE_STRICT_URLS', True, cache=False)

if not strict_urls and data is not empty and '://' not in data:
# Validate as if there were a schema provided
Expand Down
25 changes: 10 additions & 15 deletions src/backend/InvenTree/InvenTree/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

import InvenTree.helpers_model
import InvenTree.sso
from common.models import InvenTreeSetting
from common.settings import get_global_setting
from InvenTree.exceptions import log_error

logger = logging.getLogger('inventree')
Expand Down Expand Up @@ -172,12 +172,12 @@ class CustomSignupForm(SignupForm):

def __init__(self, *args, **kwargs):
"""Check settings to influence which fields are needed."""
kwargs['email_required'] = InvenTreeSetting.get_setting('LOGIN_MAIL_REQUIRED')
kwargs['email_required'] = get_global_setting('LOGIN_MAIL_REQUIRED')

super().__init__(*args, **kwargs)

# check for two mail fields
if InvenTreeSetting.get_setting('LOGIN_SIGNUP_MAIL_TWICE'):
if get_global_setting('LOGIN_SIGNUP_MAIL_TWICE'):
self.fields['email2'] = forms.EmailField(
label=_('Email (again)'),
widget=forms.TextInput(
Expand All @@ -189,7 +189,7 @@ def __init__(self, *args, **kwargs):
)

# check for two password fields
if not InvenTreeSetting.get_setting('LOGIN_SIGNUP_PWD_TWICE'):
if not get_global_setting('LOGIN_SIGNUP_PWD_TWICE'):
self.fields.pop('password2')

# reorder fields
Expand All @@ -202,7 +202,7 @@ def clean(self):
cleaned_data = super().clean()

# check for two mail fields
if InvenTreeSetting.get_setting('LOGIN_SIGNUP_MAIL_TWICE'):
if get_global_setting('LOGIN_SIGNUP_MAIL_TWICE'):
email = cleaned_data.get('email')
email2 = cleaned_data.get('email2')
if (email and email2) and email != email2:
Expand All @@ -213,10 +213,7 @@ def clean(self):

def registration_enabled():
"""Determine whether user registration is enabled."""
if (
InvenTreeSetting.get_setting('LOGIN_ENABLE_REG')
or InvenTree.sso.registration_enabled()
):
if get_global_setting('LOGIN_ENABLE_REG') or InvenTree.sso.registration_enabled():
if settings.EMAIL_HOST:
return True
else:
Expand All @@ -240,9 +237,7 @@ def is_open_for_signup(self, request, *args, **kwargs):

def clean_email(self, email):
"""Check if the mail is valid to the pattern in LOGIN_SIGNUP_MAIL_RESTRICTION (if enabled in settings)."""
mail_restriction = InvenTreeSetting.get_setting(
'LOGIN_SIGNUP_MAIL_RESTRICTION', None
)
mail_restriction = get_global_setting('LOGIN_SIGNUP_MAIL_RESTRICTION', None)
if not mail_restriction:
return super().clean_email(email)

Expand Down Expand Up @@ -273,7 +268,7 @@ def save_user(self, request, user, form, commit=True):
user = super().save_user(request, user, form)

# Check if a default group is set in settings
start_group = InvenTreeSetting.get_setting('SIGNUP_GROUP')
start_group = get_global_setting('SIGNUP_GROUP')
if start_group:
try:
group = Group.objects.get(id=start_group)
Expand Down Expand Up @@ -333,7 +328,7 @@ class CustomSocialAccountAdapter(

def is_auto_signup_allowed(self, request, sociallogin):
"""Check if auto signup is enabled in settings."""
if InvenTreeSetting.get_setting('LOGIN_SIGNUP_SSO_AUTO', True):
if get_global_setting('LOGIN_SIGNUP_SSO_AUTO', True):
return super().is_auto_signup_allowed(request, sociallogin)
return False

Expand Down Expand Up @@ -385,7 +380,7 @@ class CustomRegisterSerializer(RegisterSerializer):

def __init__(self, instance=None, data=..., **kwargs):
"""Check settings to influence which fields are needed."""
kwargs['email_required'] = InvenTreeSetting.get_setting('LOGIN_MAIL_REQUIRED')
kwargs['email_required'] = get_global_setting('LOGIN_MAIL_REQUIRED')
super().__init__(instance, data, **kwargs)

def save(self, request):
Expand Down
42 changes: 14 additions & 28 deletions src/backend/InvenTree/InvenTree/helpers_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
from djmoney.money import Money
from PIL import Image

import common.models
import InvenTree
import InvenTree.helpers_model
import InvenTree.version
Expand All @@ -24,16 +23,12 @@
NotificationBody,
trigger_notification,
)
from common.settings import get_global_setting
from InvenTree.format import format_money

logger = logging.getLogger('inventree')


def getSetting(key, backup_value=None):
"""Shortcut for reading a setting value from the database."""
return common.models.InvenTreeSetting.get_setting(key, backup_value=backup_value)


def get_base_url(request=None):
"""Return the base URL for the InvenTree server.
Expand All @@ -44,6 +39,8 @@ def get_base_url(request=None):
3. If settings.SITE_URL is set (e.g. in the Django settings), use that
4. If the InvenTree setting INVENTREE_BASE_URL is set, use that
"""
import common.models

# Check if a request is provided
if request:
return request.build_absolute_uri('/')
Expand All @@ -62,9 +59,7 @@ def get_base_url(request=None):

# Check if a global InvenTree setting is provided
try:
if site_url := common.models.InvenTreeSetting.get_setting(
'INVENTREE_BASE_URL', create=False
):
if site_url := get_global_setting('INVENTREE_BASE_URL', create=False):
return site_url
except (ProgrammingError, OperationalError):
pass
Expand Down Expand Up @@ -112,25 +107,20 @@ def download_image_from_url(remote_url, timeout=2.5):
ValueError: Server responded with invalid 'Content-Length' value
TypeError: Response is not a valid image
"""
import common.models

# Check that the provided URL at least looks valid
validator = URLValidator()
validator(remote_url)

# Calculate maximum allowable image size (in bytes)
max_size = (
int(
common.models.InvenTreeSetting.get_setting(
'INVENTREE_DOWNLOAD_IMAGE_MAX_SIZE'
)
)
* 1024
* 1024
int(get_global_setting('INVENTREE_DOWNLOAD_IMAGE_MAX_SIZE')) * 1024 * 1024
)

# Add user specified user-agent to request (if specified)
user_agent = common.models.InvenTreeSetting.get_setting(
'INVENTREE_DOWNLOAD_FROM_URL_USER_AGENT'
)
user_agent = get_global_setting('INVENTREE_DOWNLOAD_FROM_URL_USER_AGENT')

if user_agent:
headers = {'User-Agent': user_agent}
else:
Expand Down Expand Up @@ -216,6 +206,8 @@ def render_currency(
max_decimal_places: The maximum number of decimal places to render to. If unspecified, uses the PRICING_DECIMAL_PLACES setting.
include_symbol: If True, include the currency symbol in the output
"""
import common.models

if money in [None, '']:
return '-'

Expand All @@ -231,19 +223,13 @@ def render_currency(
pass

if decimal_places is None:
decimal_places = common.models.InvenTreeSetting.get_setting(
'PRICING_DECIMAL_PLACES', 6
)
decimal_places = get_global_setting('PRICING_DECIMAL_PLACES', 6)

if min_decimal_places is None:
min_decimal_places = common.models.InvenTreeSetting.get_setting(
'PRICING_DECIMAL_PLACES_MIN', 0
)
min_decimal_places = get_global_setting('PRICING_DECIMAL_PLACES_MIN', 0)

if max_decimal_places is None:
max_decimal_places = common.models.InvenTreeSetting.get_setting(
'PRICING_DECIMAL_PLACES', 6
)
max_decimal_places = get_global_setting('PRICING_DECIMAL_PLACES', 6)

value = Decimal(str(money.amount)).normalize()
value = str(value)
Expand Down
10 changes: 4 additions & 6 deletions src/backend/InvenTree/InvenTree/social_auth_urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from rest_framework.response import Response

import InvenTree.sso
from common.models import InvenTreeSetting
from common.settings import get_global_setting
from InvenTree.mixins import CreateAPI, ListAPI, ListCreateAPI
from InvenTree.serializers import EmptySerializer, InvenTreeModelSerializer

Expand Down Expand Up @@ -177,12 +177,10 @@ def get(self, request, *args, **kwargs):
data = {
'sso_enabled': InvenTree.sso.login_enabled(),
'sso_registration': InvenTree.sso.registration_enabled(),
'mfa_required': InvenTreeSetting.get_setting('LOGIN_ENFORCE_MFA'),
'mfa_required': get_global_setting('LOGIN_ENFORCE_MFA'),
'providers': provider_list,
'registration_enabled': InvenTreeSetting.get_setting('LOGIN_ENABLE_REG'),
'password_forgotten_enabled': InvenTreeSetting.get_setting(
'LOGIN_ENABLE_PWD_FORGOT'
),
'registration_enabled': get_global_setting('LOGIN_ENABLE_REG'),
'password_forgotten_enabled': get_global_setting('LOGIN_ENABLE_PWD_FORGOT'),
}
return Response(data)

Expand Down
8 changes: 4 additions & 4 deletions src/backend/InvenTree/InvenTree/sso.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import logging

from common.models import InvenTreeSetting
from common.settings import get_global_setting
from InvenTree.helpers import str2bool

logger = logging.getLogger('inventree')
Expand Down Expand Up @@ -64,14 +64,14 @@ def provider_display_name(provider):

def login_enabled() -> bool:
"""Return True if SSO login is enabled."""
return str2bool(InvenTreeSetting.get_setting('LOGIN_ENABLE_SSO'))
return str2bool(get_global_setting('LOGIN_ENABLE_SSO'))


def registration_enabled() -> bool:
"""Return True if SSO registration is enabled."""
return str2bool(InvenTreeSetting.get_setting('LOGIN_ENABLE_SSO_REG'))
return str2bool(get_global_setting('LOGIN_ENABLE_SSO_REG'))


def auto_registration_enabled() -> bool:
"""Return True if SSO auto-registration is enabled."""
return str2bool(InvenTreeSetting.get_setting('LOGIN_SIGNUP_SSO_AUTO'))
return str2bool(get_global_setting('LOGIN_SIGNUP_SSO_AUTO'))
Loading

0 comments on commit 129975a

Please sign in to comment.