<commit_before>"""
TieredWeight shipping models
"""
import logging

from datetime import date
from django.db import models
from django.conf import settings
from django.utils.translation import get_language, ugettext_lazy as _
from l10n.models import Country
from shipping.modules.base import BaseShipper

try:
    from decimal import Decimal
except:
    from django.utils._decimal import Decimal


log = logging.getLogger('shipping.TieredWeight')


class TieredWeightException(Exception):
    pass


def _get_cart_weight(cart):
    weight = Decimal('0.0')
    for item in cart.cartitem_set.all():
        if item.is_shippable and item.product.weight:
            weight = weight + (item.product.weight * item.quantity)
    return weight


class Shipper(BaseShipper):
    def __init__(self, carrier):
        self.id = 'tieredweight_%i' % carrier.pk
        self._carrier = carrier
        super(BaseShipper, self).__init__(self)


    def calculate(self, cart, contact):
        """
        Perform shipping calculations
        """
        self._cost, self._weight = None, None
        self._zone = self._carrier.get_zone(contact._shipping_address().country)

        if self._zone:
            try:
                self._weight = _get_cart_weight(cart)
                self._cost = self._zone.cost(self._weight)
            except TieredWeightException:
                pass

        super(Shipper, self).calculate(cart, contact)


    def __str__(self):
        """
        This is mainly helpful for debugging purposes
        """
        return "TieredWeight_Shipper: %s" % self.id


    def cost(self):
        """<commit_msg>Fix init bug in tiered shipping. Closes #949<commit_after>"""
TieredWeight shipping models
"""
import logging

from datetime import date
from django.db import models
from django.conf import settings
from django.utils.translation import get_language, ugettext_lazy as _
from l10n.models import Country
from shipping.modules.base import BaseShipper

try:
    from decimal import Decimal
except:
    from django.utils._decimal import Decimal


log = logging.getLogger('shipping.TieredWeight')


class TieredWeightException(Exception):
    pass


def _get_cart_weight(cart):
    weight = Decimal('0.0')
    for item in cart.cartitem_set.all():
        if item.is_shippable and item.product.weight:
            weight = weight + (item.product.weight * item.quantity)
    return weight


class Shipper(BaseShipper):
    def __init__(self, carrier):
        self.id = 'tieredweight_%i' % carrier.pk
        self._carrier = carrier
        super(BaseShipper, self).__init__()


    def calculate(self, cart, contact):
        """
        Perform shipping calculations
        """
        self._cost, self._weight = None, None
        self._zone = self._carrier.get_zone(contact._shipping_address().country)

        if self._zone:
            try:
                self._weight = _get_cart_weight(cart)
                self._cost = self._zone.cost(self._weight)
            except TieredWeightException:
                pass

        super(Shipper, self).calculate(cart, contact)


    def __str__(self):
        """
        This is mainly helpful for debugging purposes
        """
        return "TieredWeight_Shipper: %s" % self.id


    def cost(self):
        """
{'fraction_non_alphanumeric': 0.09054998480704952, 'fraction_numerical': 0.003342449103615922, 'mean_word_length': 2.653718091009989, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5212142', 'n_tokens_mistral': 963, 'n_tokens_neox': 919, 'n_words': 243}