diff --git a/cornflow-server/cornflow/tests/data/models/__init__.py b/cornflow-server/cornflow/tests/data/models/__init__.py deleted file mode 100644 index 448a0895..00000000 --- a/cornflow-server/cornflow/tests/data/models/__init__.py +++ /dev/null @@ -1,12 +0,0 @@ -""" -Initialization file for the models module -""" -from .action import ActionModel -from .apiview import ApiViewModel -from .case import CaseModel -from .dag import DeployedDAG -from .execution import ExecutionModel -from .instance import InstanceModel -from .permission import PermissionsDAG, PermissionViewRoleModel -from .roles import RoleModel, UserRoleModel -from .user import UserModel diff --git a/cornflow-server/cornflow/tests/data/models/action.py b/cornflow-server/cornflow/tests/data/models/action.py deleted file mode 100644 index e873e05c..00000000 --- a/cornflow-server/cornflow/tests/data/models/action.py +++ /dev/null @@ -1,28 +0,0 @@ -""" -""" -from cornflow.models import ActionModel - -from cornflow.shared import db - - -class ActionModel(ActionModel): - """ - This model contains the base actions over the REST API. These are: - * can get - * can patch - * can post - * can put - * can delete - """ - - __tablename__ = "actions" - __table_args__ = {"extend_existing": True} - - permissions = db.relationship( - "PermissionViewRoleModel", - backref="actions", - lazy=True, - primaryjoin="and_(ActionModel.id==PermissionViewRoleModel.action_id, " - "PermissionViewRoleModel.deleted_at==None)", - cascade="all,delete", - ) diff --git a/cornflow-server/cornflow/tests/data/models/base_data_model.py b/cornflow-server/cornflow/tests/data/models/base_data_model.py deleted file mode 100644 index a2ef366c..00000000 --- a/cornflow-server/cornflow/tests/data/models/base_data_model.py +++ /dev/null @@ -1,100 +0,0 @@ -""" -""" -# Import from internal modules -from cornflow.shared import db -from cornflow.models.meta_models import TraceAttributesModel -from cornflow.shared.utils import hash_json_256 -from flask import current_app -from sqlalchemy import desc -from sqlalchemy.dialects.postgresql import JSON -from sqlalchemy.dialects.postgresql import TEXT -from sqlalchemy.ext.declarative import declared_attr - - -class BaseDataModel(TraceAttributesModel): - """ """ - - __abstract__ = True - - data = db.Column(JSON, nullable=True) - checks = db.Column(JSON, nullable=True) - name = db.Column(db.String(256), nullable=False) - description = db.Column(TEXT, nullable=True) - data_hash = db.Column(db.String(256), nullable=False) - schema = db.Column(db.String(256), nullable=True) - - @declared_attr - def user_id(self): - return db.Column(db.Integer, db.ForeignKey("users.id"), nullable=False) - - @declared_attr - def user(self): - return db.relationship("UserModel") - - def __init__(self, data): - self.user_id = data.get("user_id") - self.data = data.get("data") or data.get("execution_results") - self.data_hash = hash_json_256(self.data) - self.name = data.get("name") - self.description = data.get("description") - self.schema = data.get("schema") - self.checks = data.get("checks") - super().__init__() - - @classmethod - def get_all_objects( - cls, - user, - schema=None, - creation_date_gte=None, - creation_date_lte=None, - offset=0, - limit=10, - ): - """ - Query to get all objects from a user - :param UserModel user: User object. - :param string schema: data_schema to filter (dag) - :param string creation_date_gte: created_at needs to be larger or equal to this - :param string creation_date_lte: created_at needs to be smaller or equal to this - :param int offset: query offset for pagination - :param int limit: query size limit - :return: The objects - :rtype: list(:class:`BaseDataModel`) - """ - query = cls.query.filter(cls.deleted_at == None) - user_access = int(current_app.config["USER_ACCESS_ALL_OBJECTS"]) - if ( - user is not None - and not user.is_admin() - and not user.is_service_user() - and user_access == 0 - ): - query = query.filter(cls.user_id == user.id) - - if schema: - query = query.filter(cls.schema == schema) - if creation_date_gte: - query = query.filter(cls.created_at >= creation_date_gte) - if creation_date_lte: - query = query.filter(cls.created_at <= creation_date_lte) - # if airflow they also return total_entries = query.count(), for some reason - - return query.order_by(desc(cls.created_at)).offset(offset).limit(limit).all() - - @classmethod - def get_one_object(cls, user=None, idx=None, **kwargs): - """ - Query to get one object from the user and the id. - :param UserModel user: user object performing the query - :param str or int idx: ID from the object to get - :return: The object or None if it does not exist - :rtype: :class:`BaseDataModel` - """ - user_access = int(current_app.config["USER_ACCESS_ALL_OBJECTS"]) - if user is None: - return super().get_one_object(idx=idx) - query = cls.query.filter_by(id=idx, deleted_at=None) - if not user.is_admin() and not user.is_service_user() and user_access == 0: - query = query.filter_by(user_id=user.id) - return query.first() diff --git a/cornflow-server/cornflow/tests/data/models/instance.py b/cornflow-server/cornflow/tests/data/models/instance.py deleted file mode 100644 index d1b87ed1..00000000 --- a/cornflow-server/cornflow/tests/data/models/instance.py +++ /dev/null @@ -1,73 +0,0 @@ -"""Model for the instances""" - -# Import from libraries -import hashlib - -# Imported from internal models -from .base_data_model import BaseDataModel -from cornflow.shared import db - - -class InstanceModel(BaseDataModel): - """ - Model class for the Instances - It inherits from :class:`BaseDataModel` to have the trace fields and user field - The :class:`InstanceModel` has the following fields: - - **id**: int, the primary key for the executions, a hash generated upon creation of the instance - and the id given back to the user.The hash is generated from the creation time and the user id. - - **data**: dict (JSON), the data structure of the instance (:class:`DataSchema`) - - **name**: str, the name given to the instance by the user. - - **description**: str, the description given to the instance by the user. It is optional. - - **executions**: relationship, not a field in the model but the relationship between the _class:`InstanceModel` - and its dependent :class:`ExecutionModel`. - - **user_id**: int, the foreign key for the user (:class:`UserModel`). It links the execution to its owner. - - **created_at**: datetime, the datetime when the execution was created (in UTC). - This datetime is generated automatically, the user does not need to provide it. - - **updated_at**: datetime, the datetime when the execution was last updated (in UTC). - This datetime is generated automatically, the user does not need to provide it. - - **deleted_at**: datetime, the datetime when the execution was deleted (in UTC). Even though it is deleted, - actually, it is not deleted from the database, in order to have a command that cleans up deleted data - after a certain time of its deletion. - This datetime is generated automatically, the user does not need to provide it. - - **data_hash**: a hash of the data json using SHA256 - """ - - # Table name in the database - __tablename__ = "instances" - - # Model fields - id = db.Column(db.String(256), nullable=False, primary_key=True) - executions = db.relationship( - "ExecutionModel", - backref="instances", - lazy=True, - primaryjoin="and_(InstanceModel.id==ExecutionModel.instance_id, " - "ExecutionModel.deleted_at==None)", - cascade="all,delete", - ) - - def __init__(self, data): - """ - :param dict data: the parsed json got from an endpoint that contains all the required - information to create a new instance - """ - super().__init__(data) - self.id = hashlib.sha1( - (str(self.created_at) + " " + str(self.user_id)).encode() - ).hexdigest() - - def __repr__(self): - """ - Method to represent the class :class:`InstanceModel` - :return: The representation of the :class:`InstanceModel` - :rtype: str - """ - return f"" - - def __str__(self): - """ - Method to print a string representation of the :class:`InstanceModel` - :return: The string for the :class:`InstanceModel` - :rtype: str - """ - return self.__repr__() diff --git a/cornflow-server/cornflow/tests/data/models/permission.py b/cornflow-server/cornflow/tests/data/models/permission.py deleted file mode 100644 index 6a88f0df..00000000 --- a/cornflow-server/cornflow/tests/data/models/permission.py +++ /dev/null @@ -1,91 +0,0 @@ -from cornflow.models import PermissionViewRoleModel -from cornflow.models.meta_models import TraceAttributesModel - -from cornflow.models.dag import DeployedDAG -from cornflow.shared import db - - -class PermissionViewRoleModel(PermissionViewRoleModel): - # TODO: trace the user that modifies the permissions - __tablename__ = "permission_view" - __table_args__ = {"extend_existing": True} - - id = db.Column(db.Integer, primary_key=True, autoincrement=True) - - action_id = db.Column(db.Integer, db.ForeignKey("actions.id"), nullable=False) - action = db.relationship("ActionModel", viewonly=True) - - api_view_id = db.Column(db.Integer, db.ForeignKey("api_view.id"), nullable=False) - api_view = db.relationship("ApiViewModel", viewonly=True) - - role_id = db.Column(db.Integer, db.ForeignKey("roles.id"), nullable=False) - role = db.relationship("RoleModel", viewonly=True) - - def __init__(self, data): - super().__init__(data) - self.action_id = data.get("action_id") - self.api_view_id = data.get("api_view_id") - self.role_id = data.get("role_id") - - @classmethod - def get_permission(cls, **kwargs): - permission = cls.query.filter_by(deleted_at=None, **kwargs).first() - if permission is not None: - return True - else: - return False - - def __repr__(self): - return f"" - - -class PermissionsDAG(TraceAttributesModel): - __tablename__ = "permission_dag" - __table_args__ = ( - {"extend_existing": True}, - # db.UniqueConstraint("dag_id", "user_id"), - ) - - id = db.Column(db.Integer, primary_key=True, autoincrement=True) - - dag_id = db.Column( - db.String(128), db.ForeignKey("deployed_dags.id"), nullable=False - ) - user_id = db.Column(db.Integer, db.ForeignKey("users.id"), nullable=False) - user = db.relationship("UserModel", viewonly=True) - - def __init__(self, data): - super().__init__() - self.dag_id = data.get("dag_id") - self.user_id = data.get("user_id") - - def __repr__(self): - return f"