Example #1
0
    def post(self, request):
        fav_id = request.POST.get('fav_id', '0')
        fav_type = request.POST.get('fav_type', '0')
        # 判断是否登录,返回前端判断跳转
        if not request.user.is_authenticated:
            return HttpResponse("{'status': 'fail','msg':'用户未登陆'}",
                                content_type='application/json')
        # 判断是否已经收藏,若已收藏则删除收藏记录
        exist_records = UserFavorite.objects.filter(user=request.user.id,
                                                    fav_id=int(fav_id),
                                                    fav_type=int(fav_type))
        if exist_records:
            exist_records.delete()
            # 收藏类型为课程1,机构2,教师3
            if int(fav_type) == 1:
                course = Course.objects.get(pk=int(fav_id))
                # 取消收藏,收藏数减1
                course.fav_nums -= 1
                if course.fav_nums < 0:
                    course.fav_nums = 0
                course.save()
            elif int(fav_type) == 2:
                course_org = CourseOrg.objects.get(pk=int(fav_id))
                course_org.fav_nums -= 1
                if course_org.fav_nums < 0:
                    course_org.fav_nums = 0
                course_org.save()
            elif int(fav_type) == 3:
                teacher = Teacher.objects.get(pk=int(fav_id))
                teacher.fav_nums -= 1
                if teacher.fav_nums < 0:
                    teacher.fav_nums = 0
                teacher.save()
            return HttpResponse("{'status': 'fail','msg':'收藏'}",
                                content_type='application/json')
        else:
            user_fav = UserFavorite()
            if int(fav_id) > 0 and int(fav_type) > 0:
                user_fav.fav_type = int(fav_type)
                user_fav.fav_id = int(fav_id)
                user_fav.user = request.user
                user_fav.save()
                if int(fav_type) == 1:
                    course = Course.objects.get(pk=int(fav_id))
                    course.fav_nums += 1
                    course.save()
                elif int(fav_type) == 2:
                    course_org = CourseOrg.objects.get(pk=int(fav_id))
                    course_org.fav_nums += 1
                    course_org.save()
                elif int(fav_type) == 3:
                    teacher = Teacher.objects.get(pk=int(fav_id))
                    teacher.fav_nums += 1
                    teacher.save()
                return HttpResponse("{'status': 'success','msg':'已收藏'}",
                                    content_type='application/json')

            else:
                return HttpResponse("{'status': 'fail','msg':'收藏出错'}",
                                    content_type='application/json')
Example #2
0
File: views.py Project: spzhu/GMOOC
    def post(self, request):
        fav_id = request.POST.get("fav_id", "")
        fav_type = request.POST.get("fav_type", "")
        if request.user.is_authenticated:
            exist_record = UserFavorite.objects.filter(user=request.user, fav_id=int(fav_id), fav_type=int(fav_type))
            if int(fav_type) == 1:
                fav_object = Course.objects.get(id=int(fav_id))
            elif int(fav_type) == 2:
                fav_object = CourseOrg.objects.get(id=int(fav_id))
            elif int(fav_type) == 3:
                fav_object = Teacher.objects.get(id=int(fav_id))
            if exist_record:
                exist_record.delete()
                fav_object.fav_nums -= 1
                fav_object.save()
                return HttpResponse('{"status": "success", "msg": "收藏"}', content_type='application/json')
            else:
                if int(fav_id) > 0 and int(fav_type) > 0:
                    user_fav = UserFavorite()
                    user_fav.user = request.user
                    user_fav.fav_id = int(fav_id)
                    user_fav.fav_type = int(fav_type)
                    user_fav.save()
                    fav_object.fav_nums += 1
                    fav_object.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')
