~homeworkprod/byceps

ba269fc932fd6ebb6782da3e73268bca67e8c585 — Jochen Kupperschmidt 1 year, 8 months ago b77ede2
Move news item body rendering out of (database) model, make it explicit
M byceps/blueprints/admin/news/templates/admin/news/item_view_version.html => byceps/blueprints/admin/news/templates/admin/news/item_view_version.html +1 -1
@@ 80,7 80,7 @@

  <h1>{{ version.title }}</h1>

{{ version.render_body()|safe }}
{{ render_body(version.body)|safe }}

{%- endblock %}


M byceps/blueprints/admin/news/views.py => byceps/blueprints/admin/news/views.py +1 -0
@@ 133,6 133,7 @@ def item_view_version(version_id):
    return {
        'version': version,
        'brand': brand,
        'render_body': news_item_service.render_body,
    }



M byceps/blueprints/news/templates/news/_item.html => byceps/blueprints/news/templates/news/_item.html +1 -1
@@ 11,6 11,6 @@
    {%- endif %}
  </footer>
  <div class="body">
{{ item.body|safe }}
{{ render_body(item.body)|safe }}
  </div>
</article>

M byceps/blueprints/news/views.py => byceps/blueprints/news/views.py +6 -0
@@ 42,6 42,7 @@ def index(page):
    return {
        'items': items,
        'page': page,
        'render_body': _render_body,
    }




@@ 61,6 62,7 @@ def view(slug):

    return {
        'item': item,
        'render_body': _render_body,
    }




@@ 88,3 90,7 @@ def _get_items_per_page_value():

def _may_view_drafts(user):
    return user.has_permission(NewsItemPermission.view_draft)


def _render_body(raw_body):
    return news_service.render_body(raw_body)

M byceps/services/news/models/item.py => byceps/services/news/models/item.py +0 -5
@@ 13,7 13,6 @@ from sqlalchemy.ext.associationproxy import association_proxy
from ....database import BaseQuery, db, generate_uuid
from ....typing import UserID
from ....util.instances import ReprBuilder
from ....util.templating import load_template

from ...user.models.user import User



@@ 120,10 119,6 @@ class ItemVersion(db.Model):
        """
        return self.id == self.item.current_version.id

    def render_body(self) -> str:
        template = load_template(self.body)
        return template.render()

    def __repr__(self) -> str:
        return ReprBuilder(self) \
            .add_with_lookup('id') \

M byceps/services/news/service.py => byceps/services/news/service.py +8 -2
@@ 12,6 12,7 @@ from typing import Dict, Optional, Sequence
from ...database import db, paginate, Pagination, Query
from ...events.news import NewsItemPublished
from ...typing import BrandID, UserID
from ...util.templating import load_template

from ..brand.models.brand import Brand as DbBrand



@@ 248,7 249,6 @@ def get_item_count_by_channel_id() -> Dict[ChannelID, int]:

def _db_entity_to_item(item: DbItem) -> Item:
    channel = _db_entity_to_channel(item.channel)
    body = item.current_version.render_body()
    external_url = item.channel.url_prefix + item.slug
    image_url_path = _assemble_image_url_path(item)
    images = [image_service._db_entity_to_image(image) for image in item.images]


@@ 260,7 260,7 @@ def _db_entity_to_item(item: DbItem) -> Item:
        published_at=item.published_at,
        published=item.published_at is not None,
        title=item.current_version.title,
        body=body,
        body=item.current_version.body,
        external_url=external_url,
        image_url_path=image_url_path,
        images=images,


@@ 274,3 274,9 @@ def _assemble_image_url_path(item: DbItem) -> Optional[str]:
        return None

    return f'/brand/news/{url_path}'


def render_body(raw_body: str) -> str:
    """Render item's raw body to HTML."""
    template = load_template(raw_body)
    return template.render()