From ee17d5d3c397b4bbb542c0aed07f64ae914d1ef2 Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Sat, 14 Sep 2019 00:03:13 +1000 Subject: [PATCH] Allow for custom url schemes to be specified in the config file --- .gitignore | 2 ++ InvenTree/InvenTree/settings.py | 8 ++++++++ InvenTree/InvenTree/validators.py | 19 +++++++++++++++++++ InvenTree/config_template.yaml | 9 +++++++++ 4 files changed, 38 insertions(+) diff --git a/.gitignore b/.gitignore index 7b9522c3b7..6b36e2d1d2 100644 --- a/.gitignore +++ b/.gitignore @@ -36,6 +36,8 @@ InvenTree/media InvenTree/static media static +inventree_media +inventree_static # Local config file config.yaml diff --git a/InvenTree/InvenTree/settings.py b/InvenTree/InvenTree/settings.py index f73a5034ca..c3d646c660 100644 --- a/InvenTree/InvenTree/settings.py +++ b/InvenTree/InvenTree/settings.py @@ -213,6 +213,14 @@ AUTH_PASSWORD_VALIDATORS = [ }, ] +# Extra (optional) URL validators +# See https://docs.djangoproject.com/en/2.2/ref/validators/#django.core.validators.URLValidator + +EXTRA_URL_SCHEMES = CONFIG.get('extra_url_schemes', []) + +if not type(EXTRA_URL_SCHEMES) in [list]: + eprint("Warning: extra_url_schemes not correctly formatted") + EXTRA_URL_SCHEMES = [] # Internationalization # https://docs.djangoproject.com/en/1.10/topics/i18n/ diff --git a/InvenTree/InvenTree/validators.py b/InvenTree/InvenTree/validators.py index 4fbcf44b1c..98c0532d14 100644 --- a/InvenTree/InvenTree/validators.py +++ b/InvenTree/InvenTree/validators.py @@ -2,10 +2,29 @@ Custom field validators for InvenTree """ +from django.conf import settings from django.core.exceptions import ValidationError from django.utils.translation import gettext_lazy as _ +def allowable_url_schemes(): + """ Return the list of allowable URL schemes. + In addition to the default schemes allowed by Django, + the install configuration file (config.yaml) can specify + extra schemas """ + + # Default schemes + schemes = ['http', 'https', 'ftp', 'ftps'] + + extra = settings.EXTRA_URL_SCHEMES + + for e in extra: + if e.lower() not in schemes: + schemes.append(e.lower()) + + return schemes + + def validate_part_name(value): """ Prevent some illegal characters in part names. """ diff --git a/InvenTree/config_template.yaml b/InvenTree/config_template.yaml index 1799b49e1a..f362048e35 100644 --- a/InvenTree/config_template.yaml +++ b/InvenTree/config_template.yaml @@ -47,7 +47,16 @@ media_root: '../inventree_media' # By default it is stored in a directory named 'static' local to the InvenTree directory static_root: '../inventree_static' +# Optional URL schemes to allow in URL fields +# By default, only the following schemes are allowed: ['http', 'https', 'ftp', 'ftps'] +# Uncomment the lines below to allow extra schemes +#extra_url_schemes: +# - mailto +# - git +# - ssh + # Logging options +# If debug mode is enabled, set log_queries to True to show aggregate database queries in the debug console log_queries: False # Backup options