Skip to content

Commit

Permalink
fix(jinja): make context attrs private on SQL templates (#10934)
Browse files Browse the repository at this point in the history
* fix(jinja): make SQLAlchemy models private on SQL templates

* add missing privates

* fix test
  • Loading branch information
dpgaspar committed Sep 22, 2020
1 parent d879944 commit e11888c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 19 deletions.
36 changes: 18 additions & 18 deletions superset/jinja_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,29 +213,29 @@ def __init__(
extra_cache_keys: Optional[List[Any]] = None,
**kwargs: Any,
) -> None:
self.database = database
self.query = query
self.schema = None
self._database = database
self._query = query
self._schema = None
if query and query.schema:
self.schema = query.schema
self._schema = query.schema
elif table:
self.schema = table.schema
self._schema = table.schema

extra_cache = ExtraCache(extra_cache_keys)

self.context = {
self._context = {
"url_param": extra_cache.url_param,
"current_user_id": extra_cache.current_user_id,
"current_username": extra_cache.current_username,
"cache_key_wrapper": extra_cache.cache_key_wrapper,
"filter_values": filter_values,
"form_data": {},
}
self.context.update(kwargs)
self.context.update(jinja_base_context)
self._context.update(kwargs)
self._context.update(jinja_base_context)
if self.engine:
self.context[self.engine] = self
self.env = SandboxedEnvironment()
self._context[self.engine] = self
self._env = SandboxedEnvironment()

def process_template(self, sql: str, **kwargs: Any) -> str:
"""Processes a sql template
Expand All @@ -244,8 +244,8 @@ def process_template(self, sql: str, **kwargs: Any) -> str:
>>> process_template(sql)
"SELECT '2017-01-01T00:00:00'"
"""
template = self.env.from_string(sql)
kwargs.update(self.context)
template = self._env.from_string(sql)
kwargs.update(self._context)
return template.render(kwargs)


Expand Down Expand Up @@ -288,20 +288,20 @@ def latest_partitions(self, table_name: str) -> Optional[List[str]]:

from superset.db_engine_specs.presto import PrestoEngineSpec

table_name, schema = self._schema_table(table_name, self.schema)
return cast(PrestoEngineSpec, self.database.db_engine_spec).latest_partition(
table_name, schema, self.database
table_name, schema = self._schema_table(table_name, self._schema)
return cast(PrestoEngineSpec, self._database.db_engine_spec).latest_partition(
table_name, schema, self._database
)[1]

def latest_sub_partition(self, table_name: str, **kwargs: Any) -> Any:
table_name, schema = self._schema_table(table_name, self.schema)
table_name, schema = self._schema_table(table_name, self._schema)

from superset.db_engine_specs.presto import PrestoEngineSpec

return cast(
PrestoEngineSpec, self.database.db_engine_spec
PrestoEngineSpec, self._database.db_engine_spec
).latest_sub_partition(
table_name=table_name, schema=schema, database=self.database, **kwargs
table_name=table_name, schema=schema, database=self._database, **kwargs
)

latest_partition = first_latest_partition
Expand Down
2 changes: 1 addition & 1 deletion tests/superset_test_custom_template_processors.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def process_template(self, sql: str, **kwargs) -> str:
# Add custom macros functions.
macros = {"DATE": partial(DATE, datetime.utcnow())} # type: Dict[str, Any]
# Update with macros defined in context and kwargs.
macros.update(self.context)
macros.update(self._context)
macros.update(kwargs)

def replacer(match):
Expand Down

0 comments on commit e11888c

Please sign in to comment.