diff --git a/README.md b/README.md index a2e47d0..144ddbd 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/parameter.py b/parameter.py index c3b33a6..978df33 100644 --- a/parameter.py +++ b/parameter.py @@ -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 diff --git a/templates/users/profile.html b/templates/users/profile.html index f91cb87..fe9d097 100644 --- a/templates/users/profile.html +++ b/templates/users/profile.html @@ -9,6 +9,11 @@

{% trans "Language and Timezone" %}

{{ form_userprofile.as_p }} + {% for head, form in ext_profiles.items %} +

{{ head }}

+ {{ form.as_p }} + {% endfor %} +

{% trans "Userdata and Password" %}

{{ form_userchange.as_p }} diff --git a/views.py b/views.py index b4f00fa..406a51b 100644 --- a/views.py +++ b/views.py @@ -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)