<commit_before># -*- encoding: utf-8 -*-
from __future__ import unicode_literals

from django.utils.text import slugify

from stock.models import (
    Product,
    ProductCategory,
    ProductType,
)
from stock.tests.model_maker import (
    make_product,
    make_product_category,
    make_product_type,
)


def init_product(name, slug, description, price, product_category, **kwargs):
    """Create a new product - if it doesn't exist."""
    slug = slugify(slug)
    try:
        result = Product.objects.get(slug=slug)
        update = False
        # If the product exists - don't update it unless it is empty...
        # The user might have set a new price or description!!
        if not result.name:
            result.name = name or ''
            update = True
        if not result.description:
            result.description = description or ''
            update = True
        if not result.price:
            result.price = price
            update = True
        if result.category.slug == 'demo':
            result.category = product_category
            update = True
        if update:
            result.save()
    except Product.DoesNotExist:
        if not description:
            description = ''
        kwargs.update(dict(description=description))
        result = make_product(name, slug, price, product_category, **kwargs)
    return result


#def init_product_bundle(name, slug, product, price, **kwargs):
#    slug = slugify(slug)
#    try:
#        result = ProductBundle.objects.get(slug=slug)
#        # If the product exists - don't update it!!!
#        # The user might have set a new price or description!!
#    except ProductBundle.DoesNotExist:
#        result = make_product_bundle(name, slug, product, price, **kwargs)
#    return result
#
#
#def init_product_bundle_add_product(product_bundle, products):
#    """If a product has not been added to the bundle, then add it.
#
#    TODO When the system is live, I don't think we need this function as we
#    will have editing screens to add products to bundles.
#    """
#    for p in products:
#        try:
#            product_bundle.bundle.get(slug=p.slug)
#        except Product.DoesNotExist:
#            product_bundle.bundle.add(p)


def init_product_category(name, slug, product_type):
    result = None
    try:
        result = ProductCategory.objects.get(slug=slug)
    except ProductCategory.DoesNotExist:
        result = make_product_category(name, slug, product_type)
    return result


def init_product_type(name, slug):
    result = None
    try:
        result = ProductType.objects.get(slug=slug)
    except ProductType.DoesNotExist:
        result = make_product_type(name, slug)
    return result<commit_msg>Allow init_product_category and init_product_type to update<commit_after># -*- encoding: utf-8 -*-
from __future__ import unicode_literals

from django.utils.text import slugify

from stock.models import (
    Product,
    ProductCategory,
    ProductType,
)
from stock.tests.model_maker import (
    make_product,
    make_product_category,
    make_product_type,
)


def init_product(name, slug, description, price, product_category, **kwargs):
    """Create a new product - if it doesn't exist."""
    slug = slugify(slug)
    try:
        result = Product.objects.get(slug=slug)
        update = False
        # If the product exists - don't update it unless it is empty...
        # The user might have set a new price or description!!
        if not result.name:
            result.name = name or ''
            update = True
        if not result.description:
            result.description = description or ''
            update = True
        if not result.price:
            result.price = price
            update = True
        if result.category.slug == 'demo':
            result.category = product_category
            update = True
        if update:
            result.save()
    except Product.DoesNotExist:
        if not description:
            description = ''
        kwargs.update(dict(description=description))
        result = make_product(name, slug, price, product_category, **kwargs)
    return result


#def init_product_bundle(name, slug, product, price, **kwargs):
#    slug = slugify(slug)
#    try:
#        result = ProductBundle.objects.get(slug=slug)
#        # If the product exists - don't update it!!!
#        # The user might have set a new price or description!!
#    except ProductBundle.DoesNotExist:
#        result = make_product_bundle(name, slug, product, price, **kwargs)
#    return result
#
#
#def init_product_bundle_add_product(product_bundle, products):
#    """If a product has not been added to the bundle, then add it.
#
#    TODO When the system is live, I don't think we need this function as we
#    will have editing screens to add products to bundles.
#    """
#    for p in products:
#        try:
#            product_bundle.bundle.get(slug=p.slug)
#        except Product.DoesNotExist:
#            product_bundle.bundle.add(p)


def init_product_category(name, slug, product_type):
    result = None
    try:
        result = ProductCategory.objects.get(slug=slug)
        update = False
        if result.name != name:
            result.name = name or ''
            update = True
        if result.product_type.slug != product_type.slug:
            result.product_type = product_type
            update = True
        if update:
            result.save()
    except ProductCategory.DoesNotExist:
        result = make_product_category(name, slug, product_type)
    return result


def init_product_type(name, slug):
    result = None
    try:
        result = ProductType.objects.get(slug=slug)
        if result.name != name:
            result.name = name or ''
            result.save()
    except ProductType.DoesNotExist:
        result = make_product_type(name, slug)
    return result
{'fraction_non_alphanumeric': 0.08999830191883172, 'fraction_numerical': 0.00033961623365596876, 'mean_word_length': 2.4484777517564402, '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': '4464930', 'n_tokens_mistral': 1654, 'n_tokens_neox': 1641, 'n_words': 497}