Recreate registration v1
This commit is contained in:
@@ -34,6 +34,11 @@ class ZendeskAdmin:
|
||||
user = self.admin.users.search(email).values[0]
|
||||
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:
|
||||
user = self.admin.users.search(email).values[0]
|
||||
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)
|
||||
|
||||
|
||||
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:
|
||||
"""
|
||||
Функция проверяет, верны ли входные данные
|
||||
@@ -94,10 +111,10 @@ def check_user_auth(email: str, password: str) -> bool:
|
||||
:rtype: :class:`bool`
|
||||
"""
|
||||
creds = {
|
||||
'email': email,
|
||||
'password': password,
|
||||
'subdomain': 'ngenix1612197338',
|
||||
}
|
||||
'email': email,
|
||||
'password': password,
|
||||
'subdomain': 'ngenix1612197338',
|
||||
}
|
||||
try:
|
||||
user = Zenpy(**creds)
|
||||
user.search(email, type='user')
|
||||
|
||||
@@ -5,30 +5,22 @@ from django_registration.forms import RegistrationFormUniqueEmail
|
||||
class CustomRegistrationForm(RegistrationFormUniqueEmail):
|
||||
"""
|
||||
Форма для регистрации :class:`django_registration.forms.RegistrationFormUniqueEmail`
|
||||
с полем для ввода пароля от Zendesk аккаунта и с добавлением bootstrap-класса 'form-control' для всех полей
|
||||
с добавлением bootstrap-класса 'form-control'
|
||||
|
||||
:param password_zen: Поле для ввода пароля от Zendesk
|
||||
: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):
|
||||
super().__init__(*args, **kwargs)
|
||||
for visible in self.visible_fields():
|
||||
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:
|
||||
visible.field.widget.attrs['class'] += 'form-control'
|
||||
else:
|
||||
visible.field.widget.attrs['class'] = 'form-control'
|
||||
if visible.html_name !='email':
|
||||
visible.field.required = False
|
||||
|
||||
class Meta(RegistrationFormUniqueEmail.Meta):
|
||||
fields = RegistrationFormUniqueEmail.Meta.fields
|
||||
fields.insert(2, 'password_zen')
|
||||
|
||||
@@ -11,14 +11,12 @@
|
||||
{% block content %}
|
||||
<form method="post" action="">
|
||||
{% csrf_token %}
|
||||
{% for field in form %}
|
||||
{{ field.label_tag }}
|
||||
{{ field }}
|
||||
{{ form.email.label_tag }}
|
||||
{{ form.email }}
|
||||
<br>
|
||||
{% if field.errors %}
|
||||
<span>{{ field.errors }}</span>
|
||||
{% if form.email.errors %}
|
||||
<span>{{ form.email.errors }}</span>
|
||||
{% endif %}
|
||||
{% endfor %}
|
||||
<input type="submit" value="Зарегистрироваться" class="clearfix">
|
||||
</form>
|
||||
{% 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.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 django.contrib.auth.models import User
|
||||
from main.forms import CustomRegistrationForm
|
||||
from django_registration.views import RegistrationView
|
||||
|
||||
from django.contrib.auth.decorators import login_required
|
||||
|
||||
from zenpy import Zenpy
|
||||
|
||||
|
||||
class CustomRegistrationView(RegistrationView):
|
||||
"""
|
||||
@@ -24,14 +22,16 @@ class CustomRegistrationView(RegistrationView):
|
||||
|
||||
def register(self, form):
|
||||
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(
|
||||
username=form.data['username'],
|
||||
username=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
|
||||
update_profile(profile)
|
||||
return user
|
||||
else:
|
||||
self.is_allowed = False
|
||||
|
||||
@@ -41,7 +41,7 @@ class CustomRegistrationView(RegistrationView):
|
||||
Используется самой django-registration
|
||||
"""
|
||||
if self.is_allowed:
|
||||
return reverse_lazy('django_registration_complete')
|
||||
return reverse_lazy('password_reset_done')
|
||||
else:
|
||||
return reverse_lazy('django_registration_disallowed')
|
||||
|
||||
|
||||
Reference in New Issue
Block a user