<commit_before># Copyright (C) 2010-2011 <NAME> <<EMAIL>>
#
# This file is part of django-webshop.
#
# django-webshop is free software; you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.

from django.db import models
from django.utils.translation import ugettext_lazy as _

from webshop.core.settings import PRODUCT_MODEL


class ProductVariationBase(models.Model):
    """ Base class for variations of a product. """
    
    class Meta:
        verbose_name = _('product variation')
        verbose_name_plural = _('product variations')
        abstract = True
    
    product = models.ForeignKey(PRODUCT_MODEL)
    """ Product this variation is linked to. """

<commit_msg>OrderedProductVariationBase class<commit_after># Copyright (C) 2010-2011 <NAME> <<EMAIL>>
#
# This file is part of django-webshop.
#
# django-webshop is free software; you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.

import logging
logger = logging.getLogger(__name__)

from django.db import models
from django.utils.translation import ugettext_lazy as _

from webshop.core.settings import PRODUCT_MODEL
from webshop.core.basemodels import OrderedItemBase


class ProductVariationBase(models.Model):
    """ Base class for variations of a product. """
    
    class Meta:
        verbose_name = _('product variation')
        verbose_name_plural = _('product variations')
        abstract = True
    
    @classmethod
    def get_default_variation(cls):
        """ Return the default variation selected for this product.
            As there is no inherent way to order these, this function
            should be overridden in classes actually implementing the
            variation model.
            
            This might, for example, be overriden by taking the first product
            in the list or by some function selecting a specific variation as
            default.
        """
        
        raise NotImplemented
    
    product = models.ForeignKey(PRODUCT_MODEL)
    """ Product this variation is linked to. """


class OrderedProductVariationBase(ProductVariationBase, OrderedItemBase):
    """ Base class for ordered product variations. """
    
    class Meta(ProductVariationBase.Meta, OrderedItemBase.Meta):
        abstract = True
    
    @classmethod
    def get_default_variation(cls):
        """ By default, this returns the first variation according to the
            default sortorder. """
        
        return self.__class__.objects.all()[0]
    
    
    def save(self, **kwargs):
        """ Calculate default ordering for products.
           
            This code is inspired by from `django-inline-ordering <https://github.com/centralniak/django-inline-ordering/blob/master/inline_ordering/models.py>`_.
            
        """
        
        if not self.sort_order:
            related_variations = \
                self.__class__.objects.filter(product=self.product)
            
            max_ordering_query = \
                related_variations.aggregate(models.Max('sort_order'))
            
            max_ordering = max_ordering_query['sort_order__max']
            
            if max_ordering:
                self.sort_order = max_ordering + 10
            else:
                self.sort_order = 10
            
            logger.debug('Generated sort_order %d for object %s',
                         self.sort_order, self)
        
        parent = super(OrderedProductVariationBase, self)    
        return parent.save(**kwargs)



{'fraction_non_alphanumeric': 0.06969376979936642, 'fraction_numerical': 0.009503695881731784, 'mean_word_length': 2.732072498029945, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 10, 'https://': 1, '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': '2752004', 'n_tokens_mistral': 1250, 'n_tokens_neox': 1183, 'n_words': 472}