updata models

scl-branch
jisyoona 1 year ago
parent a695911d1b
commit b4dd926838

@ -4,5 +4,9 @@ from .views import *
urlpatterns = [
path("", studnets),
path("selectCourse", get_select_course_by_id)
path("selectCourse", get_select_course_by_id),
path("selectGrade/", search_grade),
path("changeInfo/", change_info),
path("chooseCourse/", choose_course),
path("deleteCourse/",delete_course)
]

@ -4,6 +4,7 @@ from django.http import HttpRequest
from django.http import JsonResponse
from EduSystemServer.utils import ResponseUtil
from .models import Student
from teacher.models import Teacher
from course.models import SC, Course
from django.views.decorators.csrf import csrf_exempt
from django.http import QueryDict
@ -99,7 +100,8 @@ def studnets(request):
return JsonResponse(ResponseUtil.ok(data, "修改成功!"))
def search_grade(request):
# 查询成绩
def search_grade(request): # 获取SID返回该同学的所有成绩
if request.method == "GET":
sid = request.GET.get('sid')
cid = SC.objects.filter(sid_id=sid).values()
@ -117,5 +119,125 @@ def search_grade(request):
return JsonResponse(ResponseUtil.ok(data_list, "成绩查询成功!"))
# 修改信息
@csrf_exempt
def change_info(request):
if request.method == "GET":
sid = request.GET.get('sid')
info = Student.objects.filter(sid=sid).values()
data = {
'学生编号': sid,
'用户账号': info[0]['username'],
'用户密码': info[0]['password'],
'姓名': info[0]['name'],
'性别': info[0]['sex'],
'年级': info[0]['grade'],
'班级': info[0]['class_name'],
'专业': info[0]['major'],
}
return JsonResponse(ResponseUtil.ok(data, "个人信息查询成功!"))
elif request.method == "POST":
sid = request.GET.get('sid')
stu_info = Student.objects.get(sid=sid)
username = request.POST.get('username') or stu_info.username
password = request.POST.get('password') or stu_info.password
name = request.POST.get('name') or stu_info.name
sex = request.POST.get('sex') or stu_info.sex
grade = request.POST.get('grade') or stu_info.grade
class_name = request.POST.get('class_name') or stu_info.class_name
major = request.POST.get('major') or stu_info.major
Student.objects.filter(sid=sid).update(username=username, password=password, name=name, sex=sex, grade=grade,
class_name=class_name, major=major)
data = {
'学生编号': sid,
'用户账号': username,
'用户密码': password,
'姓名': name,
'性别': sex,
'年级': grade,
'班级': class_name,
'专业': major,
}
return JsonResponse(ResponseUtil.ok(data, "学生信息修改成功!"))
# 选课
@csrf_exempt
def choose_course(request):
if request.method == "GET": # 将所有课程信息返回
course_list = []
course = Course.objects.all().values()
for i in range(len(course)):
teacher = Teacher.objects.filter(tid=course[i]['tid_id']).values('name')
data = {
'课程编号': course[i]['cid'],
'课程名称': course[i]['name'],
'课程类型': course[i]['type'],
'课程学分': course[i]['credit'],
'教师': teacher[0]['name'],
}
course_list.append(data)
return JsonResponse(ResponseUtil.ok(course_list, "课程查询成功!"))
elif request.method == "POST": # 将选择的课程添加到选课表
sid = request.GET.get('sid')
cid = request.POST.get('cid_id')
try:
SC.objects.create(sid_id=sid, cid_id=cid)
course = Course.objects.filter(cid=cid).values()
teacher = Teacher.objects.filter(tid=course[0]['tid_id']).values('name')
data = {
'课程编号': cid,
'课程名称': course[0]['name'],
'课程类型': course[0]['type'],
'课程学分': course[0]['credit'],
'教师': teacher[0]['name'],
}
return JsonResponse(ResponseUtil.ok(data, "选课成功!"))
except:
return JsonResponse(ResponseUtil.error("选课失败!"))
# 退课
@csrf_exempt
def delete_course(request):
if request.method == "GET": # 返回用户所有选择的课程
sid = request.GET.get('sid')
course_list = []
try:
cid = SC.objects.filter(sid_id=sid).values('cid_id') # 通过选课表找到学生选的所有课程
for i in range(len(cid)):
course = Course.objects.filter(cid=cid[i]['cid_id']).values() # 在课程表中找课程详细信息
teacher = Teacher.objects.filter(tid=course[0]['tid_id']).values('name')
data = {
'课程编号': course[0]['cid'],
'课程名称': course[0]['name'],
'课程类型': course[0]['type'],
'课程学分': course[0]['credit'],
'教师': teacher[0]['name'],
}
course_list.append(data)
return JsonResponse(ResponseUtil.ok(course_list, "课程查询成功!"))
except:
return JsonResponse(ResponseUtil.error('你还没有选课!'))
elif request.method == "POST": # 将用户选择的课程删除
sid = request.GET.get('sid')
cid = request.POST.get('cid')
try:
course = Course.objects.filter(cid=cid).values() # 在课程表中找课程详细信息
teacher = Teacher.objects.filter(tid=course[0]['tid_id']).values('name')
data = {
'课程编号': course[0]['cid'],
'课程名称': course[0]['name'],
'课程类型': course[0]['type'],
'课程学分': course[0]['credit'],
'教师': teacher[0]['name'],
}
SC.objects.filter(sid_id=sid, cid_id=cid).delete()
return JsonResponse(ResponseUtil.ok(data, "退课成功!"))
except:
JsonResponse(ResponseUtil.error('退课失败!'))
def get_select_course_by_id(request):
pass

@ -25,8 +25,8 @@ class Course(models.Model):
class SC(models.Model):
sid = models.ForeignKey(Student, to_field="sid", on_delete=models.CASCADE, name="sid")
cid = models.ForeignKey(Course, to_field="cid", on_delete=models.CASCADE, name="cid")
middle_grade = models.IntegerField(name="middle_grade")
end_grade = models.IntegerField(name="end_grade")
middle_grade = models.IntegerField(name="middle_grade", blank=True, null=True)
end_grade = models.IntegerField(name="end_grade", blank=True, null=True)
def to_dict(self):
return {"sid": self.sid, "cid": self.cid, "middle_grade": self.middle_grade, "end_grade": self.end_grade}

Loading…
Cancel
Save