Example #1
0
def edit_item(request, item_id):
    if request.method == 'GET':
        active_item = ActiveItem.objects.filter(id=item_id)
        if active_item:
            item = active_item[0].item
            if not os.path.exists('static/items/' + str(item.item_id) + '/' +
                                  str(item.version) + '/'):
                os.makedirs('static/items/' + str(item.item_id) + '/' +
                            str(item.version) + '/')
            with open(
                    'static/items/' + str(item.item_id) + '/' +
                    str(item.version) + '/introduction.intro',
                    'r') as item_introduction_file:
                introduction = item_introduction_file.read()
            # form = CreateItemForm()
            # form.item_name = item.item_name
            # form.price = item.price
            # form.inventory = item.inventory
            return render(
                request, 'edit_item.html', {
                    'status_bar': get_status_bar(request),
                    'item_name': item.item_name,
                    'price': str(item.price),
                    'inventory': str(item.inventory),
                    'introduction': introduction
                })
    if request.method == 'POST':
        pass
Example #2
0
def visit_shop(request, shop_id):
    if request.method == 'GET':
        shop = Shop.objects.filter(id=shop_id)
        if shop:
            shop = shop[0]
        item_list = None
        if shop:
            item_list = ActiveItem.objects.filter(item__shop=shop)
        return render(
            request, 'shop.html', {
                'status_bar': get_status_bar(request),
                'shop_model': shop,
                'item_list': item_list
            })
Example #3
0
def create_shop(request):
    seller = UserSeller.objects.filter(user=request.user)
    if seller:
        if request.method == 'GET':
            return render(
                request, 'createshop.html', {
                    'create_shop_form': CreateShopForm(),
                    'status_bar': get_status_bar(request)
                })
        if request.method == 'POST':
            form = CreateShopForm(request.POST)
            if form.is_valid():
                shop = Shop(shop_name=form.cleaned_data['shop_name'],
                            owner=seller[0])
                shop.save()
                ActiveShop(id=shop.id, shop=shop).save()
                return redirect(reverse('shop:VisitMall', args=[shop.id]))
Example #4
0
def history_item(request, item_id, version):
    if request.method == 'GET':
        item = Item.objects.filter(item_id=item_id, version=version)
        if item:
            item = item[0]
            if os.path.exists('static/items/' + str(item.item_id) + '/' +
                              str(item.version) + '/introduction.intro'):
                with open(
                        'static/items/' + str(item.item_id) + '/' +
                        str(item.version) + '/introduction.intro',
                        'r') as item_intro_file:
                    introduction = item_intro_file.read()
            else:
                introduction = ''
            return render(
                request, 'item.html', {
                    'item_model': item,
                    'item_introduction': introduction,
                    'status_bar': get_status_bar(request),
                    'history': True
                })
        else:
            return HttpResponse(status=404)
Example #5
0
def cart(request):
    user, user_type = get_user_type(request.user)
    if user_type == 'buyer':
        cart_item_list = Cart.objects.filter(user=user)
        # for 检查购物车中的Item是否为active,同时尝试替换为最新版本
        for cart_item in cart_item_list:
            if not cart_item.item.active:  # 检查购物车中保存的版本是否active
                # 若不是最新
                # 检索ActiveItem
                active_item_queryset = ActiveItem.objects.filter(
                    id=cart_item.item.item_id)
                if active_item_queryset:  # 若存在新版本
                    cart_item.item = active_item_queryset[0].item
                    cart_item.save()
        return render(
            request, 'buyer_cart.html', {
                'cart_item_list': cart_item_list,
                'status_bar': get_status_bar(request)
            })
    else:
        return render(request, 'message.html', {
            'message_title': '您不是买家',
            'message': '您不是买家,无法查看购物车'
        })
