kaxil commented on a change in pull request #6788: WIP: [AIRFLOW-5944] 
Rendering templated_fields without accessing DAG files
URL: https://github.com/apache/airflow/pull/6788#discussion_r374086756
 
 

 ##########
 File path: airflow/models/templatedfields.py
 ##########
 @@ -0,0 +1,97 @@
+# -*- coding: utf-8 -*-
+#
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+"""Save Rendered Template Fields """
+import json
+
+from sqlalchemy import JSON, Column, String
+from sqlalchemy.orm import Session
+
+from airflow.models.base import ID_LEN, Base
+from airflow.models.taskinstance import TaskInstance
+from airflow.utils.session import provide_session
+from airflow.utils.sqlalchemy import UtcDateTime
+
+
+class RenderedTaskInstanceFields(Base):
+    """
+    Save Rendered Template Fields
+    """
+
+    __tablename__ = "rendered_task_instance_fields"
+
+    dag_id = Column(String(ID_LEN), primary_key=True)
+    task_id = Column(String(ID_LEN), primary_key=True)
+    execution_date = Column(UtcDateTime, primary_key=True)
+    rendered_fields = Column(JSON, nullable=True)
+
+    def __init__(self, ti: TaskInstance):
+        self.dag_id = ti.dag_id
+        self.task_id = ti.task_id
+        self.task = ti.task
+        self.execution_date = ti.execution_date
+
+        ti.render_templates()
+        self.rendered_fields = {
+            field: self.serialize_rendered_field(
+                getattr(self.task, field)
+            ) for field in self.task.template_fields
+        }
+
+    @staticmethod
+    @provide_session
+    def get_templated_fields(ti: TaskInstance, session: Session = None):
+        """
+        Get templated field for a TaskInstance from the 
RenderedTaskInstanceFields
+        table.
+
+        :param ti: Task Instance
+        :param session: SqlAlchemy Session
+        :return: Rendered Templated TI field
+        """
+        result = 
session.query(RenderedTaskInstanceFields.rendered_fields).filter(
+            RenderedTaskInstanceFields.dag_id == ti.dag_id,
+            RenderedTaskInstanceFields.task_id == ti.task_id,
+            RenderedTaskInstanceFields.execution_date == ti.execution_date
+        ).first()
+
+        if result:
+            return result.rendered_fields
+        else:
+            return None
+
+    @staticmethod
+    def serialize_rendered_field(rendered_field):
 
 Review comment:
   Yup, planning to put somewhere in "utils/" to avoid and import that in both 
of them to avoid cyclic imports

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to