def get(self, request, course_id): course = Course.objects.get(id=int(course_id)) all_resource = CourseResource.objects.filter(course=course) # 我要学习功能实现 if not UserCourse.objects.filter(user=request.user, course=course): user_course = UserCourse() user_course.user = request.user user_course.course = course user_course.save() course.students += 1 course.save() # 实现 该课的同学还学过 user_courses = UserCourse.objects.filter(course=course) user_ids = [user_course.user.id for user_course in user_courses] user_courses = UserCourse.objects.filter(user_id__in=user_ids) course_ids = [user_course.course.id for user_course in user_courses] related_courses = Course.objects.filter( id__in=course_ids).order_by('-click_nums')[:3] return render( request, 'course-video.html', { 'course': course, 'all_resource': all_resource, 'related_courses': related_courses })
def get(self, request, video_id): video = Video.objects.get(id=video_id) course = video.lesson.course 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) # 获取上述所有课程的id course_ids = [ user_course.course.id for user_course in all_user_courses ] # 获取上述所有课程,并根据点击量排列 relate_courses = Course.objects.filter( id__in=course_ids).order_by('-click_nums')[:5] # 查询用户是否已关联课程 student_courses = UserCourse.objects.filter(user=request.user, course=course) if not student_courses: student_course = UserCourse() student_course.course = course student_course.user = request.user course.students += 1 course.save() student_course.save() 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, })
def get(self, request, course_id): courses = Course.objects.get(id=int(course_id)) courses.learn_nums += 1 courses.save() course_resources = CourseResource.objects.filter(course=courses) # 查询用户是否关联了课程 learn_course = UserCourse.objects.filter(user=request.user, course=courses) if not learn_course: course = UserCourse() course.user = request.user course.course = courses course.save() user_courses = UserCourse.objects.filter( course=courses) # 过滤出相同course的usercourse对象 user_id = [user_course.user.id for user_course in user_courses] # 获取所有usercourse对象中user的id # user_id__in表示可以遍历user_id中的元素然后进行查找, 根据用户找到所有usercourse对象 all_user_courses = UserCourse.objects.filter(user_id__in=user_id) # 根据所有usercourse对象找到所有course的ID course_ids = [ user_course.course.id for user_course in all_user_courses ] # 取出所有的course all_courses = Course.objects.filter( id__in=course_ids).order_by('-click_nums') return render( request, 'course-video.html', { 'courses': courses, 'course_resources': course_resources, 'all_courses': all_courses, })
def get(self, request, video_id): # 获取视频资源 video = Video.objects.get(id=int(video_id)) # 获取视频对应课程 course = video.lesson.course # 判断用户是否学习过该课程 user_courses = UserCourse.objects.filter(user=request.user, course=course) # 用户未学习该课程,将课程添加到该用户课程表中 if not user_courses: user_course = UserCourse() user_course.user = request.user user_course.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 = [ user_course.course.id for user_course in all_user_courses ] relate_courses = Course.objects.filter( id__in=course_ids).order_by('-click_nums')[:5] return render(request, 'courses/course-play.html', { 'video': video, 'course': course, 'relate_courses': relate_courses, })
def get(self, request, course_id): course = Course.objects.get(id=int(course_id)) if not UserCourse.objects.filter( Q(user=request.user) & Q(course=course)): org_joined_list = [ user_course.course.course_org.id for user_course in UserCourse.objects.filter(user=request.user) ] if course.course_org.id not in org_joined_list: course.course_org.student_num += 1 course.course_org.save() course.student_num += 1 course.save() add_user_course = UserCourse() add_user_course.user = request.user add_user_course.course = course add_user_course.save() learn_students = [ user_course.user.id for user_course in UserCourse.objects.filter(course=course) ] learned_courses = [ user_course.course.id for user_course in UserCourse.objects.filter( Q(user_id__in=learn_students) & ~Q(course_id=course.id)) ] other_courses = Course.objects.filter( id__in=learned_courses).order_by("-click_num") if other_courses.count() > 3: other_courses = other_courses[:3] return render(request, 'course-video.html', { 'course': course, 'other_courses': other_courses, })
def get(self, request, course_id): current_page = "open_course" course = Course.objects.get(id=int(course_id)) course.students += 1 course.save() # 添加用户学习记录,让用户与课程相关联 user_course = UserCourse.objects.filter(user=request.user, course_id=int(course_id)) if not user_course: user_course = UserCourse() user_course.user = request.user user_course.course = course user_course.save() # 学习该课程的人还学习过的其它课程 user_courses = UserCourse.objects.filter(course_id=int(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) relate_courses = [ user_course.course for user_course in all_user_courses ] all_resources = course.courseresource_set.all() return render( request, "course-video.html", { "course": course, "current_page": current_page, "all_resources": all_resources, "relate_courses": set(relate_courses[:3]), })
def get(self, request, video_id): current_video = Video.objects.get(id=int(video_id)) course = current_video.lesson.course if not UserCourse.objects.filter( Q(user=request.user) & Q(course=course)): add_user_course = UserCourse() add_user_course.user = request.user add_user_course.course = course add_user_course.save() learn_students = [ user_course.user.id for user_course in UserCourse.objects.filter(course=course) ] learned_courses = [ user_course.course.id for user_course in UserCourse.objects.filter( Q(user_id__in=learn_students) & ~Q(course_id=course.id)) ] other_courses = Course.objects.filter( id__in=learned_courses).order_by("-click_num") if other_courses.count() > 3: other_courses = other_courses[:3] return render( request, 'course-play.html', { 'current_video': current_video, 'course': course, 'other_courses': other_courses, })
def get(self, request, course_id): course = Course.objects.get(id=int(course_id)) # 查看user是否关联的该课程 this_user_course = UserCourse.objects.filter(user=request.user, course=course) if not this_user_course: this_user_course = UserCourse() this_user_course.user = request.user this_user_course.course = course this_user_course.save() all_course_resources = CourseResource.objects.filter(course=course) # 课程推荐 学过该课程的学生 还学过哪个课程 user_courses = UserCourse.objects.filter(course=course) all_user_ids = [user_course.user.id for user_course in user_courses] all_user_courses = UserCourse.objects.filter(user_id__in=all_user_ids) # 上面只是在UserCourse这个model中取得了 模型集合 下面还要转到course这个model来 all_course_ids = [ user_course.course.id for user_course in all_user_courses ] # all_related_courses = UserCourse.objects.filter(course_id__in=all_course_ids) 这是之前的错误写法 会在产生重复的课程 # 因为在Uercourse表中 course_id并不是不重复的,因为user_id会干扰结果 所以要选择course表来filter all_related_courses = Course.objects.filter( id__in=all_course_ids).order_by("-click_nums")[:3] return render( request, 'course-video.html', { 'course': course, 'all_course_resources': all_course_resources, 'all_related_courses': all_related_courses })
def get(self, request, course_id): course = Course.objects.get(id=int(course_id)) if not course: return render(request, '404.html') user = request.user if not user.is_authenticated(): return render(request, 'login.html') # 用户学习了某课程 user_course = UserCourse.objects.filter(user=request.user, course=course) if not user_course: user_course = UserCourse() user_course.user = request.user user_course.course = course user_course.save() related_courses = get_realted_courses(course, show_num=3) all_lesson = course.lesson_set.all() all_resource = course.courseresource_set.all() return render(request, 'course-video.html', { 'page_name': 'course_video', 'course': course, 'all_lesson': all_lesson, 'all_resource': all_resource, 'related_courses': related_courses })
def get(self, request, video_id): video = Video.objects.get(id=int(video_id)) course = video.lesson.course course.students += 1 course.save() user = request.user try: user_course = UserCourse.objects.get(course=course, user=user) user_course.add_time = datetime.now() except: user_course = UserCourse() user_course.user = request.user user_course.course = course user_course.save() all_users_courses = CourseComments.objects.filter( course=course, is_add_info=False).order_by("-add_time")[:10] all_users_reply = CourseComments.objects.filter( course=course, is_add_info=True).order_by("add_time") return render( request, "video.html", { "course": course, "video": video, "all_users_courses": all_users_courses, "all_users_reply": all_users_reply, })
def get(self, request, course_id): course = Course.objects.filter(id=course_id).first() # -------用户学习课程 [开始]------- is_exist = UserCourse.objects.filter(user_id=request.user.id, course_id=course_id).first() if not is_exist: record = UserCourse() record.user = request.user record.course = course record.save() # -------用户学习课程 [结束]------- # -------该课的同学还学过 [开始]------- # 1. 查询所有学习过该课程的用户id q = UserCourse.objects.filter(course_id=course_id).all() u_id = [user_course.user_id for user_course in q] # 2. 根据用户id 查询每个用户所学的课程id q = UserCourse.objects.filter(user_id__in=u_id).all() c_id = [user_course.course_id for user_course in q] # 3. 统计每个课程id出现的次数 times = dict() for i in c_id: times[i] = times.get(i, 0) + 1 print(times) # 4. 进行排序 x = sorted(times.items(), key=lambda item: item[1], reverse=True) # 取出现次数最多的3个课程, 此处不会报错,如果3超出了边界,也不会报错 relate_courses_id = [i[0] for i in x[:4] if i[0] != course_id] relate_courses = Course.objects.filter(id__in=relate_courses_id) # -------该课的同学还学过 [完成]------- return render(request, 'course-video.html', { "course": course, 'relate_courses': relate_courses })
def get(self, request, course_id): course = None all_resources = None relate_courses = None # 相关课程,推荐本机构的其他课程 courses_list = Course.objects.filter(id=int(course_id)) if courses_list: course = courses_list[0] # 学习人数加1 course.students += + 1 course.save() # 查询用户是否已经学习了该课程,如果没有学习该门课程就关联起来 user_courses = UserCourse.objects.filter(course=course, user=request.user) if not user_courses: user_course = UserCourse() user_course.course = course user_course.user = request.user user_course.save() all_resources = CourseResource.objects.filter(course=course) # 课程资源 # 相关课程,推荐本机构的其他课程 org = course.course_org relate_courses = org.course_set.all().order_by('-click_nums')[:3] # 通过点击数排序 return render(request, 'course-video.html', { 'course': course, 'all_resources': all_resources, 'relate_courses': relate_courses, })
def get(self, request, video_id): video = None course = None all_resources = None relate_courses = None video_list = Video.objects.filter(id=video_id) if video_list: video = video_list[0] # 根据video取出本课程 course = video.lesson.course # 查询用户是否已经学习了该课程,如果没有学习该门课程就关联起来 user_courses = UserCourse.objects.filter(course=course, user=request.user) if not user_courses: user_course = UserCourse() user_course.course = course user_course.user = request.user user_course.save() # 该课的同学还学过 user_courses = course.usercourse_set.all() # 学习该课程的用户课程 user_id_list = [user_course.user.id for user_course in user_courses] # 学习该课程的所以用户id # 通过所有用户的id,找到所有用户学习过的所有课程 all_user_courses = UserCourse.objects.filter(user_id__in=user_id_list) # 取出所有课程id course_ids = [user_course.course.id for user_course in all_user_courses] # 通过所有课程的id,找到所有的课程,按点击量去五个 relate_courses = Course.objects.filter(id__in=course_ids).order_by('-click_nums')[:5] # 课程资源下载 all_resources = course.courseresource_set.filter(course=course) return render(request, 'course-play.html', { "video": video, "course": course, "all_resources": all_resources, "relate_courses": relate_courses, })
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_courses.user = request.user user_courses.course = course user_courses.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, 'all_resources': all_resources, 'relate_courses': relate_courses, })
def get(self, request): """ 处理支付宝的return_url返回 :param request: :return: """ processed_dict = {} for key, value in request.GET.items(): processed_dict[key] = value sign = processed_dict.pop("sign", None) alipay = AliPay( appid="2016091100485762", app_notify_url="http://47.104.201.172/fights/pay/", app_private_key_path=private_key_path, alipay_public_key_path= ali_pub_key_path, # 支付宝的公钥,验证支付宝回传消息使用,不是你自己的公钥, debug=True, # 默认False, return_url="http://47.104.201.172/fights/pay/") #print(processed_dict) verify_re = alipay.verify(processed_dict, sign) if verify_re is True: order_id = processed_dict.get('out_trade_no', None) trade_no = processed_dict.get('trade_no', None) trade_status = processed_dict.get('trade_status', "TRADE_SUCCESS") existed_orders = Order.objects.filter(order_id=order_id) for existed_order in existed_orders: order_details = OrderDetail.objects.filter(order=existed_order) for order_detail in order_details: course = order_detail.course if course: # 加入到我的课程 existed_course = UserCourse.objects.filter( course=course, user=existed_order.user) if not existed_course: usercourse = UserCourse() usercourse.course = course usercourse.user = existed_order.user usercourse.save() course.students += 1 course.save() #print(trade_status) existed_order.is_pay = trade_status existed_order.trade_no = trade_no existed_order.pay_time = datetime.now() existed_order.save() return HttpResponseRedirect(reverse("index")) else: return HttpResponseRedirect(reverse("index"))
def add_learned_record(course, user): """ 增加学习记录 :param course: :param user: :return: """ user_course = UserCourse() user_course.user = user user_course.course = course user_course.save() course.stu_nums += 1 course.save()
def post(self, request): fav_id = request.POST.get('fav_id', 0) if not request.user.is_authenticated(): # 判断用户是否登陆 # 判断用户登陆状态 return HttpResponse("{'status':'fail', 'msg':'用户未登录'}", content_type='application/json') exist_records = UserApply.objects.filter(user=request.user, fav_id=int(fav_id)) user_course = UserCourse.objects.filter(user=request.user) if exist_records: # 如果记录已经存在,表示用户取消报名 exist_records.delete() user_course.delete() course = Course.objects.get(id=int(fav_id)) course.students -= 1 if course.students < 0: course.students = 0 course.save() return HttpResponse("{'status':'success', 'msg':'报名'}", content_type='application/json') else: user_apply = UserApply() user_course = UserCourse() course = Course.objects.get(id=int(fav_id)) if int( fav_id ) > 0 and course.students < course.limit and request.user.category == 1: user_apply.user = request.user user_apply.fav_id = int(fav_id) user_apply.save() user_course.user = request.user user_course.course = course user_course.save() course.students += 1 course.save() return HttpResponse("{'status':'success', 'msg':'已报名'}", content_type='application/json') elif course.students >= course.limit: return HttpResponse("{'status':'fail', 'msg':'名额已满'}", content_type='application/json') elif request.user.category != 1: return HttpResponse("{'status':'fail', 'msg':'您还不是会员呢'}", content_type='application/json') else: return HttpResponse("{'status':'fail', 'msg':'报名失败'}", content_type='application/json')
def get(self, request, course_id): course = Course.objects.get(id=int(course_id)) if not UserCourse.objects.filter( Q(user=request.user) & Q(course=course)): org_joined_list = [ user_course.course.course_org.id for user_course in UserCourse.objects.filter(user=request.user) ] if course.course_org.id not in org_joined_list: course.course_org.student_num += 1 course.course_org.save() course.student_num += 1 course.save() add_user_course = UserCourse() add_user_course.user = request.user add_user_course.course = course add_user_course.save() learn_students = [ user_course.user.id for user_course in UserCourse.objects.filter(course=course) ] learned_courses = [ user_course.course.id for user_course in UserCourse.objects.filter( Q(user_id__in=learn_students) & ~Q(course_id=course.id)) ] other_courses = Course.objects.filter( id__in=learned_courses).order_by("-click_num") if other_courses.count() > 3: other_courses = other_courses[:3] all_comments = UserComment.objects.filter( course=course).order_by("-add_time") try: page = request.GET.get('page', -1) except PageNotAnInteger: page = -1 p = Paginator(all_comments, 10, request=request) # request的传入是为了保留其他get参数 comments_per_page = p.page(page) return render( request, 'course-comment.html', { 'course': course, 'other_courses': other_courses, 'all_comments': comments_per_page, })
def get(self, request, course_id=5): course = Course.objects.get(id=int(course_id)) relate_courses = Course.objects.filter(tag=course.tag)[:2] #如果用户没有学习过此课程,则在数据库UserCourse中添加 if not UserCourse.objects.filter(course=course, user=request.user): new_user_course = UserCourse() new_user_course.user = request.user new_user_course.course = course new_user_course.save() #课程学习人数+1 course.students += 1 course.save() return render(request, 'course-video.html', { 'course': course, 'relate_courses': relate_courses, })
def get(self, request, course_id): # 未登陆则跳转登陆页面 if not request.user.is_authenticated: return render(request, 'login.html', {}) course = Course.objects.get(id=int(course_id)) # 判断用户是否学过该门课程 user = request.user try: has_course = UserCourse.objects.get(course=course, user=user) except: user_cor = UserCourse() user_cor.user = user user_cor.course = course user_cor.save() course.students += 1 course.save() all_info = Lesson.objects.filter(course_id=int(course_id)) all_resource = CourseResource.objects.filter(course_id=int(course_id)) # 从用户课表中取出学过这门课的用户 user_courses = UserCourse.objects.filter(course=course) # 取出所有学过用户的ID user_ids = [user_course.user_id for user_course in user_courses] # 通过ID取出所有学过的课程 all_user_courses = UserCourse.objects.filter(user_id__in=user_ids) # 过滤掉当前课程 course_ids = [ user_course.course_id for user_course in all_user_courses if user_course.course_id != int(course_id) ] # 按照点击数排序且只取前三 relate_courses = Course.objects.filter( id__in=course_ids).order_by('-click_nums')[0:3] return render( request, 'course-video.html', { 'course': course, 'all_resource': all_resource, 'all_info': all_info, 'relate_courses': relate_courses, })
def post(self, request): # 判断用户登录状态 if not request.user.is_authenticated(): return HttpResponse(json.dumps({ 'status': 'fail', 'msg': '请先登录' }), content_type='application/json') course_id = request.POST.get("course_id", "") course = Course.objects.get(id=int(course_id)) if not course: return HttpResponse(json.dumps({ 'status': 'fail', 'msg': '课程不存在' }), content_type='application/json') has_learn = UserCourse.objects.filter(course=int(course_id), user=request.user) if has_learn: return usercourse = UserCourse() usercourse.user = request.user usercourse.course = course usercourse.save() # 学习人数+1 course.students += 1 course.save() return HttpResponse(json.dumps({ 'status': 'success', 'msg': '已添加到学习单' }), content_type='application/json')
def get(self, request, course_id): course = Course.objects.get(id=course_id) # 增加学习人数 即点击开始学习后加1 course.students += 1 course.save() # 查询用户是否已经关联该课程 user_course = UserCourse.objects.filter(user=request.user, course=course) if not user_course: user_course = UserCourse() user_course.user = request.user user_course.course = course user_course.save() user_courses = UserCourse.objects.filter( course=course) # 获取学过该课程的 所有用户课程 user_ids = [user_couser.user.id for user_couser in user_courses ] # 获取所有学过该课程的 用户课程 对应的用户的id all_user_courses = UserCourse.objects.filter( user_id__in=user_ids) # 根据用户id 获取这些用户的 用户课程 信息 # 取出所有课程的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") all_resources = CourseResource.objects.filter(course=course) return render( request, "course-video.html", { "course": course, "course_resources": all_resources, "relate_courses": relate_courses, })
def post(self, request): course_id = request.POST.get('course_id', "") # 判断用户是否登录 if not request.user.is_authenticated: return HttpResponse('{"status":"fail", "msg":"用户未登录"}', content_type='application/json') exist_course = Course.objects.get(id=int(course_id)) if exist_course: exist_user_course = UserCourse.objects.filter(user=request.user, course=exist_course) if not exist_user_course: # 扣除积分 if request.user.get_jifens() >= exist_course.jifen: jifenDetail = JifenDetail() jifenDetail.user = request.user jifenDetail.type = "xf" jifenDetail.nums = -exist_course.jifen jifenDetail.desc = "购买课程 [{0}]".format(exist_course.name) jifenDetail.save() user_course = UserCourse() user_course.user = request.user user_course.course = exist_course user_course.save() return HttpResponse( '{"status":"success", "msg":"该课程购买成功"}', content_type='application/json') else: return HttpResponse( '{"status":"fail", "msg":"购买失败, 积分不足"}', content_type='application/json') else: return HttpResponse('{"status":"fail", "msg":"该课程重复购买"}', content_type='application/json') else: return HttpResponse('{"status":"fail", "msg":"无效的课程"}', content_type='application/json')
def get(self, request, course_id): course_info = Course.objects.get(id=int(course_id)) course_info.students += 1 course_info.save() lessons = course_info.get_lesson(request) # 判断用户是否关联了该课程 user_course = UserCourse.objects.filter(user=request.user, course=course_info) if not user_course: user_course = UserCourse() user_course.user = request.user user_course.course = course_info user_course.save() # 根据课程获得所有学习过这个课程的用户 user_courses = UserCourse.objects.filter(course=course_info) # 获得用户id user_ids = [u_course.user.id for u_course in user_courses] # 根据用户id找到 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_info) return render( request, 'course-video.html', { 'course_info': course_info, 'all_resources': all_resources, 'lessons': lessons, 'relate_courses': relate_courses })