2020-09-27 18:43:01 +08:00
|
|
|
from rest_framework.decorators import api_view, action
|
|
|
|
from rest_framework import status, exceptions
|
|
|
|
from rest_framework.response import Response
|
2021-07-10 02:03:34 +08:00
|
|
|
from .models import Teams, User
|
2020-09-27 18:43:01 +08:00
|
|
|
from warehouse.models import Inventory
|
|
|
|
from rest_framework import viewsets
|
|
|
|
from django.contrib import auth
|
|
|
|
from django.db.models import F
|
|
|
|
import requests
|
|
|
|
import pendulum
|
|
|
|
import random
|
|
|
|
import re
|
|
|
|
|
|
|
|
|
|
|
|
@api_view(['POST'])
|
|
|
|
def login(request):
|
|
|
|
username = request.data.get('username')
|
|
|
|
password = request.data.get('password')
|
|
|
|
|
|
|
|
user = auth.authenticate(username=username, password=password)
|
|
|
|
teams = Teams.objects.filter(phone=username).first()
|
|
|
|
if not user:
|
|
|
|
if not teams:
|
|
|
|
raise exceptions.AuthenticationFailed({'message': '账号密码错误'})
|
|
|
|
else:
|
|
|
|
user = teams.users.filter(is_boss=True).first()
|
|
|
|
user = auth.authenticate(username=user.username, password=password)
|
|
|
|
if not user:
|
|
|
|
raise exceptions.AuthenticationFailed({'message': '账号密码错误'})
|
|
|
|
|
|
|
|
auth.login(request, user)
|
|
|
|
return Response(status=status.HTTP_200_OK)
|
|
|
|
|
|
|
|
|
|
|
|
@api_view(['POST'])
|
|
|
|
def logout(request):
|
|
|
|
auth.logout(request)
|
|
|
|
return Response(status=status.HTTP_200_OK)
|
|
|
|
|
|
|
|
|
|
|
|
@api_view(['POST'])
|
|
|
|
def register(request):
|
|
|
|
company_name = request.data.get('company_name')
|
|
|
|
name = request.data.get('name')
|
|
|
|
phone = request.data.get('phone')
|
|
|
|
username = request.data.get('username')
|
|
|
|
password = request.data.get('password')
|
|
|
|
|
|
|
|
# 数据验证
|
|
|
|
if not username or not password or not company_name or not name or not phone:
|
|
|
|
raise exceptions.ValidationError
|
|
|
|
|
|
|
|
if Teams.objects.filter(phone=phone).first():
|
|
|
|
raise exceptions.ValidationError({'message': '手机号已被注册'})
|
|
|
|
|
|
|
|
if User.objects.filter(username=username).first():
|
|
|
|
raise exceptions.ValidationError({'message': '账号已被注册'})
|
|
|
|
|
|
|
|
teams = Teams.objects.create(phone=phone, company_name=company_name)
|
2021-07-10 01:56:39 +08:00
|
|
|
User.objects.create(name=name, phone=phone, username=username, password=password, teams=teams)
|
2020-09-27 18:43:01 +08:00
|
|
|
return Response(status=status.HTTP_201_CREATED)
|
|
|
|
|
|
|
|
|
|
|
|
@api_view(['GET'])
|
|
|
|
def get_info(request):
|
|
|
|
if not request.user.is_authenticated:
|
|
|
|
raise exceptions.AuthenticationFailed({'message': '未登录'})
|
|
|
|
|
|
|
|
teams = request.user.teams
|
|
|
|
inventory_warning_count = Inventory.objects.filter(
|
|
|
|
teams=teams, quantity__lte=F('goods__inventory_warning_lower_limit')).count()
|
|
|
|
data = {'username': request.user.username, 'inventory_warning_count': inventory_warning_count}
|
|
|
|
return Response(data=data, status=status.HTTP_200_OK)
|
|
|
|
|
|
|
|
|
2021-07-10 01:56:39 +08:00
|
|
|
# @api_view(['POST'])
|
|
|
|
# def set_password(request):
|
|
|
|
# phone = request.data.get('phone')
|
|
|
|
# code = request.data.get('code')
|
|
|
|
# password = request.data.get('password')
|
|
|
|
# confirm = request.data.get('confirm')
|
2020-09-27 18:43:01 +08:00
|
|
|
|
2021-07-10 01:56:39 +08:00
|
|
|
# # 数据验证
|
|
|
|
# if not phone or not password or not confirm or password != confirm:
|
|
|
|
# raise exceptions.ValidationError
|
2020-09-27 18:43:01 +08:00
|
|
|
|
2021-07-10 01:56:39 +08:00
|
|
|
# if not code or len(code) != 6 or not re.match(r'^1[3456789]\d{9}$', phone):
|
|
|
|
# raise exceptions.ValidationError
|
2020-09-27 18:43:01 +08:00
|
|
|
|
2021-07-10 01:56:39 +08:00
|
|
|
# # 验证 code
|
|
|
|
# filters = {
|
|
|
|
# 'phone': phone,
|
|
|
|
# 'code': code,
|
|
|
|
# 'create_datetime__gte': pendulum.now().subtract(minutes=10),
|
|
|
|
# 'create_datetime__lte': pendulum.now(),
|
|
|
|
# }
|
2020-09-27 18:43:01 +08:00
|
|
|
|
2021-07-10 01:56:39 +08:00
|
|
|
# if not Captcha.objects.filter(**filters).first():
|
|
|
|
# raise exceptions.ValidationError({'message': '验证码错误'})
|
2020-09-27 18:43:01 +08:00
|
|
|
|
2021-07-10 01:56:39 +08:00
|
|
|
# teams = Teams.objects.filter(phone=phone).first()
|
|
|
|
# if not teams:
|
|
|
|
# raise exceptions.ValidationError({'message': '账号不存在'})
|
2020-09-27 18:43:01 +08:00
|
|
|
|
2021-07-10 01:56:39 +08:00
|
|
|
# user = teams.users.filter(is_boss=True).first()
|
|
|
|
# user.set_password(password)
|
|
|
|
# user.save()
|
2020-09-27 18:43:01 +08:00
|
|
|
|
2021-07-10 01:56:39 +08:00
|
|
|
# auth.logout(request)
|
|
|
|
# return Response(status=status.HTTP_200_OK)
|