Skip to content

Commit

Permalink
[AIRFLOW-2983] Add prev_ds_nodash and next_ds_nodash macro (#3821)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tao Feng authored and kaxil committed Dec 2, 2018
1 parent 4a17bed commit 0d40343
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 4 deletions.
6 changes: 6 additions & 0 deletions airflow/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -1817,12 +1817,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 @@ -1889,7 +1893,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 @@ -272,12 +272,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 @@ -607,6 +607,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

0 comments on commit 0d40343

Please sign in to comment.