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

[AIRFLOW-2983] Add prev_ds_nodash and next_ds_nodash macro #3821

Merged
merged 1 commit into from
Aug 31, 2018
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
6 changes: 6 additions & 0 deletions airflow/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -1815,12 +1815,16 @@ def get_template_context(self, session=None):
next_execution_date = task.dag.following_schedule(self.execution_date)

next_ds = None
next_ds_nodash = None
if next_execution_date:
next_ds = next_execution_date.strftime('%Y-%m-%d')
next_ds_nodash = next_ds.replace('-', '')

prev_ds = None
prev_ds_nodash = None
if prev_execution_date:
prev_ds = prev_execution_date.strftime('%Y-%m-%d')
prev_ds_nodash = prev_ds.replace('-', '')

ds_nodash = ds.replace('-', '')
ts_nodash = ts.replace('-', '').replace(':', '')
Expand Down Expand Up @@ -1887,7 +1891,9 @@ def __repr__(self):
'dag': task.dag,
'ds': ds,
'next_ds': next_ds,
'next_ds_nodash': next_ds_nodash,
'prev_ds': prev_ds,
'prev_ds_nodash': prev_ds_nodash,
'ds_nodash': ds_nodash,
'ts': ts,
'ts_nodash': ts_nodash,
Expand Down
10 changes: 6 additions & 4 deletions docs/code.rst
Original file line number Diff line number Diff line change
Expand Up @@ -242,12 +242,14 @@ Variable Description
================================= ====================================
``{{ ds }}`` the execution date as ``YYYY-MM-DD``
``{{ ds_nodash }}`` the execution date as ``YYYYMMDD``
``{{ prev_ds }}`` the previous execution date as ``YYYY-MM-DD``.
``{{ prev_ds }}`` the previous execution date as ``YYYY-MM-DD``
if ``{{ ds }}`` is ``2016-01-08`` and ``schedule_interval`` is ``@weekly``,
``{{ prev_ds }}`` will be ``2016-01-01``.
``{{ next_ds }}`` the next execution date as ``YYYY-MM-DD``.
``{{ prev_ds }}`` will be ``2016-01-01``
``{{ prev_ds_nodash }}`` the previous execution date as ``YYYYMMDD`` if exists, else ``None`
``{{ next_ds }}`` the next execution date as ``YYYY-MM-DD``
if ``{{ ds }}`` is ``2016-01-01`` and ``schedule_interval`` is ``@weekly``,
``{{ prev_ds }}`` will be ``2016-01-08``.
``{{ prev_ds }}`` will be ``2016-01-08``
``{{ next_ds_nodash }}`` the next execution date as ``YYYYMMDD`` if exists, else ``None`
``{{ yesterday_ds }}`` yesterday's date as ``YYYY-MM-DD``
``{{ yesterday_ds_nodash }}`` yesterday's date as ``YYYYMMDD``
``{{ tomorrow_ds }}`` tomorrow's date as ``YYYY-MM-DD``
Expand Down
29 changes: 29 additions & 0 deletions tests/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -626,6 +626,35 @@ def __bool__(self):
dag=self.dag)
t.resolve_template_files()

def test_task_get_template(self):
TI = models.TaskInstance
ti = TI(
task=self.runme_0, execution_date=DEFAULT_DATE)
ti.dag = self.dag_bash
ti.run(ignore_ti_state=True)
context = ti.get_template_context()

# DEFAULT DATE is 2015-01-01
self.assertEquals(context['ds'], '2015-01-01')
self.assertEquals(context['ds_nodash'], '20150101')

# next_ds is 2015-01-02 as the dag interval is daily
self.assertEquals(context['next_ds'], '2015-01-02')
self.assertEquals(context['next_ds_nodash'], '20150102')

# prev_ds is 2014-12-31 as the dag interval is daily
self.assertEquals(context['prev_ds'], '2014-12-31')
self.assertEquals(context['prev_ds_nodash'], '20141231')

self.assertEquals(context['ts'], '2015-01-01T00:00:00+00:00')
self.assertEquals(context['ts_nodash'], '20150101T000000+0000')

self.assertEquals(context['yesterday_ds'], '2014-12-31')
self.assertEquals(context['yesterday_ds_nodash'], '20141231')

self.assertEquals(context['tomorrow_ds'], '2015-01-02')
self.assertEquals(context['tomorrow_ds_nodash'], '20150102')

def test_import_examples(self):
self.assertEqual(len(self.dagbag.dags), NUM_EXAMPLE_DAGS)

Expand Down