Пример #1
0
    def get(self, request, video_id):
        video = VideoInfo.objects.get(id=int(video_id))
        course = video.lessoninfo.courseinfo
        course.study_num += 1
        course.save()

        # 查询用户是否关联了课程
        user_courses = UserCourse.objects.filter(user=request.user)
        if not user_courses:
            user_course = UserCourse(user=request.user, course=course)
            user_course.save()

        # 学习该课程的同学还学过的课程
        user_courses = UserCourse.objects.filter(study_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.study_course.id for user_course in all_user_courses]
        relate_courses = CourseInfo.objects.filter(id__in=course_ids).order_by("-click_num")[:3]

        course_resources = SourceInfo.objects.filter(courseinfo=course)
        return render(request, 'course-play.html', {
            'course': course,
            'course_resources': course_resources,
            'relate_courses': relate_courses,
            'video': video,
        })
Пример #2
0
 def get(self, request, video_id):
     video = Video.objects.get(id=int(video_id))
     course = video.lesson.course
     course.students += 1
     course.save()
     user_courses = UserCourse.objects.filter(user=request.user,
                                              course=course)
     if not user_courses:
         user_course = UserCourse(user=request.user, course=course)
         user_course.save()
     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 = [
         all_user_course.course_id for all_user_course in all_user_courses
     ]
     relate_courses = Course.objects.filter(
         id__in=course_ids).order_by("-click_nums")[:5]
     all_resources = CourseResource.objects.filter(course=course)
     return render(
         request, 'course-play.html', {
             'course': course,
             'all_resources': all_resources,
             'relate_courses': relate_courses,
             'video': video,
         })
Пример #3
0
def course_comment(request, course_id):
    if course_id:
        course = CourseInfo.objects.filter(course_id=int(course_id))[0]

        course = CourseInfo.objects.filter(id=int(course_id))[0]
        # 当用户点击学习时,代表该用户学习了该课程,如果没有学习则加上学习操作
        usercourse_list = UserCourse.objects.filter(study_course=course,
                                                    study_man=request.user)
        if not usercourse_list:
            a = UserCourse()
            a.study_man = request.user
            a.study_course = course
            a.save()

        # 学过该课程的学生还学过什么
        usercourse_list = UserCourse.objects.filter(study_course=course)
        # 列表生成式
        user_list = [usercourse.study_man for usercourse in usercourse_list]
        # __in:在哪个范围内
        # exclude去除...
        usercourse_list = UserCourse.objects.filter(
            study_man__in=user_list).exclude(study_course=course)
        # 拿到课程
        # set()用于去重
        course_list = list(
            set([usercourse.study_course for usercourse in usercourse_list]))

        return render(request, 'courses/course-comment.html', {
            'course': course,
            'course_list': course_list
        })
Пример #4
0
    def get(self, request, course_id):
        course = Course.objects.get(id=int(course_id))
        course.students += 1
        course.save()
        # 查询用户是否已经关联了该课程
        user_courses = UserCourse.objects.filter(user=request.user,
                                                 course=course)
        if not user_courses:
            user_course = UserCourse(user=request.user, course=course)
            user_course.save()

        user_cousers = UserCourse.objects.filter(course=course)
        user_ids = [user_couser.user.id for user_couser in user_cousers]
        all_user_courses = UserCourse.objects.filter(user_id__in=user_ids)
        # 取出所有课程id
        course_ids = [
            user_couser.course.id for user_couser in all_user_courses
        ]
        # 获取学过该用户学过其他的所有课程
        relate_courses = Course.objects.filter(
            id__in=course_ids).order_by("-click_nums")[:5]
        all_resources = CourseResource.objects.filter(course=course)

        return render(
            request, "course-video.html", {
                "course": course,
                "course_resources": all_resources,
                "relate_courses": relate_courses
            })
