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

65 lines
2.2 KiB

import json
1 year ago
from django.shortcuts import render
1 year ago
from django.http import JsonResponse, HttpResponse
1 year ago
1 year ago
# 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
1 year ago
@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"
1 year ago
<<<<<<< Updated upstream
1 year ago
result = ResponseUtil.ok(student.to_dict(), "login success!")
1 year ago
=======
result = {"code": 0, "message": "login success!"}
>>>>>>> Stashed changes
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"
1 year ago
<<<<<<< Updated upstream
1 year ago
result = ResponseUtil.ok(teacher.to_dict(), "login success!")
1 year ago
=======
result = {"code": 0, "message": "login success!"}
>>>>>>> Stashed changes
else:
result = ResponseUtil.error("username or password error!")
else:
result = ResponseUtil.error("type error!")
return JsonResponse(result)
1 year ago
<<<<<<< Updated upstream
1 year ago
1 year ago
=======
>>>>>>> Stashed changes
@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)