Coverage for webapp/app.py: 93%

68 statements  

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

1""" 

2A Flask application for snapcraft.io. 

3 

4The web frontend for the snap store. 

5""" 

6 

7import talisker.requests 

8import webapp.api 

9 

10from talisker import logging 

11 

12# We import the config module before anything else to make sure env vars are 

13# loaded properly and the FLASK_* prefix is stripped before they are parsed 

14import webapp.config 

15 

16from canonicalwebteam.flask_base.app import FlaskBase 

17from webapp.blog.views import init_blog 

18from webapp.docs.views import init_docs 

19from webapp.extensions import csrf 

20from webapp.handlers import set_handlers 

21from webapp.login.views import login 

22from webapp.login.oauth_views import oauth 

23from webapp.publisher.snaps.views import publisher_snaps 

24from webapp.publisher.github.views import publisher_github 

25from webapp.admin.views import admin 

26from webapp.publisher.views import account 

27from webapp.snapcraft.views import snapcraft_blueprint 

28from webapp.store.views import store_blueprint 

29from webapp.tutorials.views import init_tutorials 

30from webapp.packages.store_packages import store_packages 

31from webapp.endpoints.views import endpoints 

32from webapp.endpoints.signing_keys import signing_keys 

33from webapp.endpoints.models import models 

34from webapp.endpoints.members import members 

35from webapp.endpoints.snaps import snaps 

36from webapp.endpoints.snap_search import snap_search 

37from webapp.endpoints.validation_sets import validation_sets 

38from webapp.endpoints.invites import invites 

39from webapp.endpoints.settings import settings 

40 

41 

42TALISKER_WSGI_LOGGER = logging.getLogger("talisker.wsgi") 

43 

44 

45def create_app(testing=False): 

46 app = FlaskBase( 

47 __name__, 

48 "snapcraft.io", 

49 favicon_url="https://assets.ubuntu.com/v1/d4ca039f-favicon_16px.png", 

50 template_404="404.html", 

51 template_folder="../templates", 

52 static_folder="../static", 

53 ) 

54 app.config.from_object("webapp.config") 

55 app.name = "snapcraft" 

56 app.testing = testing 

57 

58 if not testing: 

59 init_extensions(app) 

60 talisker.requests.configure(webapp.api.sso.api_session) 

61 talisker.requests.configure(webapp.helpers.api_session) 

62 talisker.requests.configure(webapp.helpers.api_publisher_session) 

63 

64 if testing: 

65 

66 @app.context_processor 

67 def inject_csrf_token(): 

68 return dict(csrf_token=lambda: "mocked_csrf_token") 

69 

70 set_handlers(app) 

71 

72 app.register_blueprint(snapcraft_blueprint()) 

73 app.register_blueprint(store_packages) 

74 app.register_blueprint(login) 

75 app.register_blueprint(oauth) 

76 app.register_blueprint(store_blueprint()) 

77 app.register_blueprint(account, url_prefix="/account") 

78 app.register_blueprint(publisher_snaps) 

79 app.register_blueprint(publisher_github) 

80 app.register_blueprint(admin) 

81 app.register_blueprint(endpoints) 

82 app.register_blueprint(signing_keys) 

83 app.register_blueprint(models) 

84 app.register_blueprint(members) 

85 app.register_blueprint(snaps) 

86 app.register_blueprint(snap_search) 

87 app.register_blueprint(validation_sets) 

88 app.register_blueprint(invites) 

89 app.register_blueprint(settings) 

90 init_docs(app, "/docs") 

91 init_blog(app, "/blog") 

92 init_tutorials(app, "/tutorials") 

93 

94 return app 

95 

96 

97def init_extensions(app): 

98 csrf.init_app(app)