Пример #5
0
    def get(self, request, course_id):
        course = Course.objects.get(id=int(course_id))
        course.students += 1
        course.save()
        #查询用户是否关联改课程

        user_cousers = UserCourse.objects.filter(user=request.user,
                                                 course=course)
        if not user_cousers:
            user_course = UserCourse(user=request.user, course=course)
            user_course.save()
        user_cousers = UserCourse.objects.filter(course=course)
        user_id = [user_cousers.user_id for user_cousers in user_cousers]
        #取出所有课程id
        all_user_coursers = UserCourse.objects.filter(user_id__in=user_id)
        course_id = [
            user_cousers.course_id for user_cousers in all_user_coursers
        ]
        courses = Course.objects.filter(
            id__in=course_id).order_by('-click_nums')[:5]
        print(Course.objects.filter(id__in=course_id), course_id)
        all = CoursesResource.objects.filter(lesson=course)

        return render(request, 'course-video.html', {
            "course": course,
            "all": all,
            "courses": courses
        })
Пример #6
0
 def get(self, request, course_id):
     course = Course.objects.get(pk=course_id)
     # 判断用户是否登录,直接重定向
     if not request.user.is_authenticated:
         return redirect('/login/')
     else:
         user_courses = UserCourse.objects.filter(user=request.user,
                                                  course=course)
         if not user_courses:
             user_course = UserCourse(user=request.user, course=course)
             user_course.save()
     # 实现课程学习人数自增
     course.stutents += 1
     course.save()
     user_courses = UserCourse.objects.filter(course=course)
     # 根据课程获得用户id的列表
     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 user_courses]
     # 已经学习课程 根据点击数排序
     relate_courses = Course.objects.filter(
         id__in=course_ids).order_by('-click_nums')
     all_resourses = CourseResource.objects.filter(course=course)
     return render(
         request, 'course-video.html', {
             'course': course,
             'relate_courses': relate_courses,
             'all_resourses': all_resourses
         })
Пример #7
0
def course_video(request, course_id):
    if course_id:
        course = CourseInfo.objects.filter(id=int(course_id)).first()

        user_course = UserCourse.objects.filter(study_man=request.user,
                                                study_course=course)
        if not user_course:
            a = UserCourse()
            a.study_man = request.user
            a.study_course = course
            a.save()
            course.study_num += 1
            course.save()
            # 用户所学的课程
            user_course_list = UserCourse.objects.filter(
                study_man=request.user).all()
            user_course = [
                user_course.study_course for user_course in user_course_list
            ]
            # 找出这些课程所对应的机构
            org_list = list(set([course.org_info for course in user_course]))
            # 如果新学习的课程机构不在学生所学习过的机构列表中,则该机构的学习人数+1
            if course.org_info not in org_list:
                course.org_info.study_num += 1
                course.org_info.save()

        # 学过该课的同学还学过
        courses_list = course_study_list(course_id)

        return render(request, 'courses/course-video.html', locals())
Пример #8
0
    def get(self, request, course_id):
        course = Course.objects.get(id=int(course_id))
        course.students += 1
        course.save()

        # 查询用户与课程是否关联
        user_courses = UserCourse.objects.filter(user=request.user, course=course)
        if not user_courses:
            user_courses = UserCourse(user=request.user, course=course)
            user_courses.save()

        # 查找学习当前课程的所有用户
        user_courses_table = UserCourse.objects.filter(course=course)

        # 取出所有用户的ID
        user_ids = [user_course.user.id for user_course in user_courses_table]

        # 取出当前课程所用用户的相关学习课程
        all_user_courses = UserCourse.objects.filter(user_id__in=user_ids)
        course_ids = [user_courses.course.id for user_courses in all_user_courses]
        relate_courses = Course.objects.filter(id__in=course_ids).order_by('-click_nums')[:5]
        resources = CourseResource.objects.filter(course=course)
        return render(request, 'course-video.html', {
            'course':course,
            'resources':resources,
            'relate_courses':relate_courses
        })
Пример #9
0
    def get(self, request, course_id):
        '''课程章节视频信息'''
        course = Courses.objects.get(id=int(course_id))
        course.students += 1
        course.save()
        # 查询用户是否关联了该课程
        user_courses = UserCourse.objects.filter(user=request.user,
                                                 course=course)
        if not user_courses:
            user_course = UserCourse(user=request.user, course=course)
            user_course.save()

        # 用户还学过什么课程
        user_cours = UserCourse.objects.filter(course=course)
        user_ids = [user_cour.user.id for user_cour in user_cours]
        all_user_courses = UserCourse.objects.filter(user_id__in=user_ids)
        # 取出所有课程id
        course_ids = [user_coure.course.id for user_coure in all_user_courses]
        relate_courses = Courses.objects.filter(
            id__in=course_ids).order_by('-click_nums')[:5]

        all_resources = CoursesResource.objects.filter(course=course)
        return render(
            request, 'course-video.html', {
                'course': course,
                'all_resources': all_resources,
                'relate_courses': relate_courses,
            })
