From 0d4034311a6074c99974f655c08e44506e938f87 Mon Sep 17 00:00:00 2001 From: Tao Feng Date: Thu, 30 Aug 2018 20:01:28 -0700 Subject: [PATCH] [AIRFLOW-2983] Add prev_ds_nodash and next_ds_nodash macro (#3821) --- airflow/models.py | 6 ++++++ docs/code.rst | 10 ++++++---- tests/core.py | 29 +++++++++++++++++++++++++++++ 3 files changed, 41 insertions(+), 4 deletions(-) diff --git a/airflow/models.py b/airflow/models.py index d0b30b6dae235..6867dc178c6aa 100755 --- a/airflow/models.py +++ b/airflow/models.py @@ -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(':', '') @@ -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, diff --git a/docs/code.rst b/docs/code.rst index 26a721a08d975..b8271ceb1662a 100644 --- a/docs/code.rst +++ b/docs/code.rst @@ -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`` diff --git a/tests/core.py b/tests/core.py index 156b5a9ec6244..c38f5614cdb1b 100644 --- a/tests/core.py +++ b/tests/core.py @@ -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)