access-controller/main/extra_func.py
Степаненко Ольга c216b44b2a Make models documentation
2021-02-16 12:28:08 +03:00

114 lines
3.6 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(user_profile: UserProfile):
"""
Функция устанавливает поле :class:`username` текущим именем в Zendesk
.. TODO::
Переделать с получением данных через API
:param UP: Объект профиля пользователя
:type UP: :class:`main.models.UserProfile`
:return: Имя пользователя
:rtype: :class:`str`
"""
return user_profile.user.username
def set_and_get_email(user_profile: UserProfile):
"""
Функция устанавливает поле :class:`user.email` текущей почтой в Zendesk
.. TODO::
Переделать с получением данных через API
:param UP: Объект профиля пользователя
:type UP: :class:`main.models.UserProfile`
:return: Почта пользователя
:rtype: :class:`str`
"""
return user_profile.user.email
def set_and_get_role(user_profile: UserProfile):
"""
Функция устанавливает поле :class:`role` текущей ролью в Zendesk
.. TODO::
Переделать с получением данных через API
:param UP: Объект профиля пользователя
:type UP: :class:`main.models.UserProfile`
:return: Роль пользователя
:rtype: :class:`str`
"""
return user_profile.role
def load_and_get_image(user_profile: UserProfile):
"""
Функция загружает и устанавливает изображение в поле :class:`image`
.. TODO::
Переделать с получением изображения через API
:param UP: Объект профиля пользователя
:type UP: :class:`main.models.UserProfile`
:return: Название изображения
:rtype: :class:`str`
"""
return user_profile.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: :class:`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