<commit_before># -*- encoding: utf-8 -*-
from django.db import models

import reversion

from base.model_utils import TimeStampedModel


class ProductTypeManager(models.Manager):

    def create_product_type(self, slug, name):
        obj = self.model(
            slug=slug,
            name=name,
        )
        obj.save()
        return obj

    def init_product_type(self, slug, name):
        try:
            obj = self.model.objects.get(slug=slug)
            obj.name = name
            obj.save()
        except self.model.DoesNotExist:
            obj = self.create_product_type(slug, name)
        return obj


class ProductType(TimeStampedModel):
    """Type of product e.g. course or membership."""

    name = models.CharField(max_length=100)
    slug = models.SlugField(unique=True)
    objects = ProductTypeManager()

    class Meta:
        ordering = ('name',)
        verbose_name = 'Product type'
        verbose_name_plural = 'Product types'

    def __str__(self):
        return '{}'.format(self.name)

reversion.register(ProductType)


class ProductCategoryManager(models.Manager):

    def create_product_category(self, slug, name, product_type):
        obj = self.model(
            slug=slug,
            name=name,
            product_type=product_type,
        )
        obj.save()
        return obj

    def init_product_category(self, slug, name, product_type):
        try:
            obj = self.model.objects.get(slug=slug)
            obj.name = name
            obj.product_type = product_type
            obj.save()
        except self.model.DoesNotExist:
            obj = self.create_product_category(slug, name, product_type)
        return obj

    def product_type(self, slug):
        """List of all categories with the selected product type (slug)."""
        return self.model.objects.filter(product_type__slug=slug)


class ProductCategory(TimeStampedModel):
    """Category of product e.g. craft or gardening course."""

    name = models.CharField(max_length=100)
    slug = models.SlugField(unique=True)
    product_type = models.ForeignKey(ProductType)
    objects = ProductCategoryManager()

    class Meta:
        ordering = ('name',)
        verbose_name = 'Product category'
        verbose_name_plural = 'Product categories'

    def __str__(self):
        return '{}'.format(self.name)

reversion.register(ProductCategory)


class ProductManager(models.Manager):

    def create_product(self, slug, name, description, price, category):
        obj = self.model(
            slug=slug,
            name=name,
            description=description,
            price=price,
            category=category,
        )
        obj.save()
        return obj

    def init_product(self, slug, name, description, price, category):
        try:
            obj = self.model.objects.get(slug=slug)
            obj.name = name
            obj.description = description
            obj.price = price
            obj.category = category
            obj.save()
        except self.model.DoesNotExist:
            obj = self.create_product(slug, name, description, price, category)
        return obj

    def product_type(self, slug):<commit_msg>product - don't update price #906<commit_after># -*- encoding: utf-8 -*-
from django.db import models

import reversion

from base.model_utils import TimeStampedModel


class ProductTypeManager(models.Manager):

    def create_product_type(self, slug, name):
        obj = self.model(
            slug=slug,
            name=name,
        )
        obj.save()
        return obj

    def init_product_type(self, slug, name):
        try:
            obj = self.model.objects.get(slug=slug)
            obj.name = name
            obj.save()
        except self.model.DoesNotExist:
            obj = self.create_product_type(slug, name)
        return obj


class ProductType(TimeStampedModel):
    """Type of product e.g. course or membership."""

    name = models.CharField(max_length=100)
    slug = models.SlugField(unique=True)
    objects = ProductTypeManager()

    class Meta:
        ordering = ('name',)
        verbose_name = 'Product type'
        verbose_name_plural = 'Product types'

    def __str__(self):
        return '{}'.format(self.name)

reversion.register(ProductType)


class ProductCategoryManager(models.Manager):

    def create_product_category(self, slug, name, product_type):
        obj = self.model(
            slug=slug,
            name=name,
            product_type=product_type,
        )
        obj.save()
        return obj

    def init_product_category(self, slug, name, product_type):
        try:
            obj = self.model.objects.get(slug=slug)
            obj.name = name
            obj.product_type = product_type
            obj.save()
        except self.model.DoesNotExist:
            obj = self.create_product_category(slug, name, product_type)
        return obj

    def product_type(self, slug):
        """List of all categories with the selected product type (slug)."""
        return self.model.objects.filter(product_type__slug=slug)


class ProductCategory(TimeStampedModel):
    """Category of product e.g. craft or gardening course."""

    name = models.CharField(max_length=100)
    slug = models.SlugField(unique=True)
    product_type = models.ForeignKey(ProductType)
    objects = ProductCategoryManager()

    class Meta:
        ordering = ('name',)
        verbose_name = 'Product category'
        verbose_name_plural = 'Product categories'

    def __str__(self):
        return '{}'.format(self.name)

reversion.register(ProductCategory)


class ProductManager(models.Manager):

    def create_product(self, slug, name, description, price, category):
        obj = self.model(
            slug=slug,
            name=name,
            description=description,
            price=price,
            category=category,
        )
        obj.save()
        return obj

    def init_product(self, slug, name, description, price, category):
        try:
            obj = self.model.objects.get(slug=slug)
            obj.name = name
            obj.description = description
            # we don't update the price if it is already set
            # obj.price = price
            obj.category = category
            obj.save()
        except self.model.DoesNotExist:
            obj = self.create_product(slug, name, description, price, category)
        return obj

    def product_type(self, slug):
{'fraction_non_alphanumeric': 0.097138437741686, 'fraction_numerical': 0.0026295436968290795, 'mean_word_length': 2.2541519879214897, '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': '3048093', 'n_tokens_mistral': 1847, 'n_tokens_neox': 1800, 'n_words': 429}