<commit_before>from django.conf import settings
from django.template import TemplateSyntaxError
from django.template.loader import render_to_string
from wagtail.core.models import Site


def get_meta_image_url(request, image):
    """
    Resize an image for metadata tags, and return an absolute URL to it.
    """
    filter = getattr(settings, "WAGTAILMETADATA_IMAGE_FILTER", "original")
    rendition = image.get_rendition(filter=filter)
    return request.build_absolute_uri(rendition.url)


def meta_tags(request, model):
    if not request:
        raise TemplateSyntaxError(
            "'meta_tags' missing request from context")
    if not model:
        raise TemplateSyntaxError(
            "'meta_tags' tag is missing a model or object")
    context = {
        'site_name': Site.find_for_request(request).site_name,
        'object': model,
    }

    meta_image = model.get_meta_image()
    if meta_image:
        meta_image_width = meta_image.width
        meta_image_height = meta_image.height
        meta_image = get_meta_image_url(request, meta_image)
    context['meta_image_width'] = meta_image_width
    context['meta_image_height'] = meta_image_height
    context['meta_image'] = meta_image


    return render_to_string('wagtailmetadata/parts/tags.html',
                            context, request=request)<commit_msg>check metaimage exist before getting its w/h<commit_after>from django.conf import settings
from django.template import TemplateSyntaxError
from django.template.loader import render_to_string
from wagtail.core.models import Site


def get_meta_image_url(request, image):
    """
    Resize an image for metadata tags, and return an absolute URL to it.
    """
    filter = getattr(settings, "WAGTAILMETADATA_IMAGE_FILTER", "original")
    rendition = image.get_rendition(filter=filter)
    return request.build_absolute_uri(rendition.url)


def meta_tags(request, model):
    if not request:
        raise TemplateSyntaxError(
            "'meta_tags' missing request from context")
    if not model:
        raise TemplateSyntaxError(
            "'meta_tags' tag is missing a model or object")
    context = {
        'site_name': Site.find_for_request(request).site_name,
        'object': model,
    }

    meta_image = model.get_meta_image()
    if meta_image:
        context['meta_image_width'] = meta_image.width
        context['meta_image_height'] = meta_image.height
        meta_image = get_meta_image_url(request, meta_image)

    context['meta_image'] = meta_image


    return render_to_string('wagtailmetadata/parts/tags.html',
                            context, request=request)
{'fraction_non_alphanumeric': 0.08289174867524603, 'fraction_numerical': 0.0, 'mean_word_length': 3.4123539232053424, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4477274', 'n_tokens_mistral': 764, 'n_tokens_neox': 749, 'n_words': 185}