|
|
from django.http import HttpResponse
|
|
|
from django.shortcuts import redirect, render, reverse
|
|
|
from django.db.models import Q
|
|
|
from django.utils import timezone
|
|
|
|
|
|
from user.models import Student, Teacher
|
|
|
from course.models import Course, StudentCourse, Schedule, Student
|
|
|
from constants import INVALID_KIND, INVALID_REQUEST_METHOD, INVALID_VIEW_KIND, INVALID_OPERATE_KIND
|
|
|
from course.forms import CourseForm, ScheduleForm
|
|
|
|
|
|
|
|
|
def get_user(request, kind):
|
|
|
if request.session.get("kind", "") != kind or kind not in ["student", "teacher"]:
|
|
|
return None
|
|
|
|
|
|
if len(request.session.get("user", "")) != 10:
|
|
|
return None
|
|
|
|
|
|
uid = request.session.get('user')
|
|
|
if kind == "student":
|
|
|
# 找到对应学生
|
|
|
grade = uid[:4]
|
|
|
number = uid[4:]
|
|
|
student_set = Student.objects.filter(grade=grade, number=number)
|
|
|
if student_set.count() == 0:
|
|
|
return None
|
|
|
return student_set[0]
|
|
|
else:
|
|
|
# 找到对应老师
|
|
|
department_no = uid[:3]
|
|
|
number = uid[3:]
|
|
|
teacher_set = Teacher.objects.filter(department_no=department_no, number=number)
|
|
|
if teacher_set.count() == 0:
|
|
|
return None
|
|
|
return teacher_set[0]
|
|
|
|
|
|
|
|
|
# 创建视图
|
|
|
def home(request, kind):
|
|
|
if kind == "teacher":
|
|
|
return teacher_home(request)
|
|
|
elif kind == "student":
|
|
|
return student_home(request)
|
|
|
return HttpResponse(INVALID_KIND)
|
|
|
|
|
|
|
|
|
def teacher_home(request):
|
|
|
kind = "teacher"
|
|
|
user = get_user(request, kind)
|
|
|
|
|
|
if not user:
|
|
|
return redirect('login', kind=kind)
|
|
|
|
|
|
info = {
|
|
|
"name": user.name,
|
|
|
"kind": kind
|
|
|
}
|
|
|
# 这里面实现了一个搜索框,能够根据关键词去搜索课程。
|
|
|
# 为了不使用js,搜索框的信息是通过post表单信息来提交的。
|
|
|
is_search = False
|
|
|
search_key = ""
|
|
|
if request.method == "POST":
|
|
|
search_key = request.POST.get("search")
|
|
|
if search_key:
|
|
|
is_search = True
|
|
|
|
|
|
context = {
|
|
|
"info": info
|
|
|
}
|
|
|
|
|
|
q = Q(teacher=user)
|
|
|
|
|
|
if is_search:
|
|
|
q = q & Q(name__icontains=search_key)
|
|
|
context["search_key"] = search_key
|
|
|
|
|
|
context["course_list"] = Course.objects.filter(q).order_by('status')
|
|
|
|
|
|
return render(request, 'course/teacher/home.html', context)
|
|
|
|
|
|
|
|
|
def student_home(request):
|
|
|
return redirect("view_course", view_kind="current")
|
|
|
|
|
|
|
|
|
def create_course(request):
|
|
|
kind = "teacher"
|
|
|
user = get_user(request, kind)
|
|
|
if not user:
|
|
|
return redirect("login", kind=kind)
|
|
|
|
|
|
info = {
|
|
|
"name": user.name,
|
|
|
"kind": "teacher"
|
|
|
}
|
|
|
|
|
|
if request.method == "POST":
|
|
|
form = CourseForm(request.POST)
|
|
|
if form.is_valid():
|
|
|
obj = form.save(commit=False)
|
|
|
obj.status = 1
|
|
|
obj.teacher = user
|
|
|
|
|
|
obj.save()
|
|
|
return redirect("course", kind=kind)
|
|
|
elif request.method == "GET":
|
|
|
form = CourseForm()
|
|
|
else:
|
|
|
return HttpResponse(INVALID_REQUEST_METHOD)
|
|
|
|
|
|
return render(request, 'course/teacher/create_course.html', {"info": info, "form": form, })
|
|
|
|
|
|
|
|
|
def create_schedule(request, course_id):
|
|
|
kind = "teacher"
|
|
|
user = get_user(request, kind)
|
|
|
if not user:
|
|
|
return redirect("login", kind=kind)
|
|
|
|
|
|
info = {
|
|
|
"name": user.name,
|
|
|
"kind": kind
|
|
|
}
|
|
|
|
|
|
course = Course.objects.get(pk=course_id)
|
|
|
|
|
|
if request.method == 'POST':
|
|
|
form = ScheduleForm(request.POST)
|
|
|
if form.is_valid():
|
|
|
obj = form.save(commit=False)
|
|
|
obj.course = course
|
|
|
obj.save()
|
|
|
|
|
|
return redirect("view_detail", course_id=course_id)
|
|
|
elif request.method == "GET":
|
|
|
form = ScheduleForm()
|
|
|
else:
|
|
|
return HttpResponse(INVALID_REQUEST_METHOD)
|
|
|
context = {"info": info,
|
|
|
"form": form,
|
|
|
"course": course}
|
|
|
|
|
|
return render(request, 'course/teacher/create_schedule.html', context)
|
|
|
|
|
|
|
|
|
def delete_schedule(request, schedule_id):
|
|
|
kind = "teacher"
|
|
|
user = get_user(request, kind)
|
|
|
if not user:
|
|
|
return redirect("login", kind=kind)
|
|
|
|
|
|
schedule = Schedule.objects.get(pk=schedule_id)
|
|
|
|
|
|
course_id = request.GET.get("course_id") or schedule.course_id
|
|
|
|
|
|
schedule.delete()
|
|
|
|
|
|
return redirect("view_detail", course_id=course_id)
|
|
|
|
|
|
# 1: "开始选课",
|
|
|
# 2: "结束选课",
|
|
|
# 3: "结课",
|
|
|
# 4: "给分完成"
|
|
|
def handle_course(request, course_id, handle_kind):
|
|
|
kind = "teacher"
|
|
|
user = get_user(request, kind)
|
|
|
if not user:
|
|
|
return redirect("login", kind=kind)
|
|
|
|
|
|
info = {
|
|
|
"name": user.name,
|
|
|
"kind": kind,
|
|
|
}
|
|
|
|
|
|
course = Course.objects.get(pk=course_id)
|
|
|
if course.status == handle_kind and course.status < 5:
|
|
|
if course.status == 4:
|
|
|
scs = StudentCourse.objects.filter(course=course)
|
|
|
all_given = True
|
|
|
res = ""
|
|
|
for sc in scs:
|
|
|
if sc.scores is None:
|
|
|
all_given = False
|
|
|
res += "<div>%s 未打分</div>" % sc.student
|
|
|
|
|
|
if all_given:
|
|
|
course.status += 1
|
|
|
course.save()
|
|
|
return redirect("view_detail", course_id=course_id)
|
|
|
else:
|
|
|
return HttpResponse(res)
|
|
|
|
|
|
else:
|
|
|
course.status += 1
|
|
|
course.save()
|
|
|
|
|
|
course_list = Course.objects.filter(teacher=user)
|
|
|
# return render(request, 'course/teacher/home.html', {'info': info, 'course_list': course_list})
|
|
|
return redirect("course", kind=kind)
|
|
|
|
|
|
|
|
|
def view_detail(request, course_id):
|
|
|
kind = "teacher"
|
|
|
user = get_user(request, kind)
|
|
|
if not user:
|
|
|
return redirect("login", kind=kind)
|
|
|
|
|
|
info = {
|
|
|
"name": user.name,
|
|
|
"kind": kind
|
|
|
}
|
|
|
|
|
|
course = Course.objects.get(pk=course_id)
|
|
|
c_stu_list = StudentCourse.objects.filter(Q(course=course) & Q(with_draw=False))
|
|
|
sche_list = Schedule.objects.filter(course=course)
|
|
|
|
|
|
context = {
|
|
|
"info": info,
|
|
|
"course": course,
|
|
|
"course_students": c_stu_list,
|
|
|
"schedules": sche_list
|
|
|
}
|
|
|
if course.status == 5:
|
|
|
sorted_cs_list = sorted(c_stu_list, key=lambda cs: cs.scores)
|
|
|
context["sorted_course_student"] = sorted_cs_list
|
|
|
|
|
|
return render(request, "course/teacher/course.html", context)
|
|
|
|
|
|
|
|
|
# students
|
|
|
# current: 查看当前课程
|
|
|
# is_end: 查看结课课程
|
|
|
# select: 可选课的
|
|
|
# withdraw: 可撤课的
|
|
|
def view_course(request, view_kind):
|
|
|
kind = "student"
|
|
|
user = get_user(request, kind)
|
|
|
|
|
|
if not user:
|
|
|
return redirect('login', kind=kind)
|
|
|
|
|
|
is_search = False
|
|
|
search_key = ""
|
|
|
if request.method == "POST":
|
|
|
search_key = request.POST.get("search")
|
|
|
if search_key:
|
|
|
is_search = True
|
|
|
|
|
|
info = {
|
|
|
"name": user.name,
|
|
|
"kind": kind
|
|
|
}
|
|
|
|
|
|
course_list = []
|
|
|
|
|
|
if view_kind in ["select", "current", "withdraw", "is_end"]:
|
|
|
if view_kind == "select":
|
|
|
q = Q(status=2)
|
|
|
if is_search:
|
|
|
q = q & (Q(name__icontains=search_key) | Q(teacher__name__icontains=search_key))
|
|
|
|
|
|
course_list = Course.objects.filter(q)
|
|
|
|
|
|
my_course = StudentCourse.objects.filter(Q(student=user) & Q(with_draw=False))
|
|
|
my_cids = [c.course.id for c in my_course]
|
|
|
course_list = [c for c in course_list if c.id not in my_cids]
|
|
|
else:
|
|
|
q = Q(student=user) & Q(with_draw=False)
|
|
|
if is_search:
|
|
|
q = q & (Q(course__name__icontains=search_key) | Q(course__teacher__name__icontains=search_key))
|
|
|
my_course = StudentCourse.objects.filter(q)
|
|
|
if view_kind == "current":
|
|
|
course_list = [c.course for c in my_course if c.course.status < 4]
|
|
|
if view_kind == "withdraw":
|
|
|
course_list = [c.course for c in my_course if c.course.status == 2]
|
|
|
if view_kind == "is_end":
|
|
|
course_list = [c for c in my_course if c.course.status >= 4]
|
|
|
else:
|
|
|
return HttpResponse(INVALID_VIEW_KIND)
|
|
|
|
|
|
context = {
|
|
|
"info": info,
|
|
|
"view_kind": view_kind,
|
|
|
"course_list": course_list
|
|
|
}
|
|
|
|
|
|
if is_search:
|
|
|
context["search_key"] = search_key
|
|
|
|
|
|
return render(request, "course/student/home.html", context)
|
|
|
|
|
|
# select: 选课
|
|
|
# withdraw: 撤课
|
|
|
def operate_course(request, operate_kind, course_id):
|
|
|
kind = "student"
|
|
|
user = get_user(request, kind)
|
|
|
|
|
|
if not user:
|
|
|
return redirect('login', kind=kind)
|
|
|
|
|
|
if operate_kind not in ["select", "withdraw"]:
|
|
|
return HttpResponse(INVALID_OPERATE_KIND)
|
|
|
elif operate_kind == "select":
|
|
|
course = Course.objects.get(pk=course_id)
|
|
|
new_course = StudentCourse(student=user, course=course)
|
|
|
new_course.save()
|
|
|
elif operate_kind == "withdraw":
|
|
|
q = Q(course_id=course_id) & Q(student=user) & Q(with_draw=False)
|
|
|
course = StudentCourse.objects.filter(q).order_by("-with_draw_time")[0]
|
|
|
course.with_draw = True
|
|
|
course.with_draw_time = timezone.now()
|
|
|
course.save()
|
|
|
|
|
|
return redirect("view_course", view_kind=operate_kind)
|