Example #1
0
    def create_response(self):
        if not self.request.GET.get('q', ''):
            show_all = True
            shop = Shop.objects.all()

            try:
                user = User.objects.get(id=self.request.user.id)
            except User.DoesNotExist:
                return render(self.request, 'login.html',
                              {'errmsg': '用户登录信息已失效,请重新登录!'})

            # 获取店铺图片
            for_item(shop)

            # 计算配送费时间
            calculate_distance_duration(shop, user)

            # 判断店铺是否为新店
            shop_is_new(shop)

            paginator = Paginator(shop, 16)
            try:
                page = paginator.page(int(self.request.GET.get('page', 1)))
            except PageNotAnInteger:
                # 如果参数page的数据类型不是整型,则返回第一页数据
                page = paginator.page(1)
            except EmptyPage:
                # 用户访问的页数大于实际页数,则返回最后一页的数据
                page = paginator.page(paginator.num_pages)
            return render(self.request, self.template, locals())
        else:
            show_all = False
            qs = super(MySearchView, self).create_response()
            return qs
Example #2
0
    def get(self, request, goods_id, a_page, b_page, c_page):
        """显示详情页"""

        try:
            shop = Shop.objects.get(id=goods_id)
            for_item(shop)
        except Shop.DoesNotExist:
            return render(request, 'wm_index.html', {'errmsg': '店铺不存在'})

        try:
            user = User.objects.get(id=request.user.id)
        except User.DoesNotExist:
            return render(request, 'login.html',
                          {'errmsg': '用户登录信息已失效,请重新登录!'})

        # 获取店铺下所售商品种类
        type_info = GoodsType.objects.filter(shop_id=goods_id)

        # 获取该店铺所有商品
        sku_info = GoodsSKU.objects.filter(goods__shop_id=goods_id)

        # 添加图片路径
        goods_item(sku_info)

        # 是否显示为新店
        shop_is_new(shop)

        # 调用百度地图API计算配送费,时间
        calculate_distance_duration(shop, user)

        # 获取该店铺的好评
        order_info_a = get_comment_confusion(6, 11, shop, a_page)
        # 获取该店铺的中评
        order_info_b = get_comment_confusion(3, 6, shop, b_page)
        # 或取该店铺的差评
        order_info_c = get_comment_confusion(0, 3, shop, c_page)

        rate = good_rate(shop)

        # page_item(order_info_a, 1, 10)

        # 整合数据
        context = {
            'shop': shop,
            'sku_info': sku_info,
            'type_info': type_info,
            'rate': rate,
            'order_info_a': order_info_a,
            'order_info_b': order_info_b,
            'order_info_c': order_info_c
        }
        return render(request, 'wm_shop.html', context)
Example #3
0
def index_again(shop_info, user):
    """处理买家首页,抽出公共方法"""

    # 为店铺插入图片
    for_item(shop_info)

    # 判断店铺是否为新店
    shop_is_new(shop_info)

    # 调用百度地图API计算配送费,时间,可暂时注释
    calculate_distance_duration(shop_info, user)

    return shop_info
Example #4
0
    def get(self, request):

        # 接收数据
        array_id = request.GET.get('cm1').split(',')
        array_count = request.GET.get('cm2').split(',')

        if len(array_id) != len(array_count):
            return redirect(reverse('goods:wm_index'), {'errmsg': '数据错误'})

        sku_info = GoodsSKU.objects.filter(id__in=array_id)

        # 遍历出所有商品信息
        flag, total, total_goods = 0, 0, 0
        for sku in sku_info:
            sku.unite = array_count[flag]
            total_goods = total_goods + (sku.price + int(sku.pack)) * int(
                sku.unite)
            goods = Goods.objects.get(id=sku.goods_id)
            shop = Shop.objects.get(id=goods.shop_id)
            flag += 1

        # 判断店铺状态
        if not shop.business_do:
            return redirect(reverse('goods:index'))

        if request.user.id:
            print('该用户的id为:%s ' % request.user.id)
        else:
            return redirect(reverse('user:login'))

        user = User.objects.get(id=request.user.id)

        # 计算运费
        distance = calculate_distance_duration(shop, user)

        # 一共支付
        total = total_goods + int(distance.send_price)

        # 设置顺序
        address_info = Address.objects.order_by('-is_default').filter(
            user_id=request.user.id)

        # 整合数据
        context = {
            'sku_info': sku_info,
            'total_goods': total_goods,
            'total': total,
            'shop': shop,
            'user': user,
            'address_info': address_info
        }

        return render(request, 'wm_plaorder.html', context)
