test/app/apps/management/views/views_user.py
2025-07-24 15:50:36 +07:00

308 lines
12 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 traceback
import uuid
import django.contrib.auth.password_validation as validators
from django import forms
from django.contrib.auth.decorators import login_required
from django.contrib.auth.hashers import make_password
from django.contrib.auth.models import User
from django.core import exceptions
from django.core.paginator import EmptyPage, PageNotAnInteger, Paginator
from django.db.models import Q
from django.shortcuts import redirect, render
from django.views.decorators.cache import never_cache
from apps.general.models.model_settings import Settings
from apps.general.views import settingsmodule
from apps.gto.models.model_type_icons import ModelTypeIcons
from apps.management.views import views_user
from apps.util import generalmodule
from sabron.util import logging
@login_required(login_url="/")
@never_cache
def get_user(request):
try:
post_param = request.POST.dict()
response = dict()
response.update(id=0)
response.update(role = "")
response.update(first_name = "")
response.update(last_name = "")
response.update(isredonly = False)
data =dict()
if 'id' in post_param:
user = User.objects.filter(id=post_param['id']).first()
print(user)
if user is not None:
response.update(id=user.id)
response.update(first_name = user.first_name)
response.update(last_name = user.last_name)
response.update(role = user.profile.role)
response.update(isredonly = user.profile.isredonly)
data.update(status='ok')
data.update(response=response)
return generalmodule.ReturnJson(200, data)
except Exception as err:
logging.error(traceback.format_exc())
data =dict()
data.update(status='error')
return generalmodule.ReturnJson(200, data)
@login_required(login_url="/")
@never_cache
def user_delete(request):
try:
data =dict()
post_param = request.POST.dict()
user = User.objects.filter(id=post_param["id"]).first()
if user is not None:
data.update(name=user.username)
user.delete()
data.update(status='ok')
data.update(response=post_param)
return generalmodule.ReturnJson(200, data)
except Exception as err:
logging.error(traceback.format_exc())
data =dict()
data.update(status='error')
return generalmodule.ReturnJson(200, data)
@login_required(login_url="/")
@never_cache
def user_edit(request):
try:
get_param = request.GET.dict()
context = generalmodule.get_context_template()
context.update(error=False)
context.update(mt="edit")
if "id" in get_param:
edit_user = User.objects.filter(id=get_param["id"]).first()
context.update(edit_user=edit_user)
context.update(id_role=edit_user.profile.role)
context.update(first_name=edit_user.first_name)
context.update(last_name=edit_user.last_name)
context.update(username=edit_user.username)
context.update(isredonly=edit_user.profile.isredonly)
return render(request, "user_edit.html", context)
except Exception as err:
logging.error(traceback.format_exc())
return redirect("/management/")
@login_required(login_url="/")
@never_cache
def user_password_edit(request):
try:
get_param = request.GET.dict()
context = generalmodule.get_context_template()
context.update(error=False)
context.update(mt="edit")
if "id" in get_param:
edit_user = User.objects.filter(id=get_param["id"]).first()
context.update(edit_user=edit_user)
context.update(id_role=edit_user.profile.role)
context.update(first_name=edit_user.first_name)
context.update(last_name=edit_user.last_name)
context.update(username=edit_user.username)
context.update(isredonly=edit_user.profile.isredonly)
return render(request, "user_edit_password.html", context)
except Exception as err:
logging.error(traceback.format_exc())
return redirect("/management/")
@login_required(login_url="/")
@never_cache
def user_save(request):
try:
role_list = (
(0, "Пользователь"),
(1, "Администратор"),
(2, "Администратор системы"),
(3, "Начальник участка"),
(4, "Главный инженер"),
(5, "Главный геолог"),
(6, "Главный технолог"),
)
post_param = request.POST.dict()
print(post_param)
if 'id' in post_param:
username = post_param["username"].strip()
password = post_param["password"].strip()
password2 = post_param["password2"].strip()
first_name = post_param["first_name"].strip()
last_name = post_param["last_name"].strip()
role = post_param["role"].strip()
if post_param['id'] == '0':
if len(username)==0:
data =dict()
data.update(status='error')
data.update(error_comment="Не задан логин")
return generalmodule.ReturnJson(200, data)
user = User.objects.filter(username=username).first()
if user is not None:
data =dict()
data.update(status='error')
data.update(error_comment="Логин уже используется")
return generalmodule.ReturnJson(200, data)
if len(password)==0 or len(password2)==0:
data =dict()
data.update(status='error')
data.update(error_comment="Не задан пароль")
return generalmodule.ReturnJson(200, data)
if password != password2:
data =dict()
data.update(status='error')
data.update(error_comment="Пароли не совподают")
return generalmodule.ReturnJson(200, data)
try:
validators.validate_password(password=password)
except exceptions.ValidationError as e:
comment = list(e.messages)
data =dict()
data.update(status='error')
data.update(error_comment=comment)
return generalmodule.ReturnJson(200, data)
isredonly = False
if 'isredonly' in post_param:
isredonly = True
user = User.objects.create_user(
username=username,
password=password,
first_name=first_name,
last_name=last_name,
)
if user is not None:
user.profile.isredonly = True
user.profile.role = role
user.save()
else:
data =dict()
data.update(status='error')
data.update(error_comment="Ошибка записи")
return generalmodule.ReturnJson(200, data)
else:
isredonly = False
if 'isredonly' in post_param:
isredonly = True
user = User.objects.filter(id=post_param["id"]).first()
user.first_name = first_name
user.last_name=last_name
user.profile.isredonly = isredonly
user.profile.role = role
user.save()
data =dict()
data.update(status='ok')
data.update(response=post_param)
data.update(role=user.profile.get_role())
data.update(isredonly=isredonly)
data.update(username=user.username)
return generalmodule.ReturnJson(200, data)
except Exception as err:
logging.error(traceback.format_exc())
data =dict()
data.update(status='error')
return generalmodule.ReturnJson(200, data)
@login_required(login_url="/")
@never_cache
def user_password_save(request):
try:
post_param = request.POST.dict()
data =dict()
if 'password_in' in post_param:
password = post_param["password_edit"].strip()
password2 = post_param["password_edit2"].strip()
if post_param['password_in']:
if len(password)==0 or len(password2)==0:
data =dict()
data.update(status='error')
data.update(error_comment="Не задан пароль")
return generalmodule.ReturnJson(200, data)
if password != password2:
data =dict()
data.update(status='error')
data.update(error_comment="Пароли не совподают")
return generalmodule.ReturnJson(200, data)
try:
validators.validate_password(password=password)
except exceptions.ValidationError as e:
comment = list(e.messages)
data =dict()
data.update(status='error')
data.update(error_comment=comment)
return generalmodule.ReturnJson(200, data)
user = User.objects.filter(id=post_param["password_in"]).first()
user.password=make_password(password)
user.save()
print(password)
print(user.password)
data.update(username=user.username)
else:
data.update(status='error')
return generalmodule.ReturnJson(200, data)
data.update(status='ok')
return generalmodule.ReturnJson(200, data)
except Exception as err:
logging.error(traceback.format_exc())
data =dict()
data.update(status='error')
return generalmodule.ReturnJson(200, data)
@login_required(login_url="/")
@never_cache
def user_add(request):
try:
get_param = request.GET.dict()
context = generalmodule.get_context_template()
context.update(mt="add")
return render(request, "user_add.html", context)
except Exception as err:
logging.error(traceback.format_exc())
return redirect("/management/")
@login_required(login_url="/")
@never_cache
def main(request):
try:
get_param = request.GET.dict()
if request.method == "GET":
context = generalmodule.get_context_template()
myquery = Q()
myquery &= Q(is_superuser=False)
user_list = User.objects.filter(myquery).all().order_by("username")
gto_icons = ModelTypeIcons.objects.all().order_by("model")
context.update({"user_list": user_list, "gto_icons": gto_icons})
if request.method == "POST":
if get_param["cmd"] == "rtls_save":
return rtls_save(request)
if get_param["cmd"] == "oset_save":
return oset_save(request)
# context = generalmodule.get_context_template()
return render(request, "users/../templates/users/users_main.html", context)
except Exception as err:
logging.error(traceback.format_exc())
return redirect("/management/")