add login middle .... function

scl-branch
bettleChen 1 year ago
parent 61123816d4
commit e6551c7ddb

@ -14,7 +14,7 @@
</component>
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$" />
<orderEntry type="jdk" jdkName="Python 3.9" jdkType="Python SDK" />
<orderEntry type="jdk" jdkName="Python 3.7 (EduSystemServer)" jdkType="Python SDK" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
<component name="TemplatesService">

@ -1,4 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.9" project-jdk-type="Python SDK" />
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.7 (EduSystemServer)" project-jdk-type="Python SDK" />
</project>

@ -0,0 +1,23 @@
from django.http import JsonResponse
class AuthMiddleware:
"""
验证权限登录中间件
"""
def __init__(self, get_response):
self.get_response = get_response
def __call__(self, request):
path = request.path
if path in [
"/login",
]:
response = self.get_response(request)
return response
session = request.session
if not session.get("username") and not session.get("type"):
return JsonResponse({"code": -1, "msg": "not login!"}, status=401)
response = self.get_response(request)
return response

@ -13,9 +13,8 @@ Including another URLconf
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
from API import views
from API.views import *
from django.urls import path
urlpatterns = [
path("students/", views.student)
]

@ -1,9 +1,53 @@
import json
from django.shortcuts import render
from django.http import JsonResponse
# 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
def student(request):
return JsonResponse({"sid": 1, "sname": "张三"})
@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 = {"code": 0, "message": "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 = {"code": 0, "message": "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)

