예제 #1
0
    def post(self, request):
        fav_id = request.POST.get('fav_id', 0)
        fav_type = request.POST.get('fav_type', 0)
        return_data = {}
        if not request.user.is_authenticated:
            return_data['success'] = False
            return_data['msg'] = '用户未登录'
            return JsonResponse(return_data)

        exists_record = UserFavorite.objects.filter(user=request.user,
                                                    fav_id=int(fav_id),
                                                    fav_type=int(fav_type))
        if exists_record:
            exists_record.delete()
            return_data['success'] = True
            return_data['msg'] = '收藏'
            return JsonResponse(return_data)
        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_data['success'] = True
                return_data['msg'] = '已收藏'
            else:
                return_data['success'] = False
                return_data['msg'] = '未知错误'
            return JsonResponse(return_data)
예제 #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():
            #判断用户是否登陆
            result = {'status': 'fail', 'msg': '用户他妈的未登陆!'}
            return JsonResponse(result)
        #定义已存在的记录
        exist_records = UserFavorite.objects.filter(user=request.user,fav_id=int(fav_id),fav_type=int(fav_type))
        userprofile = UserProfile.objects.get(username=request.user)
        if exist_records:
            #已经存在表示用户取消收藏
            exist_records.delete()
            result = {'status': 'fail', 'msg': '收藏!'}
            return JsonResponse(result)
        else:
            user_fav=UserFavorite()
            if int(fav_id)>0 and int(fav_type)>0:
                user_fav.fav_id=fav_id
                user_fav.fav_type=fav_type
                user_fav.user_id=userprofile.id
                user_fav.save()
                result = {'status': 'success', 'msg': '收藏成功!'}
                return JsonResponse(result)
            else:
                result = {'status': 'fail', 'msg': '收藏失败!'}
                return JsonResponse(result)
예제 #3
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_num -= 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_num -= 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_num += 1
                    course_org.save()
                elif int(fav_type) == 3:
                    teacher = Teacher.objects.get(id=int(fav_id))
                    teacher.fav_num += 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):
        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')
예제 #5
0
파일: views.py 프로젝트: jdk6979/django
    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')
예제 #6
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')
예제 #7
0
파일: views.py 프로젝트: luzhisheng/mklearn
	def post(self, request):
		fav_id = request.POST.get('fav_id', 0)
		fav_type = request.POST.get('fav_type', 0)

		# 首先判断用户是否登录,这里的user是dj内置方法
		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')
예제 #8
0
    def post(self, request):
        fav_id = request.POST.get("fav_id", 0)
        if not request.user.is_authenticated:
            return HttpResponse('{"status": "fail","msg": "not login"}',
                                content_type="application/json")

        exit_records = UserFavorite.objects.filter(user=request.user,
                                                   fav_game_id=int(fav_id))
        if exit_records:
            exit_records.delete()

            return HttpResponse('{"status": "fail","msg": "cancel collect"}',
                                content_type="application/json")
        else:
            if int(fav_id) > 0:
                user_fav = UserFavorite()
                user_fav.user = request.user
                user_fav.fav_game_id = int(fav_id)
                user_fav.save()

                return HttpResponse(
                    '{"status": "success","msg": "already collect"}',
                    content_type="application/json")
            else:
                return HttpResponse(
                    '{"status": "fail","msg": "collect error"}',
                    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)
     res = dict()
     #判断用户登录操作
     if not request.user.is_authenticated():
         return HttpResponse("{'status': 'fail', 'mas': '用户未登录'}",
                             content_type='application/json')
     else:
         exist_record = UserFavorite.objects.filter(user=request,
                                                    fav_id=int(fav_id),
                                                    fav_type=fav_type)
         #判断收藏是否存在
         if exist_record:
             exist_record.delete()
             return HttpResponse("{'status': 'success', 'mas': '已取消收藏'}",
                                 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.save()
                 return HttpResponse("{'status': 'success', 'mas': '已收藏'}",
                                     content_type='application/json')
             else:
                 return HttpResponse("{'status': 'fail', 'mas': '收藏失败'}",
                                     content_type='application/json')
예제 #10
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')

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

        if exit_records:
            exit_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')
예제 #11
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:
            info = {'status': 'fail', 'msg': 'NO_LOGIN'}
            return JsonResponse(info, safe=False)

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

        if exist_record:
            exist_record.delete()
            info = {'status': 'fail', 'msg': '&hearts;&nbsp;收藏'}
            return JsonResponse(info, safe=False)
        else:
            user_fav = UserFavorite()
            if (fav_id > 0) and fav_type:
                user_fav.fav_id = fav_id
                user_fav.fav_type = fav_type
                user_fav.user = request.user
                user_fav.save()
                info = {'status': 'success', 'msg': '已收藏'}
                return JsonResponse(info, safe=False)
            else:
                info = {'status': 'fail', 'msg': '收藏失败'}
                return JsonResponse(info, safe=False)
예제 #12
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')
예제 #13
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')
예제 #14
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')
예제 #15
0
파일: views.py 프로젝트: jiangeel/MxOnline
    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')
예제 #16
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 = 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) == 2:
                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
                    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) == 2:
                    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:
                return HttpResponse('{"status":"fail", "msg":"收藏出错"}', content_type='application/json')
