Interface to define additional profile parameters added

This commit is contained in:
Dirk Alders 2024-11-01 20:26:53 +01:00
parent f31771b588
commit 89abad4430
4 changed files with 55 additions and 2 deletions

View File

@ -42,6 +42,32 @@ This parameter can be ```True``` or ```False```. It enables or disables the mail
#### USERS_ADMIN_ACTIVATION
This parameter can be ```True``` or ```False```. It enables or disables the activation by an admin after mail validation.
#### USERS_PROFILE_ADDITIONS
With the USER_PROFILE_ADDITIONS, you can define additional profile parameters for a user. You need to define a model:
```
# USERPROFILE Model
#
class YourUserProfile(models.Model):
user = models.OneToOneField(User, unique=True, on_delete=models.CASCADE)
your_parameter = models.IntegerField(default=10)
```
and a form (called with only one parameter ```request```):
```
class YourUserProfileForm(forms.ModelForm):
class Meta:
model = YourUserProfile
fields = ['your_parameter', ]
def __init__(self, request):
if request.POST:
super().__init__(data=request.POST, instance=get_pattuserprofile(request.user))
else:
super().__init__(instance=get_pattuserprofile(request.user))
```
The USER_PROFILE_ADDITIONS is a dictionary where the key is the Heading and the value is the Form to be used. It shall be defined in ```settings.py``` like this:
```
USERS_PROFILE_ADDITIONS = {"Your Profile": 'your.forms.YourUserProfileForm'}
```
## Usage
### Actionabr

View File

@ -1,18 +1,29 @@
import config
from django.conf import settings
from django.utils.translation import gettext as _
import importlib
USERS_SELF_REGISTRATION = "USERS_SELF_REGISTRATION"
USERS_MAIL_VALIDATION = "USERS_MAIL_VALIDATION"
USERS_ADMIN_ACTIVATION = "USERS_ADMIN_ACTIVATION"
USERS_PROFILE_ADDITIONS = "USERS_PROFILE_ADDITIONS"
DEFAULTS = {
USERS_SELF_REGISTRATION: False,
USERS_MAIL_VALIDATION: True,
USERS_ADMIN_ACTIVATION: True,
USERS_PROFILE_ADDITIONS: {},
}
def __get_object_by_name__(object_name):
class_data = object_name.split(".")
module_path = ".".join(class_data[:-1])
class_str = class_data[-1]
#
module = importlib.import_module(module_path)
return getattr(module, class_str)
def get(key):
# take data from config, settings or defaults
@ -23,7 +34,10 @@ def get(key):
data = getattr(settings, key)
except AttributeError:
data = DEFAULTS.get(key)
if key in [USERS_PROFILE_ADDITIONS, ]:
# Change given string to object
return {key: __get_object_by_name__(data[key]) for key in data}
#
return data

View File

@ -9,6 +9,11 @@
<h1>{% trans "Language and Timezone" %}</h1>
{{ form_userprofile.as_p }}
{% for head, form in ext_profiles.items %}
<h1>{{ head }}</h1>
{{ form.as_p }}
{% endfor %}
<h1>{% trans "Userdata and Password" %}</h1>
{{ form_userchange.as_p }}

View File

@ -32,12 +32,19 @@ def password_recovery(request):
def profile(request):
context = Context(request) # needs to be executed first because of time mesurement
profile = get_userprofile(request.user)
# External Additional forms
ext_profiles = parameter.get(parameter.USERS_PROFILE_ADDITIONS)
# change class to instance
ext_profiles = {key: ext_profiles[key](request) for key in ext_profiles}
if request.POST:
form_userprofile = UserProfileForm(request.POST, instance=profile)
form_userchange = UserPasswordChangeForm(request)
if form_userprofile.is_valid() and form_userchange.is_valid():
ext_valid = [form.is_valid() for form in ext_profiles.values()]
if form_userprofile.is_valid() and form_userchange.is_valid() and not False in ext_valid:
form_userprofile.save()
form_userchange.save()
for form in ext_profiles.values():
form.save()
return redirect(request.GET.get('next') or '/')
else:
form_userprofile = UserProfileForm(instance=profile)
@ -48,6 +55,7 @@ def profile(request):
_('Profile for %(username)s') % {'username': request.user.username},
form_userprofile=form_userprofile,
form_userchange=form_userchange,
ext_profiles=ext_profiles,
)
return render(request, 'users/profile.html', context=context)