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 815455e
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
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
12 changes: 10 additions & 2 deletions tests/modular/test_context.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# -*- coding:utf-8 -*-
import unittest
from unittest import mock, TestCase

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


class TestStatikContext(unittest.TestCase):
class TestStatikContext(TestCase):
def test_build_context(self):
context = StatikContext(dynamic={'render_elm': 'True if my_var > 10 else False'})

Expand All @@ -14,3 +14,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 815455e

Please sign in to comment.