예제 #17
0
    def post(self, request):
        fav_id = request.POST.get('fav_id', 0)
        fav_type = request.POST.get('fav_type', 0)
        fail_collect = {'status': 'fail', 'msg': u'用户未登录'}
        success_collect = {'status': 'success', 'msg': u'已收藏'}
        not_collect = {'status': 'success', 'msg': u'收藏'}
        if not request.user.is_authenticated():
            # 判断用户登录状态
            return HttpResponse(json.dumps(fail_collect), 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:
                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(json.dumps(not_collect), 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:
                    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(json.dumps(success_collect), content_type="application/json")
            else:
                return HttpResponse(json.dumps(fail_collect), content_type="application/json")
예제 #18
0
def add_fan(request):
    '''用户收藏'''
    if request.method == 'POST':
        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_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 = Couse.objects.get(id=int(fav_id))
                course.fans_num -= 1
                if course.fans_num < 0:
                    course.fans_num = 0
                course.save()
            elif int(fav_type) == 2:
                course_org = CourseOrg.objects.get(id=int(fav_id))
                course_org.fans_num -= 1
                if course_org.fans_num < 0:
                    course_org.fans_num = 0
                course_org.save()
            elif int(fav_type) == 3:
                techer = Techer.objects.get(id=int(fav_id))
                techer.fans_num -= 1
                if techer.fans_num < 0:
                    techer.fans_num = 0
                techer.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 = Couse.objects.get(id=int(fav_id))
                    course.fans_num += 1
                    course.save()
                elif int(fav_type) == 2:
                    course_org = CourseOrg.objects.get(id=int(fav_id))
                    course_org.fans_num += 1

                    course_org.save()
                elif int(fav_type) == 3:
                    techer = Techer.objects.get(id=int(fav_id))
                    techer.fans_num += 1

                    techer.save()
                return HttpResponse('{"status":"success","msg":"已收藏"}',
                                    content_type='application/json')
            else:
                return HttpResponse('{"status":"fail","msg":"收藏出错"}',
                                    content_type='application/json')
예제 #19
0
파일: views.py 프로젝트: 0Monster0/MxOnline
    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()
            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:
                course_org = CourseOrg.objects.get(id=fav_id)
                course_org.fav_nums -= 1
                if course_org.fav_nums < 0:
                    course_org.fav_nums = 0
                course_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.user = request.user
                user_fav.fav_id = fav_id
                user_fav.fav_type = fav_type
                user_fav.save()
                if fav_type == 1:
                    course = Course.objects.get(fav_id)
                    course.fav_nums += 1
                    course.save()
                elif fav_type == 2:
                    org = CourseOrg.objects.get(fav_id)
                    org.fav_nums += 1
                    org.save()
                elif fav_type == 3:
                    teacher = Teacher.objects.get(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')
예제 #20
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')


        exits_records = UserFavorite.objects.filter(user=request.user, fav_id=int(fav_id), fav_type=int(fav_type))
        if exits_records:
            #记录已经存在则表示用户取消收藏,也就是删除这条记录
            exits_records.delete()
            #取消收藏后 收藏数减1
            if int(fav_type) == 1:
                course = Course.objects.get(id=int(fav_id))
                course.fav_nums -= 1
                if course.fav_nums < 0: #如果收藏数小于0 则收藏数等于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()

                # 取消收藏后 收藏数加1
                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')
예제 #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():
            # Check if the user is login or not
            return HttpResponse(
                '{"status": "fail", "msg": "User is 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_num -= 1
                if course.fav_num < 0:
                    course.fav_num = 0
                course.save()
            elif int(fav_type) == 2:
                course_org = CourseOrg.objects.get(id=int(fav_id))
                course_org.fav_num -= 1
                if course_org.fav_num < 0:
                    course_org.fav_num = 0
                course_org.save()
            elif int(fav_type) == 3:
                teacher = Teacher.objects.get(id=int(fav_id))
                teacher.fav_num -= 1
                if teacher.fav_num < 0:
                    teacher.fav_num = 0
                teacher.save()
            return 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_num += 1
                    course.save()
                elif int(fav_type) == 2:
                    course_org = CourseOrg.objects.get(id=int(fav_id))
                    course_org.fav_num += 1
                    course_org.save()
                elif int(fav_type) == 3:
                    teacher = Teacher.objects.get(id=int(fav_id))
                    teacher.fav_num += 1
                    teacher.save()
                return HttpResponse('{"status": "success", "msg": "Favored"}',
                                    content_type='application/json')
            else:
                return HttpResponse(
                    '{"status": "fail", "msg": "Fail to Favorite"}',
                    content_type='application/json')
예제 #22
0
파일: views.py 프로젝트: TraceUL/MxCouse
    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():
            """此处为一个匿名类,django内置的一种方法,此user与征程的user有像是的用法
            所以此处调用user.is_authenticated()方法,后面带括号,
            """
            return HttpResponse('{"status":"fail","mas":"用户没登陆"}',content_type="application/json")
        exit_recods= UserFavorite.objects.filter(user = request.user,fav_id = int(fav_id),fav_type= int(fav_type))
        if exit_recods:
            """记录已经存在,则表示要取消收藏"""
            exit_recods.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")
예제 #23
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')
예제 #24
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:
            fail_dict = {'status': 'fail', 'msg': '用户未登录'}
            return HttpResponse(json.dumps(fail_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:
            exist_records.delete()
            if int(fav_type) == 1:
                course = Course.objects.get(id=int(fav_id))
                course.fav_num -= 1
                if course.fav_num<0:
                    course.fav_num=0
                course.save()
            elif int(fav_type) == 2:
                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()
            suc_dict = {'status': 'success', 'msg': '收藏'}
            return HttpResponse(json.dumps(suc_dict), 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_num += 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()
                suc_dict = {'status': 'success', 'msg': '已收藏'}
                return HttpResponse(json.dumps(suc_dict), content_type="application/json")

            else:
                fail_dict = {'status': 'fail', 'msg': '收藏出错'}
                return HttpResponse(json.dumps(fail_dict), content_type="application/json")
예제 #25
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','msg':'用户未登录'}",
                                content_type='application/json')

        exist_record = UserFavorite.objects.filter(user=request.user,
                                                   fav_id=int(id),
                                                   fav_type=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')
예제 #26
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():
         # 用户未登录,则无法进行收藏与取消收藏操作,此时request中依然有user变量,只不过是未登录的系统自带user变量
         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))
     # 如果用户已登录,则结合用户,数据id,收藏类型三者联合查询用户收藏表中是否存在满足条件的数据
     if exist_record:
         # 如果存在,则表示该用户想要取消收藏,将UserFavor表中相应数据删除;不存在,则代表收藏操作
         exist_record.delete()
         if fav_type == '1':
             course = Course.objects.get(id=int(fav_id))
             course.fav_nums -= 1
             course.save()
         elif fav_type == '2':
             course_org = CourseOrg.objects.get(id=int(fav_id))
             course_org.fav_nums -= 1
             course_org.save()
         elif fav_type == '3':
             teacher = Teacher.objects.get(id=int(fav_id))
             teacher.fav_nums -= 1
             # 收藏数-1
             teacher.save()
         return HttpResponse('{"status": "success", "msg": "收藏"}',
                             content_type='application/json')
     else:
         # 未查询到记录也可能是fav_id或fav_type为零
         if int(fav_id) and int(fav_type):
             user_fav = UserFavorite()
             user_fav.user = request.user
             user_fav.fav_id = int(fav_id)
             user_fav.fav_type = int(fav_type)
             # 从表单获取的fav_id及fav_type都为字符串类型,而数据库中相应字段数据类型为整形,故需做类型转换
             user_fav.save()
             # 保存操作先实例化一个数据对象,然后对其字段进行赋值
             if fav_type == '1':
                 course = Course.objects.get(id=int(fav_id))
                 course.fav_nums += 1
                 course.save()
             elif fav_type == '2':
                 course_org = CourseOrg.objects.get(id=int(fav_id))
                 course_org.fav_nums += 1
                 course_org.save()
             elif fav_type == '3':
                 teacher = Teacher.objects.get(id=int(fav_id))
                 teacher.fav_nums += 1
                 # 收藏数+1
                 teacher.save()
             return HttpResponse('{"status": "success", "msg": "已收藏"}',
                                 content_type='application/json')
         else:
             return HttpResponse('{"status": "fail", "msg": "收藏失败"}',
                                 content_type='application/json')
예제 #27
0
파일: views.py 프로젝트: dsh2046/OLE_app
    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':'NotLogin'}")

        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:
                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':'like'}")
        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':'dislike'}")
            else:
                return HttpResponse("{'status':'fail', 'msg':'like_error'}")
예제 #28
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', '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()

            # 完善用户各个取消收藏数的功能
            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 JsonResponse({'status': 'success', 'msg': '收藏'})
        else:
            user_fav = UserFavorite()
            if int(fav_type) > 0 and int(fav_id) > 0:
                user_fav.user = request.user
                user_fav.fav_type = int(fav_type)
                user_fav.fav_id = int(fav_id)
                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 JsonResponse({'status': 'success', 'msg': '已收藏'})
            else:
                return JsonResponse({'status': 'fail', 'msg': '收藏出错'})
예제 #29
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(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()
            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()
            if(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()
            if(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(dumps({'status':'success','msg':'收藏'}), content_type="application/json")
        else:
            if int(fav_id) > 0 and int(fav_type) > 0:
                user_fav = UserFavorite()
                user_fav.user = request.user
                user_fav.fav_id = 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()
                if (int(fav_type) == 2):
                    course_org = CourseOrg.objects.get(id=int(fav_id))
                    course_org.fav_nums += 1
                    course_org.save()
                if (int(fav_type) == 3):
                    teacher = Teacher.objects.get(id=int(fav_id))
                    teacher.fav_nums += 1
                    teacher.save()

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

            else:
                # return HttpResponse({'status': 'fail', 'msg': '收藏出错'}, content_type="application/json")
                return HttpResponse(dumps({'status':'fail','msg':'收藏出错'}), content_type="application/json")
예제 #30
0
    def post(self, request):
        # 如果用户未登录,则提示用户未登录,ajax
        if not request.user.is_authenticated:
            return HttpResponse({'status': 'fail', 'msg': '用户未登录'}, content_type='application/json')

        # 获取收藏id即类型,如果数据库中已存在,那么取消收藏,并且收藏数-1
        id = request.POST.get('fav_id', None)
        type = request.POST.get('fav_type', None)
        if id and type:
            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:
                    org = Organization.objects.get(id=int(id))
                    org.fav_nums -= 1
                    if org.fav_nums < 0:
                        org.fav_nums = 0
                    org.save()
                elif int(type) == 2:
                    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) == 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()
                user_fav.user = request.user
                user_fav.fav_id = int(id)
                user_fav.fav_type = int(type)
                user_fav.save()

                # 收藏数+1
                if int(type) == 1:
                    org = Organization.objects.get(id=int(id))
                    org.fav_nums += 1
                    org.save()
                elif int(type) == 2:
                    course = Course.objects.get(id=int(id))
                    course.fav_nums += 1
                    course.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')
예제 #31
0
파일: views.py 프로젝트: chenzwcc/hunda
    def post(self, request):
        # 收藏的id
        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_recodes = UserFavorite.objects.filter(user=request.user,
                                                    fav_id=int(fav_id),
                                                    fav_type=int(fav_type))
        if exist_recodes:
            # 取消收藏
            exist_recodes.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()
            elif int(fav_type) == 2:
                author = Author.objects.get(id=int(fav_id))
                author.fav_nums -= 1
                if author.fav_nums < 0:
                    author.fav_nums = 0
                author.save()
            return HttpResponse('{"status":"success","msg":"收藏"}',
                                content_type='application/json')
        else:
            if int(fav_id) > 0 and int(fav_type) > 0:
                user_fav = UserFavorite()
                user_fav.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:
                    article = Article.objects.get(id=int(fav_id))
                    article.fav_nums += 1
                    article.save()
                elif int(fav_type) == 2:
                    author = Author.objects.get(id=int(fav_id))
                    author.fav_nums += 1
                    author.save()

                return HttpResponse('{"status":"success","msg":"已收藏"}',
                                    content_type='application/json')
            else:
                return HttpResponse('{"status":"fail","msg":"收藏失败"}',
                                    content_type='application/json')
예제 #32
0
    def post(self, request):
        fav_id = int(request.POST.get('fav_id', 0))
        fav_type = int(request.POST.get('fav_type', 0))

        # 用于记录用户操作 获取用户收藏的对象name(课程名称、机构名称或讲师名字)
        fav_types = {
            1: (Course, '课程'),
            2: (CourseOrg, '课程机构'),
            3: (Teacher, '讲师')
        }
        # obj指代一种model obj, 如Course、CourseOrg或Teacher
        obj = fav_types[fav_type][0].objects.get(id=fav_id)
        obj_name = obj.name
        obj_type = fav_types[fav_type][1]

        # 用户必须为登录状态
        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()
            # 记录用户操作
            request.user.log('取消了收藏({type}): {name}'.format(type=obj_type,
                                                            name=obj_name))

            # 收藏数 -1   判断>0是开发调试过程需要 亦可保证安全
            if obj.fav_nums > 0:
                obj.fav_nums -= 1
                obj.save()
            return HttpResponse('{"status": "success", "msg": "收藏"}',
                                content_type='application/json')
        else:
            if fav_id > 0 and fav_type > 0:
                # 添加用户收藏
                user_fav = UserFavorite(user=request.user,
                                        fav_id=fav_id,
                                        fav_type=fav_type)
                user_fav.save()
                # 收藏数 +1
                obj.fav_nums += 1
                obj.save()

                # 记录用户操作
                request.user.log('收藏了{type}: {name}'.format(type=obj_type,
                                                            name=obj_name))
                return HttpResponse('{"status": "success", "msg": "已收藏"}',
                                    content_type='application/json')
            else:
                return HttpResponse('{"status": "fail", "msg": "收藏失败"}',
                                    content_type='application/json')
예제 #33
0
    def post(self, request):
        fav_type = request.POST.get('fav_type', '')
        fav_id = request.POST.get('fav_id', '')
        object = ''  # 初始化虚拟对象
        if int(fav_type) == 1:  # 判断传入类型是否是Course
            object = Course.objects.get(id=fav_id)
        elif int(fav_type) == 3:  # 判断传入类型是否是 CourseOrg
            object = CourseOrg.objects.get(id=fav_id)
        else:  # 判断传入类型是否是 Teacher
            object = Teacher.objects.get(id=fav_id)

        if request.user.is_authenticated():  # 判断当前发出请求的用户是否登录

            # 必须是当初发起收藏的用户才能够取消收藏,别的用户不能取消
            user_favors = UserFavorite.objects.filter(favor_id=int(fav_id),
                                                      favor_type=int(fav_type),
                                                      user_id=request.user)
            # 判断数据库是否有相匹配的数据
            if user_favors:  # 取出UserFavorite实例化对象,进行取消收藏操作,并且修改相应的model的has_favor属性;
                user_favor = UserFavorite.objects.get(favor_id=int(fav_id),
                                                      favor_type=int(fav_type),
                                                      user_id=request.user)
                user_favor.delete()
                object.favor_num -= 1
                if object.favor_num - 1 < 0:
                    object.favor_num = 0
                object.has_favor = False
                object.save()

                return HttpResponse(json.dumps({
                    'status': 'success',
                    'msg': '收藏'
                }),
                                    content_type='application/json')
            else:  # 收藏
                user_favor = UserFavorite()
                user_favor.favor_type = fav_type
                user_favor.favor_id = fav_id
                user_favor.user = request.user
                user_favor.save()
                object.favor_num += 1
                object.favor_user_id = request.user.id
                object.save()
                return HttpResponse(json.dumps({
                    'status': 'success',
                    'msg': '已收藏'
                }),
                                    content_type='application/json')

        else:
            return HttpResponse(json.dumps({
                'status': 'failed',
                'msg': '用户未登录'
            }),
                                content_type='application/json')
예제 #34
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')
예제 #35
0
파일: views.py 프로젝트: songszw/song
    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')
예제 #36
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')