|
|
|
import json
|
|
|
|
|
|
|
|
from django.shortcuts import render
|
|
|
|
from django.http import JsonResponse, HttpResponse
|
|
|
|
|
|
|
|
# Create your views here.
|
|
|
|
from django.views.decorators.csrf import csrf_exempt
|
|
|
|
from Student.models import *
|
|
|
|
from teacher.models import Teacher
|
|
|
|
from EduSystemServer.utils import ResponseUtil
|
|
|
|
|
|
|
|
|
|
|
|
@csrf_exempt
|
|
|
|
def login(request):
|
|
|
|
username = json.loads(request.body).get("username")
|
|
|
|
password = json.loads(request.body).get("password")
|
|
|
|
_type = json.loads(request.body).get("type")
|
|
|
|
if _type == "student":
|
|
|
|
student = Student.objects.filter(username=username,
|
|
|
|
password=password).first()
|
|
|
|
if student:
|
|
|
|
request.session["username"] = student.username
|
|
|
|
request.session["type"] = "student"
|
|
|
|
result = ResponseUtil.ok(student.to_dict(), "login success!")
|
|
|
|
else:
|
|
|
|
result = {"code": -1, "message": "username or password error!"}
|
|
|
|
elif _type == "teacher":
|
|
|
|
teacher = Teacher.objects.filter(username=username,
|
|
|
|
password=password).first()
|
|
|
|
if teacher:
|
|
|
|
request.session["username"] = teacher.username
|
|
|
|
request.session["type"] = "teacher"
|
|
|
|
result = ResponseUtil.ok(teacher.to_dict(), "login success!")
|
|
|
|
else:
|
|
|
|
result = ResponseUtil.error("username or password error!")
|
|
|
|
else:
|
|
|
|
result = ResponseUtil.error("type error!")
|
|
|
|
return JsonResponse(result)
|
|
|
|
|
|
|
|
@csrf_exempt
|
|
|
|
def get_user_info(request):
|
|
|
|
_type = request.GET.get("type")
|
|
|
|
username = request.GET.get("username")
|
|
|
|
if _type == "student":
|
|
|
|
student = Student.objects.filter(username=username).first()
|
|
|
|
result = ResponseUtil.ok(student.to_dict())
|
|
|
|
elif _type == "teacher":
|
|
|
|
teacher = Teacher.objects.filter(username=username).first()
|
|
|
|
result = ResponseUtil.ok(teacher.to_dict())
|
|
|
|
else:
|
|
|
|
result = ResponseUtil.error("type error")
|
|
|
|
request.session.clear()
|
|
|
|
return JsonResponse(result)
|