Example #3
0
    def post(self, request):
        id = request.POST.get('fav_id', 0)  # 防止后边int(fav_id)时出错
        type = request.POST.get('fav_type', 0)  # 防止int(fav_type)出错

        if not request.user.is_authenticated:
            return HttpResponse('{"status":"fail", "msg":"用户未登录"}',
                                content_type='application/json')

        exist_record = UserFavorite.objects.filter(user=request.user,
                                                   fav_id=int(id),
                                                   fav_type=int(type))
        if exist_record:
            # 如果记录已经存在,表示用户取消收藏
            exist_record.delete()
            if int(type) == 1:
                course = Course.objects.get(id=int(id))
                course.fav_nums -= 1
                if course.fav_nums < 0:
                    course.fav_nums = 0
                course.save()
            elif int(type) == 2:
                org = CourseOrg.objects.get(id=int(id))
                org.fav_nums -= 1
                if org.fav_nums < 0:
                    org.fav_nums = 0
                org.save()
            elif int(type) == 3:
                teacher = Teacher.objects.get(id=int(id))
                teacher.fav_nums -= 1
                if teacher.fav_nums < 0:
                    teacher.fav_nums = 0
                teacher.save()
            return HttpResponse('{"status":"success", "msg":"收藏"}',
                                content_type='application/json')
        else:
            user_fav = UserFavorite()
            if int(type) > 0 and int(id) > 0:
                user_fav.fav_id = int(id)
                user_fav.fav_type = int(type)
                user_fav.user = request.user
                user_fav.save()

                if int(type) == 1:
                    course = Course.objects.get(id=int(id))
                    course.fav_nums += 1
                    course.save()
                elif int(type) == 2:
                    org = CourseOrg.objects.get(id=int(id))
                    org.fav_nums += 1
                    org.save()
                elif int(type) == 3:
                    teacher = Teacher.objects.get(id=int(id))
                    teacher.fav_nums += 1
                    teacher.save()
                return HttpResponse('{"status":"success", "msg":"已收藏"}',
                                    content_type='application/json')
            else:
                return HttpResponse('{"status":"fail", "msg":"收藏出错"}',
                                    content_type='application/json')
Example #4
0
    def post(self, request):
        # blog_id
        blog_id = request.POST.get('blog_id', "")

        # 判断用户是否登陆
        if not request.user.is_authenticated():
            return HttpResponse('{"status": "fail", "msg":"用户未登陆"}',
                                content_type='application/json')

        exist_records = UserFavorite.objects.filter(user=request.user,
                                                    blog=int(blog_id))
        if exist_records:
            # 如果记录已经存在, 则表示用户取消收藏
            exist_records.delete()
            # 取消收藏 收藏数减一
            blog = Blog.objects.get(id=int(blog_id))
            blog.fav_num -= 1
            if blog.fav_num <= 0:
                blog.fav_num = 0
            blog.save()
            return HttpResponse('{"status": "success", "msg":"取消收藏成功"}',
                                content_type='application/json')
        else:
            # 否则用户收藏 收藏数加一
            user_fav = UserFavorite()
            user_fav.user = request.user
            user_fav.blog = blog_id
            user_fav.save()
            blog = Blog.objects.get(id=int(blog_id))
            blog.fav_num += 1
            blog.save()
            return HttpResponse('{"status": "success", "msg":"收藏成功"}',
                                content_type='application/json')
Example #5
0
    def post(self, request):
        fav_id = request.POST.get('fav_id', 0)
        fav_type = request.POST.get('fav_type', '')
        if not request.user.is_authenticated:
            return HttpResponse('{"status":"fail","msg":"用户未登录"}')
        exits = UserFavorite.objects.filter(user=request.user,
                                            fav_id=int(fav_id),
                                            fay_type=int(fav_type))
        print(exits.values('fav_id'))
        if exits:
            exits.delete()
            if int(fav_type) == 1:
                course = Course.objects.get(id=int(fav_id))
                course.fav_nums -= 1
                if course.fav_nums < 0:
                    course.fav_nums = 0
                course.save()
            elif int(fav_type) == 2:
                course_org = CourseOrg.objects.get(id=int(fav_id))
                course_org.fav_nums -= 1
                if course_org.fav_nums < 0:
                    course_org.fav_nums = 0
                course_org.save()
            elif int(fav_type) == 3:
                teacher = Teacher.objects.get(id=int(fav_id))
                teacher.fav_nums -= 1
                if teacher.fav_nums < 0:
                    teacher.fav_nums = 0
                teacher.save()
            return HttpResponse('{"status":"success","msg":"收藏"}')
        else:
            user_fav = UserFavorite()
            if int(fav_id) > 0 and int(fav_type) > 0:
                user_fav.user = request.user
                user_fav.fav_id = int(fav_id)
                user_fav.fay_type = fav_type
                user_fav.save()

                if int(fav_type) == 1:
                    course = Course.objects.get(id=int(fav_id))
                    course.fav_nums += 1
                    course.save()
                elif int(fav_type) == 2:
                    course_org = CourseOrg.objects.get(id=int(fav_id))
                    course_org.fav_nums += 1
                    course_org.save()
                elif int(fav_type) == 3:
                    teacher = Teacher.objects.get(id=int(fav_id))
                    teacher.fav_nums += 1
                    teacher.save()
                return HttpResponse('{"status":"success","msg":"已收藏"}')

            else:
                return HttpResponse('{"status":"fail","msg":"收藏出错"}')
