48 lines
1.6 KiB
Python
48 lines
1.6 KiB
Python
from django.shortcuts import render
|
|
from django.http import HttpResponse
|
|
from django.contrib.auth import get_user_model
|
|
from django.views.decorators.csrf import csrf_exempt
|
|
from django_token.models import Token
|
|
|
|
# Create your views here.
|
|
|
|
|
|
def index(request):
|
|
return HttpResponse("Hello, world. You're at the polls index.")
|
|
|
|
|
|
@csrf_exempt
|
|
def polls_login(request):
|
|
if request.method == 'POST':
|
|
phone = request.POST.get('phone')
|
|
password = request.POST.get('password')
|
|
UserModel = get_user_model()
|
|
user = UserModel.objects.filter(first_name=phone).first()
|
|
if not user:
|
|
return JsonResponse({'status': 'error', 'message': '用户名或密码错误'})
|
|
|
|
u = authenticate(request, username=user.username, password=password)
|
|
if u is not None:
|
|
login(request, u)
|
|
token = Token.objects.create(user=myuser)
|
|
|
|
result = dict()
|
|
result['id'] = user.id
|
|
result['username'] = user.username
|
|
result['email'] = user.email
|
|
result['role'] = ','.join(roles)
|
|
if teacher:
|
|
school = teacher.school.title
|
|
r = get_region(school)
|
|
if r:
|
|
result['regionId'] = r[0]
|
|
result['regionName'] = r[1]
|
|
result['unitId'] = r[2]
|
|
result['unitName'] = r[3]
|
|
result['teacher'] = {'name': teacher.name, 'school': school}
|
|
return JsonResponse(result)
|
|
else:
|
|
return JsonResponse({'status': 'error', 'message': '用户名或密码错误'})
|
|
else:
|
|
return JsonResponse({})
|