Преглед изворни кода

Interface to define additional profile parameters added

master
Dirk Alders пре 1 месец
родитељ
комит
89abad4430
4 измењених фајлова са 55 додато и 2 уклоњено
  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 Прегледај датотеку

@@ -42,6 +42,32 @@ This parameter can be ```True``` or ```False```. It enables or disables the mail
42 42
 #### USERS_ADMIN_ACTIVATION
43 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 72
 ## Usage
47 73
 ### Actionabr

+ 15
- 1
parameter.py Прегледај датотеку

@@ -1,18 +1,29 @@
1 1
 import config
2 2
 from django.conf import settings
3 3
 from django.utils.translation import gettext as _
4
+import importlib
4 5
 
5 6
 USERS_SELF_REGISTRATION = "USERS_SELF_REGISTRATION"
6 7
 USERS_MAIL_VALIDATION = "USERS_MAIL_VALIDATION"
7 8
 USERS_ADMIN_ACTIVATION = "USERS_ADMIN_ACTIVATION"
8 9
 
10
+USERS_PROFILE_ADDITIONS = "USERS_PROFILE_ADDITIONS"
11
+
9 12
 
10 13
 DEFAULTS = {
11 14
     USERS_SELF_REGISTRATION: False,
12 15
     USERS_MAIL_VALIDATION: True,
13 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 28
 def get(key):
18 29
     # take data from config, settings or defaults
@@ -23,7 +34,10 @@ def get(key):
23 34
             data = getattr(settings, key)
24 35
         except AttributeError:
25 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 41
     return data
28 42
 
29 43
 

+ 5
- 0
templates/users/profile.html Прегледај датотеку

@@ -9,6 +9,11 @@
9 9
     <h1>{% trans "Language and Timezone" %}</h1>
10 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 17
     <h1>{% trans "Userdata and Password" %}</h1>
13 18
     {{ form_userchange.as_p }}
14 19
 

+ 9
- 1
views.py Прегледај датотеку

@@ -32,12 +32,19 @@ def password_recovery(request):
32 32
 def profile(request):
33 33
     context = Context(request)      # needs to be executed first because of time mesurement
34 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 39
     if request.POST:
36 40
         form_userprofile = UserProfileForm(request.POST, instance=profile)
37 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 44
             form_userprofile.save()
40 45
             form_userchange.save()
46
+            for form in ext_profiles.values():
47
+                form.save()
41 48
             return redirect(request.GET.get('next') or '/')
42 49
     else:
43 50
         form_userprofile = UserProfileForm(instance=profile)
@@ -48,6 +55,7 @@ def profile(request):
48 55
         _('Profile for %(username)s') % {'username': request.user.username},
49 56
         form_userprofile=form_userprofile,
50 57
         form_userchange=form_userchange,
58
+        ext_profiles=ext_profiles,
51 59
     )
52 60
     return render(request, 'users/profile.html', context=context)
53 61
 

Loading…
Откажи
Сачувај