Ejemplo n.º 1
0
    def post(self, request, category_id):
        """记录分类商品访问量"""
        try:
            category = GoodsCategory.objects.get(id=category_id)
        except GoodsCategory.DoesNotExist:
            return http.HttpResponseForbidden('缺少必传参数')
        # 方法一 使用上海时区 LANGUAGE_CODE = 'Asia/Shanghai'

        # # 获取当天日期
        # t = timezone.localtime()
        # # 获取当天的时间字符串
        # today_str = '%d-%02d-%02d' % (t.year, t.month, t.day)
        # # 将当天的时间字符串转成时间对象,为了跟datetime匹配
        # today_time = datetime.strptime(today_str, '%Y-%m-%d')

        # 方法二 禁用时区 USE_TZ = False 推荐使用第二种
        today_time = datetime.today()
        # 统计指定分类商品的访问量
        try:
            # 如果存在,直接获取记录的对象
            counts_data = GoodsVisitCount.objects.get(date=today_time,
                                                      category=category)
        except GoodsVisitCount.DoesNotExist:
            # 如果不存在,创建记录所对应的对象
            counts_data = GoodsVisitCount()
        try:
            counts_data.category = category
            counts_data.count += 1
            counts_data.save()
        except Exception as e:
            logger.error(e)
            return http.HttpResponseServerError('统计失败')
        # 响应结果
        return http.JsonResponse({'code': RETCODE.OK, 'errsmg': 'OK'})
Ejemplo n.º 2
0
    def post(self, request, category_id):
        try:
            category = GoodsCategory.objects.get(id=category_id)
        except:
            return http.HttpResponseForbidden('缺少必传参数')

        t = timezone.localtime()
        today_str = '%d-%02d-%02d' % (t.year, t.month, t.day)
        today_data = datetime.strptime(today_str, '%Y-%m-%d')

        try:
            obj = GoodsVisitCount.objects.get(category_id=category_id,
                                              date=today_data)

        except:
            obj = GoodsVisitCount()
            obj.category = category

        try:
            obj.count += 1
            obj.save()
        except:
            return http.HttpResponseServerError('服务器异常')

        return http.JsonResponse({'code': RETCODE.OK, 'errmsg': 'OK'})
Ejemplo n.º 3
0
    def post(self, request, category_id):
        """记录分类商品访问量"""
        try:
            category = GoodsCategory.objects.get(id=category_id)
        except GoodsCategory.DoesNotExist:
            return http.HttpResponseForbidden('缺少必传参数')

        # 获取今天的日期
        t = timezone.localtime()
        today_str = '%d-%02d-%02d' % (t.year, t.month, t.day)
        today_date = datetime.datetime.strptime(today_str, '%Y-%m-%d')
        try:
            # 查询今天该类别的商品的访问量
            counts_data = category.goodsvisitcount_set.get(date=today_date)
        except GoodsVisitCount.DoesNotExist:
            # 如果该类别的商品在今天没有过访问记录,就新建一个访问记录
            counts_data = GoodsVisitCount()

        try:
            counts_data.category = category
            counts_data.count += 1
            counts_data.save()
        except Exception as e:
            logger.error(e)
            return http.HttpResponseServerError('服务器异常')

        return http.JsonResponse({'code': RETCODE.OK, 'errmsg': 'OK'})
Ejemplo n.º 4
0
    def post(self, request, category_id):
        try:
            goods = GoodsCategory.objects.get(id=category_id)
        except Exception as e:
            return JsonResponse({
                'code': 400,
                'errmsg': '数据库查询失败'
            })
        now = timezone.localdate()
        try:
            goods_visit = GoodsVisitCount.objects.get(date=now,
                                                      category_id=category_id)
        except Exception as e:
            goods_visit = GoodsVisitCount()
        try:

            goods_visit.category = goods
            goods_visit.count += 1
            goods_visit.save()
        except Exception as e:
            return JsonResponse({
                'code': 400,
                'errmsg': '数据库存储失败'
            })
        return JsonResponse({
            'code': 0,
            'errmsg': 'ok'
        })
