diff options
| -rw-r--r-- | ihatemoney/conf-templates/apache-vhost.conf.j2 | 3 | ||||
| -rw-r--r-- | ihatemoney/conf-templates/supervisord.conf.j2 | 2 | ||||
| -rwxr-xr-x | ihatemoney/manage.py | 21 | ||||
| -rw-r--r-- | ihatemoney/utils.py | 19 |
4 files changed, 30 insertions, 15 deletions
diff --git a/ihatemoney/conf-templates/apache-vhost.conf.j2 b/ihatemoney/conf-templates/apache-vhost.conf.j2 index 3246d27..e169589 100644 --- a/ihatemoney/conf-templates/apache-vhost.conf.j2 +++ b/ihatemoney/conf-templates/apache-vhost.conf.j2 @@ -1,8 +1,7 @@ <VirtualHost *:80> ServerAdmin admin@example.com # CUSTOMIZE ServerName ihatemoney.example.com # CUSTOMIZE - - WSGIDaemonProcess ihatemoney user=www-data group=www-data threads=5 python-path={{ pkg_path }} {% if venv_path %}python-home={{ venv_path }}{% endif %} + WSGIDaemonProcess ihatemoney user=www-data group=www-data threads=5 python-home={{ sys_prefix }} WSGIScriptAlias / {{ pkg_path }}/wsgi.py WSGIPassAuthorization On diff --git a/ihatemoney/conf-templates/supervisord.conf.j2 b/ihatemoney/conf-templates/supervisord.conf.j2 index fa16c0c..605f39d 100644 --- a/ihatemoney/conf-templates/supervisord.conf.j2 +++ b/ihatemoney/conf-templates/supervisord.conf.j2 @@ -1,5 +1,5 @@ [program:ihatemoney] -command={{ venv_path }}/bin/gunicorn -c /etc/ihatemoney/gunicorn.conf.py ihatemoney.wsgi:application +command={{ bin_path }}/gunicorn -c /etc/ihatemoney/gunicorn.conf.py ihatemoney.wsgi:application user=ihatemoney autostart=true autorestart=true diff --git a/ihatemoney/manage.py b/ihatemoney/manage.py index 315cfac..3207b55 100755 --- a/ihatemoney/manage.py +++ b/ihatemoney/manage.py @@ -1,18 +1,17 @@ #!/usr/bin/env python import os -import pkgutil import random import sys import getpass from flask_script import Manager, Command, Option from flask_migrate import Migrate, MigrateCommand -from jinja2 import Template from werkzeug.security import generate_password_hash from ihatemoney.run import create_app from ihatemoney.models import db +from ihatemoney.utils import create_jinja_env class GeneratePasswordHash(Command): @@ -44,14 +43,16 @@ class GenerateConfig(Command): for i in range(50)]) def run(self, config_file): - template_content = pkgutil.get_data( - 'ihatemoney', - os.path.join('conf-templates/', config_file) + '.j2' - ).decode('utf-8') - - print(Template(template_content).render( - pkg_path=os.path.abspath(os.path.dirname(__file__)), - venv_path=os.environ.get('VIRTUAL_ENV'), + env = create_jinja_env('conf-templates', strict_rendering=True) + template = env.get_template('%s.j2' % config_file) + + bin_path = os.path.dirname(sys.executable) + pkg_path = os.path.abspath(os.path.dirname(__file__)) + + print(template.render( + pkg_path=pkg_path, + bin_path=bin_path, + sys_prefix=sys.prefix, secret_key=self.gen_secret_key(), )) diff --git a/ihatemoney/utils.py b/ihatemoney/utils.py index aaae2a0..5dd1e7b 100644 --- a/ihatemoney/utils.py +++ b/ihatemoney/utils.py @@ -2,7 +2,7 @@ import base64 import re from io import BytesIO, StringIO -from jinja2 import filters +import jinja2 from json import dumps, JSONEncoder from flask import redirect from werkzeug.routing import HTTPException, RoutingException @@ -83,7 +83,7 @@ def minimal_round(*args, **kw): from http://stackoverflow.com/questions/28458524/ """ # Use the original round filter, to deal with the extra arguments - res = filters.do_round(*args, **kw) + res = jinja2.filters.do_round(*args, **kw) # Test if the result is equivalent to an integer and # return depending on it ires = int(res) @@ -172,6 +172,21 @@ class LoginThrottler(): self._attempts.pop(ip, None) +def create_jinja_env(folder, strict_rendering=False): + """Creates and return a Jinja2 Environment object, used, to load the + templates. + + :param strict_rendering: + if set to `True`, all templates which use an undefined variable will + throw an exception (default to `False`). + """ + loader = jinja2.PackageLoader('ihatemoney', folder) + kwargs = {'loader': loader} + if strict_rendering: + kwargs['undefined'] = jinja2.StrictUndefined + return jinja2.Environment(**kwargs) + + class IhmJSONEncoder(JSONEncoder): """Subclass of the default encoder to support custom objects. Taken from the deprecated flask-rest package.""" |
