Пример #1
0
def calculate_ups_rates(weight, street, city, state, zip, country):
    settings = get_settings()
    credentials = {
        'username': settings.ups_username,
        'password': settings.ups_password,
        'access_license': settings.ups_api_key,
        'shipper_number': settings.ups_account,
    }
    shipper = Address(
        name=settings.ship_from_name,
        address=settings.ship_from_address,
        city=settings.ship_from_city,
        state=settings.ship_from_state,
        zip=settings.ship_from_zip,
        country=settings.ship_from_country,
    )

    client = UPSClient(credentials, weight_unit='LBS', dimension_unit='IN')
    country = UPS_COUNTRY_MAP[country]
    recipient = Address(name='Recipient',
                        city=city,
                        address=street,
                        state=state,
                        zip=zip,
                        country=country)
    packages = [Package(weight, 0, 0, 0)]
    res = client.rate(
        packages=packages,
        packaging_type='02',  # Customer-supplied packaging
        shipper=shipper,
        recipient=recipient)
    rates = {s['service']: Decimal(s['cost']) for s in res['info']}
    return rates
Пример #2
0
    def test_rate_service(self):
        credentials = {
            'username': '',
            'password': '',
            'access_license': '',
            'shipper_number': ''
        }

        shipper = Address(name='shipper address name',
                          city='rio de janeiro',
                          address=u'R. Mexico, 3 - 13º andar',
                          address2='Centro',
                          state='',
                          zip='20031144',
                          country='BR')

        recipient = Address(name='shipper address name',
                            city='anniston',
                            address='',
                            state='AL',
                            zip='36201',
                            country='US')

        packages = [Package(2, 3, 4, 5)]

        ups = UPSClient(credentials)
        response = ups.rate(packages=packages,
                            packaging_type='21',
                            shipper=shipper,
                            recipient=recipient)
Пример #3
0
def calculate_ups_rates(weight, street, city, state, zip, country):
    settings = get_settings()
    credentials = {
        'username': settings.ups_username,
        'password': settings.ups_password,
        'access_license': settings.ups_api_key,
        'shipper_number': settings.ups_account,
    }
    shipper = Address(
        name=settings.ship_from_name,
        address=settings.ship_from_address,
        city=settings.ship_from_city,
        state=settings.ship_from_state,
        zip=settings.ship_from_zip,
        country=settings.ship_from_country,
    )

    client = UPSClient(credentials, weight_unit='LBS', dimension_unit='IN')
    country = UPS_COUNTRY_MAP[country]
    recipient = Address(
        name='Recipient',
        city=city, address=street, state=state, zip=zip, country=country)
    packages = [Package(weight, 0, 0, 0)]
    res = client.rate(
        packages=packages, packaging_type='02',  # Customer-supplied packaging
        shipper=shipper, recipient=recipient)
    rates = {s['service']: Decimal(s['cost']) for s in res['info']}
    return rates
Пример #4
0
    def _get_quote(self):
        ups_cfg = self._ups_config()

        credentials = {
            'username': ups_cfg.username,
            'password': ups_cfg.password,
            'access_license': ups_cfg.access_license,
            'shipper_number': ups_cfg.shipper_number,
        }

        shipper = Address(
            name=ups_cfg.shipper_name,
            address=ups_cfg.shipper_address,
            city=ups_cfg.shipper_city,
            state=ups_cfg.shipper_state,
            zip=ups_cfg.shipper_zipcode,
            country=ups_cfg.shipper_country.code
        )

        customer = get_customer(self.request)
        ship_address = customer.get_selected_shipping_address()
        recipient = Address(
            name=' '.join([ship_address.firstname or '', ship_address.lastname or '']),
            address=' '.join([ship_address.line1 or '', ship_address.line2 or '']),
            city=ship_address.city,
            state=ship_address.state,
            zip=ship_address.zip_code,
            country=ship_address.country.code
        )

        cart = get_cart(self.request)

        #weight, length, width, height
        product_info = [0, 0, 0, 0]
        for line_item in cart.get_items():
            product_info[0] += line_item.product.weight * line_item.amount
            product_info[1] += line_item.product.length * line_item.amount
            product_info[2] += line_item.product.width * line_item.amount
            product_info[3] += line_item.product.height * line_item.amount

        #import pdb; pdb.set_trace()
        quote = 0.0
        if all(product_info):
            packages = [Package(*product_info)]
            ups = UPSClient(credentials)
            response = ups.rate(
                packages=packages,
                packaging_type=ups_cfg.default_packaging_type,
                shipper=shipper,
                recipient=recipient
            )
            quote = float(response['info'][0]['cost'])

        return quote
