Browse Source

New page preview block (#155)

Vince Salvino 6 years ago
parent
commit
3fce3c831d

+ 2 - 1
coderedcms/blocks/__init__.py

@@ -26,13 +26,14 @@ HTML_STREAMBLOCKS = [
     ('quote', QuoteBlock()),
     ('table', TableBlock()),
     ('google_map', EmbedGoogleMapBlock()),
+    ('page_list', PageListBlock()),
+    ('page_preview', PagePreviewBlock()),
 ]
 
 CONTENT_STREAMBLOCKS = HTML_STREAMBLOCKS + [
     ('card', CardBlock()),
     ('carousel', CarouselBlock()),
     ('image_gallery', ImageGalleryBlock()),
-    ('page_list', PageListBlock()),
     ('modal', ModalBlock(HTML_STREAMBLOCKS)),
     ('pricelist', PriceListBlock()),
     ('reusable_content', ReusableContentBlock()),

+ 0 - 48
coderedcms/blocks/content_blocks.py

@@ -5,7 +5,6 @@ contain sub-blocks, and may require javascript to function properly.
 
 from django.utils.translation import ugettext_lazy as _
 from wagtail.core import blocks
-from wagtail.core.models import Page
 from wagtail.documents.blocks import DocumentChooserBlock
 from wagtail.images.blocks import ImageChooserBlock
 from wagtail.snippets.blocks import SnippetChooserBlock
@@ -203,49 +202,6 @@ class NavDocumentLinkWithSubLinkBlock(NavSubLinkBlock, NavDocumentLinkBlock):
         label = _('Document link with sub-links')
 
 
-class PageListBlock(BaseBlock):
-    """
-    Renders a preview of selected pages.
-    """
-    show_preview = blocks.BooleanBlock(
-        required=False,
-        default=False,
-        label=_('Show body preview'),
-    )
-    num_posts = blocks.IntegerBlock(
-        default=3,
-        label=_('Number of pages to show'),
-    )
-    indexed_by = blocks.PageChooserBlock(
-        required=False,
-        label=_('Limit to'),
-        help_text=_('Only show pages that are children of the selected page. Uses the subpage sorting as specified in the page’s LAYOUT tab.'),
-    )
-
-    class Meta:
-        template = 'coderedcms/blocks/pagelist_block.html'
-        icon = 'list-ul'
-        label = _('Latest Pages')
-
-    def get_context(self, value, parent_context=None):
-
-        context = super().get_context(value, parent_context=parent_context)
-
-        if value['indexed_by']:
-            indexer = value['indexed_by'].specific
-            # try to use the CoderedPage `get_index_children()`,
-            # but fall back to get_children if this is a non-CoderedPage
-            try:
-                pages = indexer.get_index_children()
-            except AttributeError:
-                pages = indexer.get_children().live()
-        else:
-            pages = Page.objects.live().order_by('-first_published_at')
-
-        context['pages'] = pages[:value['num_posts']]
-        return context
-
-
 class PriceListItemBlock(BaseBlock):
     """
     Represents one item in a PriceListBlock, such as an entree in a restaurant menu.
@@ -325,7 +281,3 @@ class ReusableContentBlock(BaseBlock):
         icon = 'fa-recycle'
         label = _('Reusable Content')
         template = 'coderedcms/blocks/reusable_content_block.html'
-
-class RichTextBlock(blocks.RichTextBlock):
-    class Meta:
-        template = 'coderedcms/blocks/rich_text_block.html'

+ 69 - 0
coderedcms/blocks/html_blocks.py

@@ -1,11 +1,16 @@
 """
 HTML blocks are simple blocks used to represent common HTML elements,
 with additional styling and attributes.
+
+HTML blocks should NOT contain more sub-blocks or sub-streamfields.
+They must be safe to nest within more robust "content blocks" without
+creating recursion.
 """
 
 from django.utils.translation import ugettext_lazy as _
 from wagtail.contrib.table_block.blocks import TableBlock as WagtailTableBlock
 from wagtail.core import blocks
+from wagtail.core.models import Page
 from wagtail.documents.blocks import DocumentChooserBlock
 from wagtail.embeds.blocks import EmbedBlock
 from wagtail.images.blocks import ImageChooserBlock
@@ -178,6 +183,65 @@ class ImageLinkBlock(BaseLinkBlock):
         value_class = LinkStructValue
 
 
+class PageListBlock(BaseBlock):
+    """
+    Renders a preview of selected pages.
+    """
+    show_preview = blocks.BooleanBlock(
+        required=False,
+        default=False,
+        label=_('Show body preview'),
+    )
+    num_posts = blocks.IntegerBlock(
+        default=3,
+        label=_('Number of pages to show'),
+    )
+    indexed_by = blocks.PageChooserBlock(
+        required=False,
+        label=_('Limit to'),
+        help_text=_('Only show pages that are children of the selected page. Uses the subpage sorting as specified in the page’s LAYOUT tab.'),
+    )
+
+    class Meta:
+        template = 'coderedcms/blocks/pagelist_block.html'
+        icon = 'list-ul'
+        label = _('Latest Pages')
+
+    def get_context(self, value, parent_context=None):
+
+        context = super().get_context(value, parent_context=parent_context)
+
+        if value['indexed_by']:
+            indexer = value['indexed_by'].specific
+            # try to use the CoderedPage `get_index_children()`,
+            # but fall back to get_children if this is a non-CoderedPage
+            try:
+                pages = indexer.get_index_children()
+            except AttributeError:
+                pages = indexer.get_children().live()
+        else:
+            pages = Page.objects.live().order_by('-first_published_at')
+
+        context['pages'] = pages[:value['num_posts']]
+        return context
+
+
+class PagePreviewBlock(BaseBlock):
+    """
+    Renders a preview of a specific page.
+    """
+    page = blocks.PageChooserBlock(
+        required=True,
+        label=_('Page to preview'),
+        help_text=_('Show a mini preview of the selected page.'),
+    )
+
+    class Meta:
+        template = 'coderedcms/blocks/pagepreview_block.html'
+        icon = 'doc-empty-inverse'
+        label = _('Page Preview')
+
+
 class QuoteBlock(BaseBlock):
     """
     A <blockquote>.
@@ -197,3 +261,8 @@ class QuoteBlock(BaseBlock):
         template = 'coderedcms/blocks/quote_block.html'
         icon = 'openquote'
         label = _('Quote')
+
+
+class RichTextBlock(blocks.RichTextBlock):
+    class Meta:
+        template = 'coderedcms/blocks/rich_text_block.html'

File diff suppressed because it is too large
+ 24 - 0
coderedcms/migrations/0013_pagepreview_templates.py


File diff suppressed because it is too large
+ 3 - 2
coderedcms/project_template/website/migrations/0001_initial.py


+ 4 - 0
coderedcms/settings.py

@@ -144,6 +144,10 @@ DEFAULTS = {
             ('coderedcms/blocks/pagelist_article_card_deck.html', 'Article, card deck - separate cards of equal size'),
             ('coderedcms/blocks/pagelist_article_card_columns.html', 'Article, card masonry - fluid brick pattern'),
         ),
+        'pagepreviewblock': (
+            ('coderedcms/blocks/pagepreview_card.html', 'Card'),
+            ('coderedcms/blocks/pagepreview_form.html', 'Form inputs'),
+        ),
         # templates that are available for all block types
         '*': (
             ('', 'Default'),

+ 1 - 1
coderedcms/templates/coderedcms/blocks/article_block_card.html

@@ -2,7 +2,7 @@
 
 <div class="card mb-3">
     {% if article.cover_image %}
-        {% image article.cover_image fill-600x300 as cover_image %}
+        {% image article.cover_image fill-900x600 as cover_image %}
         <a href="{{article.url}}" title="{{article.title}}"><img class="card-img-top" src="{{cover_image.url}}" alt="{{cover_image.title}}"></a>
     {% endif %}
     <div class="card-body">

+ 2 - 2
coderedcms/templates/coderedcms/blocks/pagelist_block.html

@@ -1,12 +1,12 @@
 {% extends 'coderedcms/blocks/base_block.html' %}
-{% load wagtailcore_tags wagtailimages_tags %}
+{% load wagtailcore_tags %}
 
 {% block block_render %}
 
 <ul>
     {% for page in pages %}
     {% with page=page.specific %}
-    <li><a href="{{page.url}}">
+    <li><a href="{% pageurl page.url %}">
         {{page.title}} {% if self.show_preview %}<small class="text-muted">– {{page.body_preview}}</small>{% endif %}
     </a></li>
     {% endwith %}

+ 12 - 0
coderedcms/templates/coderedcms/blocks/pagepreview_block.html

@@ -0,0 +1,12 @@
+{% extends 'coderedcms/blocks/base_block.html' %}
+{% load wagtailcore_tags %}
+
+{% block block_render %}
+
+    {% with page=self.page.specific %}
+    <a href="{% pageurl page %}">
+        {{page.title}} <small class="text-muted">– {{page.body_preview}}</small>
+    </a>
+    {% endwith %}
+
+{% endblock %}

+ 17 - 0
coderedcms/templates/coderedcms/blocks/pagepreview_card.html

@@ -0,0 +1,17 @@
+{% load wagtailcore_tags wagtailimages_tags %}
+
+<div class="card mb-3 {{self.settings.custom_css_class}}"
+{% if self.settings.custom_css_id %}id="{{self.settings.custom_css_id}}"{% endif %}>
+{% with page=self.page.specific %}
+    {% if page.cover_image %}
+    {% image page.cover_image fill-900x600 as card_img %}
+    <img class="card-img-top w-100" src="{{card_img.url}}" alt="{{card_img.title}}">
+    {% endif %}
+    <div class="card-body">
+        {% if page.title %}<h5 class="card-title">{{page.title}}</h5>{% endif %}
+        {% if page.caption %}<h6 class="card-subtitle mb-2 text-muted">{{self.caption}}</h6>{% endif %}
+        <div class="card-text">{{page.body_preview}}</div>
+        <a href="{% pageurl page %}" title="{{page.title}}">Read more &raquo;</a>
+    </div>
+{% endwith %}
+</div>

+ 24 - 0
coderedcms/templates/coderedcms/blocks/pagepreview_form.html

@@ -0,0 +1,24 @@
+{% extends 'coderedcms/blocks/base_block.html' %}
+{% load bootstrap4 coderedcms_tags wagtailcore_tags %}
+
+{% block block_render %}
+
+    {% with page=self.page.specific %}
+    {% if page.form_live %}
+    {% get_pageform page request as form %}
+    <form class='{{ page.form_css_class }}' id='{{ page.form_id }}' action="{% pageurl page %}" method="POST" {% if form|is_file_form %}enctype="multipart/form-data"{% endif %}>
+        {% csrf_token %}
+        {% bootstrap_form form layout='horizontal' %}
+        <div class="form-group mt-5 row">
+            <div class="{{'horizontal_label_class'|bootstrap_settings}}"></div>
+            <div class="{{'horizontal_field_class'|bootstrap_settings}}">
+                <button type="submit" class="btn {{page.button_size}} {{page.button_style}} {{page.button_css_class}}">
+                    {{ page.button_text }}
+                </button>
+            </div>
+        </div>
+    </form>
+    {% endif %}
+    {% endwith %}
+
+{% endblock %}

+ 4 - 0
coderedcms/templatetags/coderedcms_tags.py

@@ -90,6 +90,10 @@ def get_searchform(request=None):
         return SearchForm(request.GET)
     return SearchForm()
 
+@register.simple_tag
+def get_pageform(page, request):
+    return page.get_form(page=page, user=request.user)
+
 @register.simple_tag
 def process_form_cell(request, cell):
     if isinstance(cell, str) and cell.startswith(cr_settings['PROTECTED_MEDIA_URL']):

Some files were not shown because too many files changed in this diff