Пример #1
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:
            # 未登录时返回json提示未登录,跳转到登录页面是在ajax中做的
            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')
Пример #2
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 JsonResponse({'status': 'fail', 'meg': u'用户未登录'})

        # 判断对应的收藏记录是否存在
        records = UserFavorite.objects.filter(user=request.user,
                                              fav_id=int(fav_id),
                                              fav_type=int(fav_type))
        # 若存在则表示取消收藏,若不存在则表示收藏
        if records:
            records.delete()
            return JsonResponse({'status': 'success', 'meg': u'收藏'})
        else:
            if int(fav_id) > 0 and int(fav_type) > 0:
                record = UserFavorite()
                record.user = request.user
                record.fav_id = fav_id
                record.fav_type = fav_type
                record.save()
                return JsonResponse({'status': 'success', 'meg': u'取消收藏'})
            else:
                return JsonResponse({'status': 'fail', 'meg': u'收藏失败'})
Пример #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:
            # 未登录时返回json提示未登录,跳转到登录页面是在ajax中做的
            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()
            return HttpResponse('{"status":"success", "msg":"收藏"}',
                                content_type='application/json')
        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":"已收藏"}',
                                    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')
     fav_type = request.POST.get('fav_type','0')
     if not request.user.is_authenticated:
         #判断用户登录状态
         resp = {'status': 'fail', 'msg': '用户未登录'}
         return HttpResponse(json.dumps(resp), 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()
         resp = {'status': 'success', 'msg': '收藏'}
         return HttpResponse(json.dumps(resp), 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()
             resp = {'status': 'success', 'msg': '已收藏'}
             return HttpResponse(json.dumps(resp), content_type='application/json')
         else:
             resp = {'status': 'fail', 'msg': '收藏出错'}
             return HttpResponse(json.dumps(resp), 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)
        fav_id = int(fav_id)
        fav_type = int(fav_type)
        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()
            if fav_type == 1:
                course = Course.objects.get(id=fav_id)
                course.fav_nums -= 1
                if course.fav_nums < 0:
                    course.fav_nums = 0
                    course.save()
            elif fav_type == 2:
                org = CourseOrg.objects.get(id=fav_id)
                org.fav_nums -= 1
                if org.fav_nums < 0:
                    org.fav_nums = 0
                    org.save()
            elif fav_type == 3:
                teacher = Teacher.objects.get(id=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 fav_id > 0 and fav_type > 0:
                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_nums += 1
                    course.save()
                elif fav_type == 2:
                    org = CourseOrg.objects.get(id=fav_id)
                    org.fav_nums += 1
                    org.save()
                elif fav_type == 3:
                    teacher = Teacher.objects.get(id=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):
        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()

                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, *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']
            print(type(fav_id))
            fav_type = user_fav_form.cleaned_data['fav_type']

            existed_records = UserFavorite.objects.filter(user=request.user, fav_id=int(fav_id), fav_type=int(fav_type))
            print(bool(existed_records), request.user.id)
            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': '参数错误'
            })
Пример #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()

                elif fav_type == 3:
                    teacher = Teacher.objects.filter(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": "error",
                "msg": "参数错误",
            })
Пример #9
0
    def post(self, request):
        """机构收藏处理"""
        # 获取用户
        user = request.user
        if not user.is_authenticated:
            # 用户未登录
            return JsonResponse({'msg': '用户未登录', 'status': 'fail'})

        # 验证数据
        ope_col_form = OpeColForm(request.POST)
        if not ope_col_form.is_valid():
            # 参数出错
            return JsonResponse({'msg': '参数出错', 'status': 'fail'})

        # 处理业务
        # 获取数据
        fav_type = ope_col_form.cleaned_data['fav_type']
        fav_id = ope_col_form.cleaned_data['fav_id']

        # 查出当前用户是否收藏过此机构
        try:
            user_fav = UserFavorite.objects.get(user=user,
                                                fav_id=fav_id,
                                                fav_type=fav_type)
            is_user_fav_col = True
        except UserFavorite.DoesNotExist:
            is_user_fav_col = False

        if is_user_fav_col:  # 收藏过此机构
            # 取消收藏此机构
            user_fav.delete()
            # 在此收藏类型中减一
            OrgUtils.utils_collect(fav_type, fav_id, 'cancel')
        else:  # 未收藏过
            user_fav = UserFavorite()
            user_fav.user = user
            user_fav.fav_id = fav_id
            user_fav.fav_type = fav_type
            user_fav.save()
            OrgUtils.utils_collect(fav_type, fav_id, 'add')

        # 返回数据
        return JsonResponse({'status': 'success', 'res': fav_type})
