Beispiel #1
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:
             user_fav = UserFavorite()
             user_fav.user = request.user
             user_fav.fav_id = fav_id
             user_fav.fav_type = fav_type
             user_fav.save()
             return JsonResponse({'status': 'success', 'msg': '已收藏'})
     else:
         return JsonResponse({'status': 'fail', 'msg': '参数错误'})
Beispiel #2
0
    def post(self, request, *args, **kwargs):

        # 如果未登录,封装数据告诉ajax,ajax会返回登录页面
        # status和msg,一定要和前端的ajax一致,否则ajax处理不了
        if not request.user.is_authenticated:
            return JsonResponse({
                "status": "fail",
                "msg": "用户未登录",
            })

        # 拿到数据给modelForm验证
        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)
            # 如果已经收藏,删除当前收藏记录,其对应的收藏数量-1
            if existed_records:
                existed_records.delete()
                if fav_type == 1:
                    course = Courses.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:
                user_fav = UserFavorite()
                user_fav.fav_id = fav_id
                user_fav.fav_type = fav_type
                # 这一步非常重要,user作为外键,是必填字段,这里只能手动添加
                user_fav.user = request.user
                user_fav.save()

                return JsonResponse({
                    "status": "success",
                    "msg": "已收藏",
                })
        # 字段检验不通过
        else:
            return JsonResponse({
                "status": "fail",
                "msg": "参数格式错误",
            })
Beispiel #3
0
    def post(self, request, *args, **kwargs):
        """
        用户收藏,取消收藏
        """
        # 如果用户未登录,就给前端返回对应的js数据
        # is_authenticated
        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:
                    # 这是收藏课程,通过id找到
                    course = Course.objects.get(id=fav_id)
                    course.fav_nums -= 1  # 取消收藏,人数减一
                    course.save()

                elif fav_type == 2:
                    # 这是收藏课程机构,通过id找到
                    course_org = CourseOrg.objects.get(id=fav_id)
                    course_org.fav_nums -= 1  # 取消收藏,人数减一
                    course_org.save()

                elif fav_type == 3:
                    # 这是收藏讲师,通过id找到
                    teacher = Teacher.objects.get(id=fav_id)
                    teacher.fav_nums -= 1  # 取消收藏,人数减一
                    teacher.save()

                # 返回数据给前端
                return JsonResponse({"status": "success", "msg": "收藏"})

            # 用户收藏
            else:
                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": "参数错误"})
Beispiel #4
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"]
            if (fav_type == 2) & (int(request.user.id) == int(fav_id)):
                return JsonResponse({"status": "fail", "msg": "请不要收藏自己"})
            if fav_type == 1:
                music = Music.objects.filter(id=int(fav_id))
                if music:
                    music = music.first()
                    if music.owner == request.user:
                        return JsonResponse({
                            "status": "fail",
                            "msg": "请不要收藏自己的作品"
                        })
            # 是否已经收藏
            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:
                    music = Music.objects.get(id=fav_id)
                    music.fav_nums = F('fav_nums') - 1
                    music.save()
                elif fav_type == 2:
                    user = UserProfile.objects.get(id=fav_id)
                    user.fav_nums = F('fav_nums') - 1
                    user.save()

                return JsonResponse({"status": "success", "msg": "收藏"})
            else:
                user_fav = UserFavorite()
                user_fav.fav_id = fav_id
                user_fav.fav_type = fav_type
                user_fav.user = request.user
                user_fav.save()
                if fav_type == 1:
                    music = Music.objects.get(id=fav_id)
                    music.fav_nums = F('fav_nums') + 1
                    music.save()
                elif fav_type == 2:
                    user = UserProfile.objects.get(id=fav_id)
                    user.fav_nums = F('fav_nums') + 1
                    user.save()

                return JsonResponse({"status": "success", "msg": "已收藏"})
        else:
            return JsonResponse({"status": "fail", "msg": "参数错误"})
Beispiel #5
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"]  #前端返回的用户收藏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:
                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": "参数错误"})
Beispiel #6
0
    def post(self, request, *args, **kwargs):
        '''
        用户收藏,用户取消收藏
        '''
        # request.user.is_authenticated:用来判断用户是否登陆
        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:
                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": "参数错误",
            })
