Exemplo n.º 1
0
def vendor_detail(vendor_id):
    if not current_app.debug and vendor_id not in [12801, 12803, 12806, 12836]:
        abort(404)
    format = request.args.get('format', '', type=str)
    if format == 'json':
        data = redis_get('BRAND_ITEMS', vendor_id)
        if data is None:
            data = {}
            if current_app.debug:
                for scene_id in [2, 3, 4, 6]:
                    scene = Scene.query.get(scene_id)
                    item_list = statisitc.item_query.filter(Item.vendor_id == vendor_id, Item.scene_id == scene_id).all()
                    if not item_list:
                        continue
                    items = [random.SystemRandom().choice(item_list) for _ in range(10)]
                    data[scene_id] = {'scene': scene.scene, 'items': items_json(items)}
            else:
                for scene_id in current_app.config['ITEMS']['vendor_detail'][str(vendor_id)].keys():
                    scene = Scene.query.get(scene_id)
                    items = []
                    for item_id in current_app.config['ITEMS']['vendor_detail'][str(vendor_id)][scene_id]:
                        items.append(Item.query.get(item_id))
                    data[scene_id] = {'scene': scene.scene, 'items': items_json(items)}
            data = json.dumps(data)
            redis_set('BRAND_ITEMS', vendor_id, data, expire=86400)
        return Response(data, mimetype='application/json')
    return render_template('user/brand_detail.html')
Exemplo n.º 2
0
def brand_list():
    format = request.args.get('format', '', type=str)
    if format == 'json':
        data = redis_get('BRAND', 'ITEMS')
        if data is None:
            brands = statisitc.brands['available']
            data = {
                vendor_id: {
                    'brand': brands[vendor_id]['brand']
                }
                for vendor_id in brands
            }
            for vendor_id in data:
                if current_app.debug:
                    item_list = Item.query.filter(
                        Item.vendor_id == vendor_id, Item.is_deleted == False,
                        Item.is_component == False).all()
                    items = [
                        random.SystemRandom().choice(item_list)
                        for _ in range(5)
                    ]
                else:
                    items = current_app.config['ITEMS']['brands'][str(
                        vendor_id)]
                data[vendor_id]['items'] = items_json(items)
            data = json.dumps(data)
            redis_set('BRAND', 'ITEMS', data, expire=86400)
        return Response(data, mimetype='application/json')
    return render_template('user/brands.html')
Exemplo n.º 3
0
def furniture():
    format = request.args.get('format', '', type=str)
    if format == 'json':
        data = redis_get('STYLE', 'ITEMS')
        if data is None:
            styles = statisitc.styles['available']
            data = {
                style_id: {
                    'style': styles[style_id]['style']
                }
                for style_id in styles
            }
            for style_id in data:
                if current_app.debug:
                    item_list = Item.query.filter(
                        Item.style_id == style_id).all()
                    items = [
                        random.SystemRandom().choice(item_list)
                        for _ in range(8)
                    ]
                else:
                    items = []
                    for item_id in current_app.config['ITEMS']['furniture'][
                            str(style_id)]:
                        items.append(Item.query.get(item_id))
                data[style_id]['items'] = items_json(items)
            data = json.dumps(data)
            redis_set('STYLE', 'ITEMS', data, expire=86400)
        return Response(data, mimetype='application/json')
    return render_template('user/furniture.html')
Exemplo n.º 4
0
def collection():
    if request.method == 'GET':
        page = request.args.get('page', 1, type=int)
        per_page = 10
        query = Collection.query.filter_by(user_id=current_user.id)
        amount = query.count()
        collections = query.paginate(page, per_page, False).items
        collection_dict = {'collections': items_json([collection.item for collection in collections]),
                           'amount': amount, 'page': page, 'pages': ceil(amount / per_page)}
        return jsonify(collection_dict)

    item_id = request.form.get('item', 0, type=int)
    item = Item.query.get(item_id)
    item_collection = Collection.query.filter_by(user_id=current_user.id, item_id=item_id).first()
    if request.method == 'POST':
        if not item or item.is_deleted or item.is_component:
            return jsonify({'success': False, 'message': '该商品不存在'})
        elif not item_collection:
            item_collection = Collection(current_user.id, item_id)
            db.session.add(item_collection)
            db.session.commit()
        return jsonify({'success': True})
    else:  # DELETE
        if item_collection:
            db.session.delete(item_collection)
            db.session.commit()
        return jsonify({'success': True})
Exemplo n.º 5
0
def vendor_detail(vendor_id):
    if not current_app.debug and vendor_id not in [12801, 12803, 12806, 12836]:
        abort(404)
    format = request.args.get('format', '', type=str)
    if format == 'json':
        data = redis_get('BRAND_ITEMS', vendor_id)
        if data is None:
            data = {}
            if current_app.debug:
                for scene_id in [2, 3, 4, 6]:
                    scene = Scene.query.get(scene_id)
                    item_list = statisitc.item_query.filter(
                        Item.vendor_id == vendor_id,
                        Item.scene_id == scene_id).all()
                    if not item_list:
                        continue
                    items = [
                        random.SystemRandom().choice(item_list)
                        for _ in range(10)
                    ]
                    data[scene_id] = {
                        'scene': scene.scene,
                        'items': items_json(items)
                    }
            else:
                for scene_id in current_app.config['ITEMS']['vendor_detail'][
                        str(vendor_id)].keys():
                    scene = Scene.query.get(scene_id)
                    items = []
                    for item_id in current_app.config['ITEMS'][
                            'vendor_detail'][str(vendor_id)][scene_id]:
                        items.append(Item.query.get(item_id))
                    data[scene_id] = {
                        'scene': scene.scene,
                        'items': items_json(items)
                    }
            data = json.dumps(data)
            redis_set('BRAND_ITEMS', vendor_id, data, expire=86400)
        return Response(data, mimetype='application/json')
    return render_template('user/brand_detail.html')
