Permalink
Browse files

Add method and templatetag to get custom Product classes.

  • Loading branch information...
1 parent 75004e2 commit 4dc2df71a9380b42079b6d4b9fccbb6afd40a0ba @AlexHill AlexHill committed Sep 5, 2012
Showing with 34 additions and 1 deletion.
  1. +10 −0 cartridge/shop/models.py
  2. +24 −1 cartridge/shop/templatetags/shop_tags.py
View
@@ -115,6 +115,16 @@ class Meta:
verbose_name = _("Product")
verbose_name_plural = _("Products")
+ @classmethod
+ def get_content_models(cls):
+ """
+ Return all Page subclasses.
+ """
+ is_product_subclass = lambda cls: issubclass(cls, Product)
+ cmp = lambda a,b: (int(b is Product) - int(a is Product) or
+ a._meta.verbose_name < b._meta.verbose_name)
+ return sorted(filter(is_product_subclass, models.get_models()), cmp)
+
def get_content_model(self):
"""
Provides a generic method of retrieving the instance of the custom
@@ -1,8 +1,12 @@
from decimal import Decimal
import locale
-from django import template
+from django.core.urlresolvers import NoReverseMatch
+from mezzanine import template
+from mezzanine.utils.urls import admin_url
+
+from cartridge.shop.models import Product
from cartridge.shop.utils import set_locale
@@ -71,3 +75,22 @@ def order_totals_text(context):
Text version of order_totals.
"""
return _order_totals(context)
+
+
+@register.as_tag
+def models_for_products(*args):
+ """
+ Create a select list containing each of the models that subclass the
+ ``Product`` model, plus the Product model itself.
+ """
+ product_models = []
+ for model in Product.get_content_models():
+ try:
+ admin_add_url = admin_url(model, "add")
+ except NoReverseMatch:
+ continue
+ else:
+ setattr(model, "name", model._meta.verbose_name)
+ setattr(model, "add_url", admin_add_url)
+ product_models.append(model)
+ return product_models

0 comments on commit 4dc2df7

Please sign in to comment.