Ejemplo n.º 5
0
    def post(self, request, category_id):

        # 校验category_id的真实有效性
        try:
            category = GoodsCategory.objects.get(id=category_id)
        except GoodsCategory.DoesNotExist:
            return http.HttpResponseForbidden('category_id不存在')
        # 创建时间对象获取到今天的日期
        today = timezone.now()

        try:
            # 在统计商品类别表中查询当前的类别在今天有没有访问过的记录
            goods_visit = GoodsVisitCount.objects.get(category=category,
                                                      date=today)
        except GoodsVisitCount.DoesNotExist:
            # 如果查询不到说明今天此类别是第一次访问,  创建一个新的记录
            # goods_visit = GoodsVisitCount.objects.create(
            #     category=category
            # )
            goods_visit = GoodsVisitCount(category_id=category_id)
            # goods_visit = GoodsVisitCount()
            # goods_visit.category = category

        # 如果查询到说明今天此类别已经访问过 对原count+=1 save
        goods_visit.count += 1
        goods_visit.save()

        # 响应
        return http.JsonResponse({'code': RETCODE.OK, 'errmsg': 'OK'})
Ejemplo n.º 6
0
    def post(self, request, category_id):

        # 校验数据有效性
        try:
            category = GoodsCategory.objects.get(id=category_id)
        except GoodsCategory.DoesNotExist:
            return http.HttpResponseForbidden("参数无效")

        # 创建时间对象获取今天日期
        today = timezone.now()

        try:
            # 查看统计商品列表中查询当前的类别在今天有没有访问过的记录
            good_visit = GoodsVisitCount.objects.get(category=category, date=today)
        except GoodsVisitCount.DoesNotExist:

            # 不存在创建新纪录表明第一次访问
            good_visit = GoodsVisitCount(category=category)
            # good_visit = GoodsVisitCount()
            # good_visit.category = category

        # 访问量加一
        good_visit.count += 1
        good_visit.save()

        # 响应
        return http.JsonResponse({"code": RETCODE.OK, "errmsg": "ok"})
Ejemplo n.º 7
0
    def post(self, request, category_id):
        """记录分类商品访问量"""

        # 根据传入的 category_id 值, 获取对应类别的商品:
        try:
            category = GoodsCategory.objects.get(id=category_id)
        except GoodsCategory.DoesNotExist:
            return http.HttpResponseForbidden('缺少必传参数')

        # 获取今天的日期:
        # 先获取时间对象
        t = timezone.localtime()
        # 根据时间对象拼接日期的字符串形式:
        today_str = '%d-%02d-%02d' % (t.year, t.month, t.day)
        # 将字符串转为日期格式:
        today_date = datetime.datetime.strptime(today_str, '%Y-%m-%d')
        try:
            # 将今天的日期传入进去, 获取该商品今天的访问量:
            # 查询今天该类别的商品的访问量
            counts_data = category.goodsvisitcount_set.get(date=today_date)
        except GoodsVisitCount.DoesNotExist:
            # 如果该类别的商品在今天没有过访问记录,就新建一个访问记录
            counts_data = GoodsVisitCount()

        try:
            # 更新模型类对象里面的属性: category 和 count
            counts_data.category = category
            counts_data.count += 1
            counts_data.save()
        except Exception as e:
            logger.error(e)
            return http.HttpResponseServerError('服务器异常')

        # 返回:
        return http.JsonResponse({'code': RETCODE.OK, 'errmsg': 'OK'})