Пример #10
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']

            #是否已收藏
            exist_records = UserFavorite.objects.filter(user=request.user,
                                                        fav_id=fav_id,
                                                        fav_type=fav_type)
            # 如果已经收藏
            if exist_records:
                exist_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
                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.user = request.user
                user_fav.save()
                return JsonResponse({
                    'status': 'success',
                    'msg': "已收藏",
                })
        else:
            return JsonResponse({
                'status': 'fail',
                'msg': "课程错误",
            })
Пример #11
0
    def post(self, request):
        fav_id = request.POST.get('fav_id', 0)
        fav_type = request.POST.get('fav_type', 0)
        fav_id = int(fav_id)
        fav_type = int(fav_type)

        if not request.user.is_authenticated():
            return HttpResponse('{"status":"fail", "msg":"用户未登录"}',
                                content_type='application/json')
        else:
            # 查询收藏表和用户课程表是否存在记录
            exist_fav_rec = UserFavorite.objects.filter(user=request.user,
                                                        fav_id=fav_id,
                                                        fav_type=fav_type)
            exist_usercourse_rec = UserCourse.objects.filter(user=request.user,
                                                             course_id=fav_id)
            # ---删除收藏---
            if exist_fav_rec or exist_usercourse_rec:
                exist_fav_rec.delete()
                exist_usercourse_rec.delete()

                # 收藏数减1
                if fav_type == 1:
                    course = get_object_or_404(Course, id=fav_id)
                    course.fav_nums -= 1
                    if course.fav_nums < 0: course.fav_nums = 0
                    course.save()
                elif fav_type == 2:
                    org = get_object_or_404(CourseOrg, id=fav_id)
                    org.fav_nums -= 1
                    if org.fav_nums < 0: org.fav_nums = 0
                    org.save()
                elif fav_type == 3:
                    teacher = get_object_or_404(Teacher, id=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:
                # ---添加收藏---
                if fav_id > 0 and fav_type > 0:
                    user_fav = UserFavorite()
                    user_fav.user = request.user
                    user_fav.fav_id = fav_id
                    user_fav.fav_type = fav_type
                    user_fav.save()

                    # 收藏后记录消息
                    if fav_type == 1:
                        course = get_object_or_404(Course, id=fav_id)
                        UserMessage.objects.create(user=request.user.id,
                                                   message='您收藏了课程《%s》' %
                                                   course.name,
                                                   has_read=False)
                        course.fav_nums += 1  # 收藏数加1
                        course.save()
                    elif fav_type == 2:
                        org = get_object_or_404(CourseOrg, id=fav_id)
                        UserMessage.objects.create(user=request.user.id,
                                                   message='您收藏了机构“%s”' %
                                                   org.name,
                                                   has_read=False)
                        org.fav_nums += 1  # 收藏数加1
                        org.save()
                    elif fav_type == 3:
                        teacher = get_object_or_404(Teacher, id=fav_id)
                        UserMessage.objects.create(user=request.user.id,
                                                   message='您收藏了“%s”老师' %
                                                   teacher.name,
                                                   has_read=False)
                        teacher.fav_nums += 1  # 收藏数加1
                        teacher.save()

                    # 收藏课程时,保存用户课程记录(用户ID和课程ID)
                    if fav_type == 1:
                        UserCourse.objects.create(
                            user=request.user,
                            course=Course.objects.get(id=fav_id))
                    return HttpResponse('{"status":"success", "msg":"取消收藏"}',
                                        content_type='application/json')
                else:
                    return HttpResponse('{"status":"fail", "msg":"收藏出错"}',
                                        content_type='application/json')
Пример #12
0
    def post(self, request, *args, **kwargs):
        '''
        功能:用户收藏、取消收藏
        '''
        #1、判断用户是否登录、如果没有登录
        if not request.user.is_authenticated:
            #如果没登录 js会直接跳转到登录页面
            return JsonResponse({'status': 'fail', 'msg': '用户未登录'})
        #能走到这里说明已经登录了
        #实例化收藏wtfform的对象
        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说明收藏的是课程
                if fav_type == 1:
                    #查到是收藏的哪个课程、
                    course = Course.objects.get(id=fav_id)
                    #对课程的收藏数字段进行减一
                    course.fav_nums -= 1
                    course.save()

                elif fav_type == 2:
                    courseorg = CourseOrg.objects.get(id=fav_id)
                    courseorg.fav_nums -= 1
                    courseorg.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()
                #给收藏表 新增fav_id数据
                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': '已收藏',
                })
        #验证失败  回复fail
        else:
            return JsonResponse({
                'status': 'fail',
                'msg': '参数错误',
            })