diff --git a/README.md b/README.md index 821da6e..23c46a7 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,37 @@ # users -Django Library Users \ No newline at end of file +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. + +## Requirements +### Python +You need to ensure that pytz is available in your python environment. + +### Django libs +You need to integrate the themes library as well. + +## Integration + +Clone the library in your django application. + +### Configurations in your settings.py +Add the following line to the list ```INSTALLED_APPS```: +``` + 'users.apps.UsersConfig', +``` + +### Configurations in your urls.py +and add the following line to the list ```urlpatterns```: +``` + path('users/', include('users.urls')), +``` + +### Parameter +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```. + +#### USERS_SELF_REGISTRATION +This parameter can be ```True``` or ```False```. It enables or disables the self registration. + + +## Usage +### Actionabr +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. diff --git a/context.py b/context.py index 3ae386a..ceda0ca 100644 --- a/context.py +++ b/context.py @@ -2,6 +2,7 @@ from django.urls.base import reverse from django.utils.translation import gettext as _ from themes import empty_entry_parameters, color_icon_url from . import url_login, url_logout, url_register, url_profile +from . import parameter ADMIN_ENTRY_UID = 'admin-main' LOGIN_ENTRY_UID = 'login-main' @@ -29,7 +30,8 @@ def menubar(bar, request): def actionbar(bar, request): bar.append_entry(*login_entry_parameters(request, left=True)) - bar.append_entry(*register_entry_parameters(request, left=True)) + if parameter.get(parameter.USERS_SELF_REGISTRATION): + bar.append_entry(*register_entry_parameters(request, left=True)) def login_entry_parameters(request, left=False): diff --git a/parameter.py b/parameter.py new file mode 100644 index 0000000..59b3587 --- /dev/null +++ b/parameter.py @@ -0,0 +1,29 @@ +import config +from django.conf import settings + +USERS_SELF_REGISTRATION = "USERS_SELF_REGISTRATION" +# TODO: Implement or find the mail validation and the methods to ask for the user status +# USERS_MAIL_VALIDATION = "USERS_MAIL_VALIDATION" +# TODO: Implement or mail information (Admins: New Useraccounts, Failed logins; User: Password Change, ...) +# USERS_MAIL_INFORMATION = "USERS_MAIL_INFORMATION" + + +DEFAULTS = { + # TODO: Implement the parameer functionality USERS_SELF_REGISTRATION + USERS_SELF_REGISTRATION: False, + # USERS_MAIL_VALIDATION: True, + # USERS_MAIL_INFORMATION: True, +} + + +def get(key): + # take data from config, settings or defaults + try: + data = getattr(config, key) + except AttributeError: + try: + data = getattr(settings, key) + except AttributeError: + data = DEFAULTS.get(key) + + return data diff --git a/views.py b/views.py index 051fe29..52852b2 100644 --- a/views.py +++ b/views.py @@ -9,12 +9,24 @@ from django.contrib.auth.decorators import login_required from django.contrib.auth.forms import AuthenticationForm from django.utils.translation import gettext as _ from .forms import UserRegistrationForm, UserProfileForm +import logging from .models import get_userprofile from themes import Context import users +from users import parameter + +# TODO: Implement password change. + +try: + from config import APP_NAME as ROOT_LOGGER_NAME +except ImportError: + ROOT_LOGGER_NAME = 'root' +logger = logging.getLogger(ROOT_LOGGER_NAME).getChild(__name__) def password_recovery(request): + # TODO: Implement password recovery + messages.error(request, "Password recovery is not yet implemented!") return redirect(request.GET.get('next') or '/') @@ -53,20 +65,25 @@ def profile(request): def register(request): context = Context(request) # needs to be executed first because of time mesurement - context_adaption(context, request, _('Register')) - if not request.POST: - form = UserRegistrationForm() - messages.info(request, _('If you already have an account, login here.') % {'url': users.url_login(request)}) - else: - form = UserRegistrationForm(request.POST) - if form.is_valid(): - form.save() - messages.success(request, _('Your account has been created! You are able to log in as %(username)s.') % {'username': form.cleaned_data.get('username')}) - return redirect('users-login') + if parameter.get(parameter.USERS_SELF_REGISTRATION): + context_adaption(context, request, _('Register')) + if not request.POST: + form = UserRegistrationForm() + messages.info(request, _('If you already have an account, login here.') % {'url': users.url_login(request)}) else: - messages.error(request, _('Registration failed!')) - context['form'] = form - return render(request, 'users/register.html', context) + form = UserRegistrationForm(request.POST) + if form.is_valid(): + form.save() + messages.success(request, _('Your account has been created! You are able to log in as %(username)s.') % + {'username': form.cleaned_data.get('username')}) + return redirect('users-login') + else: + messages.error(request, _('Registration failed!')) + context['form'] = form + return render(request, 'users/register.html', context) + else: + messages.info(request, _("Self registration is deactivated. Contact your system administrator.")) + return redirect('users-login') def login(request): @@ -74,7 +91,8 @@ def login(request): context_adaption(context, request, _('Login')) if not request.POST: form = AuthenticationForm() - messages.info(request, _('If you don\'t have an acount, register here.') % {'url': users.url_register(request)}) + if parameter.get(parameter.USERS_SELF_REGISTRATION): + messages.info(request, _('If you don\'t have an acount, register here.') % {'url': users.url_register(request)}) else: form = AuthenticationForm(request, data=request.POST) if form.is_valid(): @@ -84,7 +102,13 @@ def login(request): messages.success(request, _('You are now logged in as %(username)s.') % {'username': username}) return redirect(request.GET.get('next') or '/') else: - messages.error(request, _('Login failed! You can do a password recorvery here or you can register here.') % {'url_register': users.url_register(request), 'url_recover': users.url_password_recovery(request)}) + if parameter.get(parameter.USERS_SELF_REGISTRATION): + messages.error(request, _('Login failed! You can do a password recorvery here or you can register here.') % + {'url_register': users.url_register(request), 'url_recover': users.url_password_recovery(request)}) + else: + messages.error(request, _('Login failed! You can do a password recorvery here.') % + {'url_recover': users.url_password_recovery(request)}) + context['form'] = form return render(request, 'users/login.html', context)