Beispiel #7
0
    def post(self, request, *args, **kwargs):
        # check if user logged in
        if not request.user.is_authenticated:
            return JsonResponse({
                "status": "fail",
                "msg": "user not logged in."
            })
        userfav_form = UserFavForm(request.POST)
        if userfav_form.is_valid():
            fav_id = userfav_form.cleaned_data["fav_id"]
            fav_type = userfav_form.cleaned_data["fav_type"]

            # check if already favorite
            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_num -= 1
                    course.save()
                elif fav_type == 2:
                    org = CourseOrg.objects.get(id=fav_id)
                    org.fav_nums -= 1
                    org.save()
                else:
                    teacher = Teacher.objects.get(id=fav_id)
                    teacher.fav_nums -= 1
                    teacher.save()
                return JsonResponse({
                    "status": "success",
                    "msg": "Favorite"
                })
            else:
                userfav = UserFavorite.objects.create(user=request.user, fav_id=fav_id, fav_type=fav_type)
                if fav_type == 1:
                    course = Course.objects.get(id=fav_id)
                    course.fav_num += 1
                    course.save()
                elif fav_type == 2:
                    org = CourseOrg.objects.get(id=fav_id)
                    org.fav_nums += 1
                    org.save()
                else:
                    teacher = Teacher.objects.get(id=fav_id)
                    teacher.fav_nums += 1
                    teacher.save()
                return JsonResponse({
                    "status": "success",
                    "msg": "Favorited"
                })
        else:
            return JsonResponse({
                "status": "fail",
                "msg": "parameter error."
            })
Beispiel #8
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()

                return JsonResponse({
                    "status": "success",
                    "msg": "收藏"
                })
            else:
                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": "参数错误"
            })
Beispiel #9
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()
                #1---course;2---teacher
                if fav_type == 1:
                    course = Course.objects.get(id=fav_id)
                    course.fav_nums += 1
                    course.save()
                elif fav_type == 2:
                    teacher = Teacher.objects.get(id=fav_id)
                    teacher.fav_nums += 1
                    teacher.save()

                return JsonResponse({"status": "success", "msg": "收藏"})
            else:
                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": "参数错误"})
Beispiel #10
0
    def post(self, request, *args, **kwargs):
        """
        取消收藏,收藏

        :param request:
        :param args:
        :param kwargs:
        :return:
        """
        # 判断用户是否登陆
        if not request.user.is_authenticated:
            return JsonResponse({
                "status": "fail",
                "msg": "用户未登陆"
            })
        # 表单验证
        fav_form = UserFavForm(request.POST)
        if fav_form.is_valid():
            fav_id = fav_form.cleaned_data["fav_id"]
            fav_type = 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_num -= 1
                    course.save()
                elif fav_type == 2:  # 机构
                    course = CourseOrg.objects.get(id=fav_id)
                    course.fav_num -= 1
                    course.save()
                elif fav_type == 3:  # 教师
                    course = Teacher.objects.get(id=fav_id)
                    course.fav_num -= 1
                    course.save()

                return JsonResponse({
                    "status": "success",
                    "msg": "收藏",
                })
            else:
                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": "参数错误",
            })
Beispiel #11
0
    def post(self, request, *args, **kwargs):
        if not request.user.is_authenticated:
            messages.error(request, "用户未登录!")
            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_record = UserFavourite.objects.filter(user=request.user,
                                                          fav_id=fav_id,
                                                          fav_type=fav_type)
            if existed_record:
                existed_record.delete()

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

                message = UserMessage()
                message.user = request.user
                message.messgae = "取消收藏成功!"
                message.has_read = False
                message.save()

                return JsonResponse({"status": "success", "msg": "收藏"})
            else:
                user_fav = UserFavourite()
                user_fav.fav_id = fav_id
                user_fav.fav_type = fav_type
                user_fav.user = request.user
                user_fav.save()

                if fav_type == 1:
                    course = Course.objects.get(id=fav_id)
                    course.fav_num += 1
                    course.save()
                elif fav_type == 2:
                    courseorg = CourseOrg.objects.get(id=fav_id)
                    courseorg.fav_nums += 1
                    courseorg.save()
                else:
                    teacher = Teacher.objects.get(id=fav_id)
                    teacher.fav_nums += 1
                    teacher.save()

                message = UserMessage()
                message.user = request.user
                message.messgae = "收藏成功!"
                message.has_read = False
                message.save()

                return JsonResponse({"status": "success", "msg": "已收藏"})
        else:
            return JsonResponse({"status": "fail", "msg": "参数错误"})