You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
EduSystem/EduSystemServer/API/views.py

55 lines
1.9 KiB

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 = request.POST.get("username")
password = request.POST.get("password")
_type = request.POST.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!")
response = JsonResponse(result)
return response
@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)