Ejemplo n.º 8
0
    def post(self, request, category_id):
        """记录分类商品访问量"""
        try:
            category = GoodsCategory.objects.get(id=category_id)
        except GoodsCategory.DoesNotExist:
            return http.HttpResponseForbidden('缺少必传参数')

        today_date = timezone.localdate()
        try:
            counts_data = GoodsVisitCount.objects.get(category=category,
                                                      date=today_date)
        except GoodsVisitCount.DoesNotExist:
            counts_data = GoodsVisitCount(category=category)

        counts_data.count += 1
        counts_data.save()

        return http.JsonResponse({'code': RETCODE.OK, 'errmsg': 'OK'})
Ejemplo n.º 9
0
 def post(self,request,category_id):
     try:
         category = GoodsCategory.objects.get(id=category_id)
     except Exception as e:
         return JsonResponse({'code':400,'errmsg':'缺少必传参数'})
     now_date = timezone.localdate()
     # print(now_date)
     # now_date2 = date.today()
     # print(now_date2)
     try:
         category_date_visit = category.goodsvisitcount_set.get(date=now_date)
     except Exception as e:
         category_date_visit = GoodsVisitCount()
     try:
         category_date_visit.category = category
         category_date_visit.count += 1
         category_date_visit.save()
     except Exception as e:
         return JsonResponse({'code':400,'errmsg':'服务器异常'})
     return JsonResponse({'code':0,'errmsg':'ok'})
Ejemplo n.º 10
0
    def post(self, request, category_id):
        # 校验
        try:
            category = GoodsCategory.objects.get(id=category_id)
        except GoodsCategory.DoesNotExist:
            return http.HttpResponseForbidden('category_id不存在')

        date = timezone.now()  # 获取当前的日期和时间
        try:
            # 如果今天这个类别已经记录过,修改它的count += 1
            visit_count = GoodsVisitCount.objects.get(category=category,
                                                      date=date)
        except GoodsVisitCount.DoesNotExist:
            # 如果今天这个类别还没有访问过,新增一条访问记录,再设置它的count 1
            visit_count = GoodsVisitCount(category=category)

        # 无论是新记录,还是之前已存在的记录都去累加一
        visit_count.count += 1
        visit_count.save()
        # 响应
        return http.JsonResponse({'code': RETCODE.OK, 'errmsg': 'OK'})
Ejemplo n.º 11
0
    def post(self, request, category_id):
        '''
        记录用户访问次数
        :param request:
        :param category_id:
        :return:
        '''
        # 1.根据category_id 获取类别对象
        try:
            category = GoodsCategory.objects.get(id=category_id)
        except Exception as e:
            return http.HttpResponseForbidden('参数不能为空')

        # 获取今天的日期:
        # (日期+时间)
        time = timezone.localtime()

        today_str = '%d-%02d-%02d' % (time.year, time.month, time.day)

        today_time = datetime.strptime(today_str, '%Y-%m-%d')

        # 2.现根据今天的日期, 从数据库中获取对应的记录
        try:
            count_data = category.goodsvisitcount_set.get(date=today_time)

        except GoodsVisitCount.DoesNotExist:
            # 5.如果不存在, 创建新的
            count_data = GoodsVisitCount()
        try:
            # 3.如果获取到了记录, 更新(类别, count+1)
            count_data.category = category
            count_data.count += 1
            # 4.保存
            count_data.save()
        except Exception as e:
            return http.HttpResponseForbidden('更新数据库出错')

        # 6.返回
        return http.JsonResponse({'code': RETCODE.OK,
                                  'errmsg': 'ok'})