Example #6
0
    def post(self, request, *args, **kwargs):
        # 先判断用户是否登录
        if not request.user.is_authenticated:
            return JsonResponse({"status": "fail", "msg": "用户未登录"})

        user_fav_form = UserFavForm(request.POST)
        if user_fav_form.is_valid():
            fav_id = user_fav_form.cleaned_data["fav_id"]
            fav_type = user_fav_form.cleaned_data["fav_type"]
            # 是否已经收藏
            existed_records = UserFavorite.objects.filter(user=request.user,
                                                          fav_id=fav_id,
                                                          fav_type=fav_type)
            if existed_records:
                existed_records.delete()

                if fav_type == 1:
                    course = Course.objects.get(id=fav_id)
                    course.fav_nums -= 1
                    course.save()
                elif fav_type == 2:
                    course_org = CourseOrg.objects.get(id=fav_id)
                    course_org.fav_nums -= 1
                    course_org.save()
                elif fav_type == 3:
                    teacher = Teacher.objects.get(id=fav_id)
                    teacher.fav_nums -= 1
                    teacher.save()

                return JsonResponse({"status": "success", "msg": "收藏"})
            else:
                if fav_type == 1:
                    course = Course.objects.get(id=fav_id)
                    course.fav_nums += 1
                    course.save()
                elif fav_type == 2:
                    course_org = CourseOrg.objects.get(id=fav_id)
                    course_org.fav_nums += 1
                    course_org.save()
                elif fav_type == 3:
                    teacher = Teacher.objects.get(id=fav_id)
                    teacher.fav_nums += 1
                    teacher.save()

                user_fav = UserFavorite()
                user_fav.fav_id = fav_id
                user_fav.fav_type = fav_type
                user_fav.user = request.user
                user_fav.save()

                return JsonResponse({"status": "success", "msg": "已收藏"})
        else:
            return JsonResponse({"status": "fail", "msg": "参数错误"})
Example #7
0
	def post(self, request):
		fav_id = request.POST.get('fav_id', 0)
		fav_type = request.POST.get('fav_type', 0)

		if not request.user.is_authenticated():
			return HttpResponse('{"status":"fail", "msg":"用户未登录"}', content_type='application/json')

		added_fav = UserFavorite.objects.filter(user=request.user, fav_id=int(fav_id), fav_type=int(fav_type))
		# 用户已经收藏,则判断为取消收藏
		if added_fav:
			added_fav.delete()
			if int(fav_type) == 1:
				course = Course.objects.get(id=int(fav_id))
				course.fav_nums -= 1
				if course.fav_nums < 0:
					course.fav_nums = 0
				course.save()
			elif int(fav_type) == 2:
				org = CourseOrg.objects.get(id=int(fav_id))
				org.fav_nums -= 1
				if org.fav_nums < 0:
					org.fav_nums = 0
				org.save()
			elif int(fav_type) == 3:
				teacher = Teacher.objects.get(id=int(fav_id))
				teacher.fav_nums -= 1
				if teacher.fav_nums < 0:
					teacher.fav_nums = 0
				teacher.save()
			return HttpResponse('{"status":"success", "msg":"收藏"}', content_type='application/json')
		else:
			add_fav = UserFavorite()
			if int(fav_id)>0 and int(fav_type)>0:
				add_fav.user = request.user
				add_fav.fav_id = int(fav_id)
				add_fav.fav_type = int(fav_type)
				add_fav.save()
				if int(fav_type) == 1:
					course = Course.objects.get(id=int(fav_id))
					course.fav_nums += 1
					course.save()
				elif int(fav_type) == 2:
					org = CourseOrg.objects.get(id=int(fav_id))
					org.fav_nums += 1
					org.save()
				elif int(fav_type) == 3:
					teacher = Teacher.objects.get(id=int(fav_id))
					teacher.fav_nums += 1
					teacher.save()
				return HttpResponse('{"status":"success", "msg":"已收藏"}', content_type='application/json')
			else:
				return HttpResponse('{"status":"fail", "msg":"收藏出错"}', content_type='application/json')