@ -12,6 +12,14 @@ https://docs.djangoproject.com/en/2.2/ref/settings/
import os
CORS_ALLOW_ORIGIN_WHITELIST = [
"http://localhost:8080", # 允许访问的来源
"http://localhost:8000", # 允许访问的来源
# 可以继续添加其他允许的来源
]
CORS_ALLOW_CREDENTIALS = True # 允许跨域请求携带凭据例如Cookies
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
@ -29,7 +37,6 @@ ALLOWED_HOSTS = ["*"]
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
@ -37,6 +44,7 @@ INSTALLED_APPS = [
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'corsheaders',
'API',
'Student',
'teacher',
@ -52,8 +60,11 @@ MIDDLEWARE = [
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'corsheaders.middleware.CorsMiddleware',
# 'API.middle.AuthMiddleware',
]
ROOT_URLCONF = 'EduSystemServer.urls'
TEMPLATES = [

@ -15,11 +15,14 @@ Including another URLconf
"""
from django.contrib import admin
from django.urls import path, include
from API.views import login, get_user_info
urlpatterns = [
path('Eduadmin/', admin.site.urls),
path('api/', include(("API.urls", "api"), namespace="api")),
path('student/', include(("Student.urls", "student"), namespace="student")),
path('course/', include(("course.urls", "course"), namespace="course"))
path('course/', include(("course.urls", "course"), namespace="course")),
path('login', login),
path('userinfo', get_user_info),
]

@ -0,0 +1,10 @@
class ResponseUtil:
@staticmethod
def ok(data, message="success!"):
return {"code": 0, "message": message, "data": data}
@staticmethod
def error(message="error!"):
return {"code": -1, "message": message}

@ -1,4 +1,4 @@
# Generated by Django 2.1 on 2023-09-06 14:02
# Generated by Django 2.1 on 2023-09-07 13:49
from django.db import migrations, models
@ -14,8 +14,8 @@ class Migration(migrations.Migration):
migrations.CreateModel(
name='Admin',
fields=[
('username', models.CharField(max_length=32, primary_key=True, serialize=False, verbose_name='管理员账号')),
('password', models.CharField(max_length=128, verbose_name='管理员密码')),
('username', models.CharField(blank=True, max_length=32, primary_key=True, serialize=False, verbose_name='管理员账号')),
('password', models.CharField(blank=True, max_length=128, verbose_name='管理员密码')),
],
options={
'verbose_name': '管理员',

@ -1,4 +1,4 @@
# Generated by Django 2.1 on 2023-09-06 14:01
# Generated by Django 2.1 on 2023-09-07 13:49
from django.db import migrations, models
@ -14,14 +14,14 @@ class Migration(migrations.Migration):
migrations.CreateModel(
name='Student',
fields=[
('sid', models.IntegerField(primary_key=True, serialize=False, verbose_name='学生编号')),
('s_username', models.CharField(max_length=30, verbose_name='用户名称')),
('s_password', models.CharField(max_length=100, verbose_name='密码')),
('s_name', models.CharField(max_length=100, verbose_name='姓名')),
('s_sex', models.CharField(max_length=4, verbose_name='性别')),
('s_grade', models.IntegerField(verbose_name='年级')),
('s_class_name', models.CharField(max_length=50, verbose_name='班级')),
('s_major', models.CharField(max_length=50, verbose_name='专业名称')),
('sid', models.AutoField(primary_key=True, serialize=False, verbose_name='学生编号')),
('username', models.CharField(blank=True, max_length=30, verbose_name='用户名称')),
('password', models.CharField(blank=True, max_length=100, verbose_name='密码')),
('name', models.CharField(blank=True, max_length=100, verbose_name='姓名')),
('sex', models.CharField(blank=True, max_length=4, verbose_name='性别')),
('grade', models.CharField(blank=True, max_length=20, verbose_name='年级')),
('class_name', models.CharField(blank=True, max_length=50, verbose_name='班级')),
('major', models.CharField(blank=True, max_length=50, verbose_name='专业名称')),
],
options={
'verbose_name': '学生',

@ -4,17 +4,17 @@ from django.db import models
# Create your models here.
class Student(models.Model):
sid = models.AutoField(primary_key=True, verbose_name="学生编号", name="sid")
s_username = models.CharField(max_length=30, verbose_name="用户名称", name="s_username", blank=True)
s_password = models.CharField(max_length=100, verbose_name="密码", name="s_password", blank=True)
s_name = models.CharField(max_length=100, verbose_name="姓名", name="s_name", blank=True)
s_sex = models.CharField(max_length=4, verbose_name="性别", name="s_sex", blank=True)
s_grade = models.CharField(max_length=20, verbose_name="年级", name="s_grade", blank=True)
s_class_name = models.CharField(max_length=50, verbose_name="班级", name="s_class_name", blank=True)
s_major = models.CharField(max_length=50, verbose_name="专业名称", name="s_major", blank=True)
username = models.CharField(max_length=30, verbose_name="用户名称", name="username", blank=True)
password = models.CharField(max_length=100, verbose_name="密码", name="password", blank=True)
name = models.CharField(max_length=100, verbose_name="姓名", name="name", blank=True)
sex = models.CharField(max_length=4, verbose_name="性别", name="sex", blank=True)
grade = models.CharField(max_length=20, verbose_name="年级", name="grade", blank=True)
class_name = models.CharField(max_length=50, verbose_name="班级", name="class_name", blank=True)
major = models.CharField(max_length=50, verbose_name="专业名称", name="major", blank=True)
def to_dict(self):
return {"sid": self.sid, "s_name": self.s_name, "s_username": self.s_username, "s_password": self.s_password,
"s_sex": self.s_sex, "s_grade": self.s_grade, "s_class_name": self.s_class_name, "s_major": self.s_major}
return {"sid": self.sid, "name": self.name, "username": self.username, "password": self.password,
"sex": self.sex, "grade": self.grade, "class_name": self.class_name, "major": self.major}
class Meta:
db_table = "student"

@ -4,4 +4,5 @@ from Student.views import *
urlpatterns = [
path("", studnets),
path("selectCourse", get_select_course_by_id)
]

@ -19,6 +19,14 @@ def studnets(request):
student.s_grade = request.POST.get("s_grade") or ''
student.s_class_name = request.POST.get("s_class_name") or ''
student.s_major = request.POST.get("s_major") or ''
student.username = request.POST.get("username")
student.password = request.POST.get("password")
student.name = request.POST.get("name")
student.sex = request.POST.get("sex")
student.grade = request.POST.get("grade")
student.class_name = request.POST.get("class_name")
student.major = request.POST.get("major")
student.sid = request.POST.get("sid")
student.save()
response = {"code": 200, "message": "添加成功!", "data": student.to_dict()}
return JsonResponse(response)
@ -90,3 +98,12 @@ def studnets(request):
s_major=s_major)
data = Student.objects.filter(sid=sid)[0].to_dict()
return JsonResponse({'code': 200, 'msg': 'success', 'data': data}, safe=False)
student_list = Student.objects.all()
response_json = {"code": 200, "message": "success", "data": []}
for student in student_list:
response_json["data"].append(student.to_dict())
return JsonResponse(response_json)
def get_select_course_by_id(request):
pass

@ -1,4 +1,4 @@
# Generated by Django 2.1 on 2023-09-06 14:01
# Generated by Django 2.1 on 2023-09-07 13:49
from django.db import migrations, models
import django.db.models.deletion
@ -9,8 +9,8 @@ class Migration(migrations.Migration):
initial = True
dependencies = [
('Student', '0001_initial'),
('teacher', '0001_initial'),
('Student', '0001_initial'),
]
operations = [
@ -18,8 +18,8 @@ class Migration(migrations.Migration):
name='Course',
fields=[
('cid', models.AutoField(primary_key=True, serialize=False, verbose_name='课程编号')),
('c_name', models.CharField(max_length=255, verbose_name='课程名称')),
('c_type', models.CharField(max_length=128, verbose_name='课程类型')),
('name', models.CharField(blank=True, max_length=255, verbose_name='课程名称')),
('type', models.CharField(blank=True, max_length=128, verbose_name='课程类型')),
('credit', models.IntegerField(verbose_name='课程学分')),
('tid', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='teacher.Teacher')),
],

@ -7,8 +7,8 @@ from Student.models import Student
# Create your models here.
class Course(models.Model):
cid = models.AutoField(primary_key=True, verbose_name="课程编号", name="cid")
c_name = models.CharField(max_length=255, verbose_name="课程名称", name="c_name", blank=True)
type = models.CharField(max_length=128, verbose_name="课程类型", name="c_type", blank=True)
name = models.CharField(max_length=255, verbose_name="课程名称", name="name", blank=True)
type = models.CharField(max_length=128, verbose_name="课程类型", name="type", blank=True)
credit = models.IntegerField(verbose_name="课程学分", name="credit")
tid = models.ForeignKey(Teacher, to_field="tid", on_delete=models.CASCADE, name="tid")

@ -1,4 +1,4 @@
# Generated by Django 2.1 on 2023-09-06 14:01
# Generated by Django 2.1 on 2023-09-07 13:49
from django.db import migrations, models
@ -15,11 +15,11 @@ class Migration(migrations.Migration):
name='Teacher',
fields=[
('tid', models.AutoField(primary_key=True, serialize=False, verbose_name='教师编号')),
('t_name', models.CharField(max_length=255, verbose_name='教师名称')),
('t_sex', models.CharField(max_length=128, verbose_name='教师性别')),
('t_title', models.CharField(max_length=128, verbose_name='教师职称')),
('t_education', models.CharField(max_length=128, verbose_name='教师学历')),
('t_dept', models.CharField(max_length=128, verbose_name='教师所属院系')),
('name', models.CharField(blank=True, max_length=255, verbose_name='教师名称')),
('sex', models.CharField(blank=True, max_length=128, verbose_name='教师性别')),
('title', models.CharField(blank=True, max_length=128, verbose_name='教师职称')),
('education', models.CharField(blank=True, max_length=128, verbose_name='教师学历')),
('dept', models.CharField(blank=True, max_length=128, verbose_name='教师所属院系')),
],
options={
'verbose_name': '教师',

@ -4,11 +4,15 @@ from django.db import models
# Create your models here.
class Teacher(models.Model):
tid = models.AutoField(primary_key=True, verbose_name="教师编号", name="tid")
t_name = models.CharField(max_length=255, verbose_name="教师名称", name="t_name", blank=True)
sex = models.CharField(max_length=128, verbose_name="教师性别", name="t_sex", blank=True)
title = models.CharField(max_length=128, verbose_name="教师职称", name="t_title", blank=True)
education = models.CharField(max_length=128, verbose_name="教师学历", name="t_education", blank=True)
dept = models.CharField(max_length=128, verbose_name="教师所属院系", name="t_dept", blank=True)
name = models.CharField(max_length=255, verbose_name="教师名称", name="name", blank=True)
sex = models.CharField(max_length=128, verbose_name="教师性别", name="sex", blank=True)
title = models.CharField(max_length=128, verbose_name="教师职称", name="title", blank=True)
education = models.CharField(max_length=128, verbose_name="教师学历", name="education", blank=True)
dept = models.CharField(max_length=128, verbose_name="教师所属院系", name="dept", blank=True)
def to_dict(self):
return {"tid": self.tid, "name": self.name, "sex": self.sex,
"title": self.title, "education": self.education, "dept": self.dept}
class Meta:
db_table = "teacher"

Loading…
Cancel
Save