Browse Source

Merge branch 'master' into datasource_fieldtypes

Yann Weber 8 years ago
parent
commit
93980d68a0

+ 21
- 0
Template/Loader.py View File

@@ -1,11 +1,16 @@
1 1
 #-*- coding: utf-8 -*-
2 2
 
3 3
 import jinja2
4
+
4 5
 import settings
6
+from .api import api_lodel_templates
7
+from .exceptions import NotAllowedCustomAPIKeyError
5 8
 
6 9
 
7 10
 class TemplateLoader(object):
8 11
 
12
+    __reserved_template_keys = ['lodel']
13
+
9 14
     ## @brief Initializes a template loader
10 15
     #
11 16
     # @param search_path str : the base path from which the templates are searched. To use absolute paths, you can set
@@ -26,10 +31,26 @@ class TemplateLoader(object):
26 31
     #
27 32
     # @return str. String containing the HTML output of the processed templated
28 33
     def render_to_html(self, template_file, template_vars={}, template_extra=None):
34
+
29 35
         loader = jinja2.FileSystemLoader(searchpath=self.search_path, followlinks=self.follow_links)
30 36
         environment = jinja2.Environment(loader=loader)
31 37
         template = environment.get_template(template_file)
38
+
39
+        # Lodel2 default api is loaded
40
+        template.globals['lodel'] = api_lodel_templates
41
+
42
+        # Extra modules are loaded
32 43
         if template_extra is not None:
33 44
             for extra in template_extra:
45
+                if not self.__is_allowed_template_key(extra[0]):
46
+                    raise NotAllowedCustomAPIKeyError("The name 'lodel' is a reserved one for the loaded APIs in templates")
34 47
                 template.globals[extra[0]] = extra[1]
48
+
35 49
         return template.render(template_vars)
50
+
51
+    ## @brief Checks if the key used for the template is allowed
52
+    #
53
+    # @param key str
54
+    # @return bool
55
+    def __is_allowed_template_key(self, key):
56
+        return False if key in self.__class__.__reserved_template_keys else True

+ 0
- 1
Template/__init__.py View File

@@ -1 +0,0 @@
1
-__author__ = 'roland'

+ 0
- 0
Template/api/__init__.py View File


+ 3
- 0
Template/api/api_lodel_templates.py View File

@@ -0,0 +1,3 @@
1
+# -*- coding: utf-8 -*-
2
+
3
+# Lodel 2 templates API : loaded by default

+ 7
- 0
Template/exceptions/NotAllowedCustomAPIKeyError.py View File

@@ -0,0 +1,7 @@
1
+#-*- coding: utf-8 -*-
2
+
3
+
4
+class NotAllowedCustomAPIKeyError(Exception):
5
+
6
+    def __init__(self, message):
7
+        self.message = message

+ 1
- 0
Template/exceptions/__init__.py View File

@@ -0,0 +1 @@
1
+from .NotAllowedCustomAPIKeyError import NotAllowedCustomAPIKeyError

+ 1
- 1
install/Makefile View File

@@ -7,7 +7,7 @@ dbinit:
7 7
 	python -c "import utils; utils.db_init()"
8 8
 
9 9
 dirinit:
10
-	@mkdir templates
10
+	python -c "import utils; utils.dir_init()"
11 11
 
12 12
 emgraph:
13 13
 	python -c "import utils; utils.em_graph()"

+ 26
- 0
install/utils.py View File

@@ -3,6 +3,7 @@
3 3
 from loader import *
4 4
 import warnings
5 5
 
6
+
6 7
 def refreshdyn():
7 8
     import sys
8 9
     from EditorialModel.model import Model
@@ -18,6 +19,7 @@ def refreshdyn():
18 19
     # Create the python file
19 20
     fact.create_pyfile(em, LeDataSourceSQL, {})
20 21
 
22
+
21 23
 def db_init():
22 24
     from EditorialModel.backend.json_backend import EmBackendJson
23 25
     from EditorialModel.model import Model
@@ -25,6 +27,7 @@ def db_init():
25 27
     em = Model(EmBackendJson(Settings.em_file))
26 28
     em.migrate_handler(mh)
27 29
 
30
+
28 31
 def em_graph(output_file = None, image_format = None):
29 32
     from EditorialModel.model import Model
30 33
     from EditorialModel.backend.json_backend import EmBackendJson
@@ -63,3 +66,26 @@ def em_graph(output_file = None, image_format = None):
63 66
     os.unlink(dot_file)
64 67
     print("Output image written in file : '%s'" % output_file)
65 68
 
69
+
70
+def dir_init():
71
+    import os
72
+
73
+    # Templates
74
+    print("Creating Base Templates ...")
75
+    templates = {
76
+        'base': {'file': 'base.html', 'content': '{% extends "templates/base.html" %}'},
77
+        'base_backend': {'file': 'base_backend.html', 'content': '{% extends "templates/base_backend.html" %}'}
78
+    }
79
+
80
+    current_directory = os.path.dirname(os.path.abspath(__file__))
81
+    templates_directory = os.path.join(current_directory,'templates')
82
+    if not os.path.exists(templates_directory):
83
+        os.makedirs(templates_directory)
84
+
85
+    for _, template in templates.items():
86
+        my_file_path = os.path.join(templates_directory, template['file'])
87
+        if os.path.exists(my_file_path):
88
+            os.unlink(my_file_path)
89
+        with open(my_file_path, 'w') as my_file:
90
+            my_file.write(template['content'])
91
+        print("Created %s" % my_file_path)

+ 14
- 0
templates/base.html View File

@@ -0,0 +1,14 @@
1
+<!doctype html>
2
+<html lang="en">
3
+<head>
4
+    <meta charset="UTF-8" />
5
+    {% block title %}<title></title>{% endblock %}
6
+    {% block head %}{% endblock %}
7
+</head>
8
+<body>
9
+    <div id="content">
10
+        {% block content %}{% endblock %}
11
+    </div>
12
+    {% block javascript %}{% endblock %}
13
+</body>
14
+</html>

+ 14
- 0
templates/base_backend.html View File

@@ -0,0 +1,14 @@
1
+<!doctype html>
2
+<html lang="en">
3
+<head>
4
+    <meta charset="UTF-8" />
5
+    {% block title %}<title></title>{% endblock %}
6
+    {% block head %}{% endblock %}
7
+</head>
8
+<body>
9
+    <div id="content">
10
+        {% block content %}{% endblock %}
11
+    </div>
12
+    {% block javascript %}{% endblock %}
13
+</body>
14
+</html>

Loading…
Cancel
Save