
from flask import abort, g

from ...services.snippet import mountpoint_service
from ...util.framework.blueprint import create_blueprint

from .templating import render_snippet_as_page, render_snippet_as_partial


blueprint = create_blueprint('snippet', __name__)

blueprint.add_app_template_global(render_snippet_as_partial, 'render_snippet')


def view_current_version_by_name(name):
    """Show the current version of the snippet that is mounted with that
    name.
    """
    # Note: endpoint suffix != snippet name
    version = mountpoint_service.find_current_snippet_version_for_mountpoint(
        g.site_id, name
    )

    if version is None:
        abort(404)

    return render_snippet_as_page(version)


from flask import abort, g, url_for

from ...services.snippet import mountpoint_service
from ...util.framework.blueprint import create_blueprint

from .templating import render_snippet_as_page, render_snippet_as_partial


blueprint = create_blueprint('snippet', __name__)

blueprint.add_app_template_global(render_snippet_as_partial, 'render_snippet')


@blueprint.app_template_global()
def url_for_snippet(name):
    return url_for(f'snippet.{name}')


def view_current_version_by_name(name):
    """Show the current version of the snippet that is mounted with that
    name.
    """
    # Note: endpoint suffix != snippet name
    version = mountpoint_service.find_current_snippet_version_for_mountpoint(
        g.site_id, name
    )

    if version is None:
        abort(404)

    return render_snippet_as_page(version)

Introduce global template function `url_for_snippet`
Introduce global template function `url_for_snippet`

Use it to ease the transition to a multisite-capable snippet URL rule system.

  
- from flask import abort, g
+ from flask import abort, g, url_for
  
  from ...services.snippet import mountpoint_service
  from ...util.framework.blueprint import create_blueprint
  
  from .templating import render_snippet_as_page, render_snippet_as_partial
  
  
  blueprint = create_blueprint('snippet', __name__)
  
  blueprint.add_app_template_global(render_snippet_as_partial, 'render_snippet')
+ 
+ 
+ @blueprint.app_template_global()
+ def url_for_snippet(name):
+     return url_for(f'snippet.{name}')
  
  
  def view_current_version_by_name(name):
      """Show the current version of the snippet that is mounted with that
      name.
      """
      # Note: endpoint suffix != snippet name
      version = mountpoint_service.find_current_snippet_version_for_mountpoint(
          g.site_id, name
      )
  
      if version is None:
          abort(404)
  
      return render_snippet_as_page(version)
  
Introduce global template function `url_for_snippet`
## Code Before:

from flask import abort, g

from ...services.snippet import mountpoint_service
from ...util.framework.blueprint import create_blueprint

from .templating import render_snippet_as_page, render_snippet_as_partial


blueprint = create_blueprint('snippet', __name__)

blueprint.add_app_template_global(render_snippet_as_partial, 'render_snippet')


def view_current_version_by_name(name):
    """Show the current version of the snippet that is mounted with that
    name.
    """
    # Note: endpoint suffix != snippet name
    version = mountpoint_service.find_current_snippet_version_for_mountpoint(
        g.site_id, name
    )

    if version is None:
        abort(404)

    return render_snippet_as_page(version)

## Instruction:
Introduce global template function `url_for_snippet`
## Code After:

from flask import abort, g, url_for

from ...services.snippet import mountpoint_service
from ...util.framework.blueprint import create_blueprint

from .templating import render_snippet_as_page, render_snippet_as_partial


blueprint = create_blueprint('snippet', __name__)

blueprint.add_app_template_global(render_snippet_as_partial, 'render_snippet')


@blueprint.app_template_global()
def url_for_snippet(name):
    return url_for(f'snippet.{name}')


def view_current_version_by_name(name):
    """Show the current version of the snippet that is mounted with that
    name.
    """
    # Note: endpoint suffix != snippet name
    version = mountpoint_service.find_current_snippet_version_for_mountpoint(
        g.site_id, name
    )

    if version is None:
        abort(404)

    return render_snippet_as_page(version)
