def post(self, request): result = {"code": 10000, "message": "", "status": ""} id = request.session.get("id") fav_id = request.POST.get("fav_id") fav_type = request.POST.get("fav_type") if fav_id and fav_type: if id: fav_obj = UserFavorite.objects.filter(user_id=int(id), fav_type=fav_type, fav_id=fav_id).first() if fav_obj: fav_obj.delete() result["message"] = "取消收藏成功" result["status"] = "0" result["code"] = 10003 else: fav_obj = UserFavorite() fav_obj.fav_id = int(fav_id) fav_obj.fav_type = fav_type fav_obj.user_id = int(id) fav_obj.save() result["message"] = "收藏成功" result["code"] = 10000 result["status"] = "1" else: result["message"] = "请您先进行登录才能收藏" result["code"] = 10002 result["status"] = "2" else: result["message"] = "无法获取到数据" result["code"] = 10004 result["status"] = "3" return JsonResponse(result)
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)
def post(self, request): post = request.POST fav_id = post.get('fav_id', '0') fav_type = post.get('fav_type', '0') if not request.user.is_authenticated(): return HttpResponse('{"status":"fail", "msg":"用户未登录"}', content_type='application/json') exit_record = UserFavorite.objects.filter(user=request.user, fav_id=int(fav_id), fav_type=int(fav_type)) if exit_record: #记录存在则取消收藏 exit_record.delete() if fav_type == '1': course = Course.objects.get(id=fav_id) course.fav_num -= 1 course.save() if fav_type == '2': org = CourseOrg.objects.get(id=fav_id) org.fav_num -= 1 org.save() if fav_type == '3': teacher = Teacher.objects.get(id=fav_id) teacher.fav_num -= 1 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.fav_id = int(fav_id) user_fav.fav_type = int(fav_type) user_fav.user_id = request.user.id user_fav.save() if fav_type == '1': course = Course.objects.get(id=fav_id) course.fav_num += 1 course.save() if fav_type == '2': org = CourseOrg.objects.get(id=fav_id) org.fav_num += 1 org.save() if fav_type == '3': teacher = Teacher.objects.get(id=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')
def post(self, request): user_id = request.POST.get('user_id', 0) food_id = request.POST.get('food_id', 0) if not request.user.is_authenticated(): # 判断用户登陆状态 return HttpResponse('{"status":"fail", "msg":"用户未登录"}', content_type="application/json") exist_records = UserFavorite.objects.filter(user_id=user_id, food_id=food_id) if exist_records: # 如果记录存在,则表示用户取消收藏 exist_records.delete() # 取消收藏 收藏数减一 fav_food = Food.objects.get(id=food_id) if fav_food.fav_nums > 0: fav_food.fav_nums -= 1 fav_food.save() return HttpResponse('{"status":"success", "msg":"收藏"}', content_type="application/json") else: user_fav = UserFavorite() if int(user_id) > 0 and int(food_id) > 0: user_fav.user_id = int(user_id) user_fav.food_id = int(food_id) user_fav.save() # 添加收藏 收藏数加一 fav_food = Food.objects.get(id=food_id) if fav_food.fav_nums >= 0: fav_food.fav_nums += 1 fav_food.save() return HttpResponse('{"status":"success", "msg":"已收藏"}', content_type="application/json") else: return HttpResponse('{"status":"fail", "msg":"收藏出错"}', content_type="application/json")
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(): response_data = dict() response_data['status'] = 'fail' response_data['msg'] = 'user not log in' print 'user not log in' return HttpResponse(json.dumps(response_data), content_type='application/json') else: user_id = request.user.id exist_record = UserFavorite.objects.filter(user=user_id, fav_id=fav_id, fav_type=fav_type) if exist_record: exist_record.delete() if fav_type == 1: course = Course.objects.get(id=fav_id) course.like_nums -= 1 if course.like_nums < 0: course.like_nums = 0 course.save() if fav_type == 2: org = Organizition.objects.get(id=fav_id) org.like_nums -= 1 if org.like_nums < 0: org.like_nums = 0 org.save() if fav_type == 3: teacher = Teacher.objects.get(id=fav_id) teacher.like_nums -= 1 if teacher.like_nums < 0: teacher.like_nums = 0 teacher.save() response_data = dict() response_data['status'] = 'success' response_data['msg'] = 'save' return HttpResponse(json.dumps(response_data), content_type='application/json') else: user_fav = UserFavorite() if fav_id > 0 and fav_type > 0: user_fav.user_id = user_id user_fav.fav_id = fav_id user_fav.fav_type = fav_type user_fav.save() if fav_type == 1: course = Course.objects.get(id=fav_id) course.like_nums += 1 course.save() if fav_type == 2: org = Organizition.objects.get(id=fav_id) org.like_nums += 1 org.save() if fav_type == 3: teacher = Teacher.objects.get(id=fav_id) teacher.like_nums += 1 teacher.save() response_data = dict() response_data['status'] = 'success' response_data['msg'] = 'saved' return HttpResponse(json.dumps(response_data), content_type='application/json') else: response_data = dict() response_data['status'] = 'fail' response_data['msg'] = 'save error' return HttpResponse(json.dumps(response_data), content_type='application/json')
def post(self, request): org_id = request.POST.get('org_id', '') #收藏编号 fav_id = request.POST.get('fav_id', '') #收藏类型 fav_type = request.POST.get('fav_type', '') 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)==3: author = Author.objects.get(id=int(fav_id)) author.fav_nums -= 1 if author.fav_nums < 0: author.fav_nums = 0 author.save() author_id = org_id return redirect('org:author', author_id) elif int(fav_type)==2: org=Org.objects.get(id=int(fav_id)) org.fav_nums-=1 if org.fav_nums<0: org.fav_nums=0 org.save() return redirect('org:detail', org_id) elif int(fav_type)==1: blog=Blogs.objects.get(id=int(fav_id)) blog.fav_nums-=1 if blog.fav_nums<0: blog.fav_nums=0 blog.save() blog_id=org_id return redirect('blogs:blog_detail', blog_id) else: user_fav = UserFavorite() if int(fav_id) > 0 and int(fav_type) > 0: user_fav.fav_id = int(fav_id) user_fav.user_id=request.user.id user_fav.fav_type=int(fav_type) user_fav.save() if int(fav_type) == 3: author = Author.objects.get(id=int(fav_id)) author.fav_nums += 1 author.save() author_id = org_id return redirect('org:author', author_id) elif int(fav_type) == 2: org = Org.objects.get(id=int(fav_id)) org.fav_nums += 1 org.save() return redirect('org:detail',org_id) elif int(fav_type) == 1: blog = Blogs.objects.get(id=int(fav_id)) blog.fav_nums += 1 blog.save() blog_id = org_id return redirect('blogs:blog_detail', blog_id) else: if int(fav_type) == 3: return render(request, 'author.html') elif int(fav_type) == 2: return render(request, 'org_detail.html') elif int(fav_type) == 1: return render(request, 'blog_detail.html')