Пример #5
0
def international(request):
    mro_credentials = {
        "username": "******",
        "password": "******",
        "access_license": "6CC3FBEBBDC01626",
        "shipper_number": "929702",
    }
    if request.method == "POST":
        form = InternationalForm(request.POST)
        # return HttpResponse(form.is_valid())
        if form.is_valid():
            from_zipcode = form.cleaned_data["zipcode1"]
            to_zipcode = form.cleaned_data["zipcode2"]
            from_country = form.cleaned_data["from_country"]
            to_country = form.cleaned_data["to_country"]
            weight = form.cleaned_data["weight"]
            carrier = form.cleaned_data["carrier"]

            package = [Package(weight)]
            from_address = Address(zip=from_zipcode, country=from_country)
            to_address = Address(zip=to_zipcode, country=to_country)

            if carrier == "UPS":
                ups = UPSClient(mro_credentials)
                response = ups.rate(packages=package, packaging_type="02", shipper=from_address, recipient=to_address)
                content = response["info"]
            else:
                dhl = DHLClient()
                response = dhl.rate(packages=package, shipper=from_address, recipient=to_address)
                content = response["info"]
            table = "<table><thead><tr><th>Service</th><th>Price</th></tr></thead><tbody>"
            for result in content:
                shipping_method = result["service"]
                price = result["total_cost"]
                table += "<tr><td>%s</td><td>%s</td></tr>" % (shipping_method, price)
            table += "</tbody></table>"
            context = {
                "results": table,
                "info": "Done. These are all the results.",
                "zipcode1": from_zipcode,
                "zipcode2": to_zipcode,
                "weight": weight,
            }
            return render(request, "shipping/international.html", context)

        else:
            context = {"info": "The data introduced is not valid."}
            return render(request, "shipping/international.html", context)
    else:
        form = InputForm()
        context = {"results": ""}
        return render(request, "shipping/international.html", context)
Пример #6
0
    def _get_quote(self):
        ups_cfg = self._ups_config()

        credentials = {
            'username': ups_cfg.username,
            'password': ups_cfg.password,
            'access_license': ups_cfg.access_license,
            'shipper_number': ups_cfg.shipper_number,
        }

        shipper = Address(name=ups_cfg.shipper_name,
                          address=ups_cfg.shipper_address,
                          city=ups_cfg.shipper_city,
                          state=ups_cfg.shipper_state,
                          zip=ups_cfg.shipper_zipcode,
                          country=ups_cfg.shipper_country.code)

        customer = get_customer(self.request)
        ship_address = customer.get_selected_shipping_address()
        recipient = Address(name=' '.join(
            [ship_address.firstname or '', ship_address.lastname or '']),
                            address=' '.join([
                                ship_address.line1 or '', ship_address.line2
                                or ''
                            ]),
                            city=ship_address.city,
                            state=ship_address.state,
                            zip=ship_address.zip_code,
                            country=ship_address.country.code)

        cart = get_cart(self.request)

        #weight, length, width, height
        product_info = [0, 0, 0, 0]
        for line_item in cart.get_items():
            product_info[0] += line_item.product.weight * line_item.amount
            product_info[1] += line_item.product.length * line_item.amount
            product_info[2] += line_item.product.width * line_item.amount
            product_info[3] += line_item.product.height * line_item.amount

        #import pdb; pdb.set_trace()
        quote = 0.0
        if all(product_info):
            packages = [Package(*product_info)]
            ups = UPSClient(credentials)
            response = ups.rate(packages=packages,
                                packaging_type=ups_cfg.default_packaging_type,
                                shipper=shipper,
                                recipient=recipient)
            quote = float(response['info'][0]['cost'])

        return quote
Пример #7
0
    def __init__(self, ups_carrier):

        credentials = {
            'username': ups_carrier.ups_login,
            'password': ups_carrier.ups_password,
            'access_license': ups_carrier.ups_api_key,
            'shipper_number': ups_carrier.ups_id,
        }
        self.shipper = Address(name='shipper address name',
                               city=ups_carrier.city,
                               address=ups_carrier.address_line_1,
                               state=ups_carrier.state.iso,
                               zip=ups_carrier.zip_code,
                               country=ups_carrier.country.iso,
                               address2=ups_carrier.address_line_2)

        self.ups = UPSClient(credentials,
                             weight_unit=ups_carrier.weight_unit,
                             dimension_unit=ups_carrier.dimension_unit,
                             currency_code=ups_carrier.currency_code)

        self.package_type = ups_carrier.package_type
