title: Airflow custom jinja2 filters
body: <p>I am trying to add custom filters for my airflow jinja2 templates.</p>
  
  <p>Since my folders in S3 are like </p>
  
  <blockquote>
    <p>/year/month/day/</p>
  </blockquote>
  
  <p>, my purpose is to use yesterday_ds in the Variables screen like this:</p>
  
  <blockquote>
    <p>s3://logs.web.com/AWSLogs/{{ yesterday_ds | get_year }}/{{ yesterday_ds | get_month }}/{{ yesterday_ds | get_day }}/</p>
  </blockquote>
  
  <p>I have seen in a PR (which I think is already merged..) that you can do this with the parameter 'user_defined_filters' in the dag_args parameter in the dag object creation <a href="https://github.com/apache/incubator-airflow/pull/2258/files" rel="nofollow noreferrer">here</a></p>
  
  <p>The problem is that even when doing it, it says 'no filter named get_year', for example.</p>
  
  <p>This is my code:</p>
  
  <p>dag.py</p>
  
  <pre><code>   dag = DAG(
          dag_id='dag-name',
          default_args=utils.get_dag_args(user_defined_filters=utils.get_date_filters()),
          template_searchpath=tmpl_search_path,
          schedule_interval=timedelta(days=1),
          max_active_runs=1,
          )
  </code></pre>
  
  <p>utils.py</p>
  
  <pre><code>def get_dag_args(**kwargs):
  return {
      'owner'               : kwargs.get('owner', 'owner_name'),
      'depends_on_past'     : kwargs.get('depends_on_past', False),
      'start_date'          : kwargs.get('start_date', datetime.now() - timedelta(1)),
      'email'               : kwargs.get('email', ['blabla@blabla.com']),
      'retries'             : kwargs.get('retries', 5),
      'provide_context'     : kwargs.get('provide_context', True),
      'retry_delay'         : kwargs.get('retry_delay', timedelta(minutes=5)),
      'user_defined_filters': get_date_filters()
      }
  
  
  def get_date_filters():
      return dict(
          get_year=lambda date_string: date_string.strftime('%Y'),
          get_month=lambda date_string: date_string.strftime('%m'),
          get_day=lambda date_string: date_string.strftime('%d'),
          )
  </code></pre>
  
  <p>Does anybody see where I am mistaken? Thank you!</p>
  
  <p><em>EDIT</em></p>
  
  <p>Printing this after the dag definition, shows no custom filters, unfortunately :(.</p>
  
  <pre><code>jinja_env = dag.get_template_env()
  print(jinja_env.filters)
  </code></pre>
  
  <p>Also, if I try to add it directly as a DAG object parameter, as it shows in the tests @ tests/models.py:</p>
  
  <pre><code>Broken DAG: [/home/ubuntu/airflow/dags/dag.py] __init__() got an unexpected keyword argument 'user_defined_filters'
  </code></pre>
  
  <p><em>EDIT 2</em></p>
  
  <p>Ok what I see is that I have the version 1.8.0 and this one does not have the filters. Anybody knows how to download the 1.8.2rc one via pip? Or we cant?</p>