From b26bf780c32baf2f9e08dc6cb60a1fa4036ea424 Mon Sep 17 00:00:00 2001 From: Matthias Date: Thu, 14 Oct 2021 21:27:09 +0200 Subject: [PATCH 1/6] setting to register group on signup --- InvenTree/InvenTree/forms.py | 18 ++++++++++++++++-- InvenTree/common/models.py | 11 ++++++++++- .../templates/InvenTree/settings/login.html | 1 + 3 files changed, 27 insertions(+), 3 deletions(-) diff --git a/InvenTree/InvenTree/forms.py b/InvenTree/InvenTree/forms.py index 8b4b87637c..eede6b8d32 100644 --- a/InvenTree/InvenTree/forms.py +++ b/InvenTree/InvenTree/forms.py @@ -4,10 +4,11 @@ Helper forms which subclass Django forms to provide additional functionality # -*- coding: utf-8 -*- from __future__ import unicode_literals +import logging from django.utils.translation import ugettext_lazy as _ from django import forms -from django.contrib.auth.models import User +from django.contrib.auth.models import User, Group from crispy_forms.helper import FormHelper from crispy_forms.layout import Layout, Field @@ -20,6 +21,7 @@ from allauth.socialaccount.adapter import DefaultSocialAccountAdapter from part.models import PartCategory from common.models import InvenTreeSetting +logger = logging.getLogger('inventree') class HelperForm(forms.ModelForm): """ Provides simple integration of crispy_forms extension. """ @@ -261,6 +263,18 @@ class RegistratonMixin: return super().is_open_for_signup(request) return False + def save_user(self, request, user, form, commit=True): + user = super().save_user(request, user, form, commit=commit) + start_group = InvenTreeSetting.get_setting('SIGNUP_GROUP') + if start_group: + try: + group = Group.objects.get(id=start_group) + user.groups.add(group) + except Group.DoesNotExist: + logger.error('The setting `SIGNUP_GROUP` contains an non existant group', start_group) + user.save() + return user + class CustomAccountAdapter(RegistratonMixin, DefaultAccountAdapter): """ @@ -268,7 +282,7 @@ class CustomAccountAdapter(RegistratonMixin, DefaultAccountAdapter): """ def send_mail(self, template_prefix, email, context): """only send mail if backend configured""" - if InvenTreeSetting.get_setting('EMAIL_HOST', None): + if settings.EMAIL_HOST: return super().send_mail(template_prefix, email, context) return False diff --git a/InvenTree/common/models.py b/InvenTree/common/models.py index d4f26af739..a1b48d74d3 100644 --- a/InvenTree/common/models.py +++ b/InvenTree/common/models.py @@ -11,7 +11,7 @@ import decimal import math from django.db import models, transaction -from django.contrib.auth.models import User +from django.contrib.auth.models import User, Group from django.db.utils import IntegrityError, OperationalError from django.conf import settings @@ -845,6 +845,15 @@ class InvenTreeSetting(BaseInvenTreeSetting): 'default': True, 'validator': bool, }, + 'SIGNUP_GROUP': { + 'name': _('Group on signup'), + 'description': _('Group new user are asigned on registration'), + 'default': '', + 'choices': [ + ('', _('No group')), + *[(str(a.id), str(a)) for a in Group.objects.all()] + ], + }, } class Meta: diff --git a/InvenTree/templates/InvenTree/settings/login.html b/InvenTree/templates/InvenTree/settings/login.html index 289f87a3c9..57e0adb45e 100644 --- a/InvenTree/templates/InvenTree/settings/login.html +++ b/InvenTree/templates/InvenTree/settings/login.html @@ -25,6 +25,7 @@ {% include "InvenTree/settings/setting.html" with key="LOGIN_SIGNUP_MAIL_TWICE" icon="fa-info-circle" %} {% include "InvenTree/settings/setting.html" with key="LOGIN_SIGNUP_PWD_TWICE" icon="fa-info-circle" %} {% include "InvenTree/settings/setting.html" with key="LOGIN_SIGNUP_SSO_AUTO" icon="fa-info-circle" %} + {% include "InvenTree/settings/setting.html" with key="SIGNUP_GROUP" %} From 3a586af55645e1639b15d2a1cf8d79dd56040b50 Mon Sep 17 00:00:00 2001 From: Matthias Date: Thu, 14 Oct 2021 21:27:41 +0200 Subject: [PATCH 2/6] move setting to better fit grouping --- InvenTree/templates/InvenTree/settings/login.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/InvenTree/templates/InvenTree/settings/login.html b/InvenTree/templates/InvenTree/settings/login.html index 57e0adb45e..7ffebee91e 100644 --- a/InvenTree/templates/InvenTree/settings/login.html +++ b/InvenTree/templates/InvenTree/settings/login.html @@ -14,7 +14,6 @@ {% include "InvenTree/settings/header.html" %} - {% include "InvenTree/settings/setting.html" with key="LOGIN_ENABLE_REG" icon="fa-info-circle" %} {% include "InvenTree/settings/setting.html" with key="LOGIN_ENABLE_SSO" icon="fa-info-circle" %} {% include "InvenTree/settings/setting.html" with key="LOGIN_ENABLE_PWD_FORGOT" icon="fa-info-circle" %} {% include "InvenTree/settings/setting.html" with key="LOGIN_MAIL_REQUIRED" icon="fa-info-circle" %} @@ -22,6 +21,7 @@ + {% include "InvenTree/settings/setting.html" with key="LOGIN_ENABLE_REG" icon="fa-info-circle" %} {% include "InvenTree/settings/setting.html" with key="LOGIN_SIGNUP_MAIL_TWICE" icon="fa-info-circle" %} {% include "InvenTree/settings/setting.html" with key="LOGIN_SIGNUP_PWD_TWICE" icon="fa-info-circle" %} {% include "InvenTree/settings/setting.html" with key="LOGIN_SIGNUP_SSO_AUTO" icon="fa-info-circle" %} From f95896e8ea4086d285a6d4401bab100415432d1d Mon Sep 17 00:00:00 2001 From: Matthias Date: Thu, 14 Oct 2021 21:33:35 +0200 Subject: [PATCH 3/6] this was not meant to be submitted --- InvenTree/InvenTree/forms.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/InvenTree/InvenTree/forms.py b/InvenTree/InvenTree/forms.py index eede6b8d32..dd0af9b6c3 100644 --- a/InvenTree/InvenTree/forms.py +++ b/InvenTree/InvenTree/forms.py @@ -282,7 +282,7 @@ class CustomAccountAdapter(RegistratonMixin, DefaultAccountAdapter): """ def send_mail(self, template_prefix, email, context): """only send mail if backend configured""" - if settings.EMAIL_HOST: + if InvenTreeSetting.get_setting('EMAIL_HOST', None): return super().send_mail(template_prefix, email, context) return False From 27aec4246e80115061cab9e1481c24ec9e41d99f Mon Sep 17 00:00:00 2001 From: Matthias Date: Thu, 14 Oct 2021 21:33:54 +0200 Subject: [PATCH 4/6] PEP fix --- InvenTree/InvenTree/forms.py | 1 + 1 file changed, 1 insertion(+) diff --git a/InvenTree/InvenTree/forms.py b/InvenTree/InvenTree/forms.py index dd0af9b6c3..5903fea6e0 100644 --- a/InvenTree/InvenTree/forms.py +++ b/InvenTree/InvenTree/forms.py @@ -23,6 +23,7 @@ from common.models import InvenTreeSetting logger = logging.getLogger('inventree') + class HelperForm(forms.ModelForm): """ Provides simple integration of crispy_forms extension. """ From e0887cf55f3a902a5126420c5b33bcfa60fdf635 Mon Sep 17 00:00:00 2001 From: Matthias Date: Thu, 14 Oct 2021 22:16:07 +0200 Subject: [PATCH 5/6] move goup forming into own function --- InvenTree/common/models.py | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/InvenTree/common/models.py b/InvenTree/common/models.py index a1b48d74d3..0a5e87e3fa 100644 --- a/InvenTree/common/models.py +++ b/InvenTree/common/models.py @@ -182,12 +182,9 @@ class BaseInvenTreeSetting(models.Model): else: choices = None - """ - TODO: - if type(choices) is function: + if callable(choices): # Evaluate the function (we expect it will return a list of tuples...) return choices() - """ return choices @@ -478,6 +475,8 @@ class BaseInvenTreeSetting(models.Model): return value +def group_options(): + return [('', _('No group')), *[(str(a.id), str(a)) for a in Group.objects.all()]] class InvenTreeSetting(BaseInvenTreeSetting): """ @@ -849,10 +848,7 @@ class InvenTreeSetting(BaseInvenTreeSetting): 'name': _('Group on signup'), 'description': _('Group new user are asigned on registration'), 'default': '', - 'choices': [ - ('', _('No group')), - *[(str(a.id), str(a)) for a in Group.objects.all()] - ], + 'choices': group_options }, } From 0657b71fe8fdeca1992a4f950d310219e1844d2b Mon Sep 17 00:00:00 2001 From: Matthias Date: Thu, 14 Oct 2021 22:19:52 +0200 Subject: [PATCH 6/6] clearer name and PEP foxes --- InvenTree/common/models.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/InvenTree/common/models.py b/InvenTree/common/models.py index 0a5e87e3fa..3302d06eb1 100644 --- a/InvenTree/common/models.py +++ b/InvenTree/common/models.py @@ -475,9 +475,12 @@ class BaseInvenTreeSetting(models.Model): return value -def group_options(): + +def settings_group_options(): + """build up group tuple for settings based on gour choices""" return [('', _('No group')), *[(str(a.id), str(a)) for a in Group.objects.all()]] + class InvenTreeSetting(BaseInvenTreeSetting): """ An InvenTreeSetting object is a key:value pair used for storing @@ -848,7 +851,7 @@ class InvenTreeSetting(BaseInvenTreeSetting): 'name': _('Group on signup'), 'description': _('Group new user are asigned on registration'), 'default': '', - 'choices': group_options + 'choices': settings_group_options }, }
{% trans 'Signup' %}