Recreate registration v1
This commit is contained in:
parent
227038ea38
commit
b777d47041
@ -35,6 +35,7 @@ INSTALLED_APPS = [
|
|||||||
'django.contrib.sessions',
|
'django.contrib.sessions',
|
||||||
'django.contrib.messages',
|
'django.contrib.messages',
|
||||||
'django.contrib.staticfiles',
|
'django.contrib.staticfiles',
|
||||||
|
'django_cleanup.apps.CleanupConfig',
|
||||||
'django_registration',
|
'django_registration',
|
||||||
'main',
|
'main',
|
||||||
]
|
]
|
||||||
@ -51,6 +52,17 @@ MIDDLEWARE = [
|
|||||||
|
|
||||||
ROOT_URLCONF = 'access_controller.urls'
|
ROOT_URLCONF = 'access_controller.urls'
|
||||||
|
|
||||||
|
|
||||||
|
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
|
||||||
|
EMAIL_HOST = 'smtp.mail.ru'
|
||||||
|
EMAIL_PORT = 2525
|
||||||
|
EMAIL_USE_TLS = True
|
||||||
|
EMAIL_HOST_USER = 'dj-gr-2@mail.ru'
|
||||||
|
EMAIL_HOST_PASSWORD = 'djangogroup02'
|
||||||
|
SERVER_EMAIL = EMAIL_HOST_USER
|
||||||
|
DEFAULT_FROM_EMAIL = EMAIL_HOST_USER
|
||||||
|
|
||||||
|
|
||||||
TEMPLATES = [
|
TEMPLATES = [
|
||||||
{
|
{
|
||||||
'BACKEND': 'django.template.backends.django.DjangoTemplates',
|
'BACKEND': 'django.template.backends.django.DjangoTemplates',
|
||||||
@ -119,11 +131,9 @@ STATIC_URL = '/static/'
|
|||||||
STATIC_ROOT = os.path.join(BASE_DIR, 'staticroot')
|
STATIC_ROOT = os.path.join(BASE_DIR, 'staticroot')
|
||||||
STATICFILES_DIRS = [
|
STATICFILES_DIRS = [
|
||||||
os.path.join(BASE_DIR, 'static'),
|
os.path.join(BASE_DIR, 'static'),
|
||||||
os.path.join(BASE_DIR, 'media'),
|
|
||||||
]
|
]
|
||||||
|
|
||||||
MEDIA_ROOT = BASE_DIR / 'media'
|
ACCOUNT_ACTIVATION_DAYS = 7
|
||||||
MEDIA_URL = '/media/'
|
|
||||||
|
|
||||||
LOGIN_REDIRECT_URL = '/'
|
LOGIN_REDIRECT_URL = '/'
|
||||||
LOGOUT_REDIRECT_URL = '/'
|
LOGOUT_REDIRECT_URL = '/'
|
||||||
|
@ -13,13 +13,10 @@ Including another URLconf
|
|||||||
1. Import the include() function: from django.urls import include, path
|
1. Import the include() function: from django.urls import include, path
|
||||||
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
|
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
|
||||||
"""
|
"""
|
||||||
from django.conf.urls.static import static
|
|
||||||
from django.contrib import admin
|
from django.contrib import admin
|
||||||
from django.contrib.auth.views import LoginView
|
from django.contrib.auth.views import LoginView
|
||||||
|
from django.contrib.auth import views as auth_views
|
||||||
from django.urls import path, include
|
from django.urls import path, include
|
||||||
|
|
||||||
from access_controller import settings
|
|
||||||
from access_controller.settings import DEBUG
|
|
||||||
from main.views import main_page, profile_page, CustomRegistrationView
|
from main.views import main_page, profile_page, CustomRegistrationView
|
||||||
|
|
||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
@ -29,5 +26,28 @@ urlpatterns = [
|
|||||||
path('accounts/register/', CustomRegistrationView.as_view(), name='registration'),
|
path('accounts/register/', CustomRegistrationView.as_view(), name='registration'),
|
||||||
path('accounts/login/', LoginView.as_view(extra_context={}), name='login'), # TODO add extra context
|
path('accounts/login/', LoginView.as_view(extra_context={}), name='login'), # TODO add extra context
|
||||||
path('accounts/', include('django.contrib.auth.urls')),
|
path('accounts/', include('django.contrib.auth.urls')),
|
||||||
path('accounts/', include('django_registration.backends.one_step.urls')),
|
path('accounts/', include('django_registration.backends.activation.urls')),
|
||||||
|
]
|
||||||
|
|
||||||
|
urlpatterns += [
|
||||||
|
path(
|
||||||
|
'password-reset/',
|
||||||
|
auth_views.PasswordResetView.as_view(),
|
||||||
|
name='password_reset'
|
||||||
|
),
|
||||||
|
path(
|
||||||
|
'password-reset/done/',
|
||||||
|
auth_views.PasswordResetDoneView.as_view(),
|
||||||
|
name='password_reset_done'
|
||||||
|
),
|
||||||
|
path(
|
||||||
|
'reset/<uidb64>/<token>/',
|
||||||
|
auth_views.PasswordResetConfirmView.as_view(),
|
||||||
|
name='password_reset_confirm'
|
||||||
|
),
|
||||||
|
path(
|
||||||
|
'reset/done/',
|
||||||
|
auth_views.PasswordResetCompleteView.as_view(),
|
||||||
|
name='password_reset_complete'
|
||||||
|
),
|
||||||
]
|
]
|
||||||
|
@ -34,6 +34,11 @@ class ZendeskAdmin:
|
|||||||
user = self.admin.users.search(email).values[0]
|
user = self.admin.users.search(email).values[0]
|
||||||
return user.id
|
return user.id
|
||||||
|
|
||||||
|
def get_user_org(self, email: str) -> str:
|
||||||
|
user = self.admin.users.search(email).values[0]
|
||||||
|
print(user)
|
||||||
|
return user.organization
|
||||||
|
|
||||||
def get_user_image(self, email: str) -> str:
|
def get_user_image(self, email: str) -> str:
|
||||||
user = self.admin.users.search(email).values[0]
|
user = self.admin.users.search(email).values[0]
|
||||||
return user.photo['content_url'] if user.photo else None
|
return user.photo['content_url'] if user.photo else None
|
||||||
@ -81,6 +86,18 @@ def check_user_exist(email: str) -> bool:
|
|||||||
return ZendeskAdmin().check_user(email)
|
return ZendeskAdmin().check_user(email)
|
||||||
|
|
||||||
|
|
||||||
|
def get_user_organization(email: str) -> bool:
|
||||||
|
"""
|
||||||
|
Функция возвращает организацию пользователя
|
||||||
|
|
||||||
|
:param email: Электронная почта пользователя
|
||||||
|
:type email: :class:`str`
|
||||||
|
:return: Название организации
|
||||||
|
:rtype: :class:`str`
|
||||||
|
"""
|
||||||
|
return ZendeskAdmin().get_user_org(email)
|
||||||
|
|
||||||
|
|
||||||
def check_user_auth(email: str, password: str) -> bool:
|
def check_user_auth(email: str, password: str) -> bool:
|
||||||
"""
|
"""
|
||||||
Функция проверяет, верны ли входные данные
|
Функция проверяет, верны ли входные данные
|
||||||
@ -94,10 +111,10 @@ def check_user_auth(email: str, password: str) -> bool:
|
|||||||
:rtype: :class:`bool`
|
:rtype: :class:`bool`
|
||||||
"""
|
"""
|
||||||
creds = {
|
creds = {
|
||||||
'email': email,
|
'email': email,
|
||||||
'password': password,
|
'password': password,
|
||||||
'subdomain': 'ngenix1612197338',
|
'subdomain': 'ngenix1612197338',
|
||||||
}
|
}
|
||||||
try:
|
try:
|
||||||
user = Zenpy(**creds)
|
user = Zenpy(**creds)
|
||||||
user.search(email, type='user')
|
user.search(email, type='user')
|
||||||
|
@ -5,30 +5,22 @@ from django_registration.forms import RegistrationFormUniqueEmail
|
|||||||
class CustomRegistrationForm(RegistrationFormUniqueEmail):
|
class CustomRegistrationForm(RegistrationFormUniqueEmail):
|
||||||
"""
|
"""
|
||||||
Форма для регистрации :class:`django_registration.forms.RegistrationFormUniqueEmail`
|
Форма для регистрации :class:`django_registration.forms.RegistrationFormUniqueEmail`
|
||||||
с полем для ввода пароля от Zendesk аккаунта и с добавлением bootstrap-класса 'form-control' для всех полей
|
с добавлением bootstrap-класса 'form-control'
|
||||||
|
|
||||||
:param password_zen: Поле для ввода пароля от Zendesk
|
:param password_zen: Поле для ввода пароля от Zendesk
|
||||||
:type password_zen: :class:`django.forms.CharField`
|
:type password_zen: :class:`django.forms.CharField`
|
||||||
"""
|
"""
|
||||||
password_zen = forms.CharField(
|
|
||||||
required=True,
|
|
||||||
label="Пароль от Zendesk аккаунта",
|
|
||||||
strip=False,
|
|
||||||
widget=forms.PasswordInput(attrs={
|
|
||||||
'class': 'form-control'
|
|
||||||
})
|
|
||||||
)
|
|
||||||
|
|
||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, *args, **kwargs):
|
||||||
super().__init__(*args, **kwargs)
|
super().__init__(*args, **kwargs)
|
||||||
for visible in self.visible_fields():
|
for visible in self.visible_fields():
|
||||||
if visible.field.widget.attrs.get('class', False):
|
if visible.field.widget.attrs.get('class', False):
|
||||||
print(visible.field.widget.attrs['class'].find('form-control'))
|
|
||||||
if visible.field.widget.attrs['class'].find('form-control') < 0:
|
if visible.field.widget.attrs['class'].find('form-control') < 0:
|
||||||
visible.field.widget.attrs['class'] += 'form-control'
|
visible.field.widget.attrs['class'] += 'form-control'
|
||||||
else:
|
else:
|
||||||
visible.field.widget.attrs['class'] = 'form-control'
|
visible.field.widget.attrs['class'] = 'form-control'
|
||||||
|
if visible.html_name !='email':
|
||||||
|
visible.field.required = False
|
||||||
|
|
||||||
class Meta(RegistrationFormUniqueEmail.Meta):
|
class Meta(RegistrationFormUniqueEmail.Meta):
|
||||||
fields = RegistrationFormUniqueEmail.Meta.fields
|
fields = RegistrationFormUniqueEmail.Meta.fields
|
||||||
fields.insert(2, 'password_zen')
|
|
||||||
|
@ -11,14 +11,12 @@
|
|||||||
{% block content %}
|
{% block content %}
|
||||||
<form method="post" action="">
|
<form method="post" action="">
|
||||||
{% csrf_token %}
|
{% csrf_token %}
|
||||||
{% for field in form %}
|
{{ form.email.label_tag }}
|
||||||
{{ field.label_tag }}
|
{{ form.email }}
|
||||||
{{ field }}
|
|
||||||
<br>
|
<br>
|
||||||
{% if field.errors %}
|
{% if form.email.errors %}
|
||||||
<span>{{ field.errors }}</span>
|
<span>{{ form.email.errors }}</span>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
{% endfor %}
|
|
||||||
<input type="submit" value="Зарегистрироваться" class="clearfix">
|
<input type="submit" value="Зарегистрироваться" class="clearfix">
|
||||||
</form>
|
</form>
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
13
main/templates/registration/password_reset_complete.html
Normal file
13
main/templates/registration/password_reset_complete.html
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
{% extends "base/base.html" %}
|
||||||
|
|
||||||
|
{% block title %}
|
||||||
|
{{ pagename }}
|
||||||
|
{% endblock %}
|
||||||
|
|
||||||
|
{% block heading %}
|
||||||
|
Восстановление пароля
|
||||||
|
{% endblock %}
|
||||||
|
|
||||||
|
{% block content %}
|
||||||
|
<p>Ваш новый пароль был установлен. Вы можете <a href="{% url 'login' %}">войти</a> сейчас</p>
|
||||||
|
{% endblock %}
|
23
main/templates/registration/password_reset_confirm.html
Normal file
23
main/templates/registration/password_reset_confirm.html
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
{% extends "base/base.html" %}
|
||||||
|
|
||||||
|
{% block title %}
|
||||||
|
{{ pagename }}
|
||||||
|
{% endblock %}
|
||||||
|
|
||||||
|
{% block heading %}
|
||||||
|
Восстановление пароля
|
||||||
|
{% endblock %}
|
||||||
|
|
||||||
|
{% block content %}
|
||||||
|
{% if validlink %}
|
||||||
|
<p>Пожалуйста, введите пароль дважды:</p>
|
||||||
|
<form action="." method="post">
|
||||||
|
{{ form.as_p }}
|
||||||
|
{% csrf_token %}
|
||||||
|
<p><input class="btn btn-success" type="submit" value="Сменить пароль"/></p>
|
||||||
|
</form>
|
||||||
|
{% else %}
|
||||||
|
<p>Неверная ссылка восстановления пароля, возможно она уже была использована.
|
||||||
|
Пожалуйста, запросите новый сброс пароля</p>
|
||||||
|
{% endif %}
|
||||||
|
{% endblock %}
|
14
main/templates/registration/password_reset_done.html
Normal file
14
main/templates/registration/password_reset_done.html
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
{% extends "base/base.html" %}
|
||||||
|
|
||||||
|
{% block title %}
|
||||||
|
{{ pagename }}
|
||||||
|
{% endblock %}
|
||||||
|
|
||||||
|
{% block heading %}
|
||||||
|
Восстановление пароля
|
||||||
|
{% endblock %}
|
||||||
|
|
||||||
|
{% block content %}
|
||||||
|
<p>Мы отправили вам на почту инструкцию по восстановлению</p>
|
||||||
|
<p>Если вы не получили сообщение, убедитесь что верно ввели адрес электронной почты.</p>
|
||||||
|
{% endblock %}
|
3
main/templates/registration/password_reset_email.html
Normal file
3
main/templates/registration/password_reset_email.html
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
Someone asked for password reset for email {{ email }}. Follow thelink below:
|
||||||
|
{{ protocol }}://{{ domain }}{% url "password_reset_confirm" uidb64=uid token=token %}
|
||||||
|
Your username, in case you've forgotten: {{ user.get_username }}
|
18
main/templates/registration/password_reset_form.html
Normal file
18
main/templates/registration/password_reset_form.html
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
{% extends "base/base.html" %}
|
||||||
|
|
||||||
|
{% block title %}
|
||||||
|
{{ pagename }}
|
||||||
|
{% endblock %}
|
||||||
|
|
||||||
|
{% block heading %}
|
||||||
|
Забыли пароль?
|
||||||
|
{% endblock %}
|
||||||
|
|
||||||
|
{% block content %}
|
||||||
|
<p>Введте свой e-mail адрес для восстановления пароля.</p>
|
||||||
|
<form action="." method="post">
|
||||||
|
{{ form.as_p }}
|
||||||
|
<p><input class="btn btn-success" type="submit" value="Отпрваить e-mail"></p>
|
||||||
|
{% csrf_token %}
|
||||||
|
</form>
|
||||||
|
{% endblock %}
|
@ -1,17 +1,15 @@
|
|||||||
|
from django.contrib.auth.views import PasswordResetView
|
||||||
from django.shortcuts import render
|
from django.shortcuts import render
|
||||||
from django.urls import reverse_lazy
|
from django.urls import reverse_lazy
|
||||||
|
from django_registration.backends.one_step.views import RegistrationView
|
||||||
|
|
||||||
from main.extra_func import check_user_exist, check_user_auth, update_profile
|
from main.extra_func import check_user_exist, check_user_auth, update_profile, get_user_organization
|
||||||
from main.models import UserProfile
|
from main.models import UserProfile
|
||||||
|
|
||||||
from django.contrib.auth.models import User
|
from django.contrib.auth.models import User
|
||||||
from main.forms import CustomRegistrationForm
|
from main.forms import CustomRegistrationForm
|
||||||
from django_registration.views import RegistrationView
|
|
||||||
|
|
||||||
from django.contrib.auth.decorators import login_required
|
from django.contrib.auth.decorators import login_required
|
||||||
|
|
||||||
from zenpy import Zenpy
|
|
||||||
|
|
||||||
|
|
||||||
class CustomRegistrationView(RegistrationView):
|
class CustomRegistrationView(RegistrationView):
|
||||||
"""
|
"""
|
||||||
@ -24,14 +22,16 @@ class CustomRegistrationView(RegistrationView):
|
|||||||
|
|
||||||
def register(self, form):
|
def register(self, form):
|
||||||
self.is_allowed = True
|
self.is_allowed = True
|
||||||
if check_user_exist(form.data['email']) and check_user_auth(form.data['email'], form.data['password_zen']):
|
if get_user_organization(form.data['email'])=='SYSTEM' and check_user_exist(form.data['email']) :
|
||||||
user = User.objects.create_user(
|
user = User.objects.create_user(
|
||||||
username=form.data['username'],
|
username=form.data['email'],
|
||||||
email=form.data['email'],
|
email=form.data['email'],
|
||||||
password=form.data['password1']
|
|
||||||
)
|
)
|
||||||
|
PasswordResetView.as_view()(self.request)
|
||||||
|
user.is_active = False
|
||||||
|
user.save()
|
||||||
profile = user.userprofile
|
profile = user.userprofile
|
||||||
update_profile(profile)
|
return user
|
||||||
else:
|
else:
|
||||||
self.is_allowed = False
|
self.is_allowed = False
|
||||||
|
|
||||||
@ -41,7 +41,7 @@ class CustomRegistrationView(RegistrationView):
|
|||||||
Используется самой django-registration
|
Используется самой django-registration
|
||||||
"""
|
"""
|
||||||
if self.is_allowed:
|
if self.is_allowed:
|
||||||
return reverse_lazy('django_registration_complete')
|
return reverse_lazy('password_reset_done')
|
||||||
else:
|
else:
|
||||||
return reverse_lazy('django_registration_disallowed')
|
return reverse_lazy('django_registration_disallowed')
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user