<commit_before>from decimal import Decimal

from flask.ext.babel import lazy_gettext
from mongoengine import signals
import prices

from .. import app, db, get_locale
from ..mongo import TranslationsField

class Country(db.Document):
    code = db.StringField(required=True, max_length=10,
                          verbose_name=lazy_gettext('Code'))
    default_name = db.StringField(db_field='d_name', max_length=100,
                                  verbose_name=lazy_gettext('Default name'))
    name = TranslationsField(max_length=100)

    meta = {
        'ordering': ['code']
    }

    def __unicode__(self):
        return u'{} - {}'.format(self.code,
                                self.name.get(get_locale(), self.default_name))


class CountriesGroup(db.Document):
    name = db.StringField(max_length=50, verbose_name=lazy_gettext('Name'))
    countries = db.ListField(db.ReferenceField(
                        Country,
                        reverse_delete_rule=db.DENY,
                        verbose_name=lazy_gettext('Countries')
                        ))

    meta = {
        'ordering': ['name']
    }

    def __unicode__(self):
        return self.name

rounding = Decimal(str(app.config['SHIPPING_ROUNDING']))
multiplier = Decimal(str(app.config['SHIPPING_MULTIPLIER']))
preparation = Decimal(str(app.config['PACKAGE_PREPARATION_PRICE']))
tax = 1 + Decimal(str(app.config['SHIPPING_TAX']))/100
quantizer = Decimal('0.01')
class Carrier(db.Document):
    name = db.StringField(required=True, max_length=50,
                          verbose_name=lazy_gettext('Name'))
    description = TranslationsField(db_field='desc', max_length=100,
                                    verbose_name=lazy_gettext('Description'))
    countries = db.ListField(db.ReferenceField(
                        Country,
                        reverse_delete_rule=db.DENY,
                        verbose_name=lazy_gettext('Countries')
                        ))
    countries_groups = db.ListField(db.ReferenceField(
                                CountriesGroup,
                                reverse_delete_rule=db.DENY,
                                verbose_name=lazy_gettext('Countries groups')
                                ), db_field='cgroups')
    weights = db.SortedListField(db.IntField(choices=None),
                                 verbose_name=lazy_gettext('Weights'))
    tracking_url = db.StringField(
                        db_field='tr_url',
                        max_length=250,
                        verbose_name=lazy_gettext('Tracking URL (if any) - use "@" in place of the number')
                        )
    costs = db.DictField()

    meta = {
        'ordering': ['name']
    }

    def __unicode__(self):
        return '{} ({})'.format(self.description.get(get_locale(), u''),
                                self.name)

    def get_price(self, country_or_group, weight, which=None, exact=False):
        """Get the price for the association of a country and a weight

        country_or_group = "rest" for the rest of the world

        which:
        * 'purchasing' : the price we pay
        * 'gross' : the gross price anounced to the customers
        * 'net' : the net price paid by the customers
        * None (default) : a dict with all three

        exact: exact price (for dashboard), or the price immediately greater
               (for shipping calculation)
        """
        c_or_g = self.costs.get(unicode(country_or_group))
        if c_or_g:
            # Get the price for the weight that is equal or
            # immediately greater to the requested weight
            for w in sorted([int(i) for i in c_or_g.keys()]):
                if (exact and w == weight) or (not exact and w >= weight):
                    purchasing = Decimal(c_or_g[str(w)])
                    if which == 'purchasing':
                        return purchasing.quantize(quantizer)
                    net = (purchasing * multiplier + preparation) * tax
                    how_many_roundings = net // rounding + 1
                    net = rounding * how_many_roundings
                    if which == 'net':<commit_msg>Unicode bugfix<commit_after>from decimal import Decimal

from flask.ext.babel import lazy_gettext
from mongoengine import signals
import prices

from .. import app, db, get_locale
from ..mongo import TranslationsField

class Country(db.Document):
    code = db.StringField(required=True, max_length=10,
                          verbose_name=lazy_gettext('Code'))
    default_name = db.StringField(db_field='d_name', max_length=100,
                                  verbose_name=lazy_gettext('Default name'))
    name = TranslationsField(max_length=100)

    meta = {
        'ordering': ['code']
    }

    def __unicode__(self):
        return u'{} - {}'.format(self.code,
                                self.name.get(get_locale(), self.default_name))


class CountriesGroup(db.Document):
    name = db.StringField(max_length=50, verbose_name=lazy_gettext('Name'))
    countries = db.ListField(db.ReferenceField(
                        Country,
                        reverse_delete_rule=db.DENY,
                        verbose_name=lazy_gettext('Countries')
                        ))

    meta = {
        'ordering': ['name']
    }

    def __unicode__(self):
        return self.name

rounding = Decimal(str(app.config['SHIPPING_ROUNDING']))
multiplier = Decimal(str(app.config['SHIPPING_MULTIPLIER']))
preparation = Decimal(str(app.config['PACKAGE_PREPARATION_PRICE']))
tax = 1 + Decimal(str(app.config['SHIPPING_TAX']))/100
quantizer = Decimal('0.01')
class Carrier(db.Document):
    name = db.StringField(required=True, max_length=50,
                          verbose_name=lazy_gettext('Name'))
    description = TranslationsField(db_field='desc', max_length=100,
                                    verbose_name=lazy_gettext('Description'))
    countries = db.ListField(db.ReferenceField(
                        Country,
                        reverse_delete_rule=db.DENY,
                        verbose_name=lazy_gettext('Countries')
                        ))
    countries_groups = db.ListField(db.ReferenceField(
                                CountriesGroup,
                                reverse_delete_rule=db.DENY,
                                verbose_name=lazy_gettext('Countries groups')
                                ), db_field='cgroups')
    weights = db.SortedListField(db.IntField(choices=None),
                                 verbose_name=lazy_gettext('Weights'))
    tracking_url = db.StringField(
                        db_field='tr_url',
                        max_length=250,
                        verbose_name=lazy_gettext('Tracking URL (if any) - use "@" in place of the number')
                        )
    costs = db.DictField()

    meta = {
        'ordering': ['name']
    }

    def __unicode__(self):
        return u'{} ({})'.format(self.description.get(get_locale(), u''),
                                 self.name)

    def get_price(self, country_or_group, weight, which=None, exact=False):
        """Get the price for the association of a country and a weight

        country_or_group = "rest" for the rest of the world

        which:
        * 'purchasing' : the price we pay
        * 'gross' : the gross price anounced to the customers
        * 'net' : the net price paid by the customers
        * None (default) : a dict with all three

        exact: exact price (for dashboard), or the price immediately greater
               (for shipping calculation)
        """
        c_or_g = self.costs.get(unicode(country_or_group))
        if c_or_g:
            # Get the price for the weight that is equal or
            # immediately greater to the requested weight
            for w in sorted([int(i) for i in c_or_g.keys()]):
                if (exact and w == weight) or (not exact and w >= weight):
                    purchasing = Decimal(c_or_g[str(w)])
                    if which == 'purchasing':
                        return purchasing.quantize(quantizer)
                    net = (purchasing * multiplier + preparation) * tax
                    how_many_roundings = net // rounding + 1
                    net = rounding * how_many_roundings
                    if which == 'net':
{'fraction_non_alphanumeric': 0.09974823162690324, 'fraction_numerical': 0.006234264476681453, 'mean_word_length': 1.8716006884681584, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 9, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5191489', 'n_tokens_mistral': 2403, 'n_tokens_neox': 2238, 'n_words': 507}