119 lines
4.3 KiB
Python
119 lines
4.3 KiB
Python
from django.core.exceptions import PermissionDenied
|
||
from django.shortcuts import render, get_list_or_404
|
||
from django.urls import reverse_lazy
|
||
|
||
from main.extra_func import check_user_exist, check_user_auth, update_profile, \
|
||
make_engineer, make_light_agent, get_users_list
|
||
from main.models import UserProfile
|
||
|
||
from django.contrib.auth.models import User
|
||
from main.forms import CustomRegistrationForm, AdminPageUsers
|
||
from django_registration.views import RegistrationView, FormView
|
||
|
||
from django.contrib.auth.decorators import login_required
|
||
from django.contrib.auth.mixins import LoginRequiredMixin
|
||
|
||
from zenpy import Zenpy
|
||
from access_controller.settings import ZENDESK_ROLES
|
||
|
||
|
||
class CustomRegistrationView(RegistrationView):
|
||
"""
|
||
Отображение и логика работы страницы регистрации пользователя
|
||
"""
|
||
form_class = CustomRegistrationForm
|
||
template_name = 'django_registration/registration_form.html'
|
||
success_url = reverse_lazy('django_registration_complete')
|
||
is_allowed = True
|
||
|
||
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']):
|
||
user = User.objects.create_user(
|
||
username=form.data['username'],
|
||
email=form.data['email'],
|
||
password=form.data['password1']
|
||
)
|
||
profile = user.userprofile
|
||
update_profile(profile)
|
||
else:
|
||
self.is_allowed = False
|
||
|
||
def get_success_url(self, user=None):
|
||
"""
|
||
Возвращает url-адрес страницы, куда нужно перейти после успешной/неуспешной регистрации
|
||
Используется самой django-registration
|
||
"""
|
||
if self.is_allowed:
|
||
return reverse_lazy('django_registration_complete')
|
||
else:
|
||
return reverse_lazy('django_registration_disallowed')
|
||
|
||
|
||
@login_required()
|
||
def profile_page(request):
|
||
"""
|
||
Отображение страницы профиля
|
||
|
||
:param request: объект с деталями запроса
|
||
:type request: :class:`django.http.HttpResponse`
|
||
:return: объект ответа сервера с HTML-кодом внутри
|
||
"""
|
||
user_profile = request.user.userprofile
|
||
update_profile(user_profile)
|
||
|
||
context = {
|
||
'email': user_profile.user.email,
|
||
'name': user_profile.name,
|
||
'role': user_profile.role,
|
||
'image_url': user_profile.image,
|
||
'pagename': 'Страница профиля'
|
||
}
|
||
return render(request, 'pages/profile.html', context)
|
||
|
||
|
||
def main_page(request):
|
||
return render(request, 'pages/index.html')
|
||
|
||
|
||
class AdminPageView(FormView, LoginRequiredMixin):
|
||
template_name = 'pages/adm_ruleset.html'
|
||
form_class = AdminPageUsers
|
||
success_url = '/control/'
|
||
|
||
def form_valid(self, form):
|
||
if 'engineer' in self.request.POST:
|
||
self.make_engineers(form.cleaned_data['users'])
|
||
elif 'light_agent' in self.request.POST:
|
||
self.make_light_agents(form.cleaned_data['users'])
|
||
return super().form_valid(form)
|
||
|
||
@staticmethod
|
||
def make_engineers(users):
|
||
for user in users:
|
||
make_engineer(user)
|
||
|
||
@staticmethod
|
||
def make_light_agents(users):
|
||
for user in users:
|
||
make_light_agent(user)
|
||
|
||
@staticmethod
|
||
def count_users(users): # TODO: this func counts users from all zendesk instead of just from a model
|
||
engineers, light_agents = 0, 0
|
||
for user in users:
|
||
if user.custom_role_id == ZENDESK_ROLES['engineer']:
|
||
engineers += 1
|
||
elif user.custom_role_id == ZENDESK_ROLES['light_agent']:
|
||
light_agents += 1
|
||
return engineers, light_agents
|
||
|
||
def get_context_data(self, **kwargs): # TODO: add engineers and agents count
|
||
if self.request.user.userprofile.role != 'admin':
|
||
raise PermissionDenied
|
||
context = super().get_context_data(**kwargs)
|
||
context['users'] = get_list_or_404(
|
||
UserProfile, role='agent')
|
||
context['engineers'], context['light_agents'] = self.count_users(get_users_list())
|
||
return context # TODO: need to get profile page url
|