fix bug, update registration

This commit is contained in:
Sokurov Idar 2021-02-19 17:55:27 +03:00
parent e955dfab99
commit 8b512afadb
5 changed files with 43 additions and 58 deletions

View File

@ -51,8 +51,14 @@ MIDDLEWARE = [
ROOT_URLCONF = 'access_controller.urls' ROOT_URLCONF = 'access_controller.urls'
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend' EMAIL_HOST = 'smtp.mail.ru'
EMAIL_PORT = 2525
EMAIL_USE_TLS = True
EMAIL_HOST_USER = 'djgr.02@mail.ru'
EMAIL_HOST_PASSWORD = 'djangogroup02'
SERVER_EMAIL = EMAIL_HOST_USER
DEFAULT_FROM_EMAIL = EMAIL_HOST_USER
TEMPLATES = [ TEMPLATES = [
{ {

View File

@ -22,26 +22,12 @@ class ZendeskAdmin:
def check_user(self, email: str) -> bool: def check_user(self, email: str) -> bool:
return True if self.admin.search(email, type='user') else False return True if self.admin.search(email, type='user') else False
def get_user_name(self, email: str) -> str: def get_user(self, email: str) -> str:
user = self.admin.users.search(email).values[0] return self.admin.users.search(email).values[0]
return user.name
def get_user_role(self, email: str) -> str:
user = self.admin.users.search(email).values[0]
return user.role
def get_user_id(self, email: str) -> str:
user = self.admin.users.search(email).values[0]
return user.id
def get_user_org(self, email: str) -> str: def get_user_org(self, email: str) -> str:
user = self.admin.users.search(email).values[0] user = self.admin.users.search(email).values[0]
print(user) return user.organization.name
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
def create_admin(self) -> None: def create_admin(self) -> None:
if self.email is None: if self.email is None:
@ -68,9 +54,10 @@ def update_profile(user_profile: UserProfile):
:param user_profile: Объект профиля пользователя :param user_profile: Объект профиля пользователя
:type user_profile: :class:`main.models.UserProfile` :type user_profile: :class:`main.models.UserProfile`
""" """
user_profile.name = ZendeskAdmin().get_user_name(user_profile.user.email) user = ZendeskAdmin().get_user(user_profile.user.email)
user_profile.role = ZendeskAdmin().get_user_role(user_profile.user.email) user_profile.name = user.name
user_profile.image = ZendeskAdmin().get_user_image(user_profile.user.email) user_profile.role = user.role
user_profile.image = user.photo['content_url'] if user.photo else None
user_profile.save() user_profile.save()

View File

@ -11,5 +11,5 @@
{% block content %} {% block content %}
<br> <br>
<h4> Нет пользователя с указаным адресом электронной почты, либо был введён неверный пароль</h4> <h4> Нет пользователя с указаным адресом электронной почты.</h4>
{% endblock %} {% endblock %}

View File

@ -1,3 +0,0 @@
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 }}

View File

@ -1,16 +1,14 @@
from django.contrib.auth.decorators import login_required
from django.contrib.auth.forms import PasswordResetForm from django.contrib.auth.forms import PasswordResetForm
from django.contrib.auth.models import User
from django.contrib.auth.tokens import default_token_generator from django.contrib.auth.tokens import default_token_generator
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 django_registration.backends.one_step.views import RegistrationView
from main.extra_func import check_user_exist, check_user_auth, update_profile, get_user_organization from access_controller.settings import EMAIL_HOST_USER
from main.models import UserProfile from main.extra_func import check_user_exist, update_profile, get_user_organization
from django.contrib.auth.models import User
from main.forms import CustomRegistrationForm from main.forms import CustomRegistrationForm
from django.contrib.auth.decorators import login_required
class CustomRegistrationView(RegistrationView): class CustomRegistrationView(RegistrationView):
@ -24,32 +22,29 @@ 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 get_user_organization(form.data['email']) == 'SYSTEM':
forms = PasswordResetForm(initial={'email': form.data['email']}) forms = PasswordResetForm(self.request.POST)
if forms.is_valid(): if forms.is_valid():
opts = { opts = {
'use_https': self.request.is_secure(), 'use_https': self.request.is_secure(),
'token_generator': default_token_generator, 'token_generator': default_token_generator,
'from_email': form.data['email'], 'from_email': EMAIL_HOST_USER,
'email_template_name': 'registration/password_reset_email.html', 'email_template_name': 'registration/password_reset_email.html',
'subject_template_name': 'registration/password_reset_subject.txt', 'subject_template_name': 'registration/password_reset_subject.txt',
'request': self.request, 'request': self.request,
'html_email_template_name': None, 'html_email_template_name': None,
'extra_email_context': None, 'extra_email_context': None,
} }
forms.full_clean() user = User.objects.create_user(
forms.save(**opts) username=form.data['email'],
email=form.data['email'],
if get_user_organization(form.data['email']) == 'SYSTEM' and check_user_exist(form.data['email']): password=User.objects.make_random_password(length=50)
user = User.objects.create_user( )
username=form.data['email'], forms.save(**opts)
email=form.data['email'], update_profile(user.userprofile)
) return user
else:
f = PasswordResetView raise ValueError('Непредвиденная ошибка')
profile = user.userprofile
update_profile(profile)
return user
else: else:
self.is_allowed = False self.is_allowed = False