Added Django REST but have some bugs
This commit is contained in:
parent
5909750fcc
commit
8488ea88c2
@ -36,6 +36,7 @@ INSTALLED_APPS = [
|
|||||||
'django.contrib.messages',
|
'django.contrib.messages',
|
||||||
'django.contrib.staticfiles',
|
'django.contrib.staticfiles',
|
||||||
'django_registration',
|
'django_registration',
|
||||||
|
'rest_framework',
|
||||||
'main',
|
'main',
|
||||||
]
|
]
|
||||||
|
|
||||||
@ -183,3 +184,11 @@ ZENDESK_ROLES = {
|
|||||||
'engineer': 360005209000,
|
'engineer': 360005209000,
|
||||||
'light_agent': 360005208980,
|
'light_agent': 360005208980,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
REST_FRAMEWORK = {
|
||||||
|
# Use Django's standard `django.contrib.auth` permissions,
|
||||||
|
# or allow read-only access for unauthenticated users.
|
||||||
|
'DEFAULT_PERMISSION_CLASSES': [
|
||||||
|
'rest_framework.permissions.DjangoModelPermissionsOrAnonReadOnly'
|
||||||
|
]
|
||||||
|
}
|
||||||
|
@ -17,15 +17,16 @@ from django.contrib import admin
|
|||||||
from django.contrib.auth import views as auth_views
|
from django.contrib.auth import views as auth_views
|
||||||
from django.urls import path, include
|
from django.urls import path, include
|
||||||
from main.views import work_page, work_hand_over, work_become_engineer, AdminPageView
|
from main.views import work_page, work_hand_over, work_become_engineer, AdminPageView
|
||||||
|
|
||||||
from main.views import main_page, profile_page, CustomRegistrationView, CustomLoginView
|
from main.views import main_page, profile_page, CustomRegistrationView, CustomLoginView
|
||||||
|
from main.urls import router
|
||||||
|
|
||||||
|
|
||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
path('admin/', admin.site.urls, name='admin'),
|
path('admin/', admin.site.urls, name='admin'),
|
||||||
path('', main_page, name='index'),
|
path('', main_page, name='index'),
|
||||||
path('accounts/profile/', profile_page, name='profile'),
|
path('accounts/profile/', profile_page, name='profile'),
|
||||||
path('accounts/register/', CustomRegistrationView.as_view(), name='registration'),
|
path('accounts/register/', CustomRegistrationView.as_view(), name='registration'),
|
||||||
path('accounts/login/', CustomLoginView.as_view(extra_context={}), name='login',), # TODO add extra context
|
path('accounts/login/', CustomLoginView.as_view(extra_context={}), name='login', ), # TODO add extra context
|
||||||
path('accounts/', include('django.contrib.auth.urls')),
|
path('accounts/', include('django.contrib.auth.urls')),
|
||||||
path('accounts/', include('django_registration.backends.one_step.urls')),
|
path('accounts/', include('django_registration.backends.one_step.urls')),
|
||||||
path('work/<int:id>', work_page, name="work"),
|
path('work/<int:id>', work_page, name="work"),
|
||||||
@ -34,7 +35,7 @@ urlpatterns = [
|
|||||||
path('accounts/', include('django_registration.backends.activation.urls')),
|
path('accounts/', include('django_registration.backends.activation.urls')),
|
||||||
path('accounts/login/', include('django.contrib.auth.urls')),
|
path('accounts/login/', include('django.contrib.auth.urls')),
|
||||||
path('control/', AdminPageView.as_view(), name='control')
|
path('control/', AdminPageView.as_view(), name='control')
|
||||||
]
|
]
|
||||||
|
|
||||||
urlpatterns += [
|
urlpatterns += [
|
||||||
path(
|
path(
|
||||||
@ -58,3 +59,8 @@ urlpatterns += [
|
|||||||
name='password_reset_complete'
|
name='password_reset_complete'
|
||||||
),
|
),
|
||||||
]
|
]
|
||||||
|
|
||||||
|
# Django REST
|
||||||
|
urlpatterns += [
|
||||||
|
path('api/', include(router.urls))
|
||||||
|
]
|
||||||
|
@ -3,9 +3,9 @@ import os
|
|||||||
from zenpy import Zenpy
|
from zenpy import Zenpy
|
||||||
from zenpy.lib.exception import APIException
|
from zenpy.lib.exception import APIException
|
||||||
|
|
||||||
from main.models import UserProfile
|
from main.models import UserProfile, User
|
||||||
|
|
||||||
from access_controller.settings import ZENDESK_ROLES as ROLES
|
from access_controller.settings import ZENDESK_ROLES as ROLES, ZENDESK_ROLES
|
||||||
|
|
||||||
|
|
||||||
class ZendeskAdmin:
|
class ZendeskAdmin:
|
||||||
@ -28,7 +28,7 @@ class ZendeskAdmin:
|
|||||||
email: str = os.getenv('ACCESS_CONTROLLER_API_EMAIL')
|
email: str = os.getenv('ACCESS_CONTROLLER_API_EMAIL')
|
||||||
token: str = os.getenv('ACCESS_CONTROLLER_API_TOKEN')
|
token: str = os.getenv('ACCESS_CONTROLLER_API_TOKEN')
|
||||||
password: str = os.getenv('ACCESS_CONTROLLER_API_PASSWORD')
|
password: str = os.getenv('ACCESS_CONTROLLER_API_PASSWORD')
|
||||||
_instance=None
|
_instance = None
|
||||||
|
|
||||||
def __new__(cls, *args, **kwargs):
|
def __new__(cls, *args, **kwargs):
|
||||||
if cls._instance is None:
|
if cls._instance is None:
|
||||||
@ -144,8 +144,9 @@ def get_users_list() -> list:
|
|||||||
Функция **get_users_list** возвращает список пользователей Zendesk, относящихся к организации.
|
Функция **get_users_list** возвращает список пользователей Zendesk, относящихся к организации.
|
||||||
"""
|
"""
|
||||||
zendesk = ZendeskAdmin()
|
zendesk = ZendeskAdmin()
|
||||||
admin = zendesk.get_user(zendesk.email)
|
|
||||||
org = next(zendesk.admin.users.organizations(user=admin))
|
# У пользователей должна быть организация SYSTEM
|
||||||
|
org = next(zendesk.admin.search(type='organization', name='SYSTEM'))
|
||||||
return zendesk.admin.organizations.users(org)
|
return zendesk.admin.organizations.users(org)
|
||||||
|
|
||||||
|
|
||||||
@ -191,3 +192,37 @@ def check_user_auth(email: str, password: str) -> bool:
|
|||||||
except APIException:
|
except APIException:
|
||||||
return False
|
return False
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
def update_user_in_model(profile, zendesk_user):
|
||||||
|
profile.name = zendesk_user.name
|
||||||
|
profile.role = zendesk_user.role
|
||||||
|
profile.image = zendesk_user.photo['content_url'] if zendesk_user.photo else None
|
||||||
|
profile.save()
|
||||||
|
|
||||||
|
|
||||||
|
def count_users(users) -> tuple:
|
||||||
|
"""
|
||||||
|
Функция подсчета количества сотрудников с ролями engineer и light_a
|
||||||
|
|
||||||
|
.. 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 update_users_in_model():
|
||||||
|
"""
|
||||||
|
Обновляет пользователей в модели UserProfile по списку пользователей в организации
|
||||||
|
"""
|
||||||
|
users = get_users_list()
|
||||||
|
for user in users:
|
||||||
|
profile = User.objects.get(email=user.email).userprofile
|
||||||
|
update_user_in_model(profile, user)
|
||||||
|
return users
|
||||||
|
17
main/serializers.py
Normal file
17
main/serializers.py
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
from django.contrib.auth.models import User
|
||||||
|
from rest_framework import serializers
|
||||||
|
from main.models import UserProfile
|
||||||
|
|
||||||
|
|
||||||
|
class UserSerializer(serializers.HyperlinkedModelSerializer):
|
||||||
|
class Meta:
|
||||||
|
model = User
|
||||||
|
fields = ['email']
|
||||||
|
|
||||||
|
|
||||||
|
class ProfileSerializer(serializers.HyperlinkedModelSerializer):
|
||||||
|
user = UserSerializer()
|
||||||
|
|
||||||
|
class Meta:
|
||||||
|
model = UserProfile
|
||||||
|
fields = ['user', 'role', 'name']
|
@ -27,6 +27,7 @@
|
|||||||
|
|
||||||
</style>
|
</style>
|
||||||
{% block extra_css %}{% endblock %}
|
{% block extra_css %}{% endblock %}
|
||||||
|
{% block extra_scripts %}{% endblock %}
|
||||||
</head>
|
</head>
|
||||||
|
|
||||||
<body class="d-flex flex-column h-100">
|
<body class="d-flex flex-column h-100">
|
||||||
|
@ -10,6 +10,13 @@
|
|||||||
<link rel="stylesheet" href="{% static 'main/css/work.css' %}"/>
|
<link rel="stylesheet" href="{% static 'main/css/work.css' %}"/>
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
|
||||||
|
{% block extra_scripts %}
|
||||||
|
<script src="https://unpkg.com/react@17/umd/react.development.js" crossorigin></script>
|
||||||
|
<script src="https://unpkg.com/react-dom@17/umd/react-dom.development.js" crossorigin></script>
|
||||||
|
<script src="https://unpkg.com/babel-standalone@6/babel.min.js"></script>
|
||||||
|
<script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
|
||||||
|
{% endblock%}
|
||||||
|
|
||||||
{% block content %}
|
{% block content %}
|
||||||
<div class="container-md">
|
<div class="container-md">
|
||||||
<div class="new-section">
|
<div class="new-section">
|
||||||
@ -37,25 +44,22 @@
|
|||||||
<table class="light-table">
|
<table class="light-table">
|
||||||
|
|
||||||
<thead>
|
<thead>
|
||||||
<th>ID</th>
|
<th>Name</th>
|
||||||
<th>Email</th>
|
<th>Email</th>
|
||||||
<th>Role</th>
|
<th>Role</th>
|
||||||
<th>Name(link to profile)</th>
|
|
||||||
<th>Checked</th>
|
<th>Checked</th>
|
||||||
</thead>
|
</thead>
|
||||||
|
|
||||||
<tbody>
|
<tbody id="table">
|
||||||
{% for user in users %}
|
{% for user in users %}
|
||||||
<tr>
|
<tr>
|
||||||
<td>{{ user.id }}</td>
|
<td><a href="#">{{ user.name }}</a></td>
|
||||||
<td>{{ user.user.email }}</td>
|
<td>{{ user.user.email }}</td>
|
||||||
<td>{{ user.role }}</td>
|
<td>{{ user.role }}</td>
|
||||||
<td><a href="#">{{ user.name }}</a></td>
|
|
||||||
<td class="checkbox_field"></td>
|
<td class="checkbox_field"></td>
|
||||||
</tr>
|
</tr>
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
</tbody>
|
</tbody>
|
||||||
|
|
||||||
</table>
|
</table>
|
||||||
{% endblock%}
|
{% endblock%}
|
||||||
|
|
||||||
@ -103,6 +107,6 @@
|
|||||||
{% endblock %}
|
{% endblock %}
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<script src="{% static 'main/js/control.js'%}"></script>
|
<script src="{% static 'main/js/control.js'%}" type="text/babel"></script>
|
||||||
|
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
6
main/urls.py
Normal file
6
main/urls.py
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
from rest_framework.routers import DefaultRouter
|
||||||
|
from main.views import UsersViewSet
|
||||||
|
|
||||||
|
|
||||||
|
router = DefaultRouter()
|
||||||
|
router.register(r'users', UsersViewSet)
|
@ -14,7 +14,7 @@ from zenpy import Zenpy
|
|||||||
|
|
||||||
from access_controller.settings import EMAIL_HOST_USER
|
from access_controller.settings import EMAIL_HOST_USER
|
||||||
from main.extra_func import check_user_exist, update_profile, get_user_organization, \
|
from main.extra_func import check_user_exist, update_profile, get_user_organization, \
|
||||||
make_engineer, make_light_agent, get_users_list
|
make_engineer, make_light_agent, get_users_list, update_users_in_model, count_users
|
||||||
|
|
||||||
from django.contrib.auth.models import User, Permission
|
from django.contrib.auth.models import User, Permission
|
||||||
from main.models import UserProfile
|
from main.models import UserProfile
|
||||||
@ -27,6 +27,11 @@ from django.core.exceptions import PermissionDenied
|
|||||||
from access_controller.settings import ZENDESK_ROLES
|
from access_controller.settings import ZENDESK_ROLES
|
||||||
from zenpy.lib.api_objects import User as ZenpyUser
|
from zenpy.lib.api_objects import User as ZenpyUser
|
||||||
|
|
||||||
|
# Django REST
|
||||||
|
from rest_framework import viewsets, status
|
||||||
|
from main.serializers import ProfileSerializer
|
||||||
|
from rest_framework.response import Response
|
||||||
|
from rest_framework.decorators import action
|
||||||
|
|
||||||
content_type_temp = ContentType.objects.get_for_model(UserProfile)
|
content_type_temp = ContentType.objects.get_for_model(UserProfile)
|
||||||
permission_temp, created = Permission.objects.get_or_create(
|
permission_temp, created = Permission.objects.get_or_create(
|
||||||
@ -193,22 +198,6 @@ class AdminPageView(LoginRequiredMixin, PermissionRequiredMixin, FormView):
|
|||||||
def make_light_agents(users):
|
def make_light_agents(users):
|
||||||
[make_light_agent(user) for user in users]
|
[make_light_agent(user) for user in users]
|
||||||
|
|
||||||
@staticmethod
|
|
||||||
def count_users(users) -> tuple:
|
|
||||||
"""
|
|
||||||
Функция подсчета количества сотрудников с ролями engineer и light_a
|
|
||||||
|
|
||||||
.. 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) -> dict:
|
def get_context_data(self, **kwargs) -> dict:
|
||||||
"""
|
"""
|
||||||
Функция формирования контента страницы администратора (с проверкой прав доступа)
|
Функция формирования контента страницы администратора (с проверкой прав доступа)
|
||||||
@ -216,9 +205,10 @@ class AdminPageView(LoginRequiredMixin, PermissionRequiredMixin, FormView):
|
|||||||
if self.request.user.userprofile.role != 'admin':
|
if self.request.user.userprofile.role != 'admin':
|
||||||
raise PermissionDenied
|
raise PermissionDenied
|
||||||
context = super().get_context_data(**kwargs)
|
context = super().get_context_data(**kwargs)
|
||||||
context['users'] = get_list_or_404(
|
users = get_list_or_404(
|
||||||
UserProfile, role='agent')
|
UserProfile, role='agent')
|
||||||
context['engineers'], context['light_agents'] = self.count_users(get_users_list())
|
context['users'] = users
|
||||||
|
context['engineers'], context['light_agents'] = count_users(users)
|
||||||
return context # TODO: need to get profile page url
|
return context # TODO: need to get profile page url
|
||||||
|
|
||||||
|
|
||||||
@ -227,3 +217,18 @@ class CustomLoginView(LoginView):
|
|||||||
Отображение страницы авторизации пользователя
|
Отображение страницы авторизации пользователя
|
||||||
"""
|
"""
|
||||||
form_class = CustomAuthenticationForm
|
form_class = CustomAuthenticationForm
|
||||||
|
|
||||||
|
|
||||||
|
class UsersViewSet(viewsets.ReadOnlyModelViewSet):
|
||||||
|
"""
|
||||||
|
Класс для получения пользователей с помощью api
|
||||||
|
"""
|
||||||
|
queryset = UserProfile.objects.filter(role='agent')
|
||||||
|
serializer_class = ProfileSerializer
|
||||||
|
|
||||||
|
def list(self, request, *args, **kwargs):
|
||||||
|
users = update_users_in_model()
|
||||||
|
profiles = UserProfile.objects.filter(role='agent')
|
||||||
|
count = count_users(users)
|
||||||
|
serializer = self.get_serializer(data=profiles, many=True)
|
||||||
|
return Response(serializer.data + {'engineers': count[0], 'light_agents': count[1]})
|
||||||
|
@ -3,6 +3,8 @@ Django==3.1.6
|
|||||||
Pillow==8.1.0
|
Pillow==8.1.0
|
||||||
zenpy~=2.0.24
|
zenpy~=2.0.24
|
||||||
django_registration==3.1.1
|
django_registration==3.1.1
|
||||||
|
djangorestframework==3.12.2
|
||||||
|
|
||||||
|
|
||||||
# Documentation
|
# Documentation
|
||||||
Sphinx==3.4.3
|
Sphinx==3.4.3
|
||||||
|
@ -1,9 +1,60 @@
|
|||||||
"use strict";
|
"use strict";
|
||||||
let checkboxes = document.getElementsByName("users");
|
|
||||||
let fields = document.querySelectorAll(".checkbox_field");
|
function move_checkboxes() {
|
||||||
if (checkboxes.length == fields.length) {
|
let checkboxes = document.getElementsByName("users");
|
||||||
for (let i = 0; i < fields.length; ++i) {
|
let fields = document.querySelectorAll(".checkbox_field");
|
||||||
let el = checkboxes[i].cloneNode(true);
|
if (checkboxes.length == fields.length) {
|
||||||
fields[i].appendChild(el);
|
for (let i = 0; i < fields.length; ++i) {
|
||||||
|
let el = checkboxes[i].cloneNode(true);
|
||||||
|
fields[i].appendChild(el);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class TableRow extends React.Component {
|
||||||
|
render() {
|
||||||
|
return (
|
||||||
|
<tr>
|
||||||
|
<td>
|
||||||
|
<a href="#">{this.props.user.name}</a>
|
||||||
|
</td>
|
||||||
|
<td>{this.props.user.user.email}</td>
|
||||||
|
<td>{this.props.user.role}</td>
|
||||||
|
<td className="checkbox_field"></td>
|
||||||
|
</tr>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class TableBody extends React.Component {
|
||||||
|
constructor(props) {
|
||||||
|
super(props);
|
||||||
|
this.state = { users: [] };
|
||||||
|
}
|
||||||
|
|
||||||
|
get_users() {
|
||||||
|
axios.get("/api/users").then((response) => {
|
||||||
|
this.setState({ users: response.data });
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
componentDidMount() {
|
||||||
|
this.interval = setInterval(() => {
|
||||||
|
this.get_users();
|
||||||
|
move_checkboxes();
|
||||||
|
}, 1000);
|
||||||
|
}
|
||||||
|
|
||||||
|
componentWillUnmount() {
|
||||||
|
clearInterval(this.interval);
|
||||||
|
}
|
||||||
|
|
||||||
|
render() {
|
||||||
|
return this.state.users.map((user, key) => (
|
||||||
|
<TableRow user={user} key={key} />
|
||||||
|
));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
move_checkboxes();
|
||||||
|
ReactDOM.render(<TableBody />, document.getElementById("table"));
|
||||||
|
Loading…
x
Reference in New Issue
Block a user