Example #5
0
    def post(self, request):

        try:
            user = User.objects.get(id=request.user.id)
        except User.DoesNotExist:
            return render(request, 'login.html',
                          {'errmsg': '用户登录信息已失效,请重新登录!'})

        # 接收数据
        sku_str = request.POST.get('sku_ids')[1:]
        addr = request.POST.get('address')
        remarks = request.POST.get('remarks')
        invoice_head = request.POST.get('invoice_head')
        taxpayer_number = request.POST.get('taxpayer_number')

        try:
            sku_ids = sku_str[4:sku_str.find('cm2=') - 1].split('%2C')
            sku_counts = sku_str[sku_str.find('cm2=') + 4:].split('%2C')
            sku_info = GoodsSKU.objects.filter(id__in=sku_ids)
            for index, sku in enumerate(sku_info):
                goods = Goods.objects.get(id=sku.goods_id)
                shop = Shop.objects.get(id=goods.shop_id)
        except Exception:
            return redirect(reverse('goods:wm_index'), {'errmsg': '数据错误'})

        # 校验基本数据
        if not all([sku_str, shop, addr, sku_ids, sku_counts, sku_info]):
            # 数据不完整
            return render(request, 'sj_cpgl.html', {'errmsg': '缺少相关数据'})

        # 订单id: 20200802181630+用户id
        order_id = datetime.datetime.now().strftime('%Y%m%d%H%M%S') + str(
            request.user.id)

        total_price, total_count, range_flag = 0, 0, 0

        # 设置事务保存点
        save_id = transaction.savepoint()
        try:

            # 重新计算运费
            distance = calculate_distance_duration(shop, user, address_id=addr)

            # 订单表添加数据
            order = OrderInfo.objects.create(order_id=order_id,
                                             user_id=request.user.id,
                                             addr_id=addr,
                                             shop=shop,
                                             remarks=remarks,
                                             invoice_head=invoice_head,
                                             total_price=0,
                                             total_count=0,
                                             taxpayer_number=taxpayer_number,
                                             transit_price=distance.send_price,
                                             transit_time=distance.duration)
            # 订单轨迹表添加一条数据
            order_track = OrderTrack.objects.create(order=order, status=1)

            # 乐观锁尝试3次
            for i in range(3):

                if range_flag:
                    break

                # 生成订单明细
                for index, item in enumerate(sku_info):

                    # 判断商品库存
                    if int(item.stock) < int(sku_counts[index]):
                        return HttpResponse('商品库存不足')

                    # 插入数据
                    order_goods = OrderGoods.objects.create(
                        order=order,
                        sku=item,
                        price=item.price,
                        count=sku_counts[index])
                    # 更新库存,返回受影响的行数
                    stock = item.stock - int(sku_counts[index])
                    res = GoodsSKU.objects.filter(
                        id=item.id, stock=item.stock).update(
                            stock=stock,
                            sales=item.sales + int(sku_counts[index]))

                    if res == 0:
                        if i == 2:
                            # 尝试的第3次
                            transaction.savepoint_rollback(save_id)
                            return HttpResponse('下单失败')
                        continue

                    # 累加计算订单商品的总数量和总价格
                    total_price += (item.price + int(item.pack)) * int(
                        sku_counts[index])
                    total_count += int(sku_counts[index])

                    a = len(sku_info)
                    if index == len(sku_info) - 1:
                        range_flag = 1

            # 更新订单信息表中的商品的总数量和总价格
            order.total_count = total_count
            order.total_price = total_price
            order.save()

        except Exception as e:
            transaction.savepoint_rollback(save_id)
            # return JsonResponse({'res': 7, 'errmsg': '下单失败'})

        # 提交事务
        transaction.savepoint_commit(save_id)

        total_all = int(order.transit_price) + total_price

        return render(request, 'wm_pay.html', {
            'order': order,
            'shop': shop,
            'total_all': total_all
        })
Example #6
0
    def get(self, request, code, page):

        try:
            user = User.objects.get(id=request.user.id)
        except User.DoesNotExist:
            return render(request, 'login.html',
                          {'errmsg': '用户登录信息已失效,请重新登录!'})

        type_info = None
        if code == '4':
            shop_info = Shop.objects.all()
        else:
            shop_info = Shop.objects.filter(shop_type=code)
            if code == '0':
                type_info = ShopTypeDetail.objects.filter(
                    type_code__contains='C')
            if code == '1':
                type_info = ShopTypeDetail.objects.filter(
                    type_code__contains='W')

        # 获取店铺图片
        for_item(shop_info)

        # 计算配送费时间
        calculate_distance_duration(shop_info, user)

        # 判断店铺是否为新店
        shop_is_new(shop_info)

        # 对数据进行分页
        paginator = Paginator(shop_info, 16)

        # 获取第page页的内容
        try:
            page = int(page)
        except Exception as e:
            page = 1

        if page > paginator.num_pages:
            page = 1

        # 获取第page页的Page实例对象
        skus_page = paginator.page(page)

        # 进行页码的控制,页面上最多显示5个页码
        # 1.总页数小于5页,页面上显示所有页码
        # 2.如果当前页是前3页,显示1-5页
        # 3.如果当前页是后3页,显示后5页
        # 4.其他情况,显示当前页的前2页,当前页,当前页的后2页
        num_pages = paginator.num_pages
        if num_pages < 5:
            pages = range(1, num_pages + 1)
        elif page <= 3:
            pages = range(1, 6)
        elif num_pages - page <= 2:
            pages = range(num_pages - 4, num_pages + 1)
        else:
            pages = range(page - 2, page + 3)

        context = {
            'code': code,
            'type_info': type_info,
            'skus_page': skus_page,
            'pages': pages
        }
        return render(request, 'wm_index.html', context)