Commit 0edc3494 authored by Давид Ли's avatar Давид Ли

lesson 61

parent 90c2d2d3
from django.contrib.auth import get_user_model
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth.forms import UserCreationForm, PasswordChangeForm
from django import forms
from django.forms import widgets
from accounts.models import Profile
class RegisterForm(UserCreationForm):
......@@ -28,3 +31,49 @@ class SecondaryRegisterForm(UserCreationForm):
class Meta(UserCreationForm.Meta):
model = get_user_model()
fields = ('id', 'password1', 'password2')
class UserUpdateForm(forms.ModelForm):
class Meta:
model = get_user_model()
fields = ('first_name', 'last_name', 'email')
widgets = {
'first_name': widgets.TextInput(attrs={
'class': 'form-control mb-3',
}),
'last_name': widgets.TextInput(attrs={
'class': 'form-control mb-3',
}),
'email': widgets.EmailInput(attrs={
'class': 'form-control mb-3',
}),
}
class ProfileUpdateForm(forms.ModelForm):
class Meta:
model = Profile
fields = ('birth_date', 'avatar')
widgets = {
'birth_date': widgets.DateInput(attrs={
'class': 'form-control mb-3',
'type': 'date',
}),
'avatar': widgets.FileInput(attrs={
'class': 'form-control mb-3',
}),
}
class ChangePasswordForm(PasswordChangeForm):
widgets = {
'new_password1': widgets.PasswordInput(attrs={
'class': 'form-control mb-3',
}),
'new_password2': widgets.PasswordInput(attrs={
'class': 'form-control mb-3',
}),
'old_password': widgets.PasswordInput(attrs={
'class': 'form-control mb-3',
}),
}
......@@ -8,5 +8,7 @@ urlpatterns = [
path('login', LoginView.as_view(template_name='auth/login.html'), name='login'),
path('logout', LogoutView.as_view(), name='logout'),
path('register', views.RegisterView.as_view(), name='register'),
path('<int:id>/', views.UserDetailView.as_view(), name='profile'),
path('change_password', views.ChangePasswordView.as_view(), name='change_password'),
path('<int:id>', views.UserDetailView.as_view(), name='profile'),
path('update', views.UserUpdateView.as_view(), name='user_update'),
]
from django.db.models.base import Model as Model
from django.db.models.query import QuerySet
from django.http import HttpRequest
from django.urls import reverse
from django.urls import reverse, reverse_lazy
from accounts import forms, models
from django.views import generic
from django.contrib.auth import login, get_user_model, mixins
from django.contrib.auth import login, get_user_model, mixins, update_session_auth_hash, views
from django.shortcuts import redirect
from django.core import exceptions
from django.core.paginator import Paginator
from django.contrib.auth.mixins import LoginRequiredMixin
class RegisterView(generic.CreateView):
......@@ -88,3 +91,60 @@ class UserDetailView(mixins.LoginRequiredMixin, generic.DetailView):
page_obj=page,
is_paginated=page.has_other_pages(),
)
class UserUpdateView(LoginRequiredMixin, generic.UpdateView):
model = get_user_model()
form_class = forms.UserUpdateForm
template_name = 'auth/user_update.html'
context_object_name = 'user_obj'
pk_url_kwarg = 'id'
def get_context_data(self, **kwargs):
if 'profile_form' not in kwargs:
kwargs['profile_form'] = self.get_profile_form()
return super().get_context_data(**kwargs)
def get_profile_form(self):
kwargs = {'instance': self.object.profile}
if self.request.method == 'POST':
kwargs['data'] = self.request.POST
kwargs['files'] = self.request.FILES
return forms.ProfileUpdateForm(**kwargs)
def post(self, request, *args, **kwargs):
self.object = self.get_object()
user_form = self.get_form()
profile_form = self.get_profile_form()
if user_form.is_valid() and profile_form.is_valid():
return self.form_valid(user_form, profile_form)
return self.form_invalid(user_form, profile_form)
def form_valid(self, user_form, profile_form):
response = super().form_valid(user_form)
profile_form.save()
return response
def form_invalid(self, user_form, profile_form):
context = self.get_context_data(form=user_form, profile_form=profile_form)
return self.render_to_response(context)
def get_success_url(self):
return reverse('profile', kwargs={'id': self.object.id})
def get_object(self, queryset=None):
return self.request.user
class ChangePasswordView(views.PasswordChangeView):
form_class = forms.ChangePasswordForm
template_name = 'auth/change_password.html'
def get_success_url(self):
return reverse_lazy('profile', kwargs={'id': self.request.user.id})
......@@ -113,7 +113,7 @@ TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_L10N = False
USE_TZ = True
......@@ -142,3 +142,6 @@ AUTH_USER_MODEL = 'accounts.User'
MEDIA_ROOT = BASE_DIR / 'uploads'
MEDIA_URL = '/images/'
# localhost:1025/uploads/avatars/user_1.jpg
# DATE_INPUT_FORMATS = ('%d.%m.%Y',)
{% extends 'base.html' %}
{% block content %}
<h1 class="text-center">Смена пароля</h1>
<form action="" method="post">
{% csrf_token %}
{{ form.old_password.label }}
<input type="password" class="form-control mb-3" name="old_password">
{% if form.old_password.errors %}
{% for error in form.old_password.errors %}
<div class="alert alert-danger">{{ error }}</div>
{% endfor %}
{% endif %}
{{ form.new_password1.label }}
<input type="password" class="form-control mb-3" name="new_password1">
{% if form.new_password1.errors %}
{% for error in form.new_password1.errors %}
<div class="alert alert-danger">{{ error }}</div>
{% endfor %}
{% endif %}
{{ form.new_password2.label }}
<input type="password" class="form-control mb-3" name="new_password2">
{% if form.new_password2.errors %}
{% for error in form.new_password2.errors %}
<div class="alert alert-danger">{{ error }}</div>
{% endfor %}
{% endif %}
<button class="btn btn-success">Submit</button>
</form>
{% endblock %}
\ No newline at end of file
......@@ -14,6 +14,9 @@
<p>Дата Рождения: {{ user_obj.profile.birth_date|date:'d.m.Y' }}</p>
<p>Почта: {{ user_obj.email }}</p>
<a href="{% url 'user_update' %}" class="btn btn-warning mt-3">Обновить</a>
<a href="{% url 'change_password' %}" class="btn btn-primary mt-3">Сменить пароль</a>
<h2 class="text-center">Статьи</h2>
{{page_obj.has_other_pages}}
{% include 'partial/article_list.html' with articles=page_obj.object_list is_paginated=page_obj.has_other_pages %}
......
{% extends 'base.html' %}
{% block content %}
<h1 class="text-center">Изменить личные данные</h1>
<form action="" method="post" enctype="multipart/form-data">
{% csrf_token %}
{% include 'partial/form.html' with fields_only=True %}
{% include 'partial/form.html' with form=profile_form fields_only=True %}
<button class="btn btn-success mt-5">Изменить</button>
</form>
{% endblock %}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment