def pub_course(request): if request.method == "GET": courses = Course.objects.all().order_by('-course_ctime') pub_form = PubForm() obj = MyPagination(courses.count(), request.GET.get('p'), 15, url='/e/pubCourse.html') courses = courses[obj.start():obj.end()] return render(request, "e_pubCourse.html", {"pub_form": pub_form, "courses": courses, "obj": obj}) if request.method == "POST": ret = {"status": True, "msg": None} pub_form = PubForm(request.POST) if pub_form.is_valid(): try: Course.objects.create( course_no=pub_form.cleaned_data.get("course_no"), course_name=pub_form.cleaned_data.get("course_name"), course_credit=pub_form.cleaned_data.get("course_credit"), course_desc=pub_form.cleaned_data.get("course_desc"), course_college=pub_form.cleaned_data.get("course_college"), course_starter=request.user, ) ret["msg"] = "发布成功" except Exception as e: ret["status"] = False if str(e) == "UNIQUE constraint failed: course_course.course_no": ret["msg"] = "课程号已经被使用,请重写填写" else: ret["msg"] = "数据库写入异常,请联系管理员,错误代码:" + str(e) return HttpResponse(json.dumps(ret)) else: ret["status"] = False ret["msg"] = pub_form.errors return HttpResponse(json.dumps(ret))
def s_search_course(request): if request.method == "POST": fm = CourseSearchForm(request.POST) if fm.is_valid(): content = fm.cleaned_data.get("content") if content.isdigit(): course_ = Course.objects.filter(Q(course_online=True, course_college=request.user.college, \ course_type=1, course_no__contains=content) | Q(course_online=True, course_type=2, course_no__contains=content)).exclude( studentcourse__student=request.user) \ .order_by('-course_online_time') else: course_ = Course.objects.filter(Q(course_online=True, course_college=request.user.college, \ course_type=1, course_name__contains=content) | Q(course_online=True, course_type=2, course_name__contains=content)).exclude( studentcourse__student=request.user) \ .order_by('-course_online_time') obj = MyPagination(course_.count(), request.GET.get("p"), 10, url='select.html') course_pool = course_[obj.start():obj.end()] return render(request, 's_select.html', {"obj": obj, "fm": fm, "course_pool": course_pool}) else: return HttpResponse("输入不符合要求,请重新输入")
def e_index(request): app_list = Course.objects.filter(course_status=1).order_by('course_applied_time') app_counts = app_list.count() news = News.objects.all() obj = MyPagination(news.count(), request.GET.get("p"), 5, url='/e/index.html') news = news[obj.start():obj.end()] return render(request, 'e_index.html', locals())
def s_selected(request): selected_courses = Course.objects.filter(studentcourse__student=request.user, studentcourse__is_choosed=True ) obj = MyPagination(selected_courses.count(), request.GET.get("p"), 10, url='select.html') selected_courses = selected_courses[obj.start():obj.end()] return render(request, "s_selected.html", {"obj": obj, "selected_courses": selected_courses})
def s_index(request): today_ = datetime.datetime.now().weekday() + 1 today_courses = Course.objects.filter(studentcourse__student=request.user, studentcourse__is_choosed=True, course_week=today_).order_by("course_time") news = News.objects.filter(Q(watcher=1) | Q(watcher=3)).order_by("-mtime") obj = MyPagination(news.count(), request.GET.get("p"), 5, url='select.html') news = news[obj.start():obj.end()] return render(request, "s_index.html", {"obj": obj, "today_course": today_courses, "news": news})
def t_index(request): today_ = datetime.datetime.now().weekday() + 1 today_courses = Course.objects.filter(course_choosed_student__gte=1, course_teacher=request.user, course_week=today_) news = News.objects.filter(Q(watcher=1) | Q(watcher=2)).order_by("-mtime") obj = MyPagination(news.count(), request.GET.get('p'), 5, url='index.html') news = news[obj.start():obj.end()] return render(request, "t_index.html", {"today_course": today_courses, "news": news, "obj": obj})
def t_apply(request): if request.method == "GET": obj = AppForm() courses = Course.objects.filter(course_status=0).order_by('-course_ctime') obj2 = MyPagination(courses.count(), request.GET.get('p'), 10, url='apply.html') courses = courses[obj2.start():obj2.end()] return render(request, "t_apply.html", {"courses": courses, "obj": obj, "obj2": obj2}) if request.method == "POST": ret = {"status": True, "msg": None} obj = AppForm(request.POST) if obj.is_valid(): try: Course.objects.filter(course_no=request.POST.get("cno")).update( course_teacher=request.user, course_applied_time=datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S.%f"), course_week=obj.cleaned_data.get("course_week"), course_time=obj.cleaned_data.get("course_time"), course_classroom=obj.cleaned_data.get("course_classroom"), course_total_people=obj.cleaned_data.get("course_total_people"), course_type=obj.cleaned_data.get("course_type"), course_status=1 ) ret["msg"] = "申请成功" except Exception as e: print(str(e)) if str( e) == "UNIQUE constraint failed: course_course.course_teacher_id, course_course.course_week, course_course.course_time": ret["status"] = False ret["msg"] = "该时段,你已经有其他课程了!" return HttpResponse(json.dumps(ret, ensure_ascii=False)) return HttpResponse(json.dumps(ret, ensure_ascii=False)) else: ret["status"] = False ret["msg"] = obj.errors return HttpResponse(json.dumps(ret, ensure_ascii=False))
def m_news(request): if request.method == "GET": fm = NewsSearchForm() news = News.objects.all().order_by("-ctime") obj = MyPagination(news.count(), request.GET.get("p"), 10, url='m_news') news = news[obj.start():obj.end()] return render(request, "m_news.html", locals()) if request.method == "POST": fm = NewsSearchForm(request.POST) if fm.is_valid(): content = fm.cleaned_data.get("content") news = News.objects.filter(title__contains=content).order_by("-ctime") obj = MyPagination(news.count(), request.GET.get("p"), 10, url='m_news') news = news[obj.start():obj.end()] return render(request, 'm_news.html', {"news": news, "fm": fm}) else: return HttpResponse("输入不符合要求,请重新输入")
def m_student(request): if request.method == "GET": fm = PeopleSearchForm() s = User.objects.filter(role=3) obj = MyPagination(s.count(), request.GET.get("p"), 10, url='m_student') students = s[obj.start():obj.end()] return render(request, "m_student.html", {"students": students, "obj": obj, "fm": fm}) if request.method == "POST": fm = PeopleSearchForm(request.POST) if fm.is_valid(): content = fm.cleaned_data.get("content") if content.isdigit(): s = User.objects.filter(username__contains=content, role=3) else: s = User.objects.filter(name__contains=content, role=3) obj = MyPagination(s.count(), request.GET.get("p"), 10, url='m_student') students = s[obj.start():obj.end()] return render(request, 'm_student.html', {"students": students, "fm": fm, "obj": obj}) else: return HttpResponse("输入不符合要求,请重新输入")
def m_course(request): if request.method == "GET": courses = Course.objects.all() fm = CourseSearchForm() obj = MyPagination(courses.count(), request.GET.get('p'), 10, url="m_course") courses = courses[obj.start():obj.end()] return render(request, "m_course.html", {"courses": courses, "obj": obj, "fm": fm}) if request.method == "POST": fm = CourseSearchForm(request.POST) if fm.is_valid(): content = fm.cleaned_data.get("content") if content.isdigit(): courses = Course.objects.filter(course_no__contains=content).order_by("-course_ctime") else: courses = Course.objects.filter(course_name__contains=content).order_by("-course_ctime") obj = MyPagination(courses.count(), request.GET.get("p"), 10, url='m_course') courses = courses[obj.start():obj.end()] return render(request, 'm_course.html', {"courses": courses, "fm": fm, "obj": obj}) else: return HttpResponse("输入不符合要求,请重新输入")
def e_aprrove(request): app_list = Course.objects.filter(course_status=1) obj = MyPagination(app_list.count(), request.GET.get("p"), 10, url='/e/approve.html') app_list = app_list[obj.start():obj.end()] return render(request, "e_approve.html", {"app_list": app_list, "obj": obj})
def e_news(request): news_list = News.objects.all().order_by("-ctime") obj = MyPagination(news_list.count(), request.GET.get("p"), 10, url='/e/news.html') news_list = news_list[obj.start():obj.end()] return render(request, "e_news.html", {"news_list": news_list, "obj": obj})