def addable_children(cls): """ return spokes that can be added as children """ def addable(t): """ check it it's addable, implicitly or explicitly """ if t.implicit_add: return True explicit = set(cls.children or ()) | set(cls.explicit_children or ()) return t in explicit if cls.children is None: ch = [t for t in type_registry.values() if addable(t)] else: ch = cls.children return ch
def allowed_spokes(self): """ Return the spokes that can be added to the current instance. This can be either the spoke's class default, or an instance specific config (instance.allowed). If the specific config is an empty string, no subcontent is allowed. If the specific config is a list of comma separated type names, those are allowed (even non-implicit ones) If the specific config is None, de class default applies. """ def addable(t): """ check it it's addable, implicitly or explicitly """ if t.implicit_add: return True explicit = set(self.children or ()) | \ set(self.explicit_children or ()) return t in explicit if self.instance.allowed == "": ch = () elif self.instance.allowed is None: # Class default if self.children is None: ch = [t for t in type_registry.values() if addable(t)] else: ch = self.children else: ch = [type_registry.get(p) for p in self.instance.allowed.split(",")] ## filter out stale entries. Log? ch = filter(None, ch) return ch
from django.contrib import admin from wheelcms_spokes import models from wheelcms_axle.models import type_registry ## create an admin for all registered types for spoke in type_registry.values(): class SpokeAdmin(admin.ModelAdmin): model = spoke.model admin.site.register(spoke.model, SpokeAdmin)
def __init__(self, parent, node=None, attach=False, enlarge=True, reserved=(), skip_slug=False, *args, **kwargs): """ Django will put the extra slug field at the bottom, below all model fields. I want it just after the title field """ super(BaseForm, self).__init__(*args, **kwargs) slug = self.fields.pop('slug') titlepos = self.fields.keyOrder.index('title') self.fields.insert(titlepos+1, 'slug', slug) self.node = node self.parent = parent self.attach = attach self.reserved = reserved self.advanced_fields = self.initial_advanced_fields ## Pass parent to ParentFields for field in self.fields.values(): if isinstance(field, ParentField): field.parent = parent if attach: self.fields.pop('slug') templates = template_registry.get(self._meta.model, []) if templates: self.fields['template'] = forms.ChoiceField(choices=templates, required=False) else: self.fields.pop('template') ## will default to content_view self.fields['state'] = forms.ChoiceField(choices=self.workflow_choices(), initial=self.workflow_default(), required=False) if skip_slug: self.fields.pop("slug") if enlarge: for enlargable_field in self.fields.values(): self.enlarge_field(enlargable_field) ## make the description textarea a bit smaller if 'description' in self.fields: self.fields['description'].widget.attrs['rows'] = 4 ## workaround for https://code.djangoproject.com/ticket/21173 for patch_dt in ("publication", "expire", "created"): if patch_dt in self.fields: f = self.fields[patch_dt] f.widget = DateTimeInput() ## lightforms don't have a language field if 'language' in self.fields: if self.node: ## construct allowed languages. Exclude any language for which ## the node already has content current = self.instance.language if self.instance else None c = [] for lpair in translate.languages(): if current == lpair[0] or \ not self.node.content(language=lpair[0], fallback=False): c.append(lpair) self.fields['language'].choices = c else: self.fields['language'].choices = translate.languages() for e in type_registry.extenders(self.Meta.model): e.extend_form(self, *args, **kwargs) if "allowed" in self.fields: self.fields["allowed"].choices = ((t.name(), t.title) for t in sorted(type_registry.values(), key=operator.attrgetter("title"))) if self.instance: allowed = self.instance.allowed if allowed == "" and "no_subcontent" in self.fields: self.fields["no_subcontent"].initial = True