diff --git a/InvenTree/InvenTree/forms.py b/InvenTree/InvenTree/forms.py index 084de06472..279635756a 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 django.conf import settings from crispy_forms.helper import FormHelper @@ -21,6 +22,8 @@ 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. """ @@ -262,6 +265,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): """ diff --git a/InvenTree/common/models.py b/InvenTree/common/models.py index fbe143de5a..81fee4ff65 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 @@ -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 @@ -479,6 +476,11 @@ class BaseInvenTreeSetting(models.Model): return value +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 @@ -845,6 +847,12 @@ class InvenTreeSetting(BaseInvenTreeSetting): 'default': True, 'validator': bool, }, + 'SIGNUP_GROUP': { + 'name': _('Group on signup'), + 'description': _('Group new user are asigned on registration'), + 'default': '', + 'choices': settings_group_options + }, } class Meta: diff --git a/InvenTree/templates/InvenTree/settings/login.html b/InvenTree/templates/InvenTree/settings/login.html index 289f87a3c9..7ffebee91e 100644 --- a/InvenTree/templates/InvenTree/settings/login.html +++ b/InvenTree/templates/InvenTree/settings/login.html @@ -14,7 +14,6 @@
{% trans 'Signup' %} | + {% 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" %} + {% include "InvenTree/settings/setting.html" with key="SIGNUP_GROUP" %} |