Ejemplo n.º 12
0
    def post(self, request, category_id):
        # 接收参数和校验参数
        try:
            category = GoodsCategory.objects.get(id=category_id)
        except GoodsCategory.DoesNotExist:
            return http.HttpResponseForbidden('category_id 不存在')

        # 获取当天的日期
        t = timezone.localtime()
        # 获取当天的时间字符串
        today_str = '%d-%02d-%02d' % (t.year, t.month, t.day)
        # 将当天的时间字符串转成时间对象datetime,为了跟date字段的类型匹配 2019:05:23  2019-05-23
        today_date = datetime.strptime(today_str, '%Y-%m-%d') # 时间字符串转时间对象;datetime.strftime() # 时间对象转时间字符串

        # 判断当天中指定的分类商品对应的记录是否存在
        try:
            # 如果存在,直接获取到记录对应的对象
            counts_data = GoodsVisitCount.objects.get(date=today_date, category=category)
        except GoodsVisitCount.DoesNotExist:
            # 如果不存在,直接创建记录对应的对象
            counts_data = GoodsVisitCount()

        try:
            counts_data.category = category
            counts_data.count += 1
            counts_data.date = today_date
            counts_data.save()
        except Exception as e:
            return http.HttpResponseServerError('统计失败')

        # 响应结果
        return http.JsonResponse({'code': RETCODE.OK, 'errmsg': 'OK'})
Ejemplo n.º 13
0
    def get(self, request, category_id):
        try:
            category = GoodsCategory.objects.get(id=category_id)
        except Exception as e:
            return http.HttpResponseForbidden('dtype=NONE')

        t1 = time.localtime(time.time())
        t1 = '{}-{}-{}'.format(t1.tm_year, t1.tm_mon, t1.tm_mday)

        t1 = datetime.strptime(t1,'%Y-%m-%d')
        try:
            counts_datd =GoodsVisitCount.objects.get(date=t1, category=category)
        except Exception as e:
            counts_datd = GoodsVisitCount()

        counts_datd.category = category
        counts_datd.count += 1
        counts_datd.date = t1

        try:
            counts_datd.save()
        except Exception as e:
            return http.HttpResponseServerError('Eroor')

        response = {'conde':0,'error':'info'}
        return http.JsonResponse(response)
Ejemplo n.º 14
0
    def post(self, request, category_id):

        # 接收校驗參數
        try:
            category = GoodsCategory.objects.get(id=category_id)
        except GoodsCategory.DoesNotExist:
            return http.HttpResponseForbidden('category_id不存在')

        # 獲取當天日期
        t = timezone.localtime()
        # 當日時間字串
        today_str = '%d-%02d-%02d' % (t.year, t.month, t.day)
        # 再將時間字串轉為datetime
        today_date = datetime.strptime(today_str, '%Y-%m-%d')

        # 統計指定分類商品的訪問量
        try:
            counts_data = GoodsVisitCount.objects.get(date=today_date, category=category)
        except GoodsVisitCount.DoesNotExist:
            counts_data = GoodsVisitCount()

        try:
            counts_data.category = category
            counts_data.count += 1
            counts_data.date = today_date
            counts_data.save()
        except Exception as e:
            return http.HttpResponseServerError('統計失敗')

        return http.JsonResponse({'code': RETCODE.OK, 'errmsg': 'OK'})
Ejemplo n.º 15
0
    def post(self, request, category_id):
        try:
            category = GoodsCategory.objects.get(id=category_id)
        except GoodsCategory.DoesNotExist:
            return http.HttpResponseForbidden('category_id 不存在')

        # 获取当天日期
        t = timezone.localtime()
        # 获取当天的时间字符串
        today_str = '%d-%02d-%02d' % (t.year, t.month, t.day)
        # 将当天的时间字符串转成时间对象datetime, 为了跟date字段类型匹配
        today_date = datetime.strptime(today_str, '%Y-%m-%d')

        try:
            counts_data = GoodsVisitCount.objects.get(date=today_date,
                                                      category=category)
        except GoodsVisitCount.DoesNotExist:
            counts_data = GoodsVisitCount()

        try:
            counts_data.category = category
            counts_data.count += 1
            counts_data.date = today_date
            counts_data.save()
        except Exception as e:
            return http.HttpResponseServerError('统计失败')

        return http.JsonResponse({'code': RETCODE.OK, 'errmsg': 'OK'})
