Changeset - 1333fcdea179
[Not reviewed]
0 1 0
Christopher Neugebauer - 8 years ago 2016-09-04 02:18:10
chrisjrn@gmail.com
Refactors flags and discount classes to be DRYer.
1 file changed with 75 insertions and 67 deletions:
0 comments (0 inline, 0 general)
registrasion/models/conditions.py
Show inline comments
...
 
@@ -11,3 +11,71 @@ from model_utils.managers import InheritanceManager
 

	
 
# Product Modifiers
 
# Condition Types
 

	
 
class TimeOrStockLimitCondition(models.Model):
 
    ''' Attributes for a condition that is limited by timespan or a count of
 
    purchased or reserved items.
 

	
 
    Attributes:
 
        start_time (Optional[datetime]): When the condition should start being
 
            true.
 

	
 
        end_time (Optional[datetime]): When the condition should stop being
 
            true.
 

	
 
        limit (Optional[int]): How many items may fall under the condition
 
            the condition until it stops being false -- for all users.
 
    '''
 

	
 
    class Meta:
 
        abstract = True
 

	
 
    start_time = models.DateTimeField(
 
        null=True,
 
        blank=True,
 
        verbose_name=_("Start time"),
 
        help_text=_("When the condition should start being true"),
 
    )
 
    end_time = models.DateTimeField(
 
        null=True,
 
        blank=True,
 
        verbose_name=_("End time"),
 
        help_text=_("When the condition should stop being true."),
 
    )
 
    limit = models.PositiveIntegerField(
 
        null=True,
 
        blank=True,
 
        verbose_name=_("Limit"),
 
        help_text=_(
 
            "How many times this condition may be applied for all users."
 
        ),
 
    )
 

	
 

	
 
class VoucherCondition(models.Model):
 
    ''' A condition is met when a voucher code is in the current cart. '''
 

	
 
    class Meta:
 
        abstract = True
 

	
 
    voucher = models.OneToOneField(
 
        inventory.Voucher,
 
        on_delete=models.CASCADE,
 
        verbose_name=_("Voucher"),
 
        db_index=True,
 
    )
 

	
 

	
 
class IncludedProductCondition(models.Model):
 
    class Meta:
 
        abstract = True
 

	
 
    enabling_products = models.ManyToManyField(
 
        inventory.Product,
 
        verbose_name=_("Including product"),
 
        help_text=_("If one of these products are purchased, this condition "
 
                    "is met."),
 
    )
 

	
 

	
 
# Discounts
 

	
...
 
@@ -156,3 +224,3 @@ class DiscountForCategory(models.Model):
 

	
 
class TimeOrStockLimitDiscount(DiscountBase):
 
class TimeOrStockLimitDiscount(TimeOrStockLimitCondition, DiscountBase):
 
    ''' Discounts that are generally available, but are limited by timespan or
...
 
@@ -177,23 +245,4 @@ class TimeOrStockLimitDiscount(DiscountBase):
 

	
 
    start_time = models.DateTimeField(
 
        null=True,
 
        blank=True,
 
        verbose_name=_("Start time"),
 
        help_text=_("This discount will only be available after this time."),
 
    )
 
    end_time = models.DateTimeField(
 
        null=True,
 
        blank=True,
 
        verbose_name=_("End time"),
 
        help_text=_("This discount will only be available before this time."),
 
    )
 
    limit = models.PositiveIntegerField(
 
        null=True,
 
        blank=True,
 
        verbose_name=_("Limit"),
 
        help_text=_("This discount may only be applied this many times."),
 
    )
 

	
 

	
 
class VoucherDiscount(DiscountBase):
 
class VoucherDiscount(VoucherCondition, DiscountBase):
 
    ''' Discounts that are enabled when a voucher code is in the current
...
 
@@ -212,11 +261,4 @@ class VoucherDiscount(DiscountBase):
 

	
 
    voucher = models.OneToOneField(
 
        inventory.Voucher,
 
        on_delete=models.CASCADE,
 
        verbose_name=_("Voucher"),
 
        db_index=True,
 
    )
 

	
 

	
 
class IncludedProductDiscount(DiscountBase):
 
class IncludedProductDiscount(IncludedProductCondition, DiscountBase):
 
    ''' Discounts that are enabled because another product has been purchased.
...
 
@@ -235,9 +277,2 @@ class IncludedProductDiscount(DiscountBase):
 

	
 
    enabling_products = models.ManyToManyField(
 
        inventory.Product,
 
        verbose_name=_("Including product"),
 
        help_text=_("If one of these products are purchased, the discounts "
 
                    "below will be enabled."),
 
    )
 

	
 

	
...
 
@@ -332,3 +367,3 @@ class FlagBase(models.Model):
 

	
 
class TimeOrStockLimitFlag(FlagBase):
 
class TimeOrStockLimitFlag(TimeOrStockLimitCondition, FlagBase):
 
    ''' Product groupings that can be used to enable a product during a
...
 
@@ -354,24 +389,5 @@ class TimeOrStockLimitFlag(FlagBase):
 

	
 
    start_time = models.DateTimeField(
 
        null=True,
 
        blank=True,
 
        help_text=_("Products included in this condition will only be "
 
                    "available after this time."),
 
    )
 
    end_time = models.DateTimeField(
 
        null=True,
 
        blank=True,
 
        help_text=_("Products included in this condition will only be "
 
                    "available before this time."),
 
    )
 
    limit = models.PositiveIntegerField(
 
        null=True,
 
        blank=True,
 
        help_text=_("The number of items under this grouping that can be "
 
                    "purchased."),
 
    )
 

	
 

	
 
@python_2_unicode_compatible
 
class ProductFlag(FlagBase):
 
class ProductFlag(IncludedProductCondition, FlagBase):
 
    ''' The condition is met because a specific product is purchased.
...
 
@@ -391,8 +407,2 @@ class ProductFlag(FlagBase):
 

	
 
    enabling_products = models.ManyToManyField(
 
        inventory.Product,
 
        help_text=_("If one of these products are purchased, this condition "
 
                    "is met."),
 
    )
 

	
 

	
...
 
@@ -424,3 +434,3 @@ class CategoryFlag(FlagBase):
 
@python_2_unicode_compatible
 
class VoucherFlag(FlagBase):
 
class VoucherFlag(VoucherCondition, FlagBase):
 
    ''' The condition is met because a Voucher is present. This is for e.g.
...
 
@@ -436,4 +446,2 @@ class VoucherFlag(FlagBase):
 

	
 
    voucher = models.OneToOneField(inventory.Voucher)
 

	
 

	
0 comments (0 inline, 0 general)