<commit_before>from django.db import models
from django.utils.translation import ugettext_lazy as _

import plata
from plata.product.models import ProductBase
from plata.shop.models import PriceBase


class Product(ProductBase):
    """(Nearly) the simplest product model ever"""

    is_active = models.BooleanField(_('is active'), default=True)
    name = models.CharField(_('name'), max_length=100)
    slug = models.SlugField(_('slug'), unique=True)
    ordering = models.PositiveIntegerField(_('ordering'), default=0)

    description = models.TextField(_('description'), blank=True)

    class Meta:
        ordering = ['ordering', 'name']
        verbose_name = _('product')
        verbose_name_plural = _('products')

    def __unicode__(self):
        return self.name

    @models.permalink
    def get_absolute_url(self):
        return ('plata_product_detail', (), {'object_id': self.pk})

    def get_price(self, currency=None, orderitem=None):
        if currency is None:
            currency = (orderitem.currency if orderitem else
                plata.shop_instance().default_currency())

        possible = self.prices.filter(
            currency=currency,
            from_quantity__lte=(orderitem.quantity if orderitem else 0))

        try:
            return possible.order_by('-from_quantity')[0]
        except IndexError:
            raise possible.model.DoesNotExist


class ProductPrice(PriceBase):
    product = models.ForeignKey(Product, verbose_name=_('product'),
        related_name='prices')
    from_quantity = models.IntegerField(_('From quantity'), default=0)

    class Meta:
        get_latest_by = 'id'
        ordering = ['from_quantity']
        verbose_name = _('price')
        verbose_name_plural = _('prices')<commit_msg>Use 1 as single item quantity in staggered price example. Because some major erp systems don't provide a price for 0 items.<commit_after>from django.db import models
from django.utils.translation import ugettext_lazy as _

import plata
from plata.product.models import ProductBase
from plata.shop.models import PriceBase


class Product(ProductBase):
    """(Nearly) the simplest product model ever"""

    is_active = models.BooleanField(_('is active'), default=True)
    name = models.CharField(_('name'), max_length=100)
    slug = models.SlugField(_('slug'), unique=True)
    ordering = models.PositiveIntegerField(_('ordering'), default=0)

    description = models.TextField(_('description'), blank=True)

    class Meta:
        ordering = ['ordering', 'name']
        verbose_name = _('product')
        verbose_name_plural = _('products')

    def __unicode__(self):
        return self.name

    @models.permalink
    def get_absolute_url(self):
        return ('plata_product_detail', (), {'object_id': self.pk})

    def get_price(self, currency=None, orderitem=None):
        if currency is None:
            currency = (orderitem.currency if orderitem else
                plata.shop_instance().default_currency())

        possible = self.prices.filter(
            currency=currency,
            from_quantity__lte=(orderitem.quantity if orderitem else 1))

        try:
            return possible.order_by('-from_quantity')[0]
        except IndexError:
            raise possible.model.DoesNotExist


class ProductPrice(PriceBase):
    product = models.ForeignKey(Product, verbose_name=_('product'),
        related_name='prices')
    from_quantity = models.IntegerField(_('From quantity'), default=1)

    class Meta:
        get_latest_by = 'id'
        ordering = ['from_quantity']
        verbose_name = _('price')
        verbose_name_plural = _('prices')
{'fraction_non_alphanumeric': 0.11047149122807018, 'fraction_numerical': 0.0043859649122807015, 'mean_word_length': 3.282863849765258, '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': '4795518', 'n_tokens_mistral': 1063, 'n_tokens_neox': 1000, 'n_words': 244}