Bladeren bron

Interface to define additional profile parameters added

master
Dirk Alders 1 maand geleden
bovenliggende
commit
89abad4430
4 gewijzigde bestanden met toevoegingen van 55 en 2 verwijderingen
  1. 26
    0
      README.md
  2. 15
    1
      parameter.py
  3. 5
    0
      templates/users/profile.html
  4. 9
    1
      views.py

+ 26
- 0
README.md Bestand weergeven

42
 #### USERS_ADMIN_ACTIVATION
42
 #### USERS_ADMIN_ACTIVATION
43
 This parameter can be ```True``` or ```False```. It enables or disables the activation by an admin after mail validation.
43
 This parameter can be ```True``` or ```False```. It enables or disables the activation by an admin after mail validation.
44
 
44
 
45
+#### USERS_PROFILE_ADDITIONS
46
+With the USER_PROFILE_ADDITIONS, you can define additional profile parameters for a user. You need to define a model:
47
+```
48
+# USERPROFILE Model
49
+#
50
+class YourUserProfile(models.Model):
51
+    user = models.OneToOneField(User, unique=True, on_delete=models.CASCADE)
52
+    your_parameter = models.IntegerField(default=10)
53
+```
54
+and a form (called with only one parameter ```request```):
55
+```
56
+class YourUserProfileForm(forms.ModelForm):
57
+    class Meta:
58
+        model = YourUserProfile
59
+        fields = ['your_parameter', ]
60
+    def __init__(self, request):
61
+        if request.POST:
62
+            super().__init__(data=request.POST, instance=get_pattuserprofile(request.user))
63
+        else:
64
+            super().__init__(instance=get_pattuserprofile(request.user))
65
+```
66
+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:
67
+```
68
+USERS_PROFILE_ADDITIONS = {"Your Profile": 'your.forms.YourUserProfileForm'}
69
+
70
+```
45
 
71
 
46
 ## Usage
72
 ## Usage
47
 ### Actionabr
73
 ### Actionabr

+ 15
- 1
parameter.py Bestand weergeven

1
 import config
1
 import config
2
 from django.conf import settings
2
 from django.conf import settings
3
 from django.utils.translation import gettext as _
3
 from django.utils.translation import gettext as _
4
+import importlib
4
 
5
 
5
 USERS_SELF_REGISTRATION = "USERS_SELF_REGISTRATION"
6
 USERS_SELF_REGISTRATION = "USERS_SELF_REGISTRATION"
6
 USERS_MAIL_VALIDATION = "USERS_MAIL_VALIDATION"
7
 USERS_MAIL_VALIDATION = "USERS_MAIL_VALIDATION"
7
 USERS_ADMIN_ACTIVATION = "USERS_ADMIN_ACTIVATION"
8
 USERS_ADMIN_ACTIVATION = "USERS_ADMIN_ACTIVATION"
8
 
9
 
10
+USERS_PROFILE_ADDITIONS = "USERS_PROFILE_ADDITIONS"
11
+
9
 
12
 
10
 DEFAULTS = {
13
 DEFAULTS = {
11
     USERS_SELF_REGISTRATION: False,
14
     USERS_SELF_REGISTRATION: False,
12
     USERS_MAIL_VALIDATION: True,
15
     USERS_MAIL_VALIDATION: True,
13
     USERS_ADMIN_ACTIVATION: True,
16
     USERS_ADMIN_ACTIVATION: True,
17
+    USERS_PROFILE_ADDITIONS: {},
14
 }
18
 }
15
 
19
 
20
+def __get_object_by_name__(object_name):
21
+    class_data = object_name.split(".")
22
+    module_path = ".".join(class_data[:-1])
23
+    class_str = class_data[-1]
24
+    #
25
+    module = importlib.import_module(module_path)
26
+    return getattr(module, class_str)
16
 
27
 
17
 def get(key):
28
 def get(key):
18
     # take data from config, settings or defaults
29
     # take data from config, settings or defaults
23
             data = getattr(settings, key)
34
             data = getattr(settings, key)
24
         except AttributeError:
35
         except AttributeError:
25
             data = DEFAULTS.get(key)
36
             data = DEFAULTS.get(key)
26
-
37
+    if key in [USERS_PROFILE_ADDITIONS, ]:
38
+        # Change given string to object
39
+        return {key: __get_object_by_name__(data[key]) for key in data}
40
+    #
27
     return data
41
     return data
28
 
42
 
29
 
43
 

+ 5
- 0
templates/users/profile.html Bestand weergeven

9
     <h1>{% trans "Language and Timezone" %}</h1>
9
     <h1>{% trans "Language and Timezone" %}</h1>
10
     {{ form_userprofile.as_p }}
10
     {{ form_userprofile.as_p }}
11
 
11
 
12
+    {% for head, form in ext_profiles.items %}
13
+      <h1>{{ head }}</h1>
14
+      {{ form.as_p }}
15
+    {% endfor %}
16
+
12
     <h1>{% trans "Userdata and Password" %}</h1>
17
     <h1>{% trans "Userdata and Password" %}</h1>
13
     {{ form_userchange.as_p }}
18
     {{ form_userchange.as_p }}
14
 
19
 

+ 9
- 1
views.py Bestand weergeven

32
 def profile(request):
32
 def profile(request):
33
     context = Context(request)      # needs to be executed first because of time mesurement
33
     context = Context(request)      # needs to be executed first because of time mesurement
34
     profile = get_userprofile(request.user)
34
     profile = get_userprofile(request.user)
35
+    # External Additional forms
36
+    ext_profiles = parameter.get(parameter.USERS_PROFILE_ADDITIONS)
37
+    # change class to instance
38
+    ext_profiles = {key: ext_profiles[key](request) for key in ext_profiles}
35
     if request.POST:
39
     if request.POST:
36
         form_userprofile = UserProfileForm(request.POST, instance=profile)
40
         form_userprofile = UserProfileForm(request.POST, instance=profile)
37
         form_userchange = UserPasswordChangeForm(request)
41
         form_userchange = UserPasswordChangeForm(request)
38
-        if form_userprofile.is_valid() and form_userchange.is_valid():
42
+        ext_valid = [form.is_valid() for form in ext_profiles.values()]
43
+        if form_userprofile.is_valid() and form_userchange.is_valid() and not False in ext_valid:
39
             form_userprofile.save()
44
             form_userprofile.save()
40
             form_userchange.save()
45
             form_userchange.save()
46
+            for form in ext_profiles.values():
47
+                form.save()
41
             return redirect(request.GET.get('next') or '/')
48
             return redirect(request.GET.get('next') or '/')
42
     else:
49
     else:
43
         form_userprofile = UserProfileForm(instance=profile)
50
         form_userprofile = UserProfileForm(instance=profile)
48
         _('Profile for %(username)s') % {'username': request.user.username},
55
         _('Profile for %(username)s') % {'username': request.user.username},
49
         form_userprofile=form_userprofile,
56
         form_userprofile=form_userprofile,
50
         form_userchange=form_userchange,
57
         form_userchange=form_userchange,
58
+        ext_profiles=ext_profiles,
51
     )
59
     )
52
     return render(request, 'users/profile.html', context=context)
60
     return render(request, 'users/profile.html', context=context)
53
 
61
 

Laden…
Annuleren
Opslaan