registration fixes

This commit is contained in:
Alicja Cięciwa
2020-11-15 13:32:50 +01:00
parent 0525450a90
commit 3ffce6c7c3
15 changed files with 117 additions and 63 deletions

8
.idea/workspace.xml generated
View File

@@ -3,7 +3,13 @@
<component name="ChangeListManager"> <component name="ChangeListManager">
<list default="true" id="15f590a5-5017-44f1-a85e-17dfe3fc5379" name="Default Changelist" comment=""> <list default="true" id="15f590a5-5017-44f1-a85e-17dfe3fc5379" name="Default Changelist" comment="">
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" /> <change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/static/css/main.css" beforeDir="false" afterPath="$PROJECT_DIR$/static/css/main.css" afterDir="false" /> <change beforePath="$PROJECT_DIR$/Lib/site-packages/django/contrib/auth/forms.py" beforeDir="false" afterPath="$PROJECT_DIR$/Lib/site-packages/django/contrib/auth/forms.py" afterDir="false" />
<change beforePath="$PROJECT_DIR$/Lib/site-packages/django/contrib/auth/password_validation.py" beforeDir="false" afterPath="$PROJECT_DIR$/Lib/site-packages/django/contrib/auth/password_validation.py" afterDir="false" />
<change beforePath="$PROJECT_DIR$/accounts/forms.py" beforeDir="false" afterPath="$PROJECT_DIR$/accounts/forms.py" afterDir="false" />
<change beforePath="$PROJECT_DIR$/accounts/views.py" beforeDir="false" afterPath="$PROJECT_DIR$/accounts/views.py" afterDir="false" />
<change beforePath="$PROJECT_DIR$/config/urls.py" beforeDir="false" afterPath="$PROJECT_DIR$/config/urls.py" afterDir="false" />
<change beforePath="$PROJECT_DIR$/templates/base.html" beforeDir="false" afterPath="$PROJECT_DIR$/templates/base.html" afterDir="false" />
<change beforePath="$PROJECT_DIR$/work/views.py" beforeDir="false" afterPath="$PROJECT_DIR$/work/views.py" afterDir="false" />
</list> </list>
<option name="EXCLUDED_CONVERTED_TO_IGNORED" value="true" /> <option name="EXCLUDED_CONVERTED_TO_IGNORED" value="true" />
<option name="SHOW_DIALOG" value="false" /> <option name="SHOW_DIALOG" value="false" />

View File

@@ -85,19 +85,19 @@ class UserCreationForm(forms.ModelForm):
password. password.
""" """
error_messages = { error_messages = {
'password_mismatch': _('The two password fields didnt match.'), 'password_mismatch': _('Hasła się nie zgadzają'),
} }
password1 = forms.CharField( password1 = forms.CharField(
label=_("Password"), label="",
strip=False, strip=False,
widget=forms.PasswordInput(attrs={'autocomplete': 'new-password'}), widget=forms.PasswordInput(attrs={'autocomplete': 'new-password'}),
help_text=password_validation.password_validators_help_text_html(), help_text=None,
) )
password2 = forms.CharField( password2 = forms.CharField(
label=_("Password confirmation"), label="",
widget=forms.PasswordInput(attrs={'autocomplete': 'new-password'}), widget=forms.PasswordInput(attrs={'autocomplete': 'new-password'}),
strip=False, strip=False,
help_text=_("Enter the same password as before, for verification."), help_text=None,
) )
class Meta: class Meta:
@@ -109,6 +109,32 @@ class UserCreationForm(forms.ModelForm):
super().__init__(*args, **kwargs) super().__init__(*args, **kwargs)
if self._meta.model.USERNAME_FIELD in self.fields: if self._meta.model.USERNAME_FIELD in self.fields:
self.fields[self._meta.model.USERNAME_FIELD].widget.attrs['autofocus'] = True self.fields[self._meta.model.USERNAME_FIELD].widget.attrs['autofocus'] = True
self.fields['username'].help_text = None
self.fields['username'] = forms.CharField(
widget=forms.TextInput(attrs={
'placeholder': 'Nazwa użytkownika'
}),
error_messages={
'required': 'Wpisz nazwę użytkownika',
})
self.fields['password1'] = forms.CharField(
widget=forms.PasswordInput(attrs={
'placeholder': 'Hasło'
}),
error_messages={
'required': 'Podaj hasło',
})
self.fields['password2'] = forms.CharField(
widget=forms.PasswordInput(attrs={
'placeholder': 'Powtórz hasło'
}),
error_messages={
'required': 'Podaj hasło',
})
for field in ['username', 'password1', 'password2']:
self.fields[field].help_text = None
self.fields[field].label = ''
def clean_password2(self): def clean_password2(self):
password1 = self.cleaned_data.get("password1") password1 = self.cleaned_data.get("password1")