Example #8
0
    def post(self, request):

        # 验证用户是否登录
        if request.user.is_authenticated:

            user_fav_form = AddFavform(request.POST)

            if user_fav_form.is_valid():
                fav_id = user_fav_form.cleaned_data["fav_id"]
                fav_type = user_fav_form.cleaned_data["fav_type"]

                existence_records = UserFavorite.objects.filter(
                    user=request.user, fav_id=fav_id, fav_type=fav_type)

                # 如果有已存在记录,说明点过收藏
                if existence_records:
                    existence_records.delete()

                    # fav_type==1代表课程, 2代表讲师, 3代表机构
                    if fav_type == 1:
                        course = Course.objects.get(id=fav_type)
                        course.fav_nums -= 1
                        course.save()

                    elif fav_type == 2:
                        teacher = Teacher.objects.get(id=fav_type)
                        teacher.fav_nums -= 1
                        teacher.save()

                    elif fav_type == 3:
                        courseorg = CourseOrg.objects.get(id=fav_type)
                        courseorg.fav_nums -= 1
                        courseorg.save()

                    return JsonResponse({"status": "success", "msg": "收藏"})

                else:
                    # 没有就说明没有点收藏, 保存记录!
                    user_fav = UserFavorite()
                    user_fav.fav_type = fav_type
                    user_fav.fav_id = fav_id
                    user_fav.user = request.user
                    user_fav.save()

                    return JsonResponse({
                        "status": "success",
                        "msg": "已收藏",
                    })

        else:
            return JsonResponse({"status": "fail", "msg": "用户未登录"})
Example #9
0
    def post(self, request):
        if request.user.is_authenticated:
            fav_id = int(request.POST.get('fav_id', 0))
            fav_type = int(request.POST.get('fav_type', 0))

            user_fav = UserFavorite.objects.filter(user=request.user, fav_id=fav_id, fav_type=fav_type)
            if user_fav:
                user_fav.delete()
                return HttpResponse('{"status": "success", "msg": "Favorite"}', content_type='application/json')
            else:
                user_fav = UserFavorite()
                user_fav.user = request.user
                user_fav.fav_id = fav_id
                user_fav.fav_type = fav_type
                user_fav.save()
                return HttpResponse('{"status": "success", "msg": "Un-Favorite"}', content_type='application/json')
        else:
            return HttpResponse('{"status": "fail", "err_msg": "Login Required"}', content_type='application/json')
Example #10
0
def ajax_add_prd_to_fav(request):
    if request.method == 'GET':
        is_completed, already_added, is_login = "******", "false", "true"
        try:
            # 检查是否登录,未登录用户不能收藏
            if request.user.is_authenticated:
                user_id = request.user.id
                prd_serial_number = request.GET.get('prd_serial_number')
                # 检查是否早已添加了收藏
                fav_list = UserFavorite.objects.filter(
                    Q(user_id=user_id)
                    & Q(fav_prd_serial_number=prd_serial_number))
                if fav_list:
                    already_added = "true"
                else:
                    priceStatistics = PriceStatistics.objects.get(
                        prd_serial_number=prd_serial_number)
                    userFavorite = UserFavorite()
                    userFavorite.user_id = user_id
                    userFavorite.fav_prd_serial_number = prd_serial_number
                    userFavorite.fav_type = 1
                    userFavorite.price_when_fav = priceStatistics.lowest_price
                    userFavorite.save()
                    is_completed = "true"
            else:
                is_login = "******"
        except:
            traceback.print_exc()
        finally:
            return JsonResponse({
                "is_completed": is_completed,
                "already_added": already_added,
                "is_login": is_login,
            })
    else:
        return redirect(reverse('forbidden_403'))
