Пример #1
0
    def post(self, request):
        fav_id = int(request.POST.get('fav_id', 0))
        fav_type = int(request.POST.get('fav_type', 0))

        res = dict()
        if not request.user.is_authenticated():
            res['status'] = 'fail'
            res['msg'] = '用户未登录'
            return HttpResponse(json.dumps(res), content_type='application/json')

        # 查询收藏记录
        exist_records = UserFavorite.objects.filter(user=request.user, fav_id=fav_id, fav_type=fav_type)
        if exist_records:
            exist_records.delete()
            self.set_fav_nums(fav_type, fav_id, -1)
            res['status'] = 'success'
            res['msg'] = '收藏'
        else:
            user_fav = UserFavorite()
            if fav_id and fav_type:
                user_fav.user = request.user
                user_fav.fav_id = fav_id
                user_fav.fav_type = fav_type
                user_fav.save()
                self.set_fav_nums(fav_type, fav_id, 1)

                res['status'] = 'success'
                res['msg'] = '已收藏'
            else:
                res['status'] = 'fail'
                res['msg'] = '收藏出错'
        return HttpResponse(json.dumps(res), content_type='application/json')
Пример #2
0
    def post(self, request):
        # 获得从前端传来的fav_id, fav_type
        fav_id = request.POST.get('fav_id', 0)
        fav_type = request.POST.get('fav_type', 0)

        # 判断用户是否登陆,request有一个匿名类user,可用于判断用户是否登陆
        if not request.user.is_authenticated:
            # 如果返回fail,ajax中自动跳转等登录页面,里面的msg值要和ajax中的值相同
            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()
            return HttpResponse('{"status": "fail", "msg":收藏"}', content_type='application/json')
        # 记录不存在,就收藏
        else:
            user_fav = UserFavorite()
            # 过滤掉没取到fav_id, fav_type的默认情况
            if int(fav_type) > 0 and int(fav_id) > 0:
                user_fav.fav_id = int(fav_id)
                user_fav.fav_type = int(fav_type)
                user_fav.user = request.user
                user_fav.save()
                return HttpResponse('{"status": "success", "msg":"已收藏"}', content_type='application/json')
            else:
                return HttpResponse('{"status": "fail", "msg":"收藏出错"}', content_type='application/json')
Пример #3
0
    def post(self, request):
        # 表明你收藏的不管是课程,讲师,还是机构。他们的id
        # 默认值取0是因为空串转int报错
        id = request.POST.get('fav_id', 0)
        # 取到你收藏的类别,从前台提交的ajax请求中取
        type = request.POST.get('fav_type', 0)

        # 收藏与已收藏取消收藏
        # 判断用户是否登录:即使没登录会有一个匿名的user
        if not request.user.is_authenticated:
            # 未登录时返回json提示未登录,跳转到登录页面是在ajax中做的
            return HttpResponse('{"status":"fail", "msg":"用户未登录"}', content_type='application/json')
        exist_records = UserFavorite.objects.filter(user=request.user, fav_id=int(id), fav_type=int(type))
        if exist_records:
            # 如果记录已经存在, 则表示用户取消收藏
            exist_records.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()
            # 过滤掉未取到fav_id type的默认情况
            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')
Пример #4
0
    def post(self, request):
        fav_id = request.POST.get('fav_id', 0)  # 防止后边int(fav_id)时出错
        fav_type = request.POST.get('fav_type', 0)  # 防止int(fav_type)出错

        if not request.user.is_authenticated:
            # 未登录时返回json提示未登录,跳转到登录页面是在ajax中做的
            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_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:
            user_fav = UserFavorite()
            if int(fav_type) > 0 and int(fav_id) > 0:
                user_fav.fav_id = int(fav_id)
                user_fav.fav_type = int(fav_type)
                user_fav.user = request.user
                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:
                    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')
