<commit_before>from tg import flash
from tgextecommerce.lib.exceptions import AlreadyExistingSlugException
from tgextecommerce.lib.utils import slugify


class Models(object):
    def __init__(self):
        self._models = None

    @property
    def models(self):
        if self._models is None:
            from tgextecommerce.model import models
            self._models = models
        return self._models

    def __getattr__(self, item):
        return getattr(self.models, item)

models = Models()


class ShopManager(object):
    def create_product(self, type, sku, name, description='', price=1.0,
                       vat=0.0, qty=0, initial_quantity=0,
                       variety=None, active=True, valid_from=None, valid_to=None,
                       configuration_details=None, **details):
        if variety is None:
            variety = name

        if configuration_details is None:
            configuration_details = {}

        slug = slugify(name, type)
        if models.Product.query.find({'slug': slug}).first():
            raise AlreadyExistingSlugException('Already exist a Product with slug: %s' % slug)

        product = models.Product(type=type,
                                 name=name,
                                 description=description,
                                 slug=slug,
                                 details=details,
                                 active=active,
                                 valid_from=valid_from,
                                 valid_to=valid_to,
                                 configurations=[{'sku': sku,
                                                  'variety': variety,
                                                  'price': price,
                                                  'vat': vat,
                                                  'qty': qty,
                                                  'initial_quantity': initial_quantity,
                                                  'details': configuration_details}])

    def get_product(self, sku=None, _id=None):
        if _id is not None:
            return models.Product.query.get(_id=_id)
        else:
            return models.Product.query.find({'configurations.sku': sku}).first()

    def get_products(self, type, query=None, fields=[]):
        if query is None:
            query = {}
        query['type'] = type
        if fields:
            return models.Product.query.find(query, fields=fields)
        return models.Product.query.find(query).all()

    def create_product_configuration(self, slug, sku, price=1.0, vat=0.0, qty=0, initial_quantity=0,
                                     variety=None, **configuration_details):

        product = models.Product.query.find({'slug': slug}).first()
        product.configurations.append({'sku': sku,
                                       'variety': variety,
                                       'price': price,
                                       'vat': vat,
                                       'qty': qty,
                                       'initial_quantity': initial_quantity,
                                       'details': configuration_details})<commit_msg>lookup products by slug too<commit_after>from tg import flash
from tgextecommerce.lib.exceptions import AlreadyExistingSlugException
from tgextecommerce.lib.utils import slugify


class Models(object):
    def __init__(self):
        self._models = None

    @property
    def models(self):
        if self._models is None:
            from tgextecommerce.model import models
            self._models = models
        return self._models

    def __getattr__(self, item):
        return getattr(self.models, item)

models = Models()


class ShopManager(object):
    def create_product(self, type, sku, name, description='', price=1.0,
                       vat=0.0, qty=0, initial_quantity=0,
                       variety=None, active=True, valid_from=None, valid_to=None,
                       configuration_details=None, **details):
        if variety is None:
            variety = name

        if configuration_details is None:
            configuration_details = {}

        slug = slugify(name, type)
        if models.Product.query.find({'slug': slug}).first():
            raise AlreadyExistingSlugException('Already exist a Product with slug: %s' % slug)

        product = models.Product(type=type,
                                 name=name,
                                 description=description,
                                 slug=slug,
                                 details=details,
                                 active=active,
                                 valid_from=valid_from,
                                 valid_to=valid_to,
                                 configurations=[{'sku': sku,
                                                  'variety': variety,
                                                  'price': price,
                                                  'vat': vat,
                                                  'qty': qty,
                                                  'initial_quantity': initial_quantity,
                                                  'details': configuration_details}])

    def get_product(self, sku=None, _id=None, slug=None):
        if _id is not None:
            return models.Product.query.get(_id=_id)
        elif sku is not None:
            return models.Product.query.find({'configurations.sku': sku}).first()
        elif slug is not None:
            return models.Product.query.find({'slug': slug}).first()
        else:
            return None

    def get_products(self, type, query=None, fields=[]):
        if query is None:
            query = {}
        query['type'] = type
        if fields:
            return models.Product.query.find(query, fields=fields)
        return models.Product.query.find(query).all()

    def create_product_configuration(self, slug, sku, price=1.0, vat=0.0, qty=0, initial_quantity=0,
                                     variety=None, **configuration_details):

        product = models.Product.query.find({'slug': slug}).first()
        product.configurations.append({'sku': sku,
                                       'variety': variety,
                                       'price': price,
                                       'vat': vat,
                                       'qty': qty,
                                       'initial_quantity': initial_quantity,
                                       'details': configuration_details})
{'fraction_non_alphanumeric': 0.08914787346753443, 'fraction_numerical': 0.0036325109732102314, 'mean_word_length': 1.2825561312607945, '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': 54, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6190958', 'n_tokens_mistral': 1586, 'n_tokens_neox': 1516, 'n_words': 357}