access-controller/main/extra_func.py
Sokurov Idar 938b9894fe Add Zendesk password check
Add Bootstrap to forms
Add custom form
2021-02-10 19:05:46 +03:00

102 lines
3.5 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import os
from zenpy import Zenpy
from zenpy.lib.exception import APIException
from main.models import UserProfile
# Дополнительные функции
def set_and_get_name(UP: UserProfile): # TODO: Переделать с получением данных через API
"""
Функция устанавливает поле :class:`username` текущим именем в Zendesk
:param UP: Объект профиля пользователя
:type UP: :class:`main.models.UserProfile`
:return: Имя пользователя
:rtype: :class:`str`
"""
return UP.user.username
def set_and_get_email(UP: UserProfile): # TODO: Переделать с получением данных через API
"""
Функция устанавливает поле :class:`user.email` текущей почтой в Zendesk
:param UP: Объект профиля пользователя
:type UP: :class:`main.models.UserProfile`
:return: Почта пользователя
:rtype: :class:`str`
"""
return UP.user.email
def set_and_get_role(UP: UserProfile): # TODO: Переделать с получением данных через API
"""
Функция устанавливает поле :class:`role` текущей ролью в Zendesk
:param UP: Объект профиля пользователя
:type UP: :class:`main.models.UserProfile`
:return: Роль пользователя
:rtype: :class:`str`
"""
return UP.role
def load_and_get_image(UP: UserProfile): # TODO: Переделать с получением изображения через API
"""
Функция загружает и устанавливает изображение в поле :class:`image`
:param UP: Объект профиля пользователя
:type UP: :class:`main.models.UserProfile`
:return: Название изображения
:rtype: :class:`str`
"""
return UP.image.name
def check_user_exist(email: str) -> bool:
"""
Функция проверяет, существует ли пользователь
:param email: Электронная почта пользователя
:type email: :class:`str
:return: True, если существует, иначе False
:rtype: :class:`bool'
"""
admin_creds = {
'email': os.environ.get('Admin_email'),
'subdomain': 'ngenix1612197338',
'token': os.environ.get('Oauth_token'),
}
admin = Zenpy(**admin_creds)
zenpy_user = admin.search(email, type='user')
if zenpy_user:
return True
return False
def check_user_auth(email: str, password: str) -> bool:
"""
Функция проверяет, верны ли входные данные
:param email: Электроная почта пользователя
:type email: :class:`str`
:param password: Пароль пользователя
:type password: :class:`str`
:return: True, если входные данные верны, иначе False
:raise APIException: исключение, вызываемое если пользователь не аутентифицирован
:rtype: :class:`bool`
"""
try:
creds = {
'email': email,
'subdomain': 'ngenix1612197338',
'password': password,
}
user = Zenpy(**creds)
user.search(email, type='user')
except APIException:
return False
return True