Browse Source

Parameter added, first parameter is USERS_SELF_REGISTRATION

master
Dirk Alders 2 months ago
parent
commit
57852e6ec8
4 changed files with 106 additions and 17 deletions
  1. 35
    1
      README.md
  2. 3
    1
      context.py
  3. 29
    0
      parameter.py
  4. 39
    15
      views.py

+ 35
- 1
README.md View File

@@ -1,3 +1,37 @@
1 1
 # users
2 2
 
3
-Django Library Users
3
+With the django library users, you are abel to register users, add users, login, logout and do some basic adjustments for the user. This library includes some pages for that based on the extension theme.
4
+
5
+## Requirements
6
+### Python
7
+You need to ensure that pytz is available in your python environment.
8
+
9
+### Django libs
10
+You need to integrate the themes library as well.
11
+
12
+## Integration
13
+
14
+Clone the library in your django application.
15
+
16
+### Configurations in your settings.py
17
+Add the following line to the list ```INSTALLED_APPS```:
18
+```
19
+    'users.apps.UsersConfig',
20
+``` 
21
+
22
+### Configurations in your urls.py
23
+and add the following line to the list ```urlpatterns```:
24
+```
25
+    path('users/', include('users.urls')),
26
+``` 
27
+
28
+### Parameter
29
+All parameters can be added in the django ```settings.py``` or in a ```config.py``` in the root django folder. The definitions in the ```config.py``` will be used before the definitions in ```settings.py```.
30
+
31
+#### USERS_SELF_REGISTRATION
32
+This parameter can be ```True``` or ```False```. It enables or disables the self registration.
33
+
34
+
35
+## Usage
36
+### Actionabr
37
+You might want to add the user actions by calling ```users.context.menubar(bar, request)``` with ```bar``` as actionbar. See theme dosumentation for more details.

+ 3
- 1
context.py View File

@@ -2,6 +2,7 @@ from django.urls.base import reverse
2 2
 from django.utils.translation import gettext as _
3 3
 from themes import empty_entry_parameters, color_icon_url
4 4
 from . import url_login, url_logout, url_register, url_profile
5
+from . import parameter
5 6
 
6 7
 ADMIN_ENTRY_UID = 'admin-main'
7 8
 LOGIN_ENTRY_UID = 'login-main'
@@ -29,7 +30,8 @@ def menubar(bar, request):
29 30
 
30 31
 def actionbar(bar, request):
31 32
     bar.append_entry(*login_entry_parameters(request, left=True))
32
-    bar.append_entry(*register_entry_parameters(request, left=True))
33
+    if parameter.get(parameter.USERS_SELF_REGISTRATION):
34
+        bar.append_entry(*register_entry_parameters(request, left=True))
33 35
 
34 36
 
35 37
 def login_entry_parameters(request, left=False):

+ 29
- 0
parameter.py View File

@@ -0,0 +1,29 @@
1
+import config
2
+from django.conf import settings
3
+
4
+USERS_SELF_REGISTRATION = "USERS_SELF_REGISTRATION"
5
+# TODO: Implement or find the mail validation and the methods to ask for the user status
6
+# USERS_MAIL_VALIDATION = "USERS_MAIL_VALIDATION"
7
+# TODO: Implement or mail information (Admins: New Useraccounts, Failed logins; User: Password Change, ...)
8
+# USERS_MAIL_INFORMATION = "USERS_MAIL_INFORMATION"
9
+
10
+
11
+DEFAULTS = {
12
+    # TODO: Implement the parameer functionality USERS_SELF_REGISTRATION
13
+    USERS_SELF_REGISTRATION: False,
14
+    # USERS_MAIL_VALIDATION: True,
15
+    # USERS_MAIL_INFORMATION: True,
16
+}
17
+
18
+
19
+def get(key):
20
+    # take data from config, settings or defaults
21
+    try:
22
+        data = getattr(config, key)
23
+    except AttributeError:
24
+        try:
25
+            data = getattr(settings, key)
26
+        except AttributeError:
27
+            data = DEFAULTS.get(key)
28
+
29
+    return data

+ 39
- 15
views.py View File

@@ -9,12 +9,24 @@ from django.contrib.auth.decorators import login_required
9 9
 from django.contrib.auth.forms import AuthenticationForm
10 10
 from django.utils.translation import gettext as _
11 11
 from .forms import UserRegistrationForm, UserProfileForm
12
+import logging
12 13
 from .models import get_userprofile
