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 @@