Best Python code snippet using pytest-mozwebqa_python
ssr.py
Source:ssr.py
1"""2do some kind of pseudo SSR, modifying the index.html to contain metadata3"""4import textwrap5from io import BytesIO6from textwrap import shorten7from urllib.parse import unquote8from PIL import Image, ImageFont, ImageDraw9from flask import Blueprint, redirect, render_template, abort, request, send_file10from playhouse.flask_utils import get_object_or_40411from app import cache12from data import series_data_by_slug13from models import Episode, Series14ssr_routes = Blueprint("ssr_routes", __name__, template_folder="templates")15with open("./dist/index.html") as f:16 index_html = f.read()17placeholder_token = '<title>CR Search</title>'18def draw_image(text: str, description: str, subtitle: str = None) -> BytesIO:19 text = text.replace("| CR Search", "").strip()20 text = shorten(text, 25, placeholder=" ...")21 if subtitle:22 text += "\n" + subtitle23 width, height = (1200, 628)24 img = Image.open("web/src/assets/background_small.png")25 mr_eves = ImageFont.truetype("web/fonts/Mr Eaves/Mr Eaves Small Caps.otf", 50)26 title_font = ImageFont.truetype("web/fonts/Nodesto Caps Condensed/Nodesto Caps Condensed.otf", 120)27 small_font = ImageFont.truetype("web/fonts/Scaly Sans/Scaly Sans.otf", 30)28 draw = ImageDraw.Draw(img)29 w, h = draw.multiline_textsize(text, title_font)30 draw.multiline_text(((width - w) / 2, (height - h) / 4), text,31 font=title_font, align="center",32 fill="#58180d")33 description = textwrap.fill(description, 50)34 w, h = draw.multiline_textsize(description, mr_eves)35 draw.multiline_text(((width - w) / 2, (height - h) / 3 * 2), description,36 font=mr_eves, align="center",37 fill="black")38 footer_text = "Critical Role Search"39 w, h = draw.multiline_textsize(footer_text, small_font)40 draw.multiline_text(((width - w - 10), (height - h - 10)), footer_text,41 font=small_font, align="center",42 fill="black")43 url = request.url.split("?")[0]44 url = unquote(url)45 if len(url) > 50:46 url = "/".join(url.split("/")[:-1])47 w, h = draw.multiline_textsize(url, small_font)48 draw.multiline_text((10, (height - h - 10)), url,49 font=small_font, align="center",50 fill="black")51 byte_io = BytesIO()52 img.save(byte_io, "PNG", optimize=True)53 byte_io.seek(0)54 return byte_io55@ssr_routes.route("/", defaults={'something': None})56@ssr_routes.route("/<string:something>/")57def home_redirect(something: str):58 return redirect("/campaign3/10/")59@ssr_routes.route("/episodes")60@cache.cached(timeout=60 * 60 * 24 * 30, query_string=True)61def episodes():62 num = Episode.select().count()63 description = f"Overview over {num} imported episodes of Critical Role"64 title = "Episode Overview | CR Search"65 if request.args.get("image", None) == "true":66 return send_file(draw_image(title, description), mimetype="image/png")67 additional_html = render_template(68 "header.html",69 description=description,70 title=title,71 url=request.url72 )73 return index_html.replace(placeholder_token, additional_html)74@ssr_routes.route("/transcript/<string:series>/<string:episode_number>/")75@cache.cached(timeout=60 * 60 * 24 * 30, query_string=True)76def transcript(series: str, episode_number: str):77 episode = get_object_or_404(Episode.select(Episode, Series).where(78 (Episode.episode_number == episode_number)79 &80 (Episode.series.slug == series)81 ).join(Series))82 description = f"Browse through the transcript of episode {episode_number} of {episode.series.title} (â{episode.pretty_title}â)"83 title = f"{episode.pretty_title} | Transcript | CR Search"84 if request.args.get("image", None) == "true":85 return send_file(draw_image(episode.pretty_title, description, subtitle="Transcript"), mimetype="image/png")86 additional_html = render_template(87 "header.html",88 description=description,89 title=title,90 url=request.url91 )92 return index_html.replace(placeholder_token, additional_html)93@ssr_routes.route("/<string:series_slug>/<string:episodes>/<string:keyword>")94@ssr_routes.route("/<string:series_slug>/<string:episodes>", defaults={'keyword': None})95@ssr_routes.route("/<string:series_slug>/<string:episodes>/", defaults={'keyword': None})96@cache.cached(timeout=60 * 60 * 24 * 30, query_string=True)97def search(series_slug: str, episodes: str, keyword: str):98 one_shot = Episode.select(Episode, Series).where(99 Episode.series.slug == series_slug100 ).join(Series).count() == 1101 try:102 series = series_data_by_slug[series_slug]103 except KeyError:104 return abort(404)105 if keyword:106 if one_shot:107 description = f"Search for â{keyword}â in {series.name}"108 else:109 description = f"Search for â{keyword}â up to episode {episodes} of {series.name}"110 title = f"{keyword} | CR Search"111 else:112 description = f"Search through {series.name} of Critical Role"113 title = f"{series.name} | CR Search"114 if request.args.get("image", None) == "true":115 return send_file(draw_image(title, description), mimetype="image/png")116 additional_html = render_template(117 "header.html",118 description=description,119 title=title,120 url=request.url121 )122 print(additional_html)...
additionalhtml.py
Source:additionalhtml.py
1from django.conf import settings2from django.template.loader import render_to_string3def context_additionalhtml(request):4 try:5 additional_page_includes = getattr(settings, 'ADDITIONAL_PAGE_INCLUDES')6 additional_html = ''.join([render_to_string(page) for page in additional_page_includes])7 except AttributeError:8 additional_html = ''9 return {10 'additional_html':additional_html...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!