diff --git a/airflow/models.py b/airflow/models.py index 55badf4828541..93368e1f18db6 100755 --- a/airflow/models.py +++ b/airflow/models.py @@ -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(':', '') @@ -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, diff --git a/docs/code.rst b/docs/code.rst index 80ec76193fcf3..c9e9b3d431452 100644 --- a/docs/code.rst +++ b/docs/code.rst @@ -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`` diff --git a/tests/core.py b/tests/core.py index 8df6312eeb2e6..f8b86919128cc 100644 --- a/tests/core.py +++ b/tests/core.py @@ -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)