Example #6
0
def make_order(request, item_id):
    buyer = UserBuyer.objects.filter(user=request.user)
    try:
        item_id = int(item_id)
    except BaseException:
        item_id = 0
    if request.method == 'GET':
        if buyer:
            buyer = buyer[0]
            if item_id:
                item = ActiveItem.objects.filter(id=item_id)[0].item
                order_cart_item_list = [
                    Cart(user=buyer, collected=False, item=item, quantity=1)
                ]
            else:
                # 获取cart中所有的item,除去收集的,没有货的,和不活动的item
                order_cart_item_list = Cart.objects.filter(
                    user=buyer, collected=False).exclude(
                        item__inventory=0).exclude(item__active=False)
            if order_cart_item_list:
                orders = {}  # 临时存储订单
                for cart_item in order_cart_item_list:
                    if cart_item.item.shop.id not in orders.keys(
                    ):  # 如果没有为该店铺建立订单
                        orders[cart_item.item.shop.id] = Order(
                            buyer=buyer, shop=cart_item.item.shop)
                        orders[cart_item.item.shop.id].details = []
                    orders[cart_item.item.shop.
                           id].total_amount += cart_item.item.price
                    orders[cart_item.item.shop.id].details.append({
                        'item_id':
                        cart_item.item.item_id,  # 商品id
                        'item_name':
                        cart_item.item.item_name,  # 商品名
                        'price':
                        str(cart_item.item.price),  # 销售时的商品价格
                        'version':
                        cart_item.item.version,
                        'quantity':
                        cart_item.quantity
                    })  # 商品版本
                return render(
                    request, 'make_order.html', {
                        'status_bar': get_status_bar(request),
                        'orders': orders,
                        'item_id': item_id
                    })
            else:
                return render(
                    request, 'message.html', {
                        'message_title': '没有商品',
                        'message': '您的购物车为空,无法创建订单',
                        'status_bar': get_status_bar(request)
                    })
    if request.method == 'POST':
        if buyer:
            buyer = buyer[0]
            if item_id:
                item = ActiveItem.objects.filter(id=item_id)[0].item
                order_cart_item_list = [
                    Cart(user=buyer, collected=False, item=item, quantity=1)
                ]
            else:
                # 获取cart中所有的item,除去收集的,没有货的,和不活动的item
                order_cart_item_list = Cart.objects.filter(
                    user=buyer, collected=False).exclude(
                        item__inventory=0).exclude(item__active=False)
            if order_cart_item_list:  # 如果存在
                # order_item_list_as_json = []    # 存储json化的订单信息
                # order_amount = decimal.Decimal(0)
                # shops = []      # 保存涉及的店铺
                orders = {}
                orders_details = {}
                for cart_item in order_cart_item_list:
                    if cart_item.item.shop.id not in orders.keys():
                        orders[cart_item.item.shop.id] = Order(
                            buyer=buyer, shop=cart_item.item.shop)
                        orders_details[cart_item.item.shop.id] = []
                    orders[cart_item.item.shop.
                           id].total_amount += cart_item.item.price
                    orders_details[cart_item.item.shop.id].append({
                        'item_id':
                        cart_item.item.item_id,  # 商品id
                        'item_name':
                        cart_item.item.item_name,  # 商品名
                        'price':
                        str(cart_item.item.price),  # 销售时的商品价格
                        'version':
                        cart_item.item.version,
                        'quantity':
                        cart_item.quantity
                    })  # 商品版本
                    # 库存信息处理
                    cart_item.item.inventory -= 1
                    cart_item.item.sales_volume += 1
                    cart_item.item.save()

                    # 店铺信息更新
                    cart_item.item.shop.sales_volume += 1
                    cart_item.item.shop.sales_amount += cart_item.item.price
                    cart_item.item.shop.save()
                # 将订单们写入数据库
                orders_key = orders.keys()
                for key in orders_key:
                    orders[key].details = json.dumps(orders_details[key])
                    orders[key].save()

                # # 写入数据库
                # order = Order(buyer=buyer, total_amount=order_amount, details=json.dumps(order_item_list_as_json))
                # order.save()
                # for s in shops:
                #     OrderShopList(order=order, shop=s).save()
                try:
                    order_cart_item_list.delete()
                except AttributeError:
                    pass
                if len(orders) == 1:
                    for key in orders.keys():
                        order_id = orders[key].id
                    return redirect(reverse('shop:PayPage', args=[order_id]))
                elif len(orders) > 1:
                    return redirect(reverse('account:MyOrders'))
                else:
                    return HttpResponse(status=503)
            else:
                return render(
                    request, 'message.html', {
                        'message_title': '没有商品',
                        'message': '您的购物车为空,无法创建订单',
                        'status_bar': get_status_bar(request)
                    })
        else:
            return HttpResponse(status=403)
    else:
        return HttpResponse(status=404)
Example #7
0
def show_item(request, item_id):
    active_item = ActiveItem.objects.filter(id=item_id)
    if active_item:
        active_item = active_item[0]
        user, user_type = get_user_type(request.user)
        if request.method == 'GET':

            if os.path.exists('static/items/' + str(active_item.item.item_id) +
                              '/' + str(active_item.item.version) +
                              '/introduction.intro'):
                with open(
                        'static/items/' + str(active_item.item.item_id) + '/' +
                        str(active_item.item.version) + '/introduction.intro',
                        'r') as item_intro_file:
                    introduction = item_intro_file.read()
            else:
                introduction = ''
            print(user_type)
            return render(
                request, 'item.html', {
                    'item_model': active_item.item,
                    'item_introduction': introduction,
                    'status_bar': get_status_bar(request),
                    'user_type': user_type
                })
        if request.method == 'POST':
            if user_type == 'buyer':
                print(request.POST.get('add_to_cart'))
                if request.POST.get('add_to_cart'):  # 如果选择添加到购物车
                    cart_item = Cart.objects.filter(
                        user=user, item__item_id=active_item.id)
                    if cart_item:
                        cart_item[0].quantity += 1
                        cart_item[0].save()
                    else:
                        Cart(user=user, item=active_item.item).save()
                    return render(
                        request, 'message.html', {
                            'status_bar':
                            get_status_bar(request),
                            'message_title':
                            '购物车添加成功',
                            'message':
                            '已经成功将商品添加到您的购物车',
                            'other':
                            '<a href="/shop/item/{0}">返回</a>'.format(
                                str(item_id))
                        })
                elif request.POST.get('buy_now'):  # 如果选择直接购买
                    return redirect(reverse('shop:MakeOrder', args=[item_id]))
                else:  # 以其他未知方式提交表单
                    return HttpResponse(status=403)
            elif not user_type:  # 如果未登录,或因其他原因返回none
                return redirect(reverse('account:Login'))
            elif user_type == 'seller':
                return HttpResponse(status=403)
            else:  # 如果出现其他未知原因
                return HttpResponse(status=403)

    else:
        HttpResponse(status=404)
Example #8
0
def index(request):
    return render(request, 'index.html', {'status': get_status_bar(request)})