Пример #1
0
def lazada_authorize_login():
    code = request.args.get('code')
    client = LazopClient("https://auth.lazada.com/rest",
                         LAZADA_MARKET_KEY, LAZADA_MARKET_SECRET)
    api_request = LazopRequest("/auth/token/create")
    api_request.add_api_param("code", code)
    response = client.execute(api_request)
    access_token = response.body.get('access_token')
    refresh_token = response.body.get('refresh_token')
    seller_id = response.body.get('country_user_info')[0].get('seller_id')
    short_code = response.body.get('country_user_info')[0].get('short_code')
    # email = response.body.get('account')
    print(response.body)

    new_marketplace = Marketplace(
        user_id=current_user.id,
        marketplace_name="lazada",
        shop_id=seller_id,
        shop_name=short_code,
        access_token=access_token,
        refresh_token=refresh_token
    )

    db.session.add(new_marketplace)
    db.session.commit()
    flash('Lazada has been added to your omni-marketplace!')
    return redirect(url_for('home'))  # change redirect destination later
Пример #2
0
def lazada_authorize_login():
    code = request.get_json().get('code')
    auth_header = request.headers.get('Authorization')
    if auth_header:
        auth_token = auth_header.split(" ")[1]
    else:
        responseObject = {
            'status': 'failed',
            'message': 'No authorization header found'
        }

        return make_response(jsonify(responseObject)), 401

    user_id = User.decode_auth_token(auth_token)

    user = User.query.get(user_id)

    if user:
        client = LazopClient("https://auth.lazada.com/rest",
                             LAZADA_MARKET_KEY, LAZADA_MARKET_SECRET)
        api_request = LazopRequest("/auth/token/create")
        api_request.add_api_param("code", code)
        response = client.execute(api_request)
        access_token = response.body.get('access_token')
        refresh_token = response.body.get('refresh_token')
        seller_id = response.body.get('country_user_info')[0].get('seller_id')
        short_code = response.body.get('country_user_info')[
            0].get('short_code')
        # email = response.body.get('account')
        print(response.body)

        new_marketplace = Marketplace(
            user_id=user_id,
            marketplace_name="lazada",
            shop_id=seller_id,
            shop_name=short_code,
            access_token=access_token,
            refresh_token=refresh_token
        )

        db.session.add(new_marketplace)
        db.session.commit()
        send_new_marketplace_email(user.email, user_id, "Lazada")

        responseObject = {
            'status': 'success',
            'message': 'Marketplaces connected successfully',
            'lazada_refresh': refresh_token,
            'lazada_token': access_token
        }

        return make_response(jsonify(responseObject)), 201

    else:
        responseObject = {
            'status': 'failed',
            'message': 'Authentication failed'
        }
        return make_response(jsonify(responseObject)), 401
Пример #3
0
def lazada_category_attributes():
    client = LazopClient('https://api.lazada.com.my/rest',
                         LAZADA_MARKET_KEY, LAZADA_MARKET_SECRET)
    request = LazopRequest('/category/attributes/get', 'GET')
    request.add_api_param('primary_category_id', '6614')
    response = client.execute(request)
    return jsonify(response.body)
Пример #4
0
def new_product(marketplace):
    client = LazopClient("https://api.lazada.com.my/rest", LAZADA_MARKET_KEY,
                         LAZADA_MARKET_SECRET)
    request = LazopRequest('/category/tree/get', 'GET')
    response = client.execute(request)
    print(response)
    print(response.type)
    print(response.body)
    import pdb
    pdb.set_trace()
    return render_template('products/new.html')
Пример #5
0
def check_order(order):
    client = LazopClient("https://api.lazada.com.my/rest",
                         LAZADA_MARKET_KEY, LAZADA_MARKET_SECRET)
    request = LazopRequest('/order/get', 'GET')
    request.add_api_param('order_id', str(order))
    response = client.execute(
        request, Marketplace.query.filter(
            Marketplace.user_id == current_user.id, Marketplace.marketplace_name == "lazada").first().access_token)
    print(response)
    print(response.type)
    print(response.body)
    import pdb
    pdb.set_trace()
    return redirect(url_for('home'))  # change redirect destination later
Пример #6
0
def lazada_category_tree():
    auth_header = request.headers.get('Authorization')
    if auth_header:
        auth_token = auth_header.split(" ")[1]
    else:
        responseObject = {
            'status': 'failed',
            'message': 'No authorization header found'
        }

        return make_response(jsonify(responseObject)), 401

    user_id = User.decode_auth_token(auth_token)
    user = User.query.get(user_id)

    if user:
        access_token = Marketplace.query.filter_by(
            user_id=user_id, marketplace_name='lazada').first().access_token
        refresh_token = Marketplace.query.filter_by(
            user_id=user_id, marketplace_name='lazada').first().refresh_token
        client = LazopClient('https://api.lazada.com.my/rest',
                             LAZADA_MARKET_KEY, LAZADA_MARKET_SECRET)
        laz_request = LazopRequest('/category/tree/get', 'GET')
        response = client.execute(laz_request)
        tree = response.body['data']
        # laz_request2 = LazopRequest('/brands/get', 'GET')
        # laz_request2.add_api_param('offset', '0')
        # laz_request2.add_api_param('limit', '1000')
        # response2 = client.execute(laz_request2)
        # brands = response2.body['data']

        responseObject = {
            'status': 'success',
            'message': 'category tree returned',
            'tree': tree,
            # 'brands': brands
        }

        return make_response(jsonify(responseObject)), 200

    else:
        responseObject = {
            'status': 'failed',
            'message': 'Authentication failed'
        }

        return make_response(jsonify(responseObject)), 401
