123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129 |
- 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'),
- 'group': request.POST.get('group')
- }
- if not payload['username']:
- logger.error("用户名不能为空")
- return HttpResponse(status=500, content='用户名不能为空')
- if not payload['password']:
- logger.error("密码不能为空")
- return HttpResponse(status=500, content='密码不能为空')
- if not payload['group']:
- del payload['group']
- 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')
- new_user = 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 = []
- user_group_index = {}
- i = 0
- for user in user_all:
- if user.group not in user_group_index.keys():
- user_group_index[user.group] = i
- i += 1
- user_group = {'groupName': user.group, 'userList': [{
- 'userid': user.id,
- 'username': user.username,
- 'identify': user.identify,
- 'group': user.group,
- 'is_login': user.is_login
- }]}
- users_info.append(user_group)
- else:
- users_info[user_group_index[user.group]]['userList'].append({
- 'userid': user.id,
- 'username': user.username,
- 'identify': user.identify,
- 'group': user.group,
- 'is_login': user.is_login
- })
- return Response({'groupList': 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,
- 'group': user_obj.group,
- 'token': token
- }
- return Response(user_info)
|