Пример #10
0
def courses_video(request, course_id):
    if course_id and request.user.is_authenticated:
        course = CourseInfo.objects.filter(id=course_id)[0]
        usercourse_list = UserCourse.objects.filter(study_man=request.user,
                                                    study_course=course_id)
        if not usercourse_list:
            usercourse_list = UserCourse.objects.filter(study_man=request.user)
            courses_list = [
                user_course.study_course for user_course in usercourse_list
            ]
            org_list = [course.orginfo for course in courses_list]
            if course.orginfo not in org_list:
                course.orginfo.study_num += 1
                course.orginfo.save()

            a = UserCourse()
            a.study_man = request.user
            a.study_course = course
            a.save()
            course.study_num += 1
            course.save()

        usercourse_list = UserCourse.objects.filter(study_course=course)
        user_list = [user_course.study_man for user_course in usercourse_list]
        usercourse_list = UserCourse.objects.filter(
            study_man__in=user_list).exclude(study_course=course)
        courses_list = list(
            set([user_course.study_course for user_course in usercourse_list]))

        return render(request, 'courses/course-video.html', {
            'course': course,
            'courses_list': courses_list
        })
    else:
        return render(request, 'users/login.html')
Пример #11
0
    def get(self, request, course_id, *args, **kwargs):
        course = Course.objects.get(id=int(course_id))
        course.click_nums += 1
        course.save()

        user_course = UserCourse.objects.filter(user=request.user,
                                                course=course)
        if not user_course:
            user_course = UserCourse(user=request.user, course=course)
            user_course.save()
            course.students += 1
            course.save()

        course_resource = CourseResource.objects.filter(course=course)
        user_courses = UserCourse.objects.filter(course=course)
        user_ids = [user_course.user.id for user_course in user_courses]
        all_courses = UserCourse.objects.filter(
            user_id__in=user_ids).order_by("-course__click_nums")[:5]
        related_courses = [
            user_course.course for user_course in all_courses
            if user_course.course.id != course.id
        ]
        data = {
            "course": course,
            "course_resource": course_resource,
            "related_courses": related_courses,
        }
        return render(request, "course-video.html", data)
Пример #12
0
def course_video(request, course_id):
    if course_id:

        course = CourseInfo.objects.filter(id=course_id)[0]

        usercourse_list = UserCourse.objects.filter(study_man=request.user,
                                                    study_course=course)

        if not usercourse_list:
            # 添加该课程到用户学习课程中
            a = UserCourse(study_man=request.user, study_course=course)
            a.save()

            # 课程与机构学习人数增加
            course.study_num += 1
            course.save()

            # 第一种方法
            # usercourse_list = UserCourse.objects.filter(study_man=request.user)
            # course_list = [usercourse.study_course for usercourse in usercourse_list if usercourse.study_course.orginfo.id == course.orginfo.id].pop(course)
            # if course_list:
            #     course.orginfo.study_num += 1
            #     course.orginfo.save()

            # 第二种方法
            # 第一步:从学习课程表当中查找当前个人学习所有的课程
            usercourse_list = UserCourse.objects.filter(study_man=request.user)
            course_list = [
                usercourse.study_course for usercourse in usercourse_list
            ]
            org_list = list(set([course.orginfo for course in course_list]))
            if course.orginfo not in org_list:
                course.orginfo.study_num += 1
                course.orginfo.save()

        # 学过该课的同学还学过什么课程
        # 第一步:我们需要从中间表用户课程表当中找到学过该课的所有对象
        usercourse_list = UserCourse.objects.filter(study_course=course)

        # 第二步:根据找到对象,遍历他们拿到所有学过该课的用户
        user_list = [usercourse.study_man for usercourse in usercourse_list]

        # 第三步:再根据找到的用户,从中间用户学习课程表中,找到所有用户学习其他课程的整个对象,需要用到exclude去除当前学过的这个课程对象
        usercourse_list = UserCourse.objects.filter(
            study_man__in=user_list).exclude(study_course=course)

        # 第四步:从获取到的用户课程列表中,拿到我们需要的其他课程
        course_list = list(
            set([usercourse.study_course for usercourse in usercourse_list]))

        return render(request, 'courses/course-video.html', {
            'course_id': course_id,
            'course': course,
            'course_list': course_list
        })