Example #11
0
    def post(self, request):
        fav_id = request.POST.get('fav_id', 0)
        fav_type = request.POST.get('fav_type', 0)

        if not request.user.is_authenticated():
            # 判断用户登录状态
            return HttpResponse('{"status":"fail", "msg":"用户未登录"}', content_type='application/json')

        # 使用用户、收藏id、收藏类型进行联合查询
        exist_records = UserFavorite.objects.filter(user=request.user, fav_id=int(fav_id), fav_type=int(fav_type))
        if exist_records:
            # 记录已存在 表示用户取消收藏
            exist_records.delete()
            if int(fav_type) == 1:
                topic = NewsTopic.objects.get(id=int(fav_id))
                topic.fav_nums -= 1
                if topic.fav_nums < 0:
                    topic.fav_nums = 0
                topic.save()


            return HttpResponse('{"status":"success", "msg":"收藏"}', content_type='application/json')

        else:
            # 记录不存在 表示用户未收藏,并进行收藏
            user_fav = UserFavorite()
            if int(fav_id) > 0 and int(fav_type) > 0:
                user_fav.user = request.user
                user_fav.fav_id = int(fav_id)
                user_fav.fav_type = int(fav_type)
                user_fav.save()

                if int(fav_type) == 1:
                    topic = NewsTopic.objects.get(id=int(fav_id))
                    topic.fav_nums += 1
                    topic.save()


                return HttpResponse('{"status":"success", "msg":"已收藏"}', content_type='application/json')
            else:
                return HttpResponse('{"status":"fail", "msg":"收藏出错"}', content_type='application/json')
Example #12
0
    def post(self, request):
        fav_id = request.POST.get('fav_id', 0)
        fav_type = request.POST.get('fav_type', 0)

        # 判断用户是否登录,否则通过ajax返回登录页面
        if not request.user.is_authenticated:
            return HttpResponse('{"status": "fail", "msg":"用户未登录"}',
                                content_type='application/json')

        exist_record = UserFavorite.objects.filter(user=request.user,
                                                   fav_id=int(fav_id),
                                                   fav_type=int(fav_type))

        # 记录存在则删除
        if exist_record:
            exist_record.delete()
            if int(fav_id) == 1:
                course = Course.objects.get(id=int(fav_id))
                course.fav_nums -= 1
                if course.fav_nums < 0:
                    course.fav_nums = 0
                course.save()
            elif int(fav_id) == 2:
                org = Organization.objects.get(id=int(fav_id))
                org.fav_nums -= 1
                if org.fav_nums < 0:
                    org.fav_nums = 0
                org.save()
            elif int(fav_id) == 3:
                teacher = Teacher.objects.get(id=int(fav_id))
                teacher.fav_nums -= 1
                if teacher.fav_nums < 0:
                    teacher.fav_nums = 0
                teacher.save()
            return HttpResponse('{"status": "success", "msg":"收藏"}',
                                content_type='application/json')
        else:
            user_fav = UserFavorite()
            if int(fav_id) > 0 and int(fav_type) > 0:
                user_fav.user = request.user
                user_fav.fav_id = int(fav_id)
                user_fav.fav_type = int(fav_type)
                user_fav.save()

                if int(fav_id) == 1:
                    course = Course.objects.get(id=int(fav_id))
                    course.fav_nums += 1
                    course.save()
                elif int(fav_id) == 2:
                    org = Organization.objects.get(id=int(fav_id))
                    org.fav_nums += 1
                    org.save()
                elif int(fav_id) == 3:
                    teacher = Teacher.objects.get(id=int(fav_id))
                    teacher.fav_nums += 1
                    teacher.save()
                return HttpResponse('{"status": "success", "msg":"已收藏"}',
                                    content_type='application/json')
            else:
                return HttpResponse('{"status": "fail", "msg":"收藏出错"}',
                                    content_type='application/json')
