<commit_before>from __future__ import unicode_literals

from django.core.exceptions import ImproperlyConfigured
from django.db import models
from django.utils.encoding import python_2_unicode_compatible

from ..fields import MoneyField
from ..settings import bazaar_settings


class PriceListManager(models.Manager):
    use_for_related_fields = True

    def get_default(self):
        """
        Return the default price list instance
        """
        try:
            return self.get_query_set().get(pk=bazaar_settings.DEFAULT_PRICE_LIST_ID)
        except PriceList.DoesNotExist:
            raise ImproperlyConfigured("A default price list must exists. Please create one")


@python_2_unicode_compatible
class PriceList(models.Model):
    name = models.CharField(max_length=100, unique=True)

    objects = PriceListManager()

    def __str__(self):
        return self.name


@python_2_unicode_compatible
class Product(models.Model):
    name = models.CharField(max_length=100)
    description = models.TextField(max_length=500, blank=True)
    price_lists = models.ManyToManyField("PriceList", through="ProductPrice",
                                         related_name="products")

    class UnsavedException(Exception):
        pass

    @property
    def cost(self):
        """
        Defines the cost of the good
        """
        raise NotImplementedError

    @property
    def price(self):
        """
        The price for the product on the default price list
        """
        # Use .all() to allow access to prefetched queryset
        for product_price in self.prices.all():
            if product_price.price_list_id == bazaar_settings.DEFAULT_PRICE_LIST_ID:
                return product_price.price

        return 0

    def set_price(self, price):
        """
        Sets the price for the product on the default price list<commit_msg>Product price returns money instance when it is 0<commit_after>from __future__ import unicode_literals

from django.core.exceptions import ImproperlyConfigured
from django.db import models
from django.utils.encoding import python_2_unicode_compatible

from moneyed import Money

from ..fields import MoneyField
from ..settings import bazaar_settings


class PriceListManager(models.Manager):
    use_for_related_fields = True

    def get_default(self):
        """
        Return the default price list instance
        """
        try:
            return self.get_query_set().get(pk=bazaar_settings.DEFAULT_PRICE_LIST_ID)
        except PriceList.DoesNotExist:
            raise ImproperlyConfigured("A default price list must exists. Please create one")


@python_2_unicode_compatible
class PriceList(models.Model):
    name = models.CharField(max_length=100, unique=True)

    objects = PriceListManager()

    def __str__(self):
        return self.name


@python_2_unicode_compatible
class Product(models.Model):
    name = models.CharField(max_length=100)
    description = models.TextField(max_length=500, blank=True)
    price_lists = models.ManyToManyField("PriceList", through="ProductPrice",
                                         related_name="products")

    class UnsavedException(Exception):
        pass

    @property
    def cost(self):
        """
        Defines the cost of the good
        """
        raise NotImplementedError

    @property
    def price(self):
        """
        The price for the product on the default price list
        """
        # Use .all() to allow access to prefetched queryset
        for product_price in self.prices.all():
            if product_price.price_list_id == bazaar_settings.DEFAULT_PRICE_LIST_ID:
                return product_price.price

        return Money(0, bazaar_settings.DEFAULT_CURRENCY)

    def set_price(self, price):
        """
        Sets the price for the product on the default price list
{'fraction_non_alphanumeric': 0.0722077922077922, 'fraction_numerical': 0.007012987012987013, 'mean_word_length': 2.835657370517928, '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': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7621716', 'n_tokens_mistral': 1062, 'n_tokens_neox': 1015, 'n_words': 282}