from django.db.models import Max
from django.db.models import Q
from django.shortcuts import render
from django.contrib.auth.decorators import login_required
from rest_framework.views import APIView
from django.contrib.auth import authenticate, login, logout
from .Serializer import UserProfileSerializer, AreaSerializer, RoleSerializer, CustomerSerializer, \
OrganizationSerializer, BrandSerializer, TotalWareHouseSerializer, CenterSerializer, \
SupplierSerializer, MeterageSerializer, MaterialTypeSerializer, MaterialSerializer, DepartmentSerializer
from rest_framework.response import Response
# Create your views here.
from django.shortcuts import render, redirect
from base import models
import json
# Create your views here.
from django.http import HttpResponse
def departmentToList(departments_name_list):
dpm_list = []
for department_name in departments_name_list:
id = str(models.Department.objects.get(dpm_name=department_name).id)
dpm_list.append(id)
dpm_name = "-".join(dpm_list)
return dpm_name
def roleToList(roles_name_list):
role_list = []
for role_name in roles_name_list:
id = str(models.Role.objects.get(role=role_name).id)
role_list.append(id)
role_name = "-".join(role_list)
return role_name
"""
用户维护接口
- 登录
- 添加
- 查看详细信息
- 查看所有用户信息
- 更新用户信息(单条)
"""
class LoginView(APIView):
def post(self, request):
json_data = json.loads(self.request.body.decode("utf-8"))
# user_now = models.UserNow.objects.get()
# if user_now:
# return Response({"message": "老子已经在线了,你还想挤掉我"})
user_iden = json_data['user_iden']
user_passwd = json_data['user_passwd']
# user_iden = self.request.data.get('user_iden')
# user_passwd = self.request.data.get('user_passwd')
user_now = models.UserNow.objects.filter(user_iden=user_iden)
print(user_now)
if user_now:
return Response({"message": "用户已在别处登录,请稍后再试"})
else:
try:
user = authenticate(username=user_iden, password=user_passwd)
except models.UserProfile.DoesNotExist:
return Response({'message': '登录异常', 'signal': '3'})
else:
if user:
if user.is_active == 1:
login(request, user)
user = models.UserProfile.objects.get(username=user_iden)
user_id = user.id
username = user.username
user_name = user.user_name
area_name = user.area_name
user_departments = user.user_departments
user_roles = user.user_roles
models.UserNow.objects.create(user_id=user_id, user_iden=username, user_name=user_name,
area_name=area_name,
user_departments=user_departments, user_roles=user_roles)
user_roles = user.user_roles
roles = []
if user_roles:
user_roles_list = list(map(int, user_roles.split('-')))
for user_role in user_roles_list:
role_message = []
try:
role = models.Role.objects.get(id=user_role, role_status=1)
except models.Role.DoesNotExist:
pass
else:
role_message.append(role.role)
role_message.append(role.role_power)
roles.append(role_message)
user_serializer = UserProfileSerializer(user)
return Response(
{'message': '登录成功', 'signal': '0', 'roles': roles, 'user': user_serializer.data})
elif user.is_active == 0:
return Response({'message': '账号已关闭,请联系管理员开启', 'signal': '1'})
else:
return Response({'message': '用户名或密码错误', 'signal': '2'})
class LoginExitView(APIView):
def post(self, request):
json_data = json.loads(self.request.body.decode("utf-8"))
user_now_iden = json_data['user_now_iden']
try:
user_now = models.UserNow.objects.get(user_iden=user_now_iden) # 删除当前用户表信息
except models.UserNow.DoesNotExist:
return Response({"message": "未登录", "signal": 1})
else:
logout(request)
user_now.delete()
return Response({"message": "退出登录成功", "signal": 0})
class UserView(APIView):
def post(self, request):
json_data = json.loads(self.request.body.decode("utf-8"))
user_now_iden = json_data['user_now_iden']
user_now = models.UserNow.objects.filter(user_iden=user_now_iden)
if user_now:
try:
user = models.UserProfile.objects.get(username=user_now.user_iden)
except models.UserProfile.DoesNotExist:
return Response({'message': '查询不到用户信息'})
else:
user_serializer = UserProfileSerializer(user)
return Response({"user": user_serializer.data})
return Response({"message": "用户未登录"})
class UserNewView(APIView):
def get(self, request):
max_id = models.UserProfile.objects.all().aggregate(Max('username'))['username__max']
departments = models.Department.objects.filter(dpm_status=1).values_list('dpm_name', flat=True)
roles = models.Role.objects.filter(role_status=1).values_list('role', flat=True)
areas = models.Area.objects.filter(area_status=1).values_list('area_name', flat=True)
return Response({"max_iden": max_id, "departments": departments, "roles": roles, "areas": areas})
class UserAddView(APIView):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.message = "注册成功"
self.signal = 0
self.user_now_name = ""
def post(self, request):
json_data = json.loads(self.request.body.decode("utf-8"))
user_now_iden = json_data['user_now_iden']
user_now = models.UserNow.objects.get(user_iden=user_now_iden)
if user_now:
self.user_now_name = user_now.user_name
username = json_data['username']
password = json_data['password']
user_name = json_data['user_name']
user_phone_number = json_data['user_phone_number']
email = json_data['email']
# max_id = models.UserProfile.objects.all().aggregate(Max('username'))['username__max']
# user_iden = str(int(max_id) + 1) # 编号后台处理过了
user_departments = departmentToList(json_data['user_departments']) # 传过来的是名字列表
user_roles = roleToList(json_data['user_roles']) # 传过来的是角色列表
area_name = json_data['area_name']
# user_now = models.UserNow.objects.get(user_iden=user_now_iden)
# user_creator = user_now.user_name
# user_creator_iden = user_now.user_iden
# area = models.Area.objects.filter(area_name=area_name).first()
# user_createDate = json_data['user_createDate']
if self.idCheck(username):
if self.phoneCheck(user_phone_number):
if self.emailCheck(email):
models.UserProfile.objects.create_user(username=username, password=password,
user_name=user_name, user_phone_
csbysj2020
- 粉丝: 2752
- 资源: 5571
最新资源
- 毕设和企业适用springboot社交应用平台类及金融数据分析平台源码+论文+视频.zip
- 毕设和企业适用springboot社交应用平台类及交通信息平台源码+论文+视频.zip
- 毕设和企业适用springboot人力资源管理类及用户数据分析平台源码+论文+视频.zip
- 毕设和企业适用springboot人力资源管理类及用户体验优化平台源码+论文+视频.zip
- 毕设和企业适用springboot人力资源管理类及用户行为分析平台源码+论文+视频.zip
- 毕设和企业适用springboot人力资源管理类及运动管理平台源码+论文+视频.zip
- 毕设和企业适用springboot人力资源管理类及智能化系统源码+论文+视频.zip
- 毕设和企业适用springboot社交互动平台类及社交媒体平台源码+论文+视频.zip
- 毕设和企业适用springboot社交互动平台类及人工智能客服平台源码+论文+视频.zip
- 毕设和企业适用springboot社交互动平台类及社交游戏平台源码+论文+视频.zip
- 毕设和企业适用springboot社交应用平台类及跨平台销售系统源码+论文+视频.zip
- 毕设和企业适用springboot社交应用平台类及民生服务平台源码+论文+视频.zip
- 毕设和企业适用springboot社交互动平台类及生活服务平台源码+论文+视频.zip
- 毕设和企业适用springboot社交互动平台类及食品配送管理平台源码+论文+视频.zip
- 毕设和企业适用springboot社交互动平台类及社区服务平台源码+论文+视频.zip
- 毕设和企业适用springboot社交应用平台类及无人驾驶系统源码+论文+视频.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