123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112 |
- import datetime
- import logging
- from django.http import HttpResponse
- from rest_framework.response import Response
- from rest_framework.views import APIView
- from apps.user.middleware.rolecontrol import RoleControl
- from apps.user.models import User
- from utils.util_add_id import get_id
- from utils.util_jwt import create_token_for_login
- logger = logging.getLogger('django')
- class UserListView(APIView):
- authentication_classes = []
- # register
- @staticmethod
- def post(request, *args, **kwargs):
- payload = {
- 'username': request.POST.get('username'),
- 'password': request.POST.get('password'),
- 'name': request.POST.get('name'),
- 'tel': request.POST.get('tel'),
- 'identify': request.POST.get('identify'),
- }
- if not payload['username']:
- logger.error("用户名不能为空")
- return HttpResponse(status=500, content='用户名不能为空')
- if not payload['password']:
- logger.error("密码不能为空")
- return HttpResponse(status=500, content='密码不能为空')
- users = User.objects.filter(username=payload['username'])
- if users:
- logger.error("用户名已存在,请修改用户名重新上传")
- return HttpResponse(status=500, content='用户名已存在,请修改用户名重新上传')
- create_time = update_time = datetime.datetime.now()
- payload['create_time'] = create_time
- payload['update_time'] = update_time
- payload['id'] = get_id(User, 'TestLaboratory_V1_User_1')
- User.objects.create(**payload)
- return HttpResponse(status=200, content='创建成功')
- # user list
- @staticmethod
- @RoleControl
- def get(request, *args, **kwargs):
- username = request.GET.get('username')
- identify = request.GET.get('identify')
- is_login = request.GET.get('is_login')
- sort = request.GET.get('sort')
- user_all = User.objects.order_by('-create_time')
- if username:
- user_all = user_all.filter(username__contains=username)
- if identify:
- user_all = user_all.filter(identify=identify)
- if is_login:
- if is_login == '0':
- user_all = user_all.filter(is_login=False)
- elif is_login == '1':
- user_all = user_all.filter(is_login=True)
- if sort:
- user_all = user_all.order_by(sort)
- users_info = []
- for user in user_all:
- users_info.append({
- 'userid': user.id,
- 'username': user.username,
- 'name': user.name,
- 'identify': user.identify,
- 'tel': user.tel,
- 'is_login': user.is_login
- })
- return Response(users_info)
- class UserListLogin(APIView):
- authentication_classes = []
- @staticmethod
- def post(request, *args, **kwargs):
- username = request.POST.get('username')
- password = request.POST.get('password')
- user_obj = User.objects.filter(username=username, password=password)
- if not user_obj:
- logger.error("用户名或密码错误")
- return HttpResponse(status=500, content='用户名或密码错误')
- user_obj = user_obj[0]
- token = create_token_for_login(
- {'username': username, 'password': password, 'userid': user_obj.id})
- user_obj.is_login = True
- user_obj.save()
- user_info = {
- 'userid': user_obj.id,
- 'username': user_obj.username,
- 'identify': user_obj.identify,
- 'token': token
- }
- return Response(user_info)
|