Пример #8
0
    def test_rate_service(self):
        credentials = {
            'username': '',
            'password': '',
            'access_license': '',
            'shipper_number': ''
        }

        shipper = Address(name='shipper address name', city='rio de janeiro',
            address=u'R. Mexico, 3 - 13º andar', address2='Centro',
            state='', zip='20031144',
            country='BR')

        recipient = Address(name='shipper address name', city='anniston',
            address='', state='AL', zip='36201',
            country='US')

        packages = [Package(2, 3, 4, 5)]

        ups = UPSClient(credentials)
        response = ups.rate(packages=packages, packaging_type='21',
            shipper=shipper, recipient=recipient)
Пример #9
0
    def __init__(self, ups_carrier):

        credentials = {
            'username': ups_carrier.ups_login,
            'password': ups_carrier.ups_password,
            'access_license': ups_carrier.ups_api_key,
            'shipper_number': ups_carrier.ups_id,
        }
        self.shipper = Address(name='shipper address name', city=ups_carrier.city,
            address=ups_carrier.address_line_1, state=ups_carrier.state.iso,
            zip=ups_carrier.zip_code, country=ups_carrier.country.iso,
            address2=ups_carrier.address_line_2)

        self.ups = UPSClient(credentials, weight_unit=ups_carrier.weight_unit,
            dimension_unit=ups_carrier.dimension_unit, currency_code=ups_carrier.currency_code)

        self.package_type = ups_carrier.package_type
Пример #10
0
class UPSInterface(object):

    def __init__(self, ups_carrier):

        credentials = {
            'username': ups_carrier.ups_login,
            'password': ups_carrier.ups_password,
            'access_license': ups_carrier.ups_api_key,
            'shipper_number': ups_carrier.ups_id,
        }
        self.shipper = Address(name='shipper address name', city=ups_carrier.city,
            address=ups_carrier.address_line_1, state=ups_carrier.state.iso,
            zip=ups_carrier.zip_code, country=ups_carrier.country.iso,
            address2=ups_carrier.address_line_2)

        self.ups = UPSClient(credentials, weight_unit=ups_carrier.weight_unit,
            dimension_unit=ups_carrier.dimension_unit, currency_code=ups_carrier.currency_code)

        self.package_type = ups_carrier.package_type

    def get_shipping_cost(self, bin, packages, country, zipcode=None, state=None, city=None):

        ups_packages = []
        for pack in packages:
            weight_total = sum([package.weight for package in pack]) + bin.weight

            ups_packages.append(UPSPackage(weight=weight_total, length=bin.length,
            width=bin.width, height=bin.height))

        state_iso = state.iso if state else None
        city = city or ''
        zipcode = zipcode or ''
        recipient = Address(name='recipient address name', city=city,
            address='', zip=zipcode, country=country.iso, state=state_iso)

        try:
            rate_result = self.ups.rate(ups_packages, self.shipper, recipient, self.package_type)
        except UPSError, e:
            raise InterfaceError(str(e))

        return rate_result['info'][0]['cost'], rate_result['info'][0]['currency']
Пример #11
0
class UPSInterface(object):
    def __init__(self, ups_carrier):

        credentials = {
            'username': ups_carrier.ups_login,
            'password': ups_carrier.ups_password,
            'access_license': ups_carrier.ups_api_key,
            'shipper_number': ups_carrier.ups_id,
        }
        self.shipper = Address(name='shipper address name',
                               city=ups_carrier.city,
                               address=ups_carrier.address_line_1,
                               state=ups_carrier.state.iso,
                               zip=ups_carrier.zip_code,
                               country=ups_carrier.country.iso,
                               address2=ups_carrier.address_line_2)

        self.ups = UPSClient(credentials,
                             weight_unit=ups_carrier.weight_unit,
                             dimension_unit=ups_carrier.dimension_unit,
                             currency_code=ups_carrier.currency_code)

        self.package_type = ups_carrier.package_type

    def get_shipping_cost(self,
                          bin,
                          packages,
                          country,
                          zipcode=None,
                          state=None,
                          city=None):

        ups_packages = []
        for pack in packages:
            weight_total = sum([package.weight
                                for package in pack]) + bin.weight

            ups_packages.append(
                UPSPackage(weight=weight_total,
                           length=bin.length,
                           width=bin.width,
                           height=bin.height))

        state_iso = state.iso if state else None
        city = city or ''
        zipcode = zipcode or ''
        recipient = Address(name='recipient address name',
                            city=city,
                            address='',
                            zip=zipcode,
                            country=country.iso,
                            state=state_iso)

        try:
            rate_result = self.ups.rate(ups_packages, self.shipper, recipient,
                                        self.package_type)
        except UPSError, e:
            raise InterfaceError(str(e))

        return rate_result['info'][0]['cost'], rate_result['info'][0][
            'currency']