Skip to content

Commit

Permalink
Add environment variables to context
Browse files Browse the repository at this point in the history
Add environment variables to context so they
may be used in views.

Closes #72
  • Loading branch information
kx-chen committed Dec 6, 2018
1 parent b50857d commit e0e3a99
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 0 deletions.
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ lipsum==0.1.2
Markdown==3.0.1
MarkupSafe==1.0
mlalchemy==0.2.2
mock
pathtools==0.1.2
pystache==0.5.4
python-dateutil==2.7.3
Expand Down
3 changes: 3 additions & 0 deletions statik/context.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# -*- coding: utf-8 -*-

from __future__ import unicode_literals

import os
from copy import deepcopy, copy

from future.utils import iteritems
Expand Down Expand Up @@ -67,6 +69,7 @@ def build(self, db=None, safe_mode=False, for_each_inst=None, extra=None):
"""Builds a dictionary that can be used as context for template rendering."""
result = copy(self.initial)
result.update(self.static)
result.update(os.environ)
if self.dynamic:
result.update(self.build_dynamic(db, extra=extra, safe_mode=safe_mode))
if self.for_each and for_each_inst:
Expand Down
10 changes: 10 additions & 0 deletions tests/modular/test_context.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# -*- coding:utf-8 -*-
import unittest

import mock

from statik.context import StatikContext
from statik.database import StatikDatabase

Expand All @@ -14,3 +16,11 @@ def test_build_context(self):

result = context.build(db=StatikDatabase(models={}, data_path=''), extra={'my_var': 5})
assert result.get('render_elm') is False

def test_env_var_in_context(self):
with mock.patch.dict('statik.context.os.environ',
{'HOME': 'HOME!!'}):
context = StatikContext()

result = context.build(db=StatikDatabase(models={}, data_path=''))
self.assertEqual(result.get('HOME'), 'HOME!!')

0 comments on commit e0e3a99

Please sign in to comment.