13 14
 from themes import Context
14 15
 import users
16
+from users import parameter
17
+
18
+# TODO: Implement password change.
19
+
20
+try:
21
+    from config import APP_NAME as ROOT_LOGGER_NAME
22
+except ImportError:
23
+    ROOT_LOGGER_NAME = 'root'
24
+logger = logging.getLogger(ROOT_LOGGER_NAME).getChild(__name__)
15 25
 
16 26
 
17 27
 def password_recovery(request):
28
+    # TODO: Implement password recovery
29
+    messages.error(request, "Password recovery is not yet implemented!")
18 30
     return redirect(request.GET.get('next') or '/')
19 31
 
20 32
 
@@ -53,20 +65,25 @@ def profile(request):
53 65
 
54 66
 def register(request):
55 67
     context = Context(request)      # needs to be executed first because of time mesurement
56
-    context_adaption(context, request, _('Register'))
57
-    if not request.POST:
58
-        form = UserRegistrationForm()
59
-        messages.info(request, _('If you already have an account, login <a href="%(url)s">here</a>.') % {'url': users.url_login(request)})
60
-    else:
61
-        form = UserRegistrationForm(request.POST)
62
-        if form.is_valid():
63
-            form.save()
64
-            messages.success(request, _('Your account has been created! You are able to log in as %(username)s.') % {'username': form.cleaned_data.get('username')})
65
-            return redirect('users-login')
68
+    if parameter.get(parameter.USERS_SELF_REGISTRATION):
69
+        context_adaption(context, request, _('Register'))
70
+        if not request.POST:
71
+            form = UserRegistrationForm()
72
+            messages.info(request, _('If you already have an account, login <a href="%(url)s">here</a>.') % {'url': users.url_login(request)})
66 73
         else:
67
-            messages.error(request, _('Registration failed!'))
68
-    context['form'] = form
69
-    return render(request, 'users/register.html', context)
74
+            form = UserRegistrationForm(request.POST)
75
+            if form.is_valid():
76
+                form.save()
77
+                messages.success(request, _('Your account has been created! You are able to log in as %(username)s.') %
78
+                                 {'username': form.cleaned_data.get('username')})
79
+                return redirect('users-login')
80
+            else:
81
+                messages.error(request, _('Registration failed!'))
82
+        context['form'] = form
83
+        return render(request, 'users/register.html', context)
84
+    else:
85
+        messages.info(request, _("Self registration is deactivated. Contact your system administrator."))
86
+        return redirect('users-login')
70 87
 
71 88
 
72 89
 def login(request):
@@ -74,7 +91,8 @@ def login(request):
74 91
     context_adaption(context, request, _('Login'))
75 92
     if not request.POST:
76 93
         form = AuthenticationForm()
77
-        messages.info(request, _('If you don\'t have an acount, register <a href="%(url)s">here</a>.') % {'url': users.url_register(request)})
94
+        if parameter.get(parameter.USERS_SELF_REGISTRATION):
95
+            messages.info(request, _('If you don\'t have an acount, register <a href="%(url)s">here</a>.') % {'url': users.url_register(request)})
78 96
     else:
79 97
         form = AuthenticationForm(request, data=request.POST)
80 98
         if form.is_valid():
@@ -84,7 +102,13 @@ def login(request):
84 102
             messages.success(request, _('You are now logged in as %(username)s.') % {'username': username})
85 103
             return redirect(request.GET.get('next') or '/')
86 104
         else:
87
-            messages.error(request, _('Login failed! You can do a password recorvery <a href="%(url_recover)s">here</a> or you can register <a href="%(url_register)s">here</a>.') % {'url_register': users.url_register(request), 'url_recover': users.url_password_recovery(request)})
105
+            if parameter.get(parameter.USERS_SELF_REGISTRATION):
106
+                messages.error(request, _('Login failed! You can do a password recorvery <a href="%(url_recover)s">here</a> or you can register <a href="%(url_register)s">here</a>.') %
107
+                               {'url_register': users.url_register(request), 'url_recover': users.url_password_recovery(request)})
108
+            else:
109
+                messages.error(request, _('Login failed! You can do a password recorvery <a href="%(url_recover)s">here</a>.') %
110
+                               {'url_recover': users.url_password_recovery(request)})
111
+
88 112
     context['form'] = form
89 113
     return render(request, 'users/login.html', context)
90 114
 

Loading…
Cancel
Save