Example #13
0
    def post(self, request):
        fav_id = request.POST.get('fav_id', 0)
        fav_type = request.POST.get('fav_type', 0)

        # 判断用户是否登录
        if not request.user.is_authenticated():
            return HttpResponse("{'status': 'fail', 'msg': '用户未登录'}",
                                content_type='application/json')

        # 查找用户是否已经收藏
        exist_records = UserFavorite.objects.filter(user=request.user,
                                                    fav_id=int(fav_id),
                                                    fav_type=int(fav_type))
        if exist_records:
            # 取消收藏
            exist_records.delete()
            if int(fav_type) == 1:
                course = Course.objects.get(id=int(fav_id))
                course.fav_nums -= 1
                if course.fav_nums < 0:
                    course.fav_nums = 0
                course.save()
            elif int(fav_type) == 2:
                course_org = CourseOrg.objects.get(id=int(fav_id))
                course_org.fav_nums -= 1
                if course_org.fav_nums < 0:
                    course_org.fav_nums = 0
                course_org.save()
            elif int(fav_type) == 3:
                teacher = Teacher.objects.get(id=int(fav_id))
                teacher.fav_nums -= 1
                if teacher.fav_nums < 0:
                    teacher.fav_nums = 0
                teacher.save()
            return HttpResponse("{'status': 'success', 'msg': '用户已取消收藏'}",
                                content_type='application/json')
        else:
            user_fav = UserFavorite()
            if int(fav_id) > 0 and int(fav_type) > 0:
                user_fav.user = request.user
                user_fav.fav_id = int(fav_id)
                user_fav.fav_type = int(fav_type)
                user_fav.save()

                if int(fav_type) == 1:
                    course = Course.objects.get(id=int(fav_id))
                    course.fav_nums += 1
                    course.save()
                elif int(fav_type) == 2:
                    course_org = CourseOrg.objects.get(id=int(fav_id))
                    course_org.fav_nums += 1
                    course_org.save()
                elif int(fav_type) == 3:
                    teacher = Teacher.objects.get(id=int(fav_id))
                    teacher.fav_nums += 1
                    teacher.save()

                # 发送消息
                user_message = UserMessage()
                user_message.user = request.user
                user_message.message = "welcome to China"
                user_message.save()
                return HttpResponse("{'status': 'success', 'msg': '用户已收藏'}",
                                    content_type='application/json')
            else:
                return HttpResponse("{'status': 'fail', 'msg': '收藏出错'}",
                                    content_type='application/json')
Example #14
0
 def post(self, request):
     user = request.user
     if not user.is_authenticated:
         return JsonResponse({
             'status': 'fail',
             'msg': '用户未登录',
         })
     fav_id = request.POST.get('fav_id', 0)
     fav_type = request.POST.get('fav_type', 0)
     user_fav = UserFavorite.objects.filter(user=user,
                                            fav_id=fav_id,
                                            fav_type=fav_type)
     if user_fav.exists():
         # 收藏记录已存在
         user_fav.delete()
         if int(fav_type) == 1:
             course = Course.objects.get(id=int(fav_id))
             course.fav_num -= 1
             if course.fav_num < 0:
                 course.fav_num = 0
             course.save()
         elif int(fav_type) == 2:
             org = Organization.objects.get(id=int(fav_id))
             org.fav_num -= 1
             if org.fav_num < 0:
                 org.fav_num = 0
             org.save()
         elif int(fav_type) == 3:
             teacher = Teacher.objects.get(id=int(fav_id))
             teacher.fav_num -= 1
             if teacher.fav_num < 0:
                 teacher.fav_num = 0
             teacher.save()
         return JsonResponse({
             'status': 'success',
             'msg': '收藏',
         })
     else:
         if int(fav_type) > 0 and int(fav_id) > 0:
             user_fav = UserFavorite(user=user,
                                     fav_id=int(fav_id),
                                     fav_type=int(fav_type))
             user_fav.save()
             if int(fav_type) == 1:
                 course = Course.objects.get(id=int(fav_id))
                 course.fav_num += 1
                 if course.fav_num < 0:
                     course.fav_num = 0
                 course.save()
             elif int(fav_type) == 2:
                 org = Organization.objects.get(id=int(fav_id))
                 org.fav_num += 1
                 if org.fav_num < 0:
                     org.fav_num = 0
                 org.save()
             elif int(fav_type) == 3:
                 teacher = Teacher.objects.get(id=int(fav_id))
                 teacher.fav_num += 1
                 if teacher.fav_num < 0:
                     teacher.fav_num = 0
                 teacher.save()
             return JsonResponse({'status': 'success', 'msg': '已收藏'})
         else:
             return JsonResponse({'status': 'fail', 'msg': '收藏出错'})
