<commit_before>#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2
# as published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
# Authors:
# <NAME> <<EMAIL>>, http://caner.candan.fr
# <NAME> <<EMAIL>>, http://www.vegeclic.fr
#

from django.conf import settings
from django.utils.translation import ugettext_lazy as _
from django.utils import translation
from django.core.management.base import BaseCommand, CommandError, NoArgsCommand
import datetime
from dateutil.relativedelta import relativedelta
from isoweek import Week
from ... import models
from mailbox import models as mm
from customers import models as cm
import logging, logging.config
import numpy as np, random
import matplotlib.pyplot as plt

logging.config.fileConfig('carts/management/commands/logging.conf')

class Command(NoArgsCommand):
    help = 'Fullfill the validated subscription deliveries content'

    def handle_noargs(self, **options):
        translation.activate('fr')

        logging.debug('Command in progress')

        pop_size = 50
        max_gen = 50
        lambda_algo = 100
        cxpb = .7
        mutpb = .2
        min_quantity = 0
        max_quantity = 2
        margin = .1
        zero = True
        debug = False

        if debug: logging.debug('DEBUG MODE')

        week_limit = Week.withdate(Week.thisweek().sunday() + relativedelta(days=9))
        deliveries = models.Delivery.objects.filter(date__lte=week_limit, status='p', subscription__enabled=True)
        for delivery in deliveries:
            logging.debug(delivery.__unicode__())

            subscription = delivery.subscription
            subscription_weight = subscription.size.weight - subscription.size.weight*settings.PACKAGING_WEIGHT_RATE/100
            subscription_price = subscription.price().price
            carrier = subscription.carrier
            weight_level = carrier.carrierlevel_set.filter(weight__gte=subscription.size.weight)
            if weight_level:
                logging.debug('weight_level: %s kg (%s €)' % (weight_level[0].weight, weight_level[0].price))
                subscription_price -= weight_level[0].price

            logging.debug('carrier: %s' % carrier.name)
            logging.debug('subscription_weight: %s kg' % subscription_weight)
            logging.debug('subscription_price: %s € (%s €)' % (subscription_price, subscription.price().price))

            for extent in subscription.extent_set.all():
                __extent = extent.extent

                logging.debug('meta-product: %s, extent: %s' % (extent.product.name, __extent))

                if extent.customized:
                    logging.debug("start fullfilling the delivery cart with a custom content")

                    logging.debug("create custom content object")
                    content = delivery.content_set.create(product=extent.product, extent=extent.extent, customized=extent.customized) if not debug else None
                    extent_content = extent.extentcontent_set.get()

                    logging.debug('after')

                    for ecp in extent_content.extentcontentproduct_set.all():
                        logging.debug("add product %s (%d) with a quantity %d (price: %s, weight: %s)" % (ecp.product.name, ecp.product.id, ecp.quantity, ecp.product.price().__unicode__(), ecp.product.weight))
                        if not debug:
                            content.contentproduct_set.create(product=ecp.product, quantity=ecp.quantity)

                    continue

                def get_product_products(product):
                    __products = []
                    for child in product.products_children.all():
                        __products += get_product_products(child)
                    __products += product.product_product.language('fr').filter(status='p').all()
                    return __products

                products = get_product_products(extent.product)
                nbr_items = len(products)

                prices = [p.price().get_after_tax_price_with_fee() if carrier.apply_suppliers_fee else p.price().get_after_tax_price() for p in products]
                weights = [int(p.weight) for p in products]

                criterias = []
                if subscription.criterias.all():
                    for p in products:
                        __filter = p.criterias
                        for c in subscription.criterias.filter(enabled=True).all():
                            __filter = __filter.filter(id=c.id)
                        criterias.append(len(__filter.all()))
                else:
                    criterias = [0] * nbr_items

                total_price = round(subscription_price*__extent/100, 2)
                total_weight = round(subscription_weight*__extent/100*1000, 2)
                total_criteria = round(sum(criterias), 2)

                from deap import algorithms, base, creator, tools

                pareto = tools.selNSGA2
                # pareto = tools.selSPEA2<commit_msg>* carts/management/commands/fullfillcarts.py: use new language translator and supplier product main price field<commit_after>#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2
