Permalink
Browse files

re-organise plugin admin form, display new template files without rel…

…oading server, unslugify template filenames: underscores are changed to spaces, dashes are changed to colons
  • Loading branch information...
1 parent 82b46b1 commit f1158d09f562d6cd4913c806eebfe16765612839 Zenobius Jiricek committed Dec 29, 2011
@@ -9,6 +9,11 @@
from configurableproduct.models import CProduct as Product
from configurableproduct.models.producttypes import ProductType
+from .lib.choices import (
+ DynamicTemplateChoices,
+ DynamicChoice,
+ )
+
from .models import (
CProductTypesPlugin,
CProductsPlugin,
@@ -17,12 +22,18 @@
PRODUCT_LIST_TEMPLATE_PATH,
)
+from .forms import (
+ CProductTypesAdminForm,
+ CProductsAdminForm,
+)
+
class ProductCategories(CMSPluginBase):
model = CProductTypesPlugin
name = _("List of Product Types")
render_template = os.path.join(TEMPLATE_BASE_PATH, "base.html")
default_template = os.path.join(PRODUCT_TYPE_TEMPLATE_PATH, "default.html")
admin_preview = False
+ form = CProductTypesAdminForm
def render(self, context, instance, placeholder):
objects = Product.objects.filter(active=True)
@@ -45,6 +56,22 @@ class CategoryProducts(CMSPluginBase):
render_template = os.path.join(TEMPLATE_BASE_PATH, "base.html")
default_template = os.path.join(PRODUCT_TYPE_TEMPLATE_PATH, "default.html")
admin_preview = False
+ form = CProductsAdminForm
+
+ fieldsets = (
+ ('Display Template',
+ {'fields': [ ('template', ),
+ ]}),
+
+ ('Show Products of Type...',
+ {'fields': [ ('hide_empty_categories','categories', ),
+ ]}),
+
+ ('Filtering', {
+ 'classes': ('collapse',),
+ 'fields': ['filter_action', 'filter_product_attributes']
+ }),
+ )
def render(self, context, instance, placeholder):
products = Product.objects.filter(type__pk__in = instance.categories.all())
@@ -0,0 +1,44 @@
+from django.forms import ModelForm
+from django.contrib.auth.models import User
+from django.forms import ModelChoiceField, ChoiceField
+
+from .lib.choices import (
+ DynamicTemplateChoices,
+ DynamicChoice,
+ )
+
+from .models import (
+ CProductTypesPlugin,
+ CProductsPlugin,
+ TEMPLATE_BASE_PATH,
+ PRODUCT_TYPE_TEMPLATE_PATH,
+ PRODUCT_LIST_TEMPLATE_PATH,
+)
+
+
+
+
+class CProductTypesAdminForm(ModelForm):
+
+ class Meta:
+ model = CProductTypesPlugin
+
+ def __init__(self, *args, **kwargs):
+ super(CProductTypesAdminForm, self).__init__(*args, **kwargs)
+ self.fields['template'].choices = DynamicTemplateChoices(
+ path = PRODUCT_TYPE_TEMPLATE_PATH,
+ include = '.html',
+ exclude = 'base')
+
+
+class CProductsAdminForm(ModelForm):
+
+ class Meta:
+ model = CProductsPlugin
+
+ def __init__(self, *args, **kwargs):
+ super(CProductsAdminForm, self).__init__(*args, **kwargs)
+ self.fields['template'].choices = DynamicTemplateChoices(
+ path = PRODUCT_LIST_TEMPLATE_PATH,
+ include = '.html',
+ exclude = 'base')
@@ -1,3 +1,14 @@
+import os
+
+from django.contrib import admin
+from django.template.loader import get_template
+from django.template.loaders.app_directories import app_template_dirs
+from django.core.exceptions import ImproperlyConfigured
+from django.utils.importlib import import_module
+
+#import settings
+from formatting import deslugify
+
class DynamicChoice(object):
"""
Trivial example of creating a dynamic choice
@@ -23,3 +34,71 @@ def generate(self, *args, **kwargs):
"""
import random
return [random.randint(1,100) for i in range(10)]
+
+
+
+class DynamicTemplateChoices(DynamicChoice):
+ path = None
+
+ # exclude templates whose name includes these keywords
+ exclude = None
+
+ # only include templates whos name contains these keywords
+ inlude = None
+
+ #
+ # TODO: Scan for snippets as well.
+ #
+ # scan for and include snippets in choices?
+ #scan_snippets = False
+
+ # snippets whose title prefixed with this moniker are considered to be
+ # templates for our cmsplugin.
+
+ #snippet_title_moniker = getattr(
+ # settings.CONFIGURABLEPRODUCT_CMSPLUGIN_SNIPPETS_MONIKER,
+ # "[configurableproduct-snippet]")
+
+
+ def __init__(self, path=None, include=None,
+ exclude=None, *args, **kwargs):
+
+ super(DynamicTemplateChoices, self).__init__(self, *args, **kwargs)
+ self.path = path
+ self.include = include
+ self.exlude = exclude
+
+ def generate(self,*args, **kwargs):
+ choices = list(("-[ Nothing Selected ]-", ), )
+
+ for template_dir in app_template_dirs:
+ results = self.walkdir(os.path.join(template_dir, self.path))
+ if results:
+ choices += results
+
+ return choices
+
+ def walkdir(self, path=None):
+ output = list()
+
+ if not os.path.exists(path):
+ return None
+
+ for root, dirs, files in os.walk(path):
+
+ if self.include:
+ files = filter(lambda x: self.include in x, files)
+
+ if self.exlude:
+ files = filter(lambda x: not self.exlude in x, files)
+
+ for item in files :
+ output += ( (
+ os.path.join(self.path, item),
+ deslugify(os.path.splitext(item)[0]),
+ ),)
+
+ for item in dirs :
+ output += self.walkdir(os.path.join(root, item))
+
+ return output
@@ -0,0 +1,15 @@
+import re
+
+def deslugify(value):
+ """
+ Cleans up a slug by removing slug separator characters that occur at the
+ beginning or end of a slug.
+ """
+ reverse_map = {
+ "_" : " ",
+ "-" : " : ",
+ }
+
+ for _from, _to in reverse_map.iteritems():
+ value = re.sub(_from, _to, value)
+ return value
@@ -2,75 +2,23 @@
from os.path import join, getsize
from django.db import models
-from django.template.loader import get_template
-from django.template.loaders.app_directories import app_template_dirs
-from django.core.exceptions import ImproperlyConfigured
-from django.utils.importlib import import_module
from cms.models.pluginmodel import CMSPlugin
-from .lib.choices import DynamicChoice
+from .lib.choices import (
+ DynamicTemplateChoices,
+ DynamicChoice,
+ )
TEMPLATE_BASE_PATH = "cmsplugin_configurableproduct"
PRODUCT_TYPE_TEMPLATE_PATH = os.path.join(TEMPLATE_BASE_PATH, "product-types")
PRODUCT_LIST_TEMPLATE_PATH = os.path.join(TEMPLATE_BASE_PATH, "product-list")
-class DynamicTemplateChoices(DynamicChoice):
- path = None
- exclude = None
- inlude = None
-
- def __init__(self, path=None, include=None,
- exclude=None, *args, **kwargs):
-
- super(DynamicTemplateChoices, self).__init__(self, *args, **kwargs)
- self.path = path
- self.include = include
- self.exlude = exclude
-
- def generate(self,*args, **kwargs):
- choices = list()
-
- for template_dir in app_template_dirs:
- results = self.walkdir(os.path.join(template_dir, self.path))
- if results:
- choices += results
-
- return choices
-
- def walkdir(self, path=None):
- output = list()
-
- if not os.path.exists(path):
- return None
-
- for root, dirs, files in os.walk(path):
-
- if self.include:
- files = filter(lambda x: self.include in x, files)
-
- if self.exlude:
- files = filter(lambda x: not self.exlude in x, files)
-
- for item in files :
- output += ( (
- os.path.join(self.path, item),
- os.path.splitext(item)[0],
- ),)
-
- for item in dirs :
- output += self.walkdir(os.path.join(root, item))
-
- return output
-
-
-
class CProductTypesPlugin(CMSPlugin):
""" Stores options for cmsplugin that shows lists of ProductTypes
"""
-
TEMPLATE_CHOICES = DynamicTemplateChoices(
path=PRODUCT_TYPE_TEMPLATE_PATH,
include='.html',

0 comments on commit f1158d0

Please sign in to comment.