Skip to content

Commit

Permalink
feat: Add current_user variable and slugify filter to Jinja
Browse files Browse the repository at this point in the history
  • Loading branch information
baumandm committed Apr 11, 2024
1 parent 7fa6eb6 commit 3e3078c
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 0 deletions.
8 changes: 8 additions & 0 deletions querybook/server/app/auth/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@ def id(self):
def get_id(self):
return str(self.id)

@property
def username(self):
return self._user_dict["username"]

@property
def email(self):
return self._user_dict["email"]

@property
def is_admin(self):
return UserRoleType.ADMIN.value in self._user_dict["roles"]
Expand Down
14 changes: 14 additions & 0 deletions querybook/server/lib/query_analysis/templating.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@
from jinja2.sandbox import SandboxedEnvironment
from jinja2 import meta

from slugify import slugify

from app.db import with_session
from flask_login import current_user
from lib import metastore
from logic import admin as admin_logic

Expand Down Expand Up @@ -68,6 +71,12 @@ def get_default_variables():
return {
"today": datetime.today().strftime("%Y-%m-%d"),
"yesterday": (datetime.today() - timedelta(1)).strftime("%Y-%m-%d"),
"current_user": (
current_user.username if current_user.is_authenticated else None
),
"current_user_email": (
current_user.email if current_user.is_authenticated else None
),
}


Expand Down Expand Up @@ -172,6 +181,11 @@ def get_templated_query_env(engine_id: int, session=None):
latest_partition=create_get_latest_partition(engine_id, session=session)
)

# Inject filters
jinja_env.filters.update(
slugify=lambda x: slugify(x, separator="_"),
)

# Template rendering config
jinja_env.trim_blocks = True
jinja_env.lstrip_blocks = True
Expand Down
42 changes: 42 additions & 0 deletions querybook/webapp/components/TemplateGuide/guides/predefined.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,30 @@ Gets rendered to:
SELECT * FROM users WHERE created_at = '2022-02-24';
```

`{{ current_user }}`: Map to the current user's username in Querybook. Example use case:

```sql
SELECT * FROM tickets WHERE assigned_to = '{{ current_user }}';
```

Gets rendered to:

```sql
SELECT * FROM tickets WHERE assigned_to = 'john_doe';
```

`{{ current_user_email }}`: Map to the current user's email in Querybook. Example use case:

```sql
SELECT * FROM tickets WHERE assigned_to_email = '{{ current_user_email }}';
```

Gets rendered to:

```sql
SELECT * FROM tickets WHERE assigned_to_email = '[email protected]'
```

## Functions

`{{ latest_partition('<schema_name>.<table_name>', '<partition_key>') }}`:
Expand Down Expand Up @@ -66,3 +90,21 @@ This gets rendered to:
SELECT * FROM default.pins
WHERE dt = '2022-02-25';
```

## Filters

Filters transform the output of template variables, and are applied using the pipe character `|`. Filters can be chained together by using multiple pipe characters. A list of built-in filters can be found [here](https://jinja.palletsprojects.com/en/3.1.x/templates/#list-of-builtin-filters).

Custom filters are also available:

`{{ ... | slugify }}`: Transforms a string into a format suitable for use in table names, column names, URLs, etc. It converts all characters to lowercase and replaces spaces with underscores. It also removes special characters and Unicode. Example use case:

```sql
CREATE TABLE default.report_{{ today | slugify }} AS ...
```

Gets rendered to:

```sql
CREATE TABLE default.report_2022_02_25 AS ...
```
1 change: 1 addition & 0 deletions requirements/base.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ flask-socketio==5.3.3

# Query templating
Jinja2==3.1.3 # From Flask
python-slugify==8.0.4

# Celery
celery==5.2.7
Expand Down

0 comments on commit 3e3078c

Please sign in to comment.