Coverage for webapp/app.py: 60%

47 statements  

« prev     ^ index     » next       coverage.py v7.11.0, created at 2025-10-17 22:07 +0000

1import re 

2 

3import talisker.requests 

4from flask import render_template, make_response, request, escape 

5from webapp.extensions import csrf 

6from webapp.config import APP_NAME 

7from webapp.handlers import set_handlers 

8from webapp.store.views import store 

9from webapp.helpers import markdown_to_html 

10from canonicalwebteam.flask_base.app import FlaskBase 

11 

12 

13app = FlaskBase( 

14 __name__, 

15 "rockstore.io", 

16 template_404="404.html", 

17 template_500="500.html", 

18 favicon_url="https://assets.ubuntu.com/v1/5d4edefd-jaas-favicon.png", 

19 static_folder="../static", 

20 template_folder="../templates", 

21) 

22 

23 

24app.name = APP_NAME 

25set_handlers(app) 

26 

27request_session = talisker.requests.get_session() 

28 

29 

30@app.template_filter("linkify") 

31def linkify(text): 

32 escaped_text = escape(text) 

33 url_pattern = re.compile( 

34 r"http[s]?://" 

35 r"(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\\(\\),]|" 

36 r"(?:%[0-9a-fA-F][0-9a-fA-F]))+" 

37 r"(?:#[\w-]+)?" 

38 ) 

39 

40 def replace_with_link(match): 

41 url = match.group(0) 

42 anchor_tag = f'<a href="{url}" target="_blank">{url}</a>' 

43 return anchor_tag 

44 

45 return url_pattern.sub(replace_with_link, str(escaped_text)) 

46 

47 

48csrf.init_app(app) 

49 

50app.register_blueprint(store) 

51 

52 

53app.jinja_env.filters["markdown"] = markdown_to_html 

54 

55 

56@app.route("/contact-us") 

57def contact_us(): 

58 return render_template("contact-us.html") 

59 

60 

61@app.route("/thank-you") 

62def thank_you(): 

63 return render_template("thank-you.html") 

64 

65 

66@app.route("/icon-validator") 

67def icon_validator(): 

68 return render_template("icon-validator.html") 

69 

70 

71@app.route("/sitemap.xml") 

72def site_map(): 

73 xml_sitemap = render_template( 

74 "sitemap/sitemap.xml", 

75 base_url=f"{request.scheme}://{request.host}", 

76 ) 

77 response = make_response(xml_sitemap) 

78 response.headers["Content-Type"] = "application/xml" 

79 

80 return response 

81 

82 

83@app.route("/sitemap-links.xml") 

84def site_map_links(): 

85 links = [ 

86 "/contact-us", 

87 ] 

88 

89 xml_sitemap = render_template( 

90 "sitemap/sitemap-links.xml", 

91 base_url=f"{request.scheme}://{request.host}", 

92 links=links, 

93 ) 

94 response = make_response(xml_sitemap) 

95 response.headers["Content-Type"] = "application/xml" 

96 

97 return response