Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(jinja): make context attrs private on SQL templates #10934

Merged
merged 3 commits into from
Sep 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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