Пример #7
0
def lazada_products():
    auth_header = request.headers.get('Authorization')
    if auth_header:
        auth_token = auth_header.split(" ")[1]
    else:
        responseObject = {
            'status': 'failed',
            'message': 'No authorization header found'
        }

        return make_response(jsonify(responseObject)), 401

    user_id = User.decode_auth_token(auth_token)
    user = User.query.get(user_id)

    if user:
        access_token = Marketplace.query.filter_by(
            user_id=user_id, marketplace_name='lazada').first().access_token
        refresh_token = Marketplace.query.filter_by(
            user_id=user_id, marketplace_name='lazada').first().refresh_token
        client = LazopClient('https://api.lazada.com.my/rest',
                             LAZADA_MARKET_KEY, LAZADA_MARKET_SECRET)
        laz_request = LazopRequest('/products/get', 'GET')
        laz_request.add_api_param('filter', 'live')
        laz_request.add_api_param('limit', '10')
        laz_request.add_api_param('options', '1')
        response = client.execute(laz_request, access_token)
        products = response.body['data']

        responseObject = {
            'status': 'success',
            'message': 'all products returned',
            'products': products
        }

        return make_response(jsonify(responseObject)), 200

    else:
        responseObject = {
            'status': 'failed',
            'message': 'Authentication failed'
        }

        return make_response(jsonify(responseObject)), 401
Пример #8
0
def lazada_new_product():
    auth_header = request.headers.get('Authorization')
    if auth_header:
        auth_token = auth_header.split(" ")[1]
    else:
        responseObject = {
            'status': 'failed',
            'message': 'No authorization header found'
        }

        return make_response(jsonify(responseObject)), 401

    user_id = User.decode_auth_token(auth_token)
    user = User.query.get(user_id)

    if user:
        post_data = request.form
        access_token = Marketplace.query.filter_by(
            user_id=user_id, marketplace_name='lazada').first().access_token
        refresh_token = Marketplace.query.filter_by(
            user_id=user_id, marketplace_name='lazada').first().refresh_token
        payload = post_data.get('payload')
        category_id = post_data.get('id')
        name = post_data.get('name')
        description = post_data.get('description')
        color = post_data.get('color')
        brand = post_data.get('brand')
        price = post_data.get('price')
        quantity = post_data.get('quantity')
        content = post_data.get('package_content')
        height = post_data.get('package_height')
        weight = post_data.get('package_weight')
        width = post_data.get('package_width')
        length = post_data.get('package_length')

        # grab the file
        file = request.files["image"]

        # check file size
        if len(file.read()) > (2 * 1024 * 1024):

            responseObject = {
                'status': 'fail',
                'message': "Max size allowed is 2 MB"
            }

            return make_response(jsonify(responseObject)), 400

        # check correct extension and upload if valid
        if file and allowed_file(file.filename):
            file.seek(0)
            serial_filename = f'{user_id}.{name}.{random.randint(1,100000)}.{file.filename}'
            file.filename = secure_filename(serial_filename)
            upload_image(file)

            new_image = Image(
                user_id=user_id,
                image_name=str(file.filename),
            )

            db.session.add(new_image)
            db.session.commit()

        else:

            responseObject = {
                'status': 'fail',
                'message': "Image format not supported"
            }

            return make_response(jsonify(responseObject)), 400

        image = new_image.image_url

        client = LazopClient('https://api.lazada.com.my/rest',
                             LAZADA_MARKET_KEY, LAZADA_MARKET_SECRET)
        laz_request = LazopRequest('/product/create')
        laz_request.add_api_param('payload', f"<?xml version=\"1.0\" encoding=\"UTF-8\" ?> <Request>     <Product>         <PrimaryCategory>{category_id}</PrimaryCategory>         <SPUId></SPUId>         <AssociatedSku></AssociatedSku>         <Attributes>             <name>{name}</name>             <short_description>{description}</short_description>             <brand>{brand}</brand>             <model>no model</model>    <warranty_type>No Warranty</warranty_type>     </Attributes>         <Skus>             <Sku>                 <SellerSku>MarketBucket-{name}</SellerSku>                 <color_family>{color}</color_family>                 <images><image>{image}</image></images>                                  <quantity>{quantity}</quantity>                 <price>{price}</price>                 <package_length>{length}</package_length>                 <package_height>{height}</package_height>                 <package_weight>{weight}</package_weight>                 <package_width>{width}</package_width>                 <package_content>{content}</package_content>                 <tax_class>default</tax_class>                         </Sku>         </Skus>     </Product> </Request>")
        response = client.execute(laz_request, access_token)
        code = response.code

        if code == '0':
            send_new_product_email(user.email, user_id, name)
            seller_sku = response.body['data']['sku_list'][0]['seller_sku']
            request2 = LazopRequest('/images/set')
            request2.add_api_param(
                'payload', f'<Request>   <Product>     <Skus>       <Sku>         <SellerSku>{seller_sku}</SellerSku>         <Images>           <Image>{image}</Image>                   </Images>       </Sku>     </Skus>   </Product> </Request>')
            response2 = client.execute(request2, access_token)
            responseObject = {
                'status': 'success',
                'message': f'{seller_sku} was added successfully to Lazada!',
            }
            return make_response(jsonify(responseObject)), 200

        else:
            field = response.body['detail'][0]['field']
            message = response.body['detail'][0]['message']
            responseObject = {
                'status': 'failed',
                'message': f'{field}: {message}',
            }
            return make_response(jsonify(responseObject)), 400

    else:
        responseObject = {
            'status': 'failed',
            'message': 'Authentication failed'
        }

        return make_response(jsonify(responseObject)), 401