Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Setting refactor #7404

Merged
merged 16 commits into from
Jun 13, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
- 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 @@

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)

Check warning on line 251 in src/backend/InvenTree/InvenTree/apps.py

View check run for this annotation

Codecov / codecov/patch

src/backend/InvenTree/InvenTree/apps.py#L250-L251

Added lines #L250 - L251 were not covered by tests
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 @@

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')

Check warning on line 175 in src/backend/InvenTree/InvenTree/forms.py

View check run for this annotation

Codecov / codecov/patch

src/backend/InvenTree/InvenTree/forms.py#L175

Added line #L175 was not covered by tests

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'):

Check warning on line 180 in src/backend/InvenTree/InvenTree/forms.py

View check run for this annotation

Codecov / codecov/patch

src/backend/InvenTree/InvenTree/forms.py#L180

Added line #L180 was not covered by tests
self.fields['email2'] = forms.EmailField(
label=_('Email (again)'),
widget=forms.TextInput(
Expand All @@ -189,7 +189,7 @@
)

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

Check warning on line 192 in src/backend/InvenTree/InvenTree/forms.py

View check run for this annotation

Codecov / codecov/patch

src/backend/InvenTree/InvenTree/forms.py#L192

Added line #L192 was not covered by tests
self.fields.pop('password2')

# reorder fields
Expand All @@ -202,7 +202,7 @@
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'):

Check warning on line 205 in src/backend/InvenTree/InvenTree/forms.py

View check run for this annotation

Codecov / codecov/patch

src/backend/InvenTree/InvenTree/forms.py#L205

Added line #L205 was not covered by tests
email = cleaned_data.get('email')
email2 = cleaned_data.get('email2')
if (email and email2) and email != email2:
Expand All @@ -213,10 +213,7 @@

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():

Check warning on line 216 in src/backend/InvenTree/InvenTree/forms.py

View check run for this annotation

Codecov / codecov/patch

src/backend/InvenTree/InvenTree/forms.py#L216

Added line #L216 was not covered by tests
if settings.EMAIL_HOST:
return True
else:
Expand All @@ -240,9 +237,7 @@

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)

Check warning on line 240 in src/backend/InvenTree/InvenTree/forms.py

View check run for this annotation

Codecov / codecov/patch

src/backend/InvenTree/InvenTree/forms.py#L240

Added line #L240 was not covered by tests
if not mail_restriction:
return super().clean_email(email)

Expand Down Expand Up @@ -273,7 +268,7 @@
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')

Check warning on line 271 in src/backend/InvenTree/InvenTree/forms.py

View check run for this annotation

Codecov / codecov/patch

src/backend/InvenTree/InvenTree/forms.py#L271

Added line #L271 was not covered by tests
if start_group:
try:
group = Group.objects.get(id=start_group)
Expand Down Expand Up @@ -333,7 +328,7 @@

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):

Check warning on line 331 in src/backend/InvenTree/InvenTree/forms.py

View check run for this annotation

Codecov / codecov/patch

src/backend/InvenTree/InvenTree/forms.py#L331

Added line #L331 was not covered by tests
return super().is_auto_signup_allowed(request, sociallogin)
return False

Expand Down Expand Up @@ -385,7 +380,7 @@

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')

Check warning on line 383 in src/backend/InvenTree/InvenTree/forms.py

View check run for this annotation

Codecov / codecov/patch

src/backend/InvenTree/InvenTree/forms.py#L383

Added line #L383 was not covered by tests
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 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'))

Check warning on line 72 in src/backend/InvenTree/InvenTree/sso.py

View check run for this annotation

Codecov / codecov/patch

src/backend/InvenTree/InvenTree/sso.py#L72

Added line #L72 was not covered by tests


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'))

Check warning on line 77 in src/backend/InvenTree/InvenTree/sso.py

View check run for this annotation

Codecov / codecov/patch

src/backend/InvenTree/InvenTree/sso.py#L77

Added line #L77 was not covered by tests
Loading
Loading