Skip to content

Commit

Permalink
Make models for contrib.oscar_fancypages abstract
Browse files Browse the repository at this point in the history
Split the models in the ``contrib.oscar_fancypages`` module into
separate abstract and concrete models. This makes them easier
extensible.

This commit also includes some minor changes in string formatting of the
models' ``__unicode__`` methods.

Resolves #27.
  • Loading branch information
Jonathan Moss authored and Sebastian Vetter committed Apr 14, 2014
1 parent d6b9dd6 commit 061e217
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 75 deletions.
102 changes: 102 additions & 0 deletions fancypages/contrib/oscar_fancypages/abstract_models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
# -*- coding: utf-8 -*-
from django.db import models
from django.utils.translation import ugettext_lazy as _

from ...models import ContentBlock


Product = models.get_model('catalogue', 'Product')


class AbstractSingleProductBlock(ContentBlock):
name = _("Single Product")
code = 'single-product'
group = _("Catalogue")
template_name = "fancypages/blocks/productblock.html"

product = models.ForeignKey(
'catalogue.Product', verbose_name=_("Single Product"), null=True)

def __unicode__(self):
if self.product:
return u"Product '{0}'".format(self.product.upc)
return u"Product '{0}'".format(self.id)

class Meta:
abstract = True


class AbstractHandPickedProductsPromotionBlock(ContentBlock):
name = _("Hand Picked Products Promotion")
code = 'promotion-hand-picked-products'
group = _("Catalogue")
template_name = "fancypages/blocks/promotionblock.html"

promotion = models.ForeignKey(
'promotions.HandPickedProductList', null=True,
verbose_name=_("Hand Picked Products Promotion"))

def __unicode__(self):
if self.promotion:
return u"Promotion '{0}'".format(self.promotion.pk)
return u"Promotion '{0}'".format(self.id)

class Meta:
abstract = True


class AbstractAutomaticProductsPromotionBlock(ContentBlock):
name = _("Automatic Products Promotion")
code = 'promotion-ordered-products'
group = _("Catalogue")
template_name = "fancypages/blocks/promotionblock.html"

promotion = models.ForeignKey(
'promotions.AutomaticProductList',
verbose_name=_("Automatic Products Promotion"), null=True)

def __unicode__(self):
if self.promotion:
return u"Promotion '{0}'".format(self.promotion.pk)
return u"Promotion '{0}'".format(self.id)

class Meta:
abstract = True


class AbstractOfferBlock(ContentBlock):
name = _("Offer Products")
code = 'products-range'
group = _("Catalogue")
template_name = "fancypages/blocks/offerblock.html"

offer = models.ForeignKey(
'offer.ConditionalOffer', verbose_name=_("Offer"), null=True)

@property
def products(self):
product_range = self.offer.condition.range
if product_range.includes_all_products:
return Product.browsable.filter(is_discountable=True)
return product_range.included_products.filter(is_discountable=True)

def __unicode__(self):
if self.offer:
return u"Offer '{0}'".format(self.offer.pk)
return u"Offer '{0}'".format(self.id)

class Meta:
abstract = True


class AbstractPrimaryNavigationBlock(ContentBlock):
name = _("Primary Navigation")
code = 'primary-navigation'
group = _("Content")
template_name = "fancypages/blocks/primary_navigation_block.html"

def __unicode__(self):
return u'Primary Navigation'

class Meta:
abstract = True
88 changes: 13 additions & 75 deletions fancypages/contrib/oscar_fancypages/models.py
Original file line number Diff line number Diff line change
@@ -1,92 +1,30 @@
from django.db import models
from django.utils.translation import ugettext_lazy as _
from . import abstract_models as am

from ...models import ContentBlock
from ...library import register_content_block


Product = models.get_model('catalogue', 'Product')


@register_content_block
class SingleProductBlock(ContentBlock):
name = _("Single Product")
code = 'single-product'
group = _("Catalogue")
template_name = "fancypages/blocks/productblock.html"

product = models.ForeignKey(
'catalogue.Product', verbose_name=_("Single Product"), null=True)

def __unicode__(self):
if self.product:
return u"Product '%s'" % self.product.upc
return u"Product '%s'" % self.id
class SingleProductBlock(am.AbstractSingleProductBlock):
pass


@register_content_block
class HandPickedProductsPromotionBlock(ContentBlock):
name = _("Hand Picked Products Promotion")
code = 'promotion-hand-picked-products'
group = _("Catalogue")
template_name = "fancypages/blocks/promotionblock.html"

promotion = models.ForeignKey(
'promotions.HandPickedProductList', null=True,
verbose_name=_("Hand Picked Products Promotion"))

def __unicode__(self):
if self.promotion:
return u"Promotion '%s'" % self.promotion.pk
return u"Promotion '%s'" % self.id
class HandPickedProductsPromotionBlock(
am.AbstractHandPickedProductsPromotionBlock):
pass


@register_content_block
class AutomaticProductsPromotionBlock(ContentBlock):
name = _("Automatic Products Promotion")
code = 'promotion-ordered-products'
group = _("Catalogue")
template_name = "fancypages/blocks/promotionblock.html"

promotion = models.ForeignKey(
'promotions.AutomaticProductList',
verbose_name=_("Automatic Products Promotion"), null=True)

def __unicode__(self):
if self.promotion:
return u"Promotion '%s'" % self.promotion.pk
return u"Promotion '%s'" % self.id
class AutomaticProductsPromotionBlock(
am.AbstractAutomaticProductsPromotionBlock):
pass


@register_content_block
class OfferBlock(ContentBlock):
name = _("Offer Products")
code = 'products-range'
group = _("Catalogue")
template_name = "fancypages/blocks/offerblock.html"

offer = models.ForeignKey(
'offer.ConditionalOffer', verbose_name=_("Offer"), null=True)

@property
def products(self):
product_range = self.offer.condition.range
if product_range.includes_all_products:
return Product.browsable.filter(is_discountable=True)
return product_range.included_products.filter(is_discountable=True)

def __unicode__(self):
if self.offer:
return u"Offer '%s'" % self.offer.pk
return u"Offer '%s'" % self.id
class OfferBlock(am.AbstractOfferBlock):
pass


@register_content_block
class PrimaryNavigationBlock(ContentBlock):
name = _("Primary Navigation")
code = 'primary-navigation'
group = _("Content")
template_name = "fancypages/blocks/primary_navigation_block.html"

def __unicode__(self):
return u'Primary Navigation'
class PrimaryNavigationBlock(am.AbstractPrimaryNavigationBlock):
pass

0 comments on commit 061e217

Please sign in to comment.