Example #15
0
    def post(self, request):
        fav_id = request.POST.get('fav_id', 0)
        fav_type = request.POST.get('fav_type', 0)

        if not request.user.is_authenticated():
            return HttpResponse('{"status":"fail","msg":"用户未登录"}',
                                content_type="application/json")
            #判断用户 登录状态
        exis_records = UserFavorite.objects.filter(user=request.user,
                                                   fav_id=int(fav_id),
                                                   fav_type=int(fav_type))
        if exis_records:
            '''如果已经有数据了,表示用户要取消收藏'''
            exis_records.delete()
            if int(fav_type) == 1:
                '''
                课程收藏
                '''
                course = Courses.objects.get(id=int(fav_id))
                course.fav_nums -= 1
                if course.fav_nums < 0:
                    course.fav_nums = 0
                course.save()
            elif int(fav_type) == 2:
                '''机构收藏'''
                course_org = CourseOrg.objects.get(id=int(fav_id))
                course_org.fav_nums -= 1
                if course_org.fav_nums < 0:
                    course_org.fav_nums = 0
                course_org.save()
            # elif int(fav_type) == 3:
            #     teacher = Teacher.objects.get(id=int(fav_id))
            #     teacher.fav_nums -=1
            #     teacher.save()
            return HttpResponse('{"status":"success","msg":"收藏"}',
                                content_type="application/json")
        else:
            user_fav = UserFavorite()
            if int(fav_type) > 0 and int(fav_id) > 0:
                user_fav.user = request.user
                user_fav.fav_id = int(fav_id)
                user_fav.fav_type = int(fav_type)
                user_fav.save()
                if int(fav_type) == 1:
                    '''
                    课程收藏
                    '''
                    course = Courses.objects.get(id=int(fav_id))
                    course.fav_nums += 1
                    course.save()
                elif int(fav_type) == 2:
                    '''机构收藏'''
                    course_org = CourseOrg.objects.get(id=int(fav_id))
                    course_org.fav_nums += 1

                    course_org.save()
                # elif int(fav_type) == 3:
                #     teacher = Teacher.objects.get(id=int(fav_id))
                #     teacher.fav_nums -=1
                #     teacher.save()
                return HttpResponse('{"status":"success","msg":"已收藏"}',
                                    content_type="application/json")
            else:
                return HttpResponse('{"status":"fail","msg":"收藏失败"}',
                                    content_type="application/json")
Example #16
0
    def post(self, request):

        favorite_id = request.POST.get('fav_id', 0)
        favorite_type = request.POST.get('fav_type', 0)

        if not request.user.is_authenticated:
            return HttpResponse(json.dumps({
                'status': 'fail',
                'msg': '用户未登录!'
            }), content_type='application/json')

        favorite = UserFavorite.objects.filter(user=request.user, fav_id=favorite_id, fav_type=favorite_type)

        if favorite:  # favorite非空, 取消收藏
            favorite.delete()

            if favorite_type == '1':
                course = Course.objects.get(pk=int(favorite_id))
                course.fav_num -= 1
                if course.fav_num < 0:
                    course.fav_num = 0
                course.save()

            elif favorite_type == '2':  # 课程机构
                course_org = CourseOrg.objects.get(pk=int(favorite_id))
                course_org.fav_nums -= 1
                if course_org.fav_nums < 0:
                    course_org.fav_nums = 0
                course_org.save()

            elif favorite_type == '3':  # 授课教师
                teacher = Teacher.objects.get(pk=int(favorite_id))
                teacher.fav_nums -= 1
                if teacher.fav_nums < 0:
                    teacher.fav_nums = 0
                teacher.save()

            return HttpResponse(json.dumps({
                'status': 'success',
                'msg': '收藏'
            }), content_type='application/json')
        else:  # favorite为空, 添加收藏
            favorite = UserFavorite()
            if int(favorite_type) > 0 and int(favorite_id) > 0:
                favorite.fav_id = int(favorite_id)
                favorite.fav_type = int(favorite_type)
                favorite.user = request.user
                favorite.save()

                if favorite_type == '1':
                    course = Course.objects.get(pk=int(favorite_id))
                    course.fav_num += 1
                    course.save()
                elif favorite_type == '2':  # 课程机构
                    course_org = CourseOrg.objects.get(pk=int(favorite_id))
                    course_org.fav_nums += 1
                    course_org.save()
                elif favorite_type == '3':  # 授课教师
                    teacher = Teacher.objects.get(pk=int(favorite_id))
                    teacher.fav_nums += 1
                    teacher.save()
                return HttpResponse(json.dumps({
                    'status': 'success',
                    'msg': '已收藏'
                }), content_type='application/json')
            else:
                return HttpResponse(json.dumps({
                    'status': 'success',
                    'msg': '收藏出错!'
                }), content_type='application/json')