~homeworkprod/byceps

95635235ec195610e0e7cd20a5fc54f1155ab4eb — Jochen Kupperschmidt 1 year, 7 months ago 0a5af42
Pass image list to function that renders news item body to save database queries
M byceps/blueprints/admin/news/views.py => byceps/blueprints/admin/news/views.py +6 -2
@@ 245,7 245,9 @@ def item_view_version(version_id):

def _render_item_version(version):
    """Render the news item version."""
    channel = version.item.channel
    item = version.item

    channel = item.channel
    brand = brand_service.find_brand(channel.brand_id)

    context = {


@@ 254,7 256,9 @@ def _render_item_version(version):
    }

    try:
        rendered_body = news_item_service.render_body(version.body, channel.id)
        rendered_body = news_item_service.render_body(
            version.body, channel.id, item.images
        )

        context.update({
            'rendered_body': rendered_body,

M byceps/blueprints/news/views.py => byceps/blueprints/news/views.py +3 -1
@@ 101,7 101,9 @@ def _may_view_drafts(user):

def _replace_body_with_rendered_body(item):
    try:
        rendered_body = news_service.render_body(item.body, item.channel.id)
        rendered_body = news_service.render_body(
            item.body, item.channel.id, item.images
        )
    except Exception as e:
        rendered_body = None  # Not the best error indicator.


M byceps/services/news/service.py => byceps/services/news/service.py +10 -6
@@ 8,7 8,7 @@ byceps.services.news.service

from datetime import datetime
from functools import partial
from typing import Dict, Optional, Sequence
from typing import Dict, List, Optional, Sequence
from uuid import UUID

from jinja2 import Markup


@@ 16,6 16,7 @@ from jinja2 import Markup
from ...database import db, paginate, Pagination, Query
from ...events.news import NewsItemPublished
from ...typing import BrandID, UserID
from ...util.iterables import find
from ...util.templating import load_template

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


@@ 28,7 29,7 @@ from .models.item import (
    ItemVersion as DbItemVersion,
)
from . import image_service
from .transfer.models import ChannelID, Item, ItemID, ItemVersionID
from .transfer.models import ChannelID, Image, Item, ItemID, ItemVersionID


def create_item(


@@ 279,15 280,18 @@ def _assemble_image_url_path(item: DbItem) -> Optional[str]:
    return f'/data/global/news_channels/{item.channel_id}/{url_path}'


def render_body(raw_body: str, channel_id: ChannelID) -> str:
def render_body(
    raw_body: str, channel_id: ChannelID, images: List[Image]
) -> str:
    """Render item's raw body to HTML."""
    template = load_template(raw_body)
    render_image_with_channel_id = partial(_render_image, channel_id)
    return template.render(render_image=render_image_with_channel_id)
    render_image= partial(_render_image, channel_id, images)
    return template.render(render_image=render_image)


def _render_image(
    channel_id: ChannelID,
    images: List[Image],
    image_id: str,
    *,
    width: Optional[int] = None,


@@ 297,7 301,7 @@ def _render_image(
    if not _is_uuid(image_id):
        raise Exception(f'Invalid image ID "{image_id}"')

    image = image_service.find_image(image_id)
    image = find(lambda image: str(image.id) == image_id, images)

    if image is None:
        raise Exception(f'Unknown image ID "{image_id}"')