def post(self, request, category_id): # 1.校验参数 try: category = GoodsCategory.objects.get(id=category_id) except Exception as e: return http.HttpResponseForbidden('商品不存在!') # 2. 判断 (当天 这个商品分类) 有没有 记录数据 today_date = datetime.now() # 将 日期格式 ----"Y-m-d" 字符串的日期 today_str = today_date.strftime('%Y-%m-%d') # 将 字符串日期------日期格式 today_date = datetime.strptime(today_str, '%Y-%m-%d') try: # visit_count = category.goodsvisitcount_set.get(date=today_date) visit_count = GoodsVisitCount.objects.get(category=category, date=today_date) except Exception as e: # 3. 如果没有 新建一条数据 visit_count = GoodsVisitCount() # 4. 累加 count += 1 visit_count.category = category visit_count.count += 1 visit_count.save() # 5. save() # 6. 返回响应对象 return http.JsonResponse({'code': RETCODE.OK, 'errmsg': 'OK'})
def post(self, request, category_id): try: #1、校验三级分类是否存在 category = GoodsCategory.objects.get(id=category_id) except Exception as e: return HttpResponseNotFound('缺少必传参数') #2、查询日期数据,根据日期判断,记录是否存在 from datetime import datetime # 将日期按照格式转换成字符串(将日期转换成制定格式) today_str = datetime.now().strftime('%Y-%m-%d') # 将字符串再转换成日期格式,方便与时间字段做对比 today_date = datetime.strptime(today_str, '%Y-%m-%d') from apps.goods.models import GoodsVisitCount try: # 3.如果有当天商品分类的数据 就累加数量 count_data = category.goodsvisitcount_set.get(date=today_date) except: # 4. 没有, 就新建之后在增加 count_data = GoodsVisitCount() try: count_data.count += 1 count_data.category = category count_data.save() except Exception as e: return HttpResponseServerError('新增失败') return http.JsonResponse({'code': RETCODE.OK, 'errmsg': 'OK'})
def post(self, request, category_id): # 1.校验 三级分类 是否存在 try: category = GoodsCategory.objects.get(id=category_id) except Exception as e: return HttpResponseNotFound('缺少必传参数') # 2.根据日期来判断 记录是否存在 2019-8-24 today_date_str = datetime.now().strftime('%Y-%m-%d') # 将日期的字符串--> date数据格式 today_date = datetime.strptime(today_date_str, '%Y-%m-%d') try: visit = GoodsVisitCount.objects.get(category=category, date=today_date) # 外键 --> 纯模型类小写_set visit = category.goodsvisitcount_set.get(date=today_date) except Exception as e: # 记录不存在 创建一条新纪录 visit = GoodsVisitCount() # 3.count += 1 try: visit.count += 1 visit.category = category visit.save() except Exception as e: return HttpResponseServerError('新增失败') # 4.返回响应对象 return JsonResponse({'code': RETCODE.OK, 'errmsg': 'OK'})
def post(self, request, category_id): """ 1.组织生成当天的时间 yyyy-mm-dd 2.保存数据 3.返回相应 """ try: category = GoodsCategory.objects.get(id=category_id) except GoodsCategory.DoesNotExist: return http.HttpResponseBadRequest('缺少必传参数') # 1.获取当天的时间 now = datetime.now() today_str = '%s-%s-%s' % (now.year, now.month, now.day) today_date = datetime.strptime(today_str, '%Y-%m-%d') # 2.保存数据 try: gvc = GoodsVisitCount.objects.get(date=today_date, category=category) except GoodsVisitCount.DoesNotExist: gvc = GoodsVisitCount() try: gvc.category = category gvc.count += 1 gvc.save() except Exception as e: return http.HttpResponseServerError('服务器异常') # 3.返回相应 return http.JsonResponse({'code': RETCODE.OK, 'errmsg': 'OK'})
def post(self, request, category_id): # print(category_id) try: category = GoodsCategory.objects.get(id=category_id) except Exception as e: logger.error(e) return http.HttpResponseNotFound('缺少必传参数') # # 用strftime将日期按照格式转化为字符串 # time_str = datetime.now().strftime("%Y-%m-%d") # # 用strptime将字符串按照日期格式在转换成日期形式 # time_date = datetime.strptime(time_str,"%Y-%m-%d") # 将日期按照格式转换成字符串 today_str = datetime.now().strftime('%Y-%m-%d') # 将字符串再转换成日期格式 time_date = datetime.strptime(today_str, '%Y-%m-%d') try: count_data = category.goodsvisitcount_set.get(date=time_date) except: count_data = GoodsVisitCount() try: count_data.count += 1 count_data.category = category count_data.save() except: return http.HttpResponseServerError('新增失败') return http.JsonResponse({'code': RETCODE.OK, 'errmsg': 'OK'})
def post(self, request, category_id): # 1.根据id 找 商品分类 try: category = GoodsCategory.objects.get(id=category_id) except Exception as e: return HttpResponseNotFound('商品分类不存在') # 2.获取当天的日期 进行格式化处理 from datetime import datetime today_str = datetime.now().strftime('%Y-%m-%d') # 将处理完的日期字符串 转换成 日期类型 today_date = datetime.strptime(today_str, '%Y-%m-%d') # 3.判断一下 商品分类-->对应的(当天日期)访问量的数据 有还是没有 try: count_data = category.goodsvisitcount_set.get(date=today_date) except Exception as e: # 3.2 没有 新建模型对象 count_data = GoodsVisitCount() # 4.无论又还是没有记录 累加 += 1 保存数据 try: count_data.count += 1 count_data.category = category count_data.save() except Exception as e: return HttpResponseNotFound('记录失败了!') # 5. 返回前端 告诉记录成功 return JsonResponse({'code': 0, 'errmsg': 'OK'})
def post(self, request, category_id): ''' 商品访问 也就是所谓的日活 如果访问记录存在,说明今天不是第一次访问,不新建记录,访问量直接累加。 如果访问记录不存在,说明今天是第一次访问,新建记录并保存访问量。 :param request: :param category_id: :return: ''' try: # 1.获取当前商品 category = GoodsCategory.objects.get(id=category_id) except Exception as e: return HttpResponseNotFound('缺少必传参数') # 2.查询日期数据 # 将日期按照格式转换成字符串 today_str = datetime.now().strftime('%Y-%m-%d') # 将字符串再转换成日期格式 today_date = datetime.strptime(today_str, '%Y-%m-%d') try: # 3.如果有当天商品分类的数据 就累加数量 count_data = category.goodsvisitcount_set.get(date=today_date) except: # 4. 没有, 就新建之后在增加 count_data = GoodsVisitCount() try: count_data.count += 1 count_data.category = category count_data.save() except Exception as e: return HttpResponseServerError('新增失败') return JsonResponse({'code': RETCODE.OK, 'errmsg': 'OK'})
def post(self, request, category_id): # 1.校验 category try: category = GoodsCategory.objects.get(pk=category_id) except: return http.HttpResponseForbidden('商品不存在!') # 2.判断日期 当前日期 这个分类 有没有数据 today_date = date.today() # 当天 一个分类 只有一条记录 try: # 如果有 count+=1 # 1.站在 统计表的 角度 查询 visit = GoodsVisitCount.objects.get(category=category, date=today_date) # 2.站在 分类的角度 查询 # visit = category.goodsvisitcount_set.get(date=today_date) except: # 如果没有 新建 一条数据 再进行累加 count += 1 visit = GoodsVisitCount() # 统一都累加 visit.count += 1 visit.category = category visit.save() # 3.返回响应 return http.JsonResponse({'code': 0, 'errormsg': '记录成功!'})
def post(self, request, category_id): # 根据三级cotegort_id获取当前商品类别 try: category = GoodsCategory.objects.get(id=category_id) except: return http.HttpResponseNotFound('缺少必传参数') # 构造查询日期 from datetime import datetime today_str = datetime.now().strftime('%Y-%m-%d') today_date = datetime.strptime(today_str, '%Y-%m-%d') # 查询当前商品类别在表中今天是否已经有记录 try: # 如果有 就累加数量 count_data = category.goodsvisitcount_set.get(date=today_date) except Exception as e: # 如果没有 就新建一条记录 再增加数量 count_data = GoodsVisitCount() # 修改访问量 try: count_data.count += 1 count_data.category = category count_data.save() except Exception as e: return http.HttpResponseServerError('新增失败') return http.JsonResponse({'code': RETCODE.OK, 'errmsg': 'OK'})
def post(self, request, category_id): # 获取当前商品 try: category = GoodsCategory.objects.get(id=category_id) except Exception as e: logger.error(e) return http.HttpResponseNotFound('缺少必传参数') # 查询日期数据 from datetime import datetime # 将日期格式转换为字符串 today_str = datetime.now().strftime('%Y-%m-%d') # 将字符串转换为日期 today_date = datetime.strptime(today_str, '%Y-%m-%d') try: # 如果有当天商品分类的数据 就直接增加数量 count_data = category.goodsvisitcount_set.get(date=today_date) except Exception as e: # 否则 新增一条记录 然后增加数量 count_data = GoodsVisitCount() # 增加数量 try: count_data.count += 1 count_data.category = category count_data.save() except Exception as e: logger.error(e) return http.HttpResponseServerError('新增失败') return http.JsonResponse({'code': RETCODE.OK, 'errmsg': 'OK'})
def post(self, request, category_id): try: category = GoodsCategory.objects.get(id=category_id) except GoodsCategory.DoesNotExist: return http.HttpResponseForbidden('缺少必传参数') date = datetime.now() try: visit_obj = GoodsVisitCount.objects.get(date=date, category=category) except GoodsVisitCount.DoesNotExist: visit_obj = GoodsVisitCount(category=category) visit_obj.count += 1 visit_obj.save() return http.JsonResponse({'code': RETCODE.OK, 'errmsg': 'OK'})
def post(self, request, category_id): # 1.接收分类id # 2.查询对应的分类信息 try: category = GoodsCategory.objects.get(pk=category_id) except GoodsCategory.DoesNotExist: return http.JsonResponse({ 'code': RETCODE.NODATAERR, 'errmsg': '没有此分类' }) # 3.统计数据的更新 from django.utils import timezone # 先查询对应当天和对应分类的记录 today = timezone.localdate() try: gvc = GoodsVisitCount.objects.get(category=category, date=today) except GoodsVisitCount.DoesNotExist: # 查询不出来,创建新的记录 gvc = GoodsVisitCount(category=category, count=1, date=today) gvc.save() else: # 查询出来就进行+1 gvc.count += 1 gvc.save() # 4.返回数据 return http.JsonResponse({'code': RETCODE.OK, 'errmsg': 'ok'})
def post(self, request, category_id): try: category = GoodsCategory.objects.get(id=category_id) except: return http.HttpResponseForbidden('不存在') today_str = datetime.now().strftime('%Y-%m-%d') today_date = datetime.strptime(today_str, '%Y-%m-%d') try: visit = GoodsVisitCount.objects.get(category=category, date=today_date) # visit = category.goodsvisitcount_set.get(date= today_date) except: visit = GoodsVisitCount() visit.count += 1 visit.category = category visit.save() return JsonResponse({'code': RETCODE.OK, 'errmsg': 'OK'})
def post(self, request, category_id): """ #1.通过商品分类id来查询该商品 #2.获取当天的时间 #3.查询当天类别分类的记录 #如果当天有访问记录===>则更新数据+1 #如果当天没有访问记录,就新建一个记录 #4.更新数据 #5.返回数据 :param request: 商品id 时间 :param category_id: :return: """ try: #1.通过商品分类id来查询该商品 category = GoodsCategory.objects.get(pk=category_id) except GoodsCategory.DoesNotExist: return http.JsonResponse({'code': RETCODE, 'errmsg': '记录商品失败'}) # 2.获取当天的时间 from django.utils import timezone today_time = timezone.localdate() #3.查询当天类别分类的记录 try: #如果当天有访问记录===>则更新数据+1 counts_data = GoodsVisitCount.objects.get(category=category, date=today_time) except GoodsVisitCount.DoesNotExist: #如果当天没有访问记录,就新建一个记录 counts_data = GoodsVisitCount(category=category, count=1, date=today_time) counts_data.save() #4.更新数据 else: #查询出来了 #3.3 更新数据 counts_data.count += 1 counts_data.save() #5.返回数据 return http.JsonResponse({'code': RETCODE.OK, 'errmsg': 'OK'})