# as published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
# Authors:
# <NAME> <<EMAIL>>, http://caner.candan.fr
# <NAME> <<EMAIL>>, http://www.vegeclic.fr
#

from django.conf import settings
from django.utils.translation import ugettext_lazy as _
from django.utils import translation
from django.core.management.base import BaseCommand, CommandError, NoArgsCommand
import datetime
from dateutil.relativedelta import relativedelta
from isoweek import Week
from ... import models
from mailbox import models as mm
from customers import models as cm
import logging, logging.config
import numpy as np, random
import matplotlib.pyplot as plt

logging.config.fileConfig('carts/management/commands/logging.conf')

class Command(NoArgsCommand):
    help = 'Fullfill the validated subscription deliveries content'

    def handle_noargs(self, **options):
        translation.activate('fr')

        logging.debug('Command in progress')

        pop_size = 50
        max_gen = 50
        lambda_algo = 100
        cxpb = .7
        mutpb = .2
        min_quantity = 0
        max_quantity = 2
        margin = .1
        zero = True
        debug = False

        if debug: logging.debug('DEBUG MODE')

        week_limit = Week.withdate(Week.thisweek().sunday() + relativedelta(days=9))
        deliveries = models.Delivery.objects.filter(date__lte=week_limit, status='p', subscription__enabled=True)
        for delivery in deliveries:
            logging.debug(delivery.__unicode__())

            subscription = delivery.subscription
            subscription_weight = subscription.size.weight - subscription.size.weight*settings.PACKAGING_WEIGHT_RATE/100
            subscription_price = subscription.price().price
            carrier = subscription.carrier
            weight_level = carrier.carrierlevel_set.filter(weight__gte=subscription.size.weight)
            if weight_level:
                logging.debug('weight_level: %s kg (%s €)' % (weight_level[0].weight, weight_level[0].price))
                subscription_price -= weight_level[0].price

            logging.debug('carrier: %s' % carrier.name)
            logging.debug('subscription_weight: %s kg' % subscription_weight)
            logging.debug('subscription_price: %s € (%s €)' % (subscription_price, subscription.price().price))

            for extent in subscription.extent_set.all():
                __extent = extent.extent

                logging.debug('meta-product: %s, extent: %s' % (extent.product.name, __extent))

                if extent.customized:
                    logging.debug("start fullfilling the delivery cart with a custom content")

                    logging.debug("create custom content object")
                    content = delivery.content_set.create(product=extent.product, extent=extent.extent, customized=extent.customized) if not debug else None
                    extent_content = extent.extentcontent_set.get()

                    logging.debug('after')

                    for ecp in extent_content.extentcontentproduct_set.all():
                        logging.debug("add product %s (%d) with a quantity %d (price: %s, weight: %s)" % (ecp.product.name, ecp.product.id, ecp.quantity, ecp.product.price().__unicode__(), ecp.product.weight))
                        if not debug:
                            content.contentproduct_set.create(product=ecp.product, quantity=ecp.quantity)

                    continue

                def get_product_products(product):
                    __products = []
                    for child in product.products_children.all():
                        __products += get_product_products(child)
                    __products += product.product_product.filter(status='p').all()
                    return __products

                products = get_product_products(extent.product)
                nbr_items = len(products)

                prices = [p.main_price.get_after_tax_price_with_fee() if carrier.apply_suppliers_fee else p.main_price.get_after_tax_price() for p in products]
                weights = [int(p.weight) for p in products]

                criterias = []
                if subscription.criterias.all():
                    for p in products:
                        __filter = p.criterias
                        for c in subscription.criterias.filter(enabled=True).all():
                            __filter = __filter.filter(id=c.id)
                        criterias.append(len(__filter.all()))
                else:
                    criterias = [0] * nbr_items

                total_price = round(subscription_price*__extent/100, 2)
                total_weight = round(subscription_weight*__extent/100*1000, 2)
                total_criteria = round(sum(criterias), 2)

                from deap import algorithms, base, creator, tools

                pareto = tools.selNSGA2
                # pareto = tools.selSPEA2
{'fraction_non_alphanumeric': 0.08842313573805882, 'fraction_numerical': 0.008995232526760816, 'mean_word_length': 2.3477868112014453, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '357715', 'n_tokens_mistral': 3016, 'n_tokens_neox': 2832, 'n_words': 798}