def get(self, request, course_id): course = Course.objects.get(id=course_id) course.students += 1 user_courses = UserCourse.objects.filter(course=course) user_ids = [user_course.user.id for user_course in user_courses] all_user_courses = UserCourse.objects.filter(user_id__in=user_ids) course_ids = [user_course.course.id for user_course in all_user_courses] related_courses = Course.objects.filter(id__in=course_ids).order_by("-click_nums") all_lessons = course.get_course_lessons() all_resources = CourseResource.objects.filter(course=course) if not UserCourse.objects.filter(course=course, user=request.user): user_course = UserCourse() user_course.user = request.user user_course.course = course user_course.save() return render(request, "course-video.html", { "course": course, "all_lessons": all_lessons, "all_resources": all_resources, "related_courses": related_courses, })
def post(self, request): # 获取课程的 id course_id = request.POST.get('course_id', '') # 判断用户登录状态 if not request.user.is_authenticated: return JsonResponse({'status': 'fail', 'msg': u'用户未登录'}) # 判断用户是否已经处于学习状态 user_course = UserCourse.objects.filter(user_id=request.user.id, course_id=course_id) if user_course: return JsonResponse({'status': 'success', 'msg': u'已添加学习'}) else: user_course = UserCourse() # 实例化一个课程对象 course = Course.objects.get(id=course_id) user_course.course = course user_course.user = request.user user_course.save() # 点击学习,学习人数加 1 course.student_number += 1 course.save() # 课程机构的学习人加 1 org = course.course_teacher.organization org.students += 1 org.save() # 用户添加学习时,发送消息给用户 user_message = UserMessage() user_message.user = request.user.id user_message.message = '欢迎你学习<{}>这门课程\n{}'.format( course.course_name, course.description) user_message.save() return JsonResponse({'status': 'success', 'msg': u'已添加学习'})