Пример #5
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":"success","msg":"用户未登录"}', content_type='application/json')

        exist_records = UserFavorite.objects.filter(user=request.user,fav_type=int(fav_type), fav_id=int(fav_id))
        if exist_records:
            # 取消收藏
            exist_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
                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 = 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')
Пример #6
0
    def post(self, request):
        favor_id = request.POST.get('fav_id', 0)
        favor_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(favor_id), fav_type=int(favor_type))
        if exist_records:
            # 如果记录已经存在,则表示用户取消收藏
            exist_records.delete()
            # 删除后数量自减
            if int(favor_type) == 1:
                course = Course.objects.get(id=int(favor_id))
                course.favor_nums -= 1
                if course.favor_nums <= 0:
                    course.favor_nums = 0
                course.save()
            elif int(favor_type) == 2:
                course_org = CourseOrg.objects.get(id=int(favor_id))
                course_org.fav_nums -= 1
                if course_org.fav_nums <= 0:
                    course_org.fav_nums = 0
                course_org.save()
            elif int(favor_type) == 3:
                teacher = Teacher.objects.get(id=int(favor_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(favor_id) > 0 and int(favor_type) > 0:
                user_fav.user = request.user
                user_fav.fav_id = int(favor_id)
                user_fav.fav_type = int(favor_type)
                user_fav.save()
                return HttpResponse('{"status": "success", "msg": "已收藏"}', content_type='application/json')
            else:
                return HttpResponse('{"status": "fail", "msg": "收藏出错"}', content_type='application/json')
Пример #7
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(json.dumps({
                'status': 'fail',
                'msg': '请先登录'
            }),
                                content_type='application/json')

        exit_records = UserFavorite.objects.filter(user=request.user,
                                                   fav_id=int(fav_id),
                                                   fav_type=fav_type)
        # 记录已经存在, 则取消收藏
        if exit_records:
            exit_records.delete()
            return HttpResponse(json.dumps({
                '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()
                return HttpResponse(json.dumps({
                    'status': 'success',
                    'msg': '已收藏'
                }),
                                    content_type='application/json')

            else:
                return HttpResponse(json.dumps({
                    'status': 'fail',
                    'msg': '收藏出错'
                }),
                                    content_type='application/json')
Пример #8
0
    def post(self, request):
        fav_id = request.POST.get('fav_id', '')
        fav_type = request.POST.get('fav_type', '')

        if not request.user.is_authenticated:
            # 判断用户登录状态
            return HttpResponse(json.dumps({
                'status': 'user_none',
                '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()

            return HttpResponse(json.dumps({
                'status': 'fail',
                'msg': '收藏'
            }),
                                content_type='application/json')
        else:
            # 不存在添加收藏
            user_fav = UserFavorite()
            if int(fav_id) > 0 and int(fav_type) > 0:
                user_fav.fav_id = int(fav_id)
                user_fav.fav_type = int(fav_type)
                user_fav.user = request.user
                user_fav.save()
                return HttpResponse(json.dumps({
                    'status': 'fail',
                    'msg': '已收藏'
                }),
                                    content_type='application/json')
            else:
                return HttpResponse(json.dumps({
                    'status': 'fail',
                    'msg': '收藏出错'
                }),
                                    content_type='application/json')
Пример #9
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(json.dumps({
                '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()
            return HttpResponse(json.dumps({
                'status': 'fail',
                'msg': '收藏'
            }),
                                content_type='application/json')
        else:
            # 如果不存在,则存储进数据库
            user_fav = UserFavorite()
            if int(fav_id) > 0 and int(fav_type) > 0:
                # model中的三个字段
                user_fav.user = request.user
                user_fav.fav_id = int(fav_id)
                user_fav.fav_type = int(fav_type)
                user_fav.save()
                return HttpResponse(json.dumps({
                    'status': 'success',
                    'msg': '已收藏'
                }),
                                    content_type='application/json')
            else:
                return HttpResponse(json.dumps({
                    'status': 'fail',
                    'msg': '收藏出错'
                }),
                                    content_type='application/json')
Пример #10
0
 def post(self, request):
     if not request.user.is_authenticated():
         return JsonResponse({'status': 'fail', 'msg': '用户未登录'})
     fav_id = request.POST.get('fav_id', 0)
     fav_type = request.POST.get('fav_type', 0)
     exists_record = UserFavorite.objects.filter(fav_id=int(fav_id),
                                                 fav_type=int(fav_type),
                                                 user=request.user)
     try:
         if fav_type == '1':
             fav_obj = Course.objects.get(pk=fav_id)
             # 添加course收藏数+1
         elif fav_type == '2':
             fav_obj = CourseOrg.objects.get(pk=fav_id)
             # 添加course_org收藏数+1
         elif fav_type == '3':
             fav_obj = Teacher.objects.get(pk=fav_id)
             # 添加teacher收藏数+1
         else:
             return JsonResponse({
                 'status': 'fail',
                 'msg': 'fav_type error'
             })
     except:
         return JsonResponse({'status': 'fail', 'msg': 'fav_id error'})
     if exists_record:
         exists_record.delete()  # 如果记录已存在说明用户已收藏,这次请求是要取消收藏
         fav_obj.fav_nums -= 1
         fav_obj.save()
         return JsonResponse({'status': 'success', 'msg': '收藏'})
     else:
         if int(fav_id) > 0 and int(fav_type) > 0:
             user_fav = UserFavorite()
             user_fav.user = request.user
             user_fav.fav_type = fav_type
             user_fav.fav_id = fav_id
             user_fav.save()
             fav_obj.fav_nums += 1
             fav_obj.save()
             return JsonResponse({'status': 'success', 'msg': '已收藏'})
         else:
             return JsonResponse({'status': 'failt', 'msg': '收藏出错'})
Пример #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":"Not logged in"}', content_type='application/json')

        existed_records= UserFavorite.objects.filter(user=request.user, fav_id = int(fav_id), fav_type= int(fav_type))
        if existed_records:
            existed_records.delete()
            return HttpResponse('{"status":"success","msg":"Favor"}', content_type='application/json')
        else:
            user_favorite= UserFavorite()
            if int(fav_id) > 0 and int(fav_type) >0:
                user_favorite.user= request.user
                user_favorite.fav_id= int(fav_id)
                user_favorite.fav_type = int(fav_type)
                user_favorite.save()
                return HttpResponse('{"status":"success", "msg":"Favored"}', content_type='application/json')
            else:
                return HttpResponse('{"status":"fail","msg":"Favor error"}', content_type='application/json')
Пример #12
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(json.dumps({
                '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()
            return HttpResponse(json.dumps({
                'status': 'fail',
                '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()
                return HttpResponse(json.dumps({
                    'status': 'success',
                    'msg': '已收藏'
                }),
                                    content_type='application/json')
            else:
                return HttpResponse(json.dumps({
                    'status': 'fail',
                    'msg': '收藏错误'
                }),
                                    content_type='application/json')
Пример #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:
                article = Article.objects.get(id=int(fav_id))
                article.fav_nums -= 1
                if article.fav_nums < 0:
                    article.fav_nums = 0
                article.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:
                    article = Article.objects.get(id=int(fav_id))
                    article.fav_nums += 1
                    article.save()
                return HttpResponse('{"status":"success", "msg":"已收藏"}',
                                    content_type='application/json')

            else:
                return HttpResponse('{"status":"fail", "msg":"收藏出错"}',
                                    content_type='application/json')
Пример #14
0
    def post(self, request):
        fav_id = request.POST.get('fav_id', 0)
        is_fav = request.POST.get('is_fav', 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),
                                                    is_fav=int(is_fav))
        if exist_records:
            # 如果记录已经存在, 则表示用户取消收藏
            exist_records.delete()
            if int(is_fav) == 1:
                video = Video.objects.get(id=int(fav_id))
                video.fav_nums -= 1
                if video.fav_nums < 0:
                    video.fav_nums = 0
                video.save()
            return HttpResponse('{"status":"success", "msg":"收藏"}',
                                content_type='application/json')
        else:
            user_fav = UserFavorite()
            if int(fav_id) > 0 and int(is_fav) > 0:
                user_fav.user = request.user
                user_fav.fav_id = int(fav_id)
                user_fav.is_fav = int(is_fav)
                user_fav.save()

                if int(is_fav) == 1:
                    video = Video.objects.get(id=int(fav_id))
                    video.fav_nums += 1
                    video.save()
                return HttpResponse('{"status":"success", "msg":"已收藏"}',
                                    content_type='application/json')
            else:
                return HttpResponse('{"status":"fail", "msg":"收藏出错"}',
                                    content_type='application/json')
Пример #15
0
    def post(self, request):
        fav_id = request.POST.get('fav_id', 0)  # 防止后边int(fav_id)时出错
        fav_type = request.POST.get('fav_type', 0)  # 防止int(fav_type)出错

        if not request.user.is_authenticated:
            # 未登录时返回json提示未登录,跳转到登录页面是在ajax中做的
            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 int(fav_type) == 1:
            fav = Course.objects.get(id=int(fav_id))
        elif int(fav_type) == 2:
            fav = CourseOrg.objects.get(id=int(fav_id))
        else:
            fav = Teacher.objects.get(id=int(fav_id))
        if exist_record:
            # 如果记录已经存在,表示用户取消收藏
            exist_record.delete()
            fav.fav_nums -= 1
            fav.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()
                fav.fav_nums += 1
                fav.save()
                return HttpResponse('{"status":"success", "msg":"已收藏"}',
                                    content_type='application/json')
            else:
                return HttpResponse('{"status":"fail", "msg":"收藏出错"}',
                                    content_type='application/json')
Пример #16
0
    def post(self, request):
        fav_id = request.POST.get('fav_id', 0)    #这边如果是空字符串,用int会报错
        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()
            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.fav_id = int(fav_id)
                user_fav.user = request.user
                user_fav.fav_type = int(fav_type)
                user_fav.save()
                return HttpResponse('{"status": "success", "msg": "已收藏!"}', content_type='application/json')
            else:
                return HttpResponse('{"status": "fail", "msg": "收藏出错!"}', content_type='application/json')
Пример #17
0
    def post(self, request):
        id = request.POST.get('fav_id', 0)
        type = request.POST.get("fav_type", 0)

        if not request.user.is_authenticated:
            return HttpResponse('{"status":"fail"}')
        record = UserFavorite.objects.filter(user=request.user,
                                             fav_id=int(id),
                                             fav_type=int(type))
        if record:
            record.delete()
            return HttpResponse('{"status":"success", "msg":"已取消收藏"}')
        else:
            user_fav = UserFavorite()
            if int(id) > 0 and int(type) > 0:
                user_fav.user = request.user
                user_fav.fav_id = int(id)
                user_fav.fav_type = int(type)
                user_fav.save()
                return HttpResponse('{"status":"success", "msg":"已收藏"}')
            else:
                return HttpResponse('{"status":"fail", "msg":"收藏出错"}')
Пример #18
0
    def post(self, request):
        # 获取 类型
        fav_type = int(request.POST.get('fav_type', 0))
        # 获取id
        fav_id = int(request.POST.get('fav_id', 0))
        res = dict()
        #判断用户是否登录
        if not request.user.is_authenticated():  # 判断用户是否登录
            res['status'] = 'fail'
            res['msg'] = '用户未登录'
            return HttpResponse(json.dumps(res),
                                content_type='application/json')
        #根据类型和id判断是否收藏
        exist_records = UserFavorite.objects.filter(user=request.user,
                                                    fav_id=fav_id,
                                                    fav_type=fav_type)
        #1)如果收藏了删除数据库,对应收藏-1
        if exist_records:  # 如果值存在
            exist_records.delete()
            self.set_fav_nums(fav_type, fav_id, -1)
            res['status'] = 'success'
            res['msg'] = '收藏'
        #2)如果没有收藏,保存数据库,对应收藏+1
        else:
            user_fav = UserFavorite()
            if fav_id and fav_type:
                user_fav.user = request.user
                user_fav.fav_id = fav_id
                user_fav.fav_type = fav_type
                user_fav.save()
                self.set_fav_nums(fav_type, fav_id, 1)

                res['status'] = 'success'
                res['msg'] = '已收藏'
            else:
                res['status'] = 'fail'
                res['msg'] = '收藏出错'
        return HttpResponse(json.dumps(res), content_type='application/json')
Пример #19
0
    def post(self, request):
        fav_id = int(request.POST.get('fav_id', 0))
        fav_type = int(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=fav_id, fav_type=fav_type)
        if exist_records:
            # 如果记录已经存在,则表示用户取消收藏
            exist_records.delete()
            return HttpResponse('{"status": "success", "msg": "收藏"}', content_type='application/json')
        else:
            # 添加收藏
            user_fav = UserFavorite()
            if fav_id > 0 and fav_type > 0:
                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": "已收藏"}', content_type='application/json')
            else:
                return HttpResponse('{"status": "fail", "msg": "收藏出错"}', content_type='application/json')
Пример #20
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('failed1')  # 用户未登录
        exist_records = UserFavorite.objects.filter(user=request.user,
                                                    fav_id=int(fav_id),
                                                    fav_type=int(fav_type))
        if exist_records:
            # 若记录存在,表示用户取消收藏
            exist_records.delete()
            return HttpResponse("success1")  # 收藏
        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()
                return HttpResponse('success2')  # 已收藏
            else:
                return HttpResponse('failed2')  # 收藏出错
Пример #21
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()
            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()
                return HttpResponse('{"status": "success", "msg": "已收藏"}', content_type="application/json")
            else:
                return HttpResponse('{"status": "fail", "msg": "收藏出错"}', content_type="application/json")
Пример #22
0
    def post(self, request):
        fav_type = int(request.POST.get('fav_type', 0))
        fav_id = int(request.POST.get('fav_id', 0))

        res = dict()
        if not request.user.is_authenticated():
            res['status'] = 'success'
            res['msg'] = '用户未登录'
            return HttpResponse(json.dumps(res),
                                content_type='application/json')

        exist_records = UserFavorite.objects.filter(user=request.user,
                                                    fav_type=fav_type,
                                                    fav_id=fav_id)
        if exist_records:
            exist_records.delete()
            self.set_fav_nums(fav_type, fav_id, -1)
            res['status'] = 'success'
            res['msg'] = '取消收藏'

        else:
            user_fav = UserFavorite()
            if fav_id and fav_type:
                user_fav.user = request.user
                user_fav.fav_id = fav_id
                user_fav.fav_type = fav_type
                user_fav.save()
                self.set_fav_nums(fav_type, fav_id, 1)

                res['status'] = 'success'
                res['msg'] = '收藏成功'

            else:
                res['status'] = 'fail'
                res['msg'] = '收藏出错'

        return render(json.dumps(res), content_type='application/json')
Пример #23
0
 def post(self, request):
     fav_id = int(request.POST.get('fav_id', '0'))
     fav_type = int(request.POST.get('fav_type', '0'))
     user = request.user
     # 如果用户未登录,返回相应的msg,让前端跳转到登录页面
     if not user.is_authenticated():
         return HttpResponse('{"status":"fail", "msg":"用户未登录"}',
                             content_type='application/json')
     existed_fav = UserFavorite.objects.filter(user=user,
                                               fav_id=fav_id,
                                               fav_type=fav_type)
     # 如果用户已收藏该机构,则取消收藏,否则添加收藏
     if existed_fav:
         existed_fav.delete()
         return HttpResponse('{"status":"success", "msg":"收藏"}',
                             content_type='application/json')
     else:
         user_fav = UserFavorite()
         user_fav.user = user
         user_fav.fav_id = fav_id
         user_fav.fav_type = fav_type
         user_fav.save()
         return HttpResponse('{"status":"success", "msg":"已收藏"}',
                             content_type='application/json')
Пример #24
0
    def post(self, request):
        user_id = request.POST.get('user_id', 0)
        food_id = request.POST.get('food_id', 0)

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

        exist_records = UserFavorite.objects.filter(user_id=user_id,
                                                    food_id=food_id)
        if exist_records:
            # 如果记录存在,则表示用户取消收藏
            exist_records.delete()

            # 取消收藏 收藏数减一
            fav_food = Food.objects.get(id=food_id)
            if fav_food.fav_nums > 0:
                fav_food.fav_nums -= 1
                fav_food.save()

            return HttpResponse('{"status":"success", "msg":"收藏"}',
                                content_type="application/json")
        else:
            user_fav = UserFavorite()
            if int(user_id) > 0 and int(food_id) > 0:
                user_fav.user_id = int(user_id)
                user_fav.food_id = int(food_id)
                user_fav.save()

                # 添加收藏 收藏数加一
                fav_food = Food.objects.get(id=food_id)
                if fav_food.fav_nums >= 0:
                    fav_food.fav_nums += 1
                    fav_food.save()

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

        if not request.user.is_authenticated:
            return JsonResponse({'status': 'fail', 'msg': '用户未登录'})

        exist_records = UserFavorite.objects.filter(user=request.user,
                                                    fav_id=int(fav_id),
                                                    fav_type=int(fav_type))
        if exist_records:
            exist_records.delete()
            return JsonResponse({'status': 'success', 'msg': '收藏'})
        else:
            user_fav = UserFavorite()
            if int(fav_id) > 0 and int(fav_type) > 0:
                user_fav.fav_id = int(fav_id)
                user_fav.fav_type = int(fav_type)
                user_fav.user = request.user
                user_fav.save()
                #   return HttpResponse("{'status':'scucess'}", content_type='application/json')
                return JsonResponse({'status': 'success', 'msg': '已收藏'})
            else:
                return JsonResponse({'status': 'fail', 'msg': '收藏失败'})
Пример #26
0
 def post(self, request):
     if request.user.is_authenticated():
         fav_id = request.POST.get("fav_id", -1)
         fav_type = request.POST.get("fav_type", -1)
         try:
             is_record = UserFavorite.objects.get(user=request.user,
                                                  fav_id=int(fav_id),
                                                  fav_type=int(fav_type))
             print int(fav_id)
         except (UserFavorite.DoesNotExist,
                 UserFavorite.MultipleObjectsReturned):
             is_record = ''
         if is_record:
             is_record.delete()
             return HttpResponse(json.dumps({
                 'status': 'fail',
                 'msg': "收藏"
             }),
                                 content_type='application/json')
         else:
             user_tmep = UserFavorite()
             user_tmep.user = request.user
             user_tmep.fav_type = int(fav_type)
             user_tmep.fav_id = int(fav_id)
             user_tmep.save()
             return HttpResponse(json.dumps({
                 'status': 'success',
                 'msg': '已收藏'
             }),
                                 content_type='application/json')
     else:
         return HttpResponse(json.dumps({
             'status': 'fail',
             'msg': '用户未登录'
         }),
                             content_type='application/json')
Пример #27
0
    def post(self, request):
        fav_id = request.POST.get('fav_id', 0)         # 防止后边int(fav_id)时出错
        fav_type = request.POST.get('fav_type', 0)     # 防止int(fav_type)出错

        if not request.user.is_authenticated:
            # 未登录时返回json提示未登录,跳转到登录页面是在ajax中做的
            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()
            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.user = request.user
                user_fav.fav_id = int(fav_id)
                user_fav.fav_type = int(fav_type)
                user_fav.save()
                return HttpResponse('{"status":"success", "msg":"已收藏"}', content_type='application/json')
            else:
                return HttpResponse('{"status":"fail", "msg":"收藏出错"}', content_type='application/json')
Пример #28
0
    def post(self, request):
        pro_id = request.POST.get('pro_id', 0)

        product = Product.objects.get(id=int(pro_id))

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

        exist_records = UserFavorite.objects.filter(user=request.user,
                                                    product=product)
        if exist_records:
            # 记录已存在,表示取消收藏
            exist_records.delete()
            '''
			course = Course.objects.get(id=int(fav_id))
			course.fav_nums -= 1
			if course.fav_nums < 0:
				course.fav_nums = 0
			course.save()
			'''

            return HttpResponse('{"status":"success", "msg":"收藏"}',
                                content_type='application/json')
        else:
            user_fav = UserFavorite()
            user_fav.user = request.user
            user_fav.product = product
            user_fav.save()
            '''
			course = Course.objects.get(id=int(fav_id))
			course.fav_nums += 1
			'''

            return HttpResponse('{"status":"success", "msg":"已收藏"}',
                                content_type='application/json')
Пример #29
0
    def post(self, request):
        fav_id = request.POST.get('fav_id', '')
        fav_type = request.POST.get('fav_type', 0)

        if not request.user.is_authenticated():
            data = {'status': 'fail', 'msg': '用户未登录'}
            return HttpResponse(json.dumps(data),
                                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 = 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()

            data = {'status': 'success', 'msg': '收藏'}
            return HttpResponse(json.dumps(data),
                                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()

                data = {'status': 'success', 'msg': '已收藏'}
                return HttpResponse(json.dumps(data),
                                    content_type="application/json")
            else:
                data = {'status': 'fail', 'msg': '收藏出错'}
                return HttpResponse(json.dumps(data),
                                    content_type="application/json")
Пример #30
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 = Course.objects.get(id=int(fav_id))
                course_org -= 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 = Course.objects.get(id=int(fav_id))
                    course_org += 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')
    def post(self, request):
        fav_id = int(request.POST.get('fav_id', 0))
        fav_type = int(request.POST.get('fav_type', 0))
        res = dict()
        if not request.user.is_authenticated():
            res['status'] = 'fail'
            res['msg'] = '用户未登录'
            return HttpResponse(json.dumps(res),
                                content_type='application/json')
        # 查询收藏记录
        exist_records = UserFavorite.objects.filter(user=request.user,
                                                    fav_id=fav_id,
                                                    fav_type=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()

            # self.set_fav_nums(fav_type, fav_id, -1)
            res['status'] = 'success'
            res['msg'] = '收藏'
        else:
            user_fav = UserFavorite()
            if fav_id and fav_type:
                user_fav.user = request.user
                user_fav.fav_id = fav_id
                user_fav.fav_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()

                # self.set_fav_nums(fav_type, fav_id, 1)
                res['status'] = 'success'
                res['msg'] = '已收藏'
            else:
                res['status'] = 'fail'
                res['msg'] = '收藏出错'
        return HttpResponse(json.dumps(res), content_type='application/json')
Пример #32
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': 'User does not login'
                },
                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":"favorite"}',
                                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()
                return HttpResponse(
                    '{"status":"success", "msg":"Already added"}',
                    content_type='application/json')
            else:
                return HttpResponse('{"status":"success", "msg":"Error"}',
                                    content_type='application/json')
Пример #33
0
    def post(self, request):
        # 表明你收藏的不管是课程,讲师,还是机构。他们的id
        # 默认值取0是因为空串转int报错
        # 取到你收藏的课程id,从前台提交的ajax请求中取
        id = request.POST.get('fav_id', 0)
        # 取到你收藏的课程机构类别,从前台提交的ajax请求中取
        type = request.POST.get('fav_type', 0)

        # 收藏与已收藏取消收藏
        # 判断用户是否登录:即使没登录会有一个匿名的user
        if not request.user.is_authenticated:
            # 未登录时返回json提示未登录,跳转到登录页面是在ajax中做的
            return HttpResponse('{"status":"fail", "msg":"用户未登录"}',
                                content_type='application/json')
        # operation models 里的UserFavorite 中有fav_id fav_type
        exist_records = UserFavorite.objects.filter(user=request.user,
                                                    fav_id=int(id),
                                                    fav_type=int(type))
        if exist_records:
            # 如果记录已经存在, 则表示用户取消收藏
            exist_records.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()
            # 过滤掉未取到fav_id type的默认情况
            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')
Пример #34
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:
                school = School.objects.get(id=int(fav_id))
                school.fav_nums -= 1
                if school.fav_nums < 0:
                    school.fav_nums = 0
                school.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 and request.user.is_student:
                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:
                    school = School.objects.get(id=int(fav_id))
                    school.fav_nums += 1
                    school.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')
Пример #35
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():
        # 这个问题困扰我两天,只要单步运行到166行,就报服务器500错误,经过多次努力,只需要把括号去掉就能正常运行了
        # 同时在org_base.html页面175行加入  window.location.reload();  就能正常刷新页面了

        if not request.user.is_authenticated:
            # 判断用户登录状态
            return HttpResponse('{"status":"fail","msg":"用户未登录"}',
                                content_type='application/json')
        else:
            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()
                    return HttpResponse('{"status":"success","msg":"已收藏"}',
                                        content_type='application/json')

                else:
                    return HttpResponse('{"status":"fail","msg":"收藏出错"}',
                                        content_type='application/json')
Пример #36
0
    def post(self, request):
        fav_id = request.POST.get('fav_id', '')
        fav_type = request.POST.get('fav_type', '')

        if not request.user.is_authenticated():
            # 判断用户状态
            name_dict = {'status': 'fail', 'msg': '用户未登录'}
            return HttpResponse(json.dumps(name_dict),
                                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:
            # if用户存在,则表示用户取消收藏
            exist_records.delete()
            name_dict = {'status': 'success', 'msg': '收藏'}
            if int(fav_type) == 1:
                course = Course.objects.get(id=int(fav_id))
                course.fav_nums -= 1
                course.save()
            elif int(fav_type) == 3:
                teacher = Teacher.objects.get(id=int(fav_id))
                teacher.fav_nums -= 1
                teacher.save()
            if int(fav_type) == 2:
                org = CourseOrg.objects.get(id=int(fav_id))
                org.fav_nums -= 1
                org.save()

            return HttpResponse(
                json.dumps(name_dict),
                content_type='application/json'
            )
        else:
            user_fav = UserFavorite()
            if int(fav_id) > 0 and int(fav_type) > 0:
                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) == 3:
                    teacher = Teacher.objects.get(id=int(fav_id))
                    teacher.fav_nums -= 1
                    if teacher.fav_nums < 0:
                        teacher.fav_nums = 0
                    teacher.save()
                if 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()
                user_fav.user = request.user
                user_fav.fav_id = int(fav_id)
                user_fav.fav_type = int(fav_type)
                user_fav.save()
                name_dict = {'status': 'success', 'msg': '已收藏'}
                return HttpResponse(json.dumps(name_dict),
                                    content_type='application/json'
                                    )
            else:
                name_dict = {'status': 'fail', 'msg': '收藏出错'}
                return HttpResponse(json.dumps(name_dict),
                                    content_type='application/json'
                                    )