Ejemplo n.º 16
0
    def post(self, request, category_id):
        """
        :param request:
        :param category_id: 商品ID
        :return:
        """

        # 从GoodsCategory中查询商品 category_id 是否存在

        try:
            category = GoodsCategory.objects.get(id=category_id)
        except GoodsCategory.DoesNotExist:
            return http.JsonResponse({
                'code': RETCODE.DBERR,
                'errmsg': '缺少必传参数'
            })

        # 判断该商品今天是否存在访问记录 存在  记录+1 不存在新建记录
        t = timezone.localtime()
        today_str = '%d-%02d-%02d' % (t.year, t.month, t.day)
        today_date = datetime.datetime.strptime(today_str, '%Y-%m-%d')

        try:
            counts_data = category.goodsvisitcount_set.get(date=today_date)
        except GoodsVisitCount.DoesNotExist:
            counts_data = GoodsVisitCount()
        # 响应Json 类型 数据今日访问量

        try:
            counts_data.category = category
            counts_data.count += 1
            counts_data.save()
        except Exception as e:
            logger.error(e)
            return http.JsonResponse({
                'code': RETCODE.DBERR,
                'errmsg': '服务器异常'
            })

        return http.JsonResponse({'code': RETCODE.OK, 'errmsg': 'OK'})
Ejemplo n.º 17
0
    def post(self, request, category_id):

        # 校验category_id 是否真实存在
        try:
            category = GoodsCategory.objects.get(id=category_id)
        except GoodsCategory.DoesNotExist:
            return http.HttpResponseForbidden('商品类型不存在')
        # 获取当前日期
        today_date = timezone.localdate()
        # 查询当前类别今天有没有统计过
        try:
            count_data = GoodsVisitCount.objects.get(category=category,
                                                     date=today_date)

        except GoodsVisitCount.DoesNotExist:

            # 如果当前类别今天是第一次来统计,就创建一个新记录,并给它指定是统计那一个类别

            count_data = GoodsVisitCount(category=category)
        count_data.count += 1
        count_data.save()
        # 累加浏览量
        return http.JsonResponse({'code': RETCODE.OK, 'errmsg': 'OK'})
Ejemplo n.º 18
0
    def post(self, request, category_id):
        '''
        保存访问记录到数据库
        :param request:
        :param category_id:
        :return:
        '''
        # 1.验证一下category_id
        try:
            category = GoodsCategory.objects.get(id=category_id)
        except GoodsCategory.DoesNotExist:
            return http.HttpResponseForbidden("缺少必传参数")

        # 2.生成一个当前的时间对象
        time = timezone.localtime()

        today_time_str = '%d-%02d-%02d' % (time.year, time.month, time.day)

        today_time = datetime.datetime.strptime(today_time_str, '%Y-%m-%d')

        # 3.根据当前时间, 区数据库中查询该表的对象是否存在
        try:
            obj = category.goodsvisitcount_set.get(date=today_time)
        except Exception as e:
            # 4.如果不存在, 新建一个
            obj = GoodsVisitCount()

        # 5.如果存在, 保存数据(category,  次数)
        try:
            obj.category = category
            obj.count += 1
            obj.save()
        except Exception as e:
            return http.HttpResponseForbidden('数据库保存失败')

        # 6.返回
        return http.JsonResponse({'code': RETCODE.OK, 'errmsg': 'ok'})