View File

@@ -99,8 +99,8 @@ class MinimumLengthValidator:
if len(password) < self.min_length: if len(password) < self.min_length:
raise ValidationError( raise ValidationError(
ngettext( ngettext(
"This password is too short. It must contain at least %(min_length)d character.", "Hasło jest zbyt krótkie - musi zawierać przynajmniej %(min_length)d znaków.",
"This password is too short. It must contain at least %(min_length)d characters.", "Hasło jest zbyt krótkie - musi zawierać przynajmniej %(min_length)d znaków.",
self.min_length self.min_length
), ),
code='password_too_short', code='password_too_short',
@@ -109,8 +109,8 @@ class MinimumLengthValidator:
def get_help_text(self): def get_help_text(self):
return ngettext( return ngettext(
"Your password must contain at least %(min_length)d character.", "Hasło musi zawierać przynajmniej %(min_length)d znaków.",
"Your password must contain at least %(min_length)d characters.", "Hasło musi zawierać przynajmniej %(min_length)d znaków.",
self.min_length self.min_length
) % {'min_length': self.min_length} ) % {'min_length': self.min_length}
@@ -148,7 +148,7 @@ class UserAttributeSimilarityValidator:
except FieldDoesNotExist: except FieldDoesNotExist:
verbose_name = attribute_name verbose_name = attribute_name
raise ValidationError( raise ValidationError(
_("The password is too similar to the %(verbose_name)s."), _("Hasło jest zbyt podobne do %(verbose_name)s."),
code='password_too_similar', code='password_too_similar',
params={'verbose_name': verbose_name}, params={'verbose_name': verbose_name},
) )
@@ -180,7 +180,7 @@ class CommonPasswordValidator:
def validate(self, password, user=None): def validate(self, password, user=None):
if password.lower().strip() in self.passwords: if password.lower().strip() in self.passwords:
raise ValidationError( raise ValidationError(
_("This password is too common."), _("To hasło jest zbyt powszechne."),
code='password_too_common', code='password_too_common',
) )

View File

@@ -3,40 +3,40 @@ from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth.models import User from django.contrib.auth.models import User
class RegisterForm(UserCreationForm): # class RegisterForm(UserCreationForm):
#
class Meta: # class Meta:
model = User # model = User
fields = ['username', 'password1', 'password2'] # fields = ['username', 'password1', 'password2']
#
def __init__(self, *args, **kwargs): # def __init__(self, *args, **kwargs):
super(RegisterForm, self).__init__(*args, **kwargs) # super(RegisterForm, self).__init__(*args, **kwargs)
self.fields['username'] = forms.CharField( # self.fields['username'] = forms.CharField(
widget=forms.TextInput(attrs={ # widget=forms.TextInput(attrs={
'placeholder': 'Nazwa użytkownika' # 'placeholder': 'Nazwa użytkownika'
}), # }),
error_messages={ # error_messages={
'required': 'Wpisz nazwę użytkownika', # 'required': 'Wpisz nazwę użytkownika',
}) # })
self.fields['password1'] = forms.CharField( # self.fields['password1'] = forms.CharField(
widget=forms.PasswordInput(attrs={ # widget=forms.PasswordInput(attrs={
'placeholder': 'Hasło' # 'placeholder': 'Hasło'
}), # }),
error_messages={ # error_messages={
'required': 'Podaj hasło', # 'required': 'Podaj hasło',
}) # })
self.fields['password2'] = forms.CharField( # self.fields['password2'] = forms.CharField(
widget=forms.PasswordInput(attrs={ # widget=forms.PasswordInput(attrs={
'placeholder': 'Powtórz hasło' # 'placeholder': 'Powtórz hasło'
}), # }),
error_messages={ # error_messages={
'required': 'Podaj hasło', # 'required': 'Podaj hasło',
}) # })
#
for field in ['username', 'password1', 'password2']: # for field in ['username', 'password1', 'password2']:
self.fields[field].help_text = None # self.fields[field].help_text = None
self.fields[field].label = '' # self.fields[field].label = ''
#

View File

@@ -1,14 +1,21 @@
from django.shortcuts import render, redirect from django.shortcuts import render, redirect
from .forms import RegisterForm from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth.decorators import login_required
from django.contrib.auth import logout
def register(response): @login_required
def sign_up(request):
title = "Rejestracja" title = "Rejestracja"
if response.method == "POST": form = UserCreationForm(request.POST or None)
form = RegisterForm(response.POST) if request.method == "POST":
if form.is_valid(): if form.is_valid():
form.save() user = form.save()
return redirect("/") return redirect("/")
else: return render(request, 'registration/signup.html', {"form": form, 'title': title})
form = RegisterForm()
return render(response, "registration/signup.html", {"form": form, 'title': title})
def logout_view(request):
logout(request)
return redirect('/accounts/login')

View File

@@ -15,14 +15,23 @@ Including another URLconf
""" """
from django.contrib import admin from django.contrib import admin
from django.urls import path, include from django.urls import path, include
from work import views from work import views as work_views
from accounts import views as accounts_views from accounts import views as accounts_views
from django.contrib.auth import views
# from django.contrib.auth import views as auth_views
from django.conf.urls import url
urlpatterns = [ urlpatterns = [
path('admin/', admin.site.urls), path('admin/', admin.site.urls),
path('accounts/', include('django.contrib.auth.urls')), path('accounts/', include('django.contrib.auth.urls')),
path('', views.get_basic_workdata, name='home'), path('', work_views.get_basic_workdata, name='home'),
path('register/', accounts_views.register, name='register'), path('accounts/sign_up/', accounts_views.sign_up, name='sign-up'),
path('accounts/login/', views.LoginView.as_view(), name='login'),
path('accounts/logout/', accounts_views.logout_view, name='logout'),
# path('accounts/logout/', views.LogoutView.as_view(), name='logout'),
] ]

View File

@@ -42,14 +42,14 @@
</li> </li>
<!--Dodaj godziny--> <!--Dodaj godziny-->
<li class="nav-item"> <li class="nav-item">
<a class="nav-link" href="/">Dodaj godziny</a> <a class="nav-link" href="{% url 'home' %}">Dodaj godziny</a>
</li> </li>
{% load auth_extras %} {% load auth_extras %}
{% if request.user|has_group:"moderators" %} {% if request.user|has_group:"moderators" %}
<!--Dodaj pracownika--> <!--Dodaj pracownika-->
<li class="nav-item"> <li class="nav-item">
<a class="nav-link" href="/register">Dodaj pracownika</a> <a class="nav-link" href="{% url 'sign-up' %}">Dodaj pracownika</a>
</li> </li>
{% endif %} {% endif %}
</ul> </ul>
@@ -62,7 +62,7 @@
<!-- <div class="text-right">--> <!-- <div class="text-right">-->
<img src="{% static 'img/usericon.png' %}" width="17" alt="user" <img src="{% static 'img/usericon.png' %}" width="17" alt="user"
class="d-inline-block mr-1"> {{ user.username }} class="d-inline-block mr-1"> {{ user.username }}
<a href="{% url 'login' %}" class="btn btn-sm btn-secondary text-nowrap btn-logout">Wyloguj</a> <a href="{% url 'logout' %}" class="btn btn-sm btn-secondary text-nowrap btn-logout">Wyloguj</a>
<!-- </div>--> <!-- </div>-->
{% endif %} {% endif %}
</li> </li>

View File

@@ -1,10 +1,16 @@
from django.http import HttpResponseRedirect from django.http import HttpResponseRedirect
from django.shortcuts import render from django.shortcuts import render
from work.forms import HoursInputForm from work.forms import HoursInputForm
from django.contrib.auth.decorators import login_required
#
# @login_required
# def index(request):
# return render(request,'registration/login.html')
@login_required
def get_basic_workdata(request): def get_basic_workdata(request):
title = None title = "Godziny"
if request.method == 'POST': if request.method == 'POST':
form = HoursInputForm(request.POST) form = HoursInputForm(request.POST)
if form.is_valid(): if form.is_valid():