class BaseTemplateWidget(Widget): template_source = schema.CharField(choices=TEMPLATE_SOURCE_CHOICES, default='name') template_name = schema.CharField(blank=True) template_html = schema.TextField(blank=True) class Meta: proxy = True def get_template(self): if self.template_source == 'name': return get_template(self.template_name) else: return Template(self.template_html) def get_context(self, context): return Context({'widget': self}) def render(self, context): template = self.get_template() context = self.get_context(context) return mark_safe(template.render(context)) @classmethod def get_admin_form_class(cls): from forms import BaseTemplateWidgetForm return BaseTemplateWidgetForm
class ConfigurableWidget(SchemaEntry, schema.Document): ''' Allows for a widget class to be dynamically defined ''' template_source = schema.CharField(choices=TEMPLATE_SOURCE_CHOICES, default='name') template_name = schema.CharField(blank=True) template_html = schema.TextField(blank=True) def get_template(self): if self.template_source == 'name': return get_template(self.template_name) else: return Template(self.template_html) def get_schema_name(self): return str(''.join([part for part in self.title.split()])) def register_widget(self): #presumably this would be called on save or site load params = { 'module': 'dockitcms.widgetblock.models.virtual', 'virtual': False, 'verbose_name': self.title, #'collection': self.get_collection_name(), 'parents': (ConfiguredWidget,), 'name': self.get_schema_name(), 'attrs': SortedDict(), 'fields': self.get_fields(), 'typed_key': 'configured.%s' % self.get_schema_name(), } params['attrs']['_configurable_widget'] = self return create_schema(**params)
class TemplateEntry(schema.Schema): path = schema.CharField() source = schema.TextField() #TODO template validator js_files = schema.ListField(schema.FileField(upload_to='dockitcms/u-js/'), blank=True) css_files = schema.ListField(schema.FileField(upload_to='dockitcms/u-css/'), blank=True) def get_template_object(self): return Template(self.source)
class TextWidget(Widget): text = schema.TextField() class Meta: typed_key = 'widgetblock.textwidget' def render(self, context): return self.text
class BasePage(schema.Schema): parent = schema.ReferenceField('self', blank=True, null=True) url = schema.CharField(blank=True) url_name = schema.SlugField(blank=True, help_text='registers the page with the url tag with this name') path = schema.CharField(editable=False) title = schema.CharField() slug = schema.SlugField() published = schema.BooleanField() template = schema.CharField() inline_css = schema.TextField(blank=True) inline_js = schema.TextField(blank=True) def clean_path(self): if self.url: return self.url if self.parent: return self.parent.path + self.slug + '/' return self.slug + '/'
class TemplateMixin(schema.Schema): ''' View point mixin that allows for template rendering to be overriden. ''' template_source = schema.CharField(choices=TEMPLATE_SOURCE_CHOICES, default='name') template_name = schema.CharField(default='dockitcms/list.html', blank=True) template_html = schema.TextField(blank=True) content = schema.TextField(blank=True) def render_content(self, context): if self.content: template = Template(self.content) return mark_safe(template.render(Context(context))) return '' def get_template_names(self): if self.template_source == 'name': return [self.template_name] if self.template_source == 'html': return Template(self.template_html)
class BaseTemplateWidget(Widget): template_source = schema.CharField(choices=TEMPLATE_SOURCE_CHOICES, default='name') template_name = schema.CharField(blank=True) template_html = schema.TextField(blank=True) class Meta: proxy = True def get_template(self): if self.template_source == 'name': return get_template(self.template_name) else: return Template(self.template_html) def get_context(self, context): subcontext = Context(context) subcontext['widget'] = self return subcontext def render(self, context): template = self.get_template() context = self.get_context(context) return mark_safe(template.render(context))
class CategoryViewPoint(ViewPoint, CanonicalMixin): category_collection = schema.ReferenceField(Collection) #TODO #category_index = schema.ReferenceField(CollectionIndex) #category_index_param = schema.CharField() category_slug_field = schema.CharField(blank=True) category_template_source = schema.CharField( choices=TEMPLATE_SOURCE_CHOICES, default='name') category_template_name = schema.CharField(default='dockitcms/detail.html', blank=True) category_template_html = schema.TextField(blank=True) category_content = schema.TextField(blank=True, help_text=CATEGORY_CONTEXT_DESCRIPTION) item_collection = schema.ReferenceField(Collection) #TODO #item_index = schema.ReferenceField(CollectionIndex) #item_index_param = schema.CharField() item_slug_field = schema.CharField(blank=True) #TODO #item_category_index = schema.ReferenceField(CollectionIndex) #dot_path = schema.CharField() item_category_index_param = schema.CharField() item_template_source = schema.CharField(choices=TEMPLATE_SOURCE_CHOICES, default='name') item_template_name = schema.CharField(default='dockitcms/detail.html', blank=True) item_template_html = schema.TextField(blank=True) item_content = schema.TextField(blank=True, help_text=ITEM_CONTEXT_DESCRIPTION) category_view_class = CategoryDetailView item_view_class = ItemDetailView class Meta: typed_key = 'dockitcms.collectioncategoryview' def get_category_index(self): return self.category_index.get_index() def get_item_index(self): return self.item_index.get_index() def get_item_category_index(self): return self.item_category_index.get_index() def get_category_document(self): doc_cls = self.category_index.get_object_class() view_point = self def get_absolute_url_for_instance(instance): if view_point.category_slug_field: return view_point.reverse( 'category-detail', instance[view_point.category_slug_field]) return view_point.reverse('category-detail', instance.pk) if self.canonical: setattr(doc_cls, 'get_absolute_url', get_absolute_url_for_instance) #doc_cls.get_absolute_url = get_absolute_url_for_instance class WrappedDoc(doc_cls): get_absolute_url = get_absolute_url_for_instance class Meta: proxy = True return WrappedDoc def get_item_document(self): doc_cls = self.item_index.get_object_class() view_point = self def get_absolute_url_for_instance(instance): if view_point.item_slug_field: return view_point.reverse('item-detail', instance[view_point.item_slug_field]) return view_point.reverse('item-detail', instance.pk) if self.canonical: doc_cls.get_absolute_url = get_absolute_url_for_instance class WrappedDoc(doc_cls): get_absolute_url = get_absolute_url_for_instance class Meta: proxy = True return WrappedDoc def _configuration_from_prefix(self, params, prefix): config = dict() for key in ('template_source', 'template_name', 'template_html', 'content'): config[key] = params.get('%s_%s' % (prefix, key), None) return config def get_inner_urls(self): category_document = self.get_category_document() item_document = self.get_item_document() category_index = self.get_category_index() item_index = self.get_item_index() item_category_index = self.get_item_category_index() params = self.to_primitive(self) urlpatterns = patterns( '', #url(r'^$', # self.list_view_class.as_view(document=document, # configuration=self._configuration_from_prefix(params, 'list'), # paginate_by=params.get('paginate_by', None)), # name='index', #), ) if self.category_slug_field: urlpatterns += patterns( '', url( r'^c/(?P<slug>.+)/$', self.category_view_class.as_view( document=item_document, slug_field=self.category_slug_field, view_point=self, category_index=category_index, item_category_index=item_category_index, item_category_index_param=self. item_category_index_param, configuration=self._configuration_from_prefix( params, 'category'), ), name='category-detail', ), ) else: urlpatterns += patterns( '', url( r'^c/(?P<pk>.+)/$', self.category_view_class.as_view( document=item_document, view_point=self, category_index=category_index, item_category_index=item_category_index, item_category_index_param=self. item_category_index_param, configuration=self._configuration_from_prefix( params, 'category'), ), name='category-detail', ), ) if self.item_slug_field: urlpatterns += patterns( '', url( r'^i/(?P<slug>.+)/$', self.item_view_class.as_view( document=item_document, slug_field=self.item_slug_field, queryset=item_index, view_point=self, configuration=self._configuration_from_prefix( params, 'item'), ), name='item-detail', ), ) else: urlpatterns += patterns( '', url( r'^i/(?P<pk>.+)/$', self.item_view_class.as_view( document=item_document, queryset=item_index, view_point=self, configuration=self._configuration_from_prefix( params, 'item'), ), name='item-detail', ), ) return urlpatterns
class ListingViewPoint(ViewPoint, CanonicalMixin, IndexMixin): slug_field = schema.SlugField(blank=True) list_template_source = schema.CharField(choices=TEMPLATE_SOURCE_CHOICES, default='name') list_template_name = schema.CharField(default='dockitcms/list.html', blank=True) list_template_html = schema.TextField(blank=True) list_content = schema.TextField(blank=True, help_text=LIST_CONTEXT_DESCRIPTION) detail_template_source = schema.CharField(choices=TEMPLATE_SOURCE_CHOICES, default='name') detail_template_name = schema.CharField(default='dockitcms/detail.html', blank=True) detail_template_html = schema.TextField(blank=True) detail_content = schema.TextField(blank=True, help_text=DETAIL_CONTEXT_DESCRIPTION) paginate_by = schema.IntegerField(blank=True, null=True) list_view_class = PointListView detail_view_class = PointDetailView def get_object_class(self): doc_cls = self.index.get_object_class() view_point = self def get_absolute_url_for_instance(instance): if view_point.slug_field: return view_point.reverse('detail', instance[view_point.slug_field]) return view_point.reverse('detail', instance.pk) if self.canonical: #TODO this does not work setattr(doc_cls, 'get_absolute_url', get_absolute_url_for_instance) assert hasattr(doc_cls, 'get_absolute_url') #TODO support model or do this gently class WrappedDoc(doc_cls): get_absolute_url = get_absolute_url_for_instance class Meta: proxy = True return WrappedDoc def _configuration_from_prefix(self, params, prefix): config = dict() for key in ('template_source', 'template_name', 'template_html', 'content'): config[key] = params.get('%s_%s' % (prefix, key), None) return config def get_urls(self): document = self.get_object_class() params = self.to_primitive(self) index = self.get_index() urlpatterns = patterns( '', url( r'^$', self.list_view_class.as_view( document=document, queryset=index, view_point=self, configuration=self._configuration_from_prefix( params, 'list'), paginate_by=params.get('paginate_by', None)), name='index', ), ) if params.get('slug_field', None): urlpatterns += patterns( '', url( r'^(?P<slug>.+)/$', self.detail_view_class.as_view( document=document, queryset=index, slug_field=params['slug_field'], view_point=self, configuration=self._configuration_from_prefix( params, 'detail'), ), name='detail', ), ) else: urlpatterns += patterns( '', url( r'^(?P<pk>.+)/$', self.detail_view_class.as_view( document=document, queryset=index, view_point=self, configuration=self._configuration_from_prefix( params, 'detail'), ), name='detail', ), ) return urlpatterns class Meta: typed_key = 'dockitcms.listing' @classmethod def get_admin_form_class(cls): return ListingViewPointForm
class TemplateMixin(schema.Schema): template_source = schema.CharField(choices=TEMPLATE_SOURCE_CHOICES, default='name') template_name = schema.CharField(default='dockitcms/list.html', blank=True) template_html = schema.TextField(blank=True) content = schema.TextField(blank=True)