Пример #13
0
    def get(self, request, vodeo_id):
        if vodeo_id:
            video = VideoInfo.objects.filter(id=vodeo_id)[0]
            video_url = video.url
            course = video.lesson.course
            # 记录用户学习课程
            usercourse_list = UserCourse.objects.filter(study_man=request.user,
                                                        study_course=course)
            if not usercourse_list:
                user = UserCourse()
                user.study_man = request.user
                user.study_course = course
                user.save()
                course.study_num += 1
                course.save()

                usercourse_list = UserCourse.objects.filter(
                    study_man=request.user)
                couse_list = [
                    usercourse.study_man for usercourse in usercourse_list
                ]
                org_list = set([course.org for course in couse_list])

                if course.org not in org_list:
                    course.org.love_num += 1
                    course.org.save()

            # 学过该课程的人还学过
            # 1.从中建表中获取到用户课程表对象
            usercourse_list = UserCourse.objects.filter(study_course=course)

            # 2.根据找到的用户学习课程,找到用户列表
            user_list = [
                usercourse.study_man for usercourse in usercourse_list
            ]

            # 3.再根据找到的用户从学习的课程表中找到用户学习其它课程的整个对象
            usercourse_list = UserCourse.objects.filter(
                study_man__in=user_list).exclude(study_course=course)

            # 4.从获取到的用户课程列表中拿到我们需要的其它课程,使用set集合去重
            course_list = set(
                [usercourse.study_course for usercourse in usercourse_list])

            if course:
                return render(
                    request, 'courses/course-play.html', {
                        'course': course,
                        'course_list': course_list,
                        'video_url': video_url,
                    })
Пример #14
0
def course_video(request, course_id):
    if course_id:
        course = CourseInfo.objects.filter(id=int(course_id))[0]

        #当用户点击开始学习以后,代表这个用户学习了这个课程,我们需要去判断用户学习课程的表当中有没有学习这门课程的记录,如果没有,需要给加上这条记录,代表用户学习了这门课程
        usercourse_list = UserCourse.objects.filter(study_man=request.user,
                                                    study_course=course)
        if not usercourse_list:
            a = UserCourse()
            a.study_man = request.user
            a.study_course = course
            a.save()
            course.study_num += 1
            course.save()

            #打个比方:用户小蓝昨天学了Python课程、JS课程、ORACLE课程,其Python课程是尚硅谷机构的。今天小蓝又点了一门Python课程学习,而这次的Python课程是属于北大青鸟的。
            #且 小蓝昨天学的所有课程,没有任何一门课是属于北大青鸟的。则 北大青鸟的学习人数要加1。
            #那如果小蓝昨天学的ORACLE课程刚好是属于北大青鸟的,北大青鸟的学习人数不用加1,避免重复。

            #第一步:从学习课程的表当中查找当前这个人学习的所有的课程
            usercourse_list = UserCourse.objects.filter(study_man=request.user)
            course_list = [
                usercourse.study_course for usercourse in usercourse_list
            ]
            #第二步:根据拿到的所有课程,找到这个用户学过的机构有哪些
            org_list = list(set([course1.orginfo for course1 in course_list]))
            #第三步:判断当前所学的课程所属机构,是不是在这个用户之前所学的机构当中,如果不在,那么机构的学习人数+1
            if course.orginfo not in org_list:
                course.orginfo.study_num += 1
                course.orginfo.save()

        #学过该课的同学还学过什么课程
        #第一步:我们需要从中间表用户课程表当中找到学过该课的所有对象
        usercourse_list = UserCourse.objects.filter(study_course=course)

        #第二步:根据找到的用户学习课程列表,遍历拿到所有学习过这门课程的用户列表
        user_list = [usercourse.study_man for usercourse in usercourse_list]

        #第三步:再根据找到的用户,从中间用户学习课程表当中,找到所有用户学习其它课程的 整个对象,需要用到exclude去除当前学过的这个课程对象
        usercourse_list = UserCourse.objects.filter(
            study_man__in=user_list).exclude(study_course=course)

        #第四步:从获取到的用户课程列表当中,拿到我们需要的其它课程          备注:用集合set()去重
        course_list = list(
            set([usercourse.study_course for usercourse in usercourse_list]))

        return render(request, 'courses/course-video.html', {
            'course': course,
            'course_list': course_list
        })
