def post(self, request, *args, **kwargs): # 如果未登录,封装数据告诉ajax,ajax会返回登录页面 # status和msg,一定要和前端的ajax一致,否则ajax处理不了 if not request.user.is_authenticated: return JsonResponse({ "status": "fail", "msg": "用户未登录", }) # 拿到数据给modelForm验证 user_fav_form = UserFavForm(request.POST) # 字段检验通过 if user_fav_form.is_valid(): fav_id = user_fav_form.cleaned_data['fav_id'] fav_type = user_fav_form.cleaned_data['fav_type'] # 是否已经收藏 existed_records = UserFavorite.objects.filter(user=request.user, fav_id=fav_id, fav_type=fav_type) # 如果已经收藏,删除当前收藏记录,其对应的收藏数量-1 if existed_records: existed_records.delete() if fav_type == 1: course = Courses.objects.get(id=fav_id) course.fav_nums -= 1 course.save() elif fav_type == 2: course_org = CourseOrg.objects.get(id=fav_id) course_org.fav_nums -= 1 course_org.save() elif fav_type == 3: teacher = Teacher.objects.get(id=fav_id) teacher.fav_nums -= 1 teacher.save() return JsonResponse({ "status": "success", "msg": "收藏", }) # 如果没有收藏,自己新建一个收藏对象, else: user_fav = UserFavorite() user_fav.fav_id = fav_id user_fav.fav_type = fav_type # 这一步非常重要,user作为外键,是必填字段,这里只能手动添加 user_fav.user = request.user user_fav.save() return JsonResponse({ "status": "success", "msg": "已收藏", }) # 字段检验不通过 else: return JsonResponse({ "status": "fail", "msg": "参数格式错误", })
def post(self, request, *args, **kwargs): if not request.user.is_authenticated: return JsonResponse({'status': 'fail', 'msg': '用户未登录'}) user_fav_form = UserFavForm(request.POST) if user_fav_form.is_valid(): fav_id = user_fav_form.cleaned_data['fav_id'] fav_type = user_fav_form.cleaned_data['fav_type'] existed_records = UserFavorite.objects.filter(user=request.user, fav_id=fav_id, fav_type=fav_type) if existed_records: existed_records.delete() if fav_type == 1: course = Course.objects.get(id=fav_id) course.fav_nums -= 1 course.save() elif fav_type == 2: course_org = CourseOrg.objects.get(id=fav_id) course_org.fav_nums -= 1 course_org.save() elif fav_type == 3: teacher = Teacher.objects.get(id=fav_id) teacher.fav_nums -= 1 teacher.save() return JsonResponse({'status': 'success', 'msg': '收藏'}) else: user_fav = UserFavorite() user_fav.user = request.user user_fav.fav_id = fav_id user_fav.fav_type = fav_type user_fav.save() return JsonResponse({'status': 'success', 'msg': '已收藏'}) else: return JsonResponse({'status': 'fail', 'msg': '参数错误'})
def post(self, request, *args, **kwargs): """ 用户收藏,取消收藏 """ # 如果用户未登录,就给前端返回对应的js数据 # is_authenticated if not request.user.is_authenticated: return JsonResponse({"status": "fail", "msg": "用户未登录"}) # 先表单验证 user_fav_form = UserFavForm(request.POST) if user_fav_form.is_valid(): fav_id = user_fav_form.cleaned_data["fav_id"] fav_type = user_fav_form.cleaned_data["fav_type"] # 判断是否已经收藏,对数据库进行查询筛选 existed_records = UserFavorite.objects.filter(user=request.user, fav_id=fav_id, fav_type=fav_type) if existed_records: # 已经收藏,本次操作是取消收藏 existed_records.delete() """ 收藏人数变化 """ if fav_type == 1: # 这是收藏课程,通过id找到 course = Course.objects.get(id=fav_id) course.fav_nums -= 1 # 取消收藏,人数减一 course.save() elif fav_type == 2: # 这是收藏课程机构,通过id找到 course_org = CourseOrg.objects.get(id=fav_id) course_org.fav_nums -= 1 # 取消收藏,人数减一 course_org.save() elif fav_type == 3: # 这是收藏讲师,通过id找到 teacher = Teacher.objects.get(id=fav_id) teacher.fav_nums -= 1 # 取消收藏,人数减一 teacher.save() # 返回数据给前端 return JsonResponse({"status": "success", "msg": "收藏"}) # 用户收藏 else: user_fav = UserFavorite() user_fav.fav_id = fav_id user_fav.fav_type = fav_type user_fav.user = request.user user_fav.save() # 返回数据给前端 return JsonResponse({"status": "success", "msg": "已收藏"}) # 表单验证失败 else: # 返回数据给前端 return JsonResponse({"status": "fail", "msg": "参数错误"})
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')
def post(self, request, *args, **kwargs): """ 用户收藏,取消收藏 """ # 先判断用户是否登录 if not request.user.is_authenticated: return JsonResponse({"status": "fail", "msg": "用户未登录"}) user_fav_form = UserFavForm(request.POST) if user_fav_form.is_valid(): fav_id = user_fav_form.cleaned_data["fav_id"] fav_type = user_fav_form.cleaned_data["fav_type"] if (fav_type == 2) & (int(request.user.id) == int(fav_id)): return JsonResponse({"status": "fail", "msg": "请不要收藏自己"}) if fav_type == 1: music = Music.objects.filter(id=int(fav_id)) if music: music = music.first() if music.owner == request.user: return JsonResponse({ "status": "fail", "msg": "请不要收藏自己的作品" }) # 是否已经收藏 existed_records = UserFavorite.objects.filter(user=request.user, fav_id=fav_id, fav_type=fav_type) if existed_records: existed_records.delete() if fav_type == 1: music = Music.objects.get(id=fav_id) music.fav_nums = F('fav_nums') - 1 music.save() elif fav_type == 2: user = UserProfile.objects.get(id=fav_id) user.fav_nums = F('fav_nums') - 1 user.save() return JsonResponse({"status": "success", "msg": "收藏"}) else: user_fav = UserFavorite() user_fav.fav_id = fav_id user_fav.fav_type = fav_type user_fav.user = request.user user_fav.save() if fav_type == 1: music = Music.objects.get(id=fav_id) music.fav_nums = F('fav_nums') + 1 music.save() elif fav_type == 2: user = UserProfile.objects.get(id=fav_id) user.fav_nums = F('fav_nums') + 1 user.save() return JsonResponse({"status": "success", "msg": "已收藏"}) else: return JsonResponse({"status": "fail", "msg": "参数错误"})
def post(self, request, *args, **kwargs): """ 用户收藏,取消收藏 """ #如果用户未登陆 if not request.user.is_authenticated: #把这些数据返回给前端 return JsonResponse({"status": "fail", "msg": "用户未登录"}) user_fav_form = UserFavForm(request.POST) if user_fav_form.is_valid(): fav_id = user_fav_form.cleaned_data["fav_id"] #前端返回的用户收藏ID fav_type = user_fav_form.cleaned_data["fav_type"] #是否已经收藏 existed_records = UserFavorite.objects.filter(user=request.user, fav_id=fav_id, fav_type=fav_type) #已经收藏则取消收藏 if existed_records: existed_records.delete() #收藏分三类 if fav_type == 1: course = Course.objects.get(id=fav_id) course.fav_nums -= 1 course.save() elif fav_type == 2: course_org = CourseOrg.objects.get(id=fav_id) course_org.fav_nums -= 1 course_org.save() elif fav_type == 3: teacher = Teacher.objects.get(id=fav_id) teacher.fav_nums -= 1 teacher.save() return JsonResponse({ "status": "success", "msg": "收藏", }) #未收藏则保存该收藏 else: user_fav = UserFavorite() user_fav.fav_id = fav_id user_fav.fav_type = fav_type user_fav.user = request.user user_fav.save() return JsonResponse({ "status": "success", "msg": "已收藏", }) else: return JsonResponse({"status": "fail", "msg": "参数错误"})
def post(self, request, *args, **kwargs): ''' 用户收藏,用户取消收藏 ''' # request.user.is_authenticated:用来判断用户是否登陆 if not request.user.is_authenticated: return JsonResponse({ "status": "fail", "msg": "用户未登录", }) user_fav_form = UserFavForm(request.POST) if user_fav_form.is_valid(): fav_id = user_fav_form.cleaned_data['fav_id'] fav_type = user_fav_form.cleaned_data['fav_type'] # 该数据是否收藏过 existed_records = UserFavorite.objects.filter(user=request.user, fav_id=fav_id, fav_type=fav_type) if existed_records: existed_records.delete() if fav_type == 1: course = Course.objects.get(id=fav_id) course.fav_nums -= 1 course.save() elif fav_type == 2: course_org = CourseOrg.objects.get(id=fav_id) course_org.fav_nums -= 1 course_org.save() elif fav_type == 3: teacher = Teacher.objects.get(id=fav_id) teacher.fav_nums -= 1 teacher.save() return JsonResponse({ "status": "success", "msg": "收藏", }) else: user_fav = UserFavorite() user_fav.fav_id = fav_id user_fav.fav_type = fav_type user_fav.user = request.user user_fav.save() return JsonResponse({ "status": "success", "msg": "已收藏", }) else: return JsonResponse({ "status": "fail", "msg": "参数错误", })
def addFav(request): if request.method == "GET": resp = {'code': 200, 'msg': '操作成功', 'data': {}} token = request.GET.get('token') # 用户token key = request.GET.get("key") # 课程id isCollection = request.GET.get("isCollection") # 是否已经收藏 openid = deCryption(token) ''' 如果isCollection 是true 说明已经收藏就取消收藏 如果isCollection 是false 说明没有收藏就收藏 ''' if isCollection == 'true': # 通过openid 查找到用户 user = UserProfile.objects.filter(openid=openid).first() # 通过user 查找用户的favlist favlist = UserFavorite.objects.filter(user_id=user.id) # 从收藏中删除 该课程 favlist.filter(fav_id=key).delete() # 课程收藏数量-1 course = Course.objects.get(id=key) course.fav_nums -= 1 if course.fav_nums <= 0: course.fav_nums = 0 course.save() print(course.fav_nums) # 返回isCollection 为false删除成功 resp['msg'] = "操作成功" resp['data'] = {'isCollection': False} return HttpResponse(json.dumps(resp)) else: # 通过openid 查询user 的id user = UserProfile.objects.filter(openid=openid).first() # 创建一个UserFavorite对象 userFav = UserFavorite() userFav.user = user userFav.fav_id = key userFav.fav_type = 1 userFav.save() # 课程收藏数量+1 course = Course.objects.get(id=key) course.fav_nums += 1 course.save() print(course.fav_nums) # 返回isCollection为true 添加成功 resp['msg'] = "操作成功" resp['data'] = {'isCollection': True} return HttpResponse(json.dumps(resp)) return HttpResponse(json.dumps(resp)) else: resp = {'code': 200, 'msg': '操作成功', 'data': {}} return HttpResponse(json.dumps(resp))
def post(self, request, *args, **kwarg): # ユーザーがログインできているかどうか確認 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"] # ブックマークしてたかどうか確認 exitsted_recode = UserFavorite.objects.filter(user=request.user, fav_id=fav_id, fav_type=fav_type) if exitsted_recode: exitsted_recode.delete() if fav_type == 1: course = Course.objects.get(id=fav_id) course.fav_nums -= 1 course.save() elif fav_type == 2: course = CourseOrg.objects.get(id=fav_id) course.fav_nums -= 1 course.save() elif fav_type == 3: course = Teacher.objects.get(id=fav_id) course.fav_nums -= 1 course.save() return JsonResponse({ "status": "success", "msg": "收藏", }) else: user_fav = UserFavorite() user_fav.fav_id = fav_id user_fav.fav_type = fav_type user_fav.user = request.user user_fav.save() return JsonResponse({ "status": "success", "msg": "已收藏", }) else: return JsonResponse({ "status": "fail", "msg": "参数错误", })
def post(self, request, *args, **kwargs): """ 用户收藏,取消收藏 """ #先判断用户是否登录 if not request.user.is_authenticated: return JsonResponse({ "status":"fail", "msg":"用户未登录" }) user_fav_form = UserFavForm(request.POST) if user_fav_form.is_valid(): fav_id = user_fav_form.cleaned_data["fav_id"] fav_type = user_fav_form.cleaned_data["fav_type"] #是否已经收藏 existed_records = UserFavorite.objects.filter(user=request.user, fav_id=fav_id, fav_type=fav_type) if existed_records: existed_records.delete() if fav_type == 1: course = Course.objects.get(id=fav_id) course.fav_nums -= 1 course.save() elif fav_type == 2: course_org = CourseOrg.objects.get(id=fav_id) course_org.fav_nums -= 1 course_org.save() return JsonResponse({ "status": "success", "msg": "收藏" }) else: user_fav = UserFavorite() user_fav.fav_id = fav_id user_fav.fav_type = fav_type user_fav.user = request.user user_fav.save() return JsonResponse({ "status": "success", "msg": "已收藏" }) else: return JsonResponse({ "status": "fail", "msg": "参数错误" })
def post(self, request, *args, **kwargs): # 如果用户未登录 if not request.user.is_authenticated: return JsonResponse({"status": "fail", "msg": "用户未登录"}) # 如果检验全部通过 user_fav_form = UserFavForm(request.POST) if user_fav_form.is_valid(): fav_id = user_fav_form.cleaned_data["fav_id"] fav_type = user_fav_form.cleaned_data["fav_type"] # 是否已经收藏 existed_records = UserFavorite.objects.filter(user=request.user, fav_id=fav_id, fav_type=fav_type) # 如果用户已经收藏,则取消收藏 if existed_records: # 将收藏数据从数据库中删除 existed_records.delete() """:收藏类型 1.课程 2.课程机构 3.讲师 """ if fav_type == 1: course = Course.objects.get(id=fav_id) course.fav_num -= 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: tracher = Teacher.objects.get(id=fav_id) tracher.fav_nums -= 1 tracher.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": "参数错误"})
def post(self, request, *args, **kwargs): # 用户收藏/取消收藏 # 首先判断用户是否登陆 if not request.user.is_authenticated: return JsonResponse({"status": "fail", "msg": "用户未登录"}) user_fav_form = UserFavForm(request.POST) if user_fav_form.is_valid(): fav_id = user_fav_form.cleaned_data["fav_id"] fav_type = user_fav_form.cleaned_data["fav_type"] #判断是否已经收藏 existed_records = UserFavorite.objects.filter(user=request.user, fav_id=fav_id, fav_type=fav_type) #已经收藏就删除 if existed_records: existed_records.delete() #1---course;2---teacher if fav_type == 1: course = Course.objects.get(id=fav_id) course.fav_nums += 1 course.save() elif fav_type == 2: teacher = Teacher.objects.get(id=fav_id) teacher.fav_nums += 1 teacher.save() return JsonResponse({"status": "success", "msg": "收藏"}) else: user_fav = UserFavorite() user_fav.fav_id = fav_id user_fav.fav_type = fav_type user_fav.user = request.user user_fav.save() return JsonResponse({"status": "success", "msg": "已收藏"}) else: return JsonResponse({"status": "fail", "msg": "参数错误"})
def post(self, request, *args, **kwargs): '''先判断是否登录''' if not request.user.is_authenticated: return JsonResponse({'status': 'fail', 'msg': '用户未登录'}) add_fav_form = addFavForm(request.POST) if add_fav_form.is_valid(): fav_id = request.POST.get('fav_id', 0) fav_type = request.POST.get('fav_type', 0) user = request.user #是否已收藏,收藏过就要将收藏记录删掉,收藏次数减一 exist_fav = UserFavorite.objects.filter(user=user, fav_id=fav_id, fav_type=fav_type) if exist_fav: exist_fav.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 = 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': '参数错误111111'})
def post(self, request, *args, **kwargs): """处理用户收藏,取消收藏""" # 判断用户是否登录 if not request.user.is_authenticated: return JsonResponse({ 'status': 'file', 'msg': '用户未登录' }) user_fav_form = UserFavoriteForm(request.POST) if user_fav_form.is_valid(): fav_id = int(request.POST.get('fav_id', 0)) fav_type = int(request.POST.get('fav_type', '')) # 是否已经收藏 existed_records = UserFavorite.objects.filter( user=request.user, fav_id=fav_id, fav_type=fav_type) if existed_records: existed_records.delete() if fav_type == 1: course = Course.objects.get(id=fav_id) course.fav_nums -= 1 course.save() elif fav_type == 2: course_org = CourseOrg.objects.get(id=fav_id) course_org.fav_nums -= 1 course_org.save() elif fav_type == 3: teacher = Teacher.objects.get(id=fav_id) teacher.fav_nums -= 1 teacher.save() return JsonResponse({ 'status': 'success', 'msg': '收藏' }) else: user_fav = UserFavorite() user_fav.fav_id = fav_id user_fav.user = request.user user_fav.fav_type = fav_type user_fav.save() 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: return JsonResponse({ 'status': 'fail', 'msg': '参数错误!' })
def post(self, request, *args, **kwargs): """ 取消收藏,收藏 :param request: :param args: :param kwargs: :return: """ # 判断用户是否登陆 if not request.user.is_authenticated: return JsonResponse({ "status": "fail", "msg": "用户未登陆" }) # 表单验证 fav_form = UserFavForm(request.POST) if fav_form.is_valid(): fav_id = fav_form.cleaned_data["fav_id"] fav_type = fav_form.cleaned_data["fav_type"] # 判断数据是否被收藏 existed_records = UserFavorite.objects.filter(user=request.user, fav_id=fav_id, fav_type=fav_type) # 如果已经被收藏 if existed_records: # 删除数据 existed_records.delete() if fav_type == 1: # 课程 course = Course.objects.get(id=fav_id) course.fav_num -= 1 course.save() elif fav_type == 2: # 机构 course = CourseOrg.objects.get(id=fav_id) course.fav_num -= 1 course.save() elif fav_type == 3: # 教师 course = Teacher.objects.get(id=fav_id) course.fav_num -= 1 course.save() return JsonResponse({ "status": "success", "msg": "收藏", }) else: user_fav = UserFavorite() user_fav.fav_id = fav_id user_fav.fav_type = fav_type user_fav.user = request.user user_fav.save() return JsonResponse({ "status": "success", "msg": "已收藏", }) else: # 添加数据 return JsonResponse({ "status": "fail", "msg": "参数错误", })
def post(self, request): fav_id = request.POST.get('fav_id', 0) fav_type = request.POST.get('fav_type', 2) 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')