Ejemplo n.º 19
0
 def post(self, request, category_id):
     """处理统计商品访问量的逻辑"""
     # 接收、校验参数
     try:
         category = GoodsCategory.objects.get(id=category_id)
     except GoodsCategory.DoesNotExist:
         return http.HttpResponseForbidden('category_id不存在')
     # 获取当天的服务器系统日期
     t = timezone.localtime()
     # 获取当天的时间字符串
     # today_str = "%d-%02d-%02d" % (t.year, t.month, t.day)
     today_str = "{:d}-{:0>2d}-{:0>2d}".format(t.year, t.month, t.day)
     # 将时间字符串转为时间对象datetime,为了与DateField字段类型匹配
     today_date = datetime.strptime(today_str, '%Y-%m-%d')
     # 统计指定分类商品的访问量  ---> GoodsVisitCount.objects.filter(data='当天日期', category_id=category.id)
     # 判断当天中指定的分类商品对应的记录是否存在
     try:
         # 如果查询到记录,直接获取到记录对应的对象
         counts_obj = GoodsVisitCount.objects.get(date=today_date,
                                                  category=category)
     except GoodsVisitCount.DoesNotExist:
         # 如果查询不到记录,则新建一条记录对应的对象
         counts_obj = GoodsVisitCount()
     try:
         # 更新该条记录
         counts_obj.category = category
         counts_obj.count += 1  # count默认为0
         counts_obj.date = today_date
         counts_obj.save()
     except Exception as e:
         logger.error(e)
         return http.HttpResponseServerError('统计失败')
     else:
         # 响应结果
         return http.JsonResponse({
             'code': RETCODE.OK,
             'errmsg': err_msg[RETCODE.OK]
         })
Ejemplo n.º 20
0
    def post(self, request, category_id):
        """
        实现统计分类商品的访问量逻辑
        :param category_id: 要统计的商品的分类
        :return: JSON
        """
        # 校验category_id
        try:
            category = GoodsCategory.objects.get(id=category_id)
        except GoodsCategory.DoesNotExist:
            return http.HttpResponseForbidden('缺少必传参数')

        # 获取当天的日期:t 对应的是时间对象DateTime 2019-11-29
        t = timezone.localtime()
        # 拼接时间字符串
        today_str = '%d-%02d-%02d' % (t.year, t.month, t.day)
        # 将时间字符串转时间对象 strptime : 将时间字符串转成时间对象。strftime :时间对象转时间字符串
        today_date = timezone.datetime.strptime(today_str, '%Y-%m-%d')

        # 判断当天category_id对应的访问记录是否存在
        try:
            goods_count = GoodsVisitCount.objects.get(category=category,
                                                      date=today_date)
        except GoodsVisitCount.DoesNotExist:
            # 如果不存在:新建记录,并保存本次访问量
            goods_count = GoodsVisitCount()

        # 如果已存在或者新建了记录,直接累加本次访问量
        try:
            goods_count.category_id = category_id
            goods_count.date = today_date
            goods_count.count += 1
            goods_count.save()
        except DatabaseError:
            return http.HttpResponseServerError('统计失败')

        return http.JsonResponse({'code': RETCODE.OK, 'errmsg': 'OK'})
Ejemplo n.º 21
0
    def post(self, request, category_id):
        """
        统计商品用户的访问量
        :param request:
        :param category_id:
        :return:
        """
        # 先根据category_id获取当前的类别
        try:
            category = GoodsCategory.objects.get(id=category_id)
        except GoodsCategory.DoesNotExist:
            return http.HttpResponseForbidden('缺少必传参数')

        # 1.获取今天的时间
        t = timezone.localtime()  # 获取时间对象
        # 根据时间对象拼接日期的字符串形式
        today_str = '%d-%02d-%02d' % (t.year, t.month, t.day)
        # 将字符串转为日期格式
        today_date = datetime.datetime.strptime(today_str, '%Y-%m-%d')
        # 2.在数据库中进行查询看当前商品是否有被浏览过
        try:
            counts_data = GoodsVisitCount.objects.get(date=today_date,
                                                      category=category)
        # 3.如果有就进行加一
        except GoodsVisitCount.DoesNotExist:
            counts_data = GoodsVisitCount()
        try:
            # 4.如果没有就新建记录并保存
            counts_data.category = category
            counts_data.count += 1
            counts_data.date = today_date
            counts_data.save()
        except Exception as e:
            logger.error(e)
            return http.HttpResponseForbidden('数据保存失败')
        # 5.返回
        return http.JsonResponse({'code': RETCODE.OK, 'errmsg': 'OK'})