Exemplo n.º 6
0
def navbar():
    data = redis_get('INDEX_NAVBAR', 'ITEMS')
    if data is None:
        data = {}
        for scene_id in [2, 3, 4, 6]:   # 客厅 书房 卧室 餐厅
            scene = Scene.query.get(scene_id)
            if current_app.debug:
                item_list = statisitc.item_query.filter(Item.scene_id == scene_id).all()
                if not item_list:
                    items = []
                else:
                    items = [random.SystemRandom().choice(item_list) for _ in range(8)]
            else:
                items = current_app.config['ITEMS']['navbars'][str(scene_id)]
            data[scene.id] = {'scene': scene.scene, 'items': items_json(items)}
        data = json.dumps(data)
        redis_set('INDEX_NAVBAR', 'ITEMS', data, expire=86400)
    return Response(data, mimetype='application/json')
Exemplo n.º 7
0
def brand_list():
    format = request.args.get('format', '', type=str)
    if format == 'json':
        data = redis_get('BRAND', 'ITEMS')
        if data is None:
            brands = statisitc.brands['available']
            data = {vendor_id: {'brand': brands[vendor_id]['brand']} for vendor_id in brands}
            for vendor_id in data:
                if current_app.debug:
                    item_list = Item.query.filter(Item.vendor_id == vendor_id, Item.is_deleted == False,
                                                  Item.is_component == False).all()
                    items = [random.SystemRandom().choice(item_list) for _ in range(5)]
                else:
                    items = current_app.config['ITEMS']['brands'][str(vendor_id)]
                data[vendor_id]['items'] = items_json(items)
            data = json.dumps(data)
            redis_set('BRAND', 'ITEMS', data, expire=86400)
        return Response(data, mimetype='application/json')
    return render_template('user/brands.html')
Exemplo n.º 8
0
def furniture():
    format = request.args.get('format', '', type=str)
    if format == 'json':
        data = redis_get('STYLE', 'ITEMS')
        if data is None:
            styles = statisitc.styles['available']
            data = {style_id: {'style': styles[style_id]['style']} for style_id in styles}
            for style_id in data:
                if current_app.debug:
                    item_list = Item.query.filter(Item.style_id == style_id).all()
                    items = [random.SystemRandom().choice(item_list) for _ in range(8)]
                else:
                    items = []
                    for item_id in current_app.config['ITEMS']['furniture'][str(style_id)]:
                        items.append(Item.query.get(item_id))
                data[style_id]['items'] = items_json(items)
            data = json.dumps(data)
            redis_set('STYLE', 'ITEMS', data, expire=86400)
        return Response(data, mimetype='application/json')
    return render_template('user/furniture.html')
Exemplo n.º 9
0
def navbar():
    data = redis_get('INDEX_NAVBAR', 'ITEMS')
    if data is None:
        data = {}
        for scene_id in [2, 3, 4, 6]:  # 客厅 书房 卧室 餐厅
            scene = Scene.query.get(scene_id)
            if current_app.debug:
                item_list = statisitc.item_query.filter(
                    Item.scene_id == scene_id).all()
                if not item_list:
                    items = []
                else:
                    items = [
                        random.SystemRandom().choice(item_list)
                        for _ in range(8)
                    ]
            else:
                items = current_app.config['ITEMS']['navbars'][str(scene_id)]
            data[scene.id] = {'scene': scene.scene, 'items': items_json(items)}
        data = json.dumps(data)
        redis_set('INDEX_NAVBAR', 'ITEMS', data, expire=86400)
    return Response(data, mimetype='application/json')
Exemplo n.º 10
0
def collection():
    if request.method == 'GET':
        page = request.args.get('page', 1, type=int)
        per_page = 10
        query = Collection.query.filter_by(user_id=current_user.id)
        amount = query.count()
        collections = query.paginate(page, per_page, False).items
        collection_dict = {
            'collections':
            items_json([collection.item for collection in collections]),
            'amount':
            amount,
            'page':
            page,
            'pages':
            ceil(amount / per_page)
        }
        return jsonify(collection_dict)

    item_id = request.form.get('item', 0, type=int)
    item = Item.query.get(item_id)
    item_collection = Collection.query.filter_by(user_id=current_user.id,
                                                 item_id=item_id).first()
    if request.method == 'POST':
        if not item or item.is_deleted or item.is_component:
            return jsonify({'success': False, 'message': '该商品不存在'})
        elif not item_collection:
            item_collection = Collection(current_user.id, item_id)
            db.session.add(item_collection)
            db.session.commit()
        return jsonify({'success': True})
    else:  # DELETE
        if item_collection:
            db.session.delete(item_collection)
            db.session.commit()
        return jsonify({'success': True})