Пример #15
0
def course_video(request, course_id):
    if course_id:
        course = CourseInfo.objects.filter(id=int(course_id))[0]

        #只要用户点击了开始学习按钮,就要把它添加到UserCourse表里,但要先检查一下是否已经添加过了
        usercourse_list = UserCourse.objects.filter(study_man=request.user,
                                                    study_course=course)
        if not usercourse_list:

            #给课程的学习人数增加1
            course.study_num += 1
            course.save()
            #给课程所属的机构的学习人数加一(前提是这个用户没学过这个机构的任何课程)
            #第一步,查找当前用户学习的所有课程
            usercourse_list = UserCourse.objects.filter(study_man=request.user)
            print(usercourse_list)
            course_list = [
                usercourse.study_course for usercourse in usercourse_list
            ]
            print(course_list)
            #第二步,判断这些课程是否都不是这个机构的
            org_list = list(set([c.orginfo for c in course_list]))
            print(org_list)
            if course.orginfo not in org_list:
                course.orginfo.study_num += 1
                course.orginfo.save()

            #必须最后存入UserCourse表,否则跟前面的机构的逻辑矛盾(新增的课程就是这个机构,那么就不需要再给机构加学习人数了)
            usercourse = UserCourse()
            usercourse.study_man = request.user
            usercourse.study_course = course
            usercourse.save()

        #下面是找出学过这门课的同学还学过什么课
        #第一步,找到UserCourse里所有跟这门课有关的对象
        usercourse_list = UserCourse.objects.filter(study_course=course)
        #第二步,遍历找出所有学过这门课的学生
        user_list = [usercourse.study_man for usercourse in usercourse_list]
        #第三步,找出这些用户还学过什么课程,拿出所有的UserCourse对象,要除去这门课本身(这里用了filter里的in)
        usercourse_list = UserCourse.objects.filter(
            study_man__in=user_list).exclude(study_course=course)
        #第四步,拿出所有的课程对象
        course_list = list(
            set([usercourse.study_course for usercourse in usercourse_list]))

        return render(request, 'courses/course-video.html', {
            'course': course,
            'course_list': course_list
        })
Пример #16
0
def course_video(request,course_id):
    if course_id:
        course = CourseInfo.objects.filter(id = int(course_id))[0]

        #当用户点击开始学习以后,代表这个用户学习了这个课程,我们需要去判断用户学习课程的表当中有没有学习这门课程的记录,如果没有,需要给加上这条记录,代表用户学习了这门课程
        usercourse_list = UserCourse.objects.filter(study_man=request.user,study_course=course)
        if not usercourse_list:
            a = UserCourse()
            a.study_man = request.user
            a.study_course = course
            a.save()
            course.study_num += 1
            course.save()


            #第一步:从学习课程的表当中查找当前这个人学习的所有的课程,出去新学的这门课程
            usercourse_list = UserCourse.objects.filter(study_man=request.user).exclude(study_course=course)
            course_list = [usercourse.study_course for usercourse in usercourse_list]
            #第二步:根据拿到的所有课程,找到这个用户学过课程的机构
            org_list = list(set([course1.orginfo for course1 in course_list]))
            #第三步:判断当前所学的课程机构,是不是在这个用户之前所学的机构当中,如果不在,那么机构的学习人数+1
            if course.orginfo not in org_list:
                course.orginfo.study_num += 1
                course.orginfo.save()





        #学过该课的同学还学过什么课程
        #第一步:我们需要从中间表用户课程表当中找到学过该课的所有对象
        usercourse_list = UserCourse.objects.filter(study_course=course)

        #第二步:根据找到的用户学习课程列表,遍历拿到所有学习过这门课程的用户列表
        user_list = [usercourse.study_man for usercourse in usercourse_list]

        #第三步:再根据找到的用户,从中间用户学习课程表当中,找到所有用户学习其它课程的 整个对象,需要用到exclude去除当前学过的这个课程对象
        usercourse_list = UserCourse.objects.filter(study_man__in=user_list).exclude(study_course=course)

        #第四步:从获取到的用户课程列表当中,拿到我们需要的其它课程
        course_list = list(set([usercourse.study_course for usercourse in usercourse_list]))



        return render(request,'courses/course-video.html',{
            'course':course,
            'course_list':course_list
        })
Пример #17
0
def course_video(request, course_id):
    if course_id:
        course = CourseInfo.objects.filter(id=int(course_id))[0]

        # 当用户点击我要"开始学习",代表会把这条记录添加到UserCourse表中
        userCourse_list = UserCourse.objects.filter(study_course=course,
                                                    study_man=request.user)
        if not userCourse_list:  # 表示用户之前并未学习这个课程,也未添加到UserCourse表
            userCourse = UserCourse()
            userCourse.study_course = course
            userCourse.study_man = request.user
            userCourse.save()

            # 从学习课程表中查找该用户学习的所有课程
            usercourse_list = UserCourse.objects.filter(study_man=request.user)
            course_list = [
                usercourse.study_course for usercourse in usercourse_list
            ]
            # 根据获取到课程列表,找到该课程所属机构
            org_list = list(set([course.orginfo for course in course_list]))
            # 当学习的该课程所属机构 不在 此人学习课程的所机构中, 则机构学习人数动态+1
            if course.orginfo not in org_list:
                course.orginfo.study_num += 1
                course.orginfo.save()

        # 学过该课程的用户还学过哪些课程???(指当前用户还是学习该课程的所有用户)
        # 1.从用户课程表(UserCourse)中查找到所有学习过该课程的 所有对象(用户课程)
        usercourse_list = UserCourse.objects.filter(study_course=course)
        # 2.根据查询得到的所有用户课程对象获取对应的用户信息列表-----列表生成式
        user_list = [usercourse.study_man for usercourse in usercourse_list]
        # 3.根据获取到的用户信息列表查询用户学习的其他课程的 所有对象(用户课程)
        usercourse_list = UserCourse.objects.filter(
            study_man__in=user_list).exclude(study_course=course)
        # 4.从获取到的用户课程列表中获取所有课程列表
        course_list = list(
            set([usercourse.study_course for usercourse in usercourse_list]))

        return render(request, 'courses/course-video.html', {
            'course': course,
            'course_list': course_list,
        })
Пример #18
0
 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,
     })
Пример #19
0
 def get(self, request, course_id):
     course = Course.objects.get(id=int(course_id))
     course.students += 1
     course.save()
     user_courses = UserCourse.objects.filter(user=request.user,
                                              course=course)
     if not user_courses:
         user_course = UserCourse(user=request.user, course=course)
         user_course.save()
     all_resources = CourseResource.objects.filter(course=course)
     user_cousers = UserCourse.objects.filter(course=course)
     user_ids = [user_couser.user.id for user_couser in user_cousers]
     all_user_couses = UserCourse.objects.filter(user_id__in=user_ids)
     course_ids = [user_couser.course.id for user_couser in all_user_couses]
     relate_courses = Course.objects.filter(
         id__in=course_ids).order_by("-click_nums")[:5]
     return render(
         request, "course-video.html", {
             'course': course,
             "course_resources": all_resources,
             'relate_courses': relate_courses,
         })
Пример #20
0
    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'已添加学习'})
Пример #21
0
 def get(self, request, course_id):
     course = Course.objects.get(id=int(course_id))
     course.stu_nums += 1
     course.save()
     all_resource = CourseResource.objects.filter(course=course)
     # 关联用户与课程
     user_course = UserCourse.objects.filter(user=request.user,
                                             course=course)
     if not user_course:
         user_course = UserCourse(user=request.user, course=course)
         user_course.save()
     #用户学过其它课程
     user_courses = UserCourse.objects.filter(course=course)
     users_id = [user_course.user_id for user_course in user_courses]
     courses = UserCourse.objects.filter(user_id__in=users_id)
     courses_id = [course.course_id for course in courses]
     related_courses = Course.objects.filter(
         id__in=courses_id).order_by('-click_nums')[:3]
     return render(
         request, 'course-video.html', {
             'course': course,
             'all_resource': all_resource,
             'related_courses': related_courses,
         })