Browse Source

convert index to mako template

Guénaël Muller 5 years ago
parent
commit
9f041f2858

+ 2 - 0
backend/setup.py View File

@@ -41,6 +41,8 @@ requires = [
41 41
     'lxml',
42 42
     'redis',
43 43
     'rq',
44
+    # frontend file serve
45
+    'pyramid_mako',
44 46
 ]
45 47
 
46 48
 tests_require = [

+ 1 - 0
backend/tracim_backend/__init__.py View File

@@ -125,6 +125,7 @@ def web(global_config, **local_settings):
125 125
     configurator.include(file_controller.bind, route_prefix=BASE_API_V2)
126 126
 
127 127
     if app_config.FRONTEND_SERVE:
128
+        configurator.include('pyramid_mako')
128 129
         frontend_controller = FrontendController(app_config.FRONTEND_DIST_FOLDER_PATH)  # nopep8
129 130
         configurator.include(frontend_controller.bind)
130 131
 

+ 13 - 11
backend/tracim_backend/views/frontend.py View File

@@ -1,13 +1,13 @@
1 1
 import os
2 2
 
3
-from pyramid.response import Response
3
+from pyramid.renderers import render_to_response
4 4
 from pyramid.config import Configurator
5 5
 from tracim_backend.exceptions import PageNotFound
6 6
 from tracim_backend.views import BASE_API_V2
7 7
 from tracim_backend.lib.utils.request import TracimRequest
8 8
 from tracim_backend.views.controllers import Controller
9 9
 
10
-INDEX_PAGE_NAME = 'index.html'
10
+INDEX_PAGE_NAME = 'index.mak'
11 11
 
12 12
 
13 13
 class FrontendController(Controller):
@@ -15,21 +15,23 @@ class FrontendController(Controller):
15 15
     def __init__(self, dist_folder_path: str):
16 16
         self.dist_folder_path = dist_folder_path
17 17
 
18
+    def _get_index_file_path(self):
19
+        index_file_path = os.path.join(self.dist_folder_path, INDEX_PAGE_NAME)
20
+        if not os.path.exists(index_file_path):
21
+            raise FileNotFoundError()
22
+        return index_file_path
23
+
18 24
     def not_found_view(self, context, request: TracimRequest):
19 25
 
20 26
         if request.path.startswith(BASE_API_V2):
21
-            raise PageNotFound('{} is not a valid path'.format(request.path)) from context
27
+            raise PageNotFound('{} is not a valid path'.format(request.path)) from context # nopep8
22 28
         return self.index(context, request)
23 29
 
24 30
     def index(self, context, request: TracimRequest):
25
-        index_file_path = os.path.join(self.dist_folder_path, INDEX_PAGE_NAME)
26
-        if not os.path.exists(index_file_path):
27
-            raise FileNotFoundError()
28
-        with open(index_file_path) as file:
29
-            return Response(
30
-                content_type='text/html',
31
-                body=file.read()
32
-            )
31
+        return render_to_response(
32
+            self._get_index_file_path(),
33
+            {}
34
+        )
33 35
 
34 36
     def bind(self, configurator: Configurator) -> None:
35 37
 

frontend/dist/index.html → frontend/dist/index.mak View File