예제 #1
0
class ReleasedData(Base, audit.AuditColumnsMixin, ReleasedDataMixin):
    __tablename__ = "released_data"
    __table_args__ = (schema.PrimaryKeyConstraint(
        "program_name",
        "project_code",
        "data_type",
        name="released_data_pk",
    ), )

    def __repr__(self):
        return "<ReleasedData(project_id='{}', data_type='{}', is_controlled={}, is_open={})>".format(
            self.project_id,
            self.data_type,
            self.is_controlled,
            self.is_open,
        )

    is_controlled = schema.Column(sqltypes.Boolean, nullable=False)

    def to_json(self):
        return {
            "program_name": self.program_name,
            "project_code": self.project_code,
            "data_type": self.data_type,
            "is_controlled": self.is_controlled,
            "is_open": self.is_open,
        }
예제 #2
0
class BatchMembership(Base, audit.AuditColumnsMixin):
    """Membership class to represent which nodes belong in a batch.

    Attributes:
        batch_id: id of the batch
        node_id: id of the node
        created_datetime: the date and time when node was added to batch
        updated_datetime: the date and time when node membership was last updated
    """

    __tablename__ = "batch_membership"
    __table_args__ = (
        schema.PrimaryKeyConstraint("batch_id", "node_id", name="batch_membership_pk"),
        schema.ForeignKeyConstraint(
            ("batch_id",),
            ("batch.id",),
            name="batch_membership_batch_id_fk",
        ),
    )

    batch_id = sqlalchemy.Column(sqlalchemy.BigInteger, nullable=False)
    node_id = sqlalchemy.Column(sqlalchemy.Text, nullable=False)

    batch = orm.relationship("Batch", back_populates="members")

    def __repr__(self):
        created_datetime = (
            self.created_datetime.isoformat() if self.created_datetime else None
        )
        updated_datetime = (
            self.updated_datetime.isoformat() if self.updated_datetime else None
        )

        return "<BatchMembership(batch_id='{}', node_id='{}', created_datetime='{}', updated_datetime='{}')>".format(
            self.batch_id,
            self.node_id,
            created_datetime,
            updated_datetime,
        )

    def to_dict(self):
        created_datetime = (
            self.created_datetime.isoformat() if self.created_datetime else None
        )
        updated_datetime = (
            self.updated_datetime.isoformat() if self.updated_datetime else None
        )

        return {
            "batch_id": self.batch_id,
            "node_id": self.node_id,
            "created_datetime": created_datetime,
            "updated_datetime": updated_datetime,
        }

    def to_json(self):
        """Returns a JSON safe representation of a membership object"""
        return json.loads(json.dumps(self.to_dict()))
예제 #3
0
class ReleasedDataLog(Base, audit.AuditColumnsMixin, ReleasedDataMixin):
    __tablename__ = "released_data_log"
    __table_args__ = (
        schema.Index(
            "released_data_log_program_name_project_code_idx",
            "program_name",
            "project_code",
        ),
        schema.PrimaryKeyConstraint("id", name="released_data_log_pk"),
    )

    def __repr__(self):
        return "<ReleasedDataLog(project_id='{}', release_number={}, data_type='{}', is_open={}, action='{}')>".format(
            self.project_id,
            self.release_number,
            self.data_type,
            self.is_open,
            self.action,
        )

    release_data_log_id_seq = schema.Sequence(name="release_data_log_id_seq",
                                              metadata=Base.metadata)
    id = schema.Column(
        sqltypes.Integer,
        nullable=False,
        server_default=release_data_log_id_seq.next_value(),
    )
    release_number = schema.Column(sqltypes.Text, nullable=False)
    action = schema.Column(sqltypes.Text, nullable=False)

    @validates("action")
    def validate_action(self, key, action):
        if action not in RELEASED_DATA_LOG_ACTION_VALUES:
            raise ValueError(
                """"{action}" is not a valid value for {key}""".format(
                    action=action, key=key))
        return action

    def to_json(self):
        return {
            "program_name": self.program_name,
            "project_code": self.project_code,
            "release_number": self.release_number,
            "data_type": self.data_type,
            "is_open": self.is_open,
            "action": self.action,
        }
예제 #4
0
class StudyRuleProgramProject(Base, audit.AuditColumnsMixin):
    """A relationship between study rules, programs, and projects.

    A study rule can contain one or more programs. For each program, one or more projects
    can be associated.

    This relationship is used when the study rule includes a subset of projects from a
    program.

    Attributes:
        study_rule_id: The id of the associated study rule.
        program_name: The name of the program to associate with the study rule.
        project_code: The code of the project to associate with the study rule.
        created_datetime: The date and time when the record is created.
        updated_datetime: The date and time when the record is updated.
    """

    __tablename__ = "study_rule_program_project"
    study_rule_id = schema.Column(sqltypes.Integer, nullable=False)
    program_name = schema.Column(sqltypes.Text, nullable=False)
    project_code = schema.Column(sqltypes.Text, nullable=False)

    __table_args__ = (
        schema.PrimaryKeyConstraint("study_rule_id", "program_name", "project_code", name="study_rule_program_project_pk"),
        schema.ForeignKeyConstraint(("study_rule_id",), ("study_rule.id",), name="study_rule_program_project_study_rule_id_fk"),
    )

    def __repr__(self):
        return "<StudyRuleProgramProject(study_rule_id={study_rule_id}, program_name='{program_name}', project_code='{project_code}', created_datetime={created_datetime}, updated_datetime={updated_datetime})>".format(
            study_rule_id=self.study_rule_id,
            program_name=self.program_name,
            project_code=self.project_code,
            created_datetime=self.created_datetime.isoformat() if self.created_datetime else None,
            updated_datetime=self.updated_datetime.isoformat() if self.updated_datetime else None,
        )

    def to_json(self):
        return {
            "study_rule_id": self.study_rule_id,
            "program_name": self.program_name,
            "project_code": self.project_code,
            "created_datetime": self.created_datetime.isoformat() if self.created_datetime else None,
            "updated_datetime": self.updated_datetime.isoformat() if self.updated_datetime else None,
        }
예제 #5
0
class StudyRule(Base, audit.AuditColumnsMixin):
    """A study rule for use with single-study controlled-access.

    Attributes:
        id: A unique identifier for the study rule.
        name: A unique, informational name for the study rule.
        created_datetime: The date and time when the record is created.
        updated_datetime: The date and time when the record is updated.
    """

    __tablename__ = "study_rule"
    id_seq = schema.Sequence(name="study_rule_id_seq", metadata=Base.metadata)
    id = schema.Column(sqltypes.Integer, nullable=False, server_default=id_seq.next_value())
    name = schema.Column(sqltypes.Text, nullable=False)

    __table_args__ = (
        schema.PrimaryKeyConstraint("id", name="study_rule_pk"),
        schema.Index("study_rule_name_idx", "name", unique=True),
    )

    whole_programs = orm.relationship("StudyRuleProgram", lazy="joined")
    partial_programs = orm.relationship("StudyRuleProgramProject", lazy="joined")

    def __repr__(self):
        return "<StudyRule(id={id}, name='{name}', created_datetime={created_datetime}, updated_datetime={updated_datetime})>".format(
            id=self.id,
            name=self.name,
            created_datetime=self.created_datetime.isoformat() if self.created_datetime else None,
            updated_datetime=self.updated_datetime.isoformat() if self.updated_datetime else None,
        )

    def to_json(self):
        return {
            "id": self.id,
            "name": self.name,
            "created_datetime": self.created_datetime.isoformat() if self.created_datetime else None,
            "updated_datetime": self.updated_datetime.isoformat() if self.updated_datetime else None,
        }
예제 #6
0
class Batch(Base, audit.AuditColumnsMixin):
    """Batch class to represent a collection of nodes.

    Attributes:
        id: unique identifier for the batch
        name: name given to the batch
        project_id: project the batch is a part of
        created_datetime: the date and time when the batch is created
        updated_datetime: the date and time when the batch was last updated
    """

    __tablename__ = "batch"
    __mapper_args__ = {"eager_defaults": True}
    __table_args__ = (
        schema.PrimaryKeyConstraint("id", name="batch_pk"),
        schema.Index("batch_name_idx", "name"),
        schema.Index("batch_project_id_idx", "project_id"),
        schema.Index("batch_status_idx", "status"),
    )

    VALID_STATUSES = ("OPEN", "CLOSED")

    # start sequence at high number to avoid collisions with existing batch_id
    id_seq = sqlalchemy.Sequence("batch_id_seq", metadata=Base.metadata, start=1000)

    id = sqlalchemy.Column(
        sqlalchemy.BigInteger,
        nullable=False,
        server_default=id_seq.next_value(),
    )
    name = sqlalchemy.Column(sqlalchemy.Text, nullable=False)
    project_id = sqlalchemy.Column(sqlalchemy.Text, nullable=False)
    status = sqlalchemy.Column(sqlalchemy.Text, default="OPEN", nullable=False)

    members = orm.relationship(
        "BatchMembership", back_populates="batch", lazy="selectin"
    )

    @orm.validates("status")
    def validate_status(self, key, status):
        if not status:
            raise ValueError("status is required")

        status = status.upper()

        if status not in self.VALID_STATUSES:
            raise ValueError("invalid status specified")

        return status

    def __repr__(self):
        created_datetime = (
            self.created_datetime.isoformat() if self.created_datetime else None
        )
        updated_datetime = (
            self.updated_datetime.isoformat() if self.updated_datetime else None
        )

        return "<Batch(id='{}', name='{}', project_id='{}', status='{}', created_datetime='{}', updated_datetime='{}')>".format(
            self.id,
            self.name,
            self.project_id,
            self.status,
            created_datetime,
            updated_datetime,
        )

    def to_dict(self):
        created_datetime = (
            self.created_datetime.isoformat() if self.created_datetime else None
        )
        updated_datetime = (
            self.updated_datetime.isoformat() if self.updated_datetime else None
        )

        return {
            "id": self.id,
            "name": self.name,
            "project_id": self.project_id,
            "status": self.status,
            "created_datetime": created_datetime,
            "updated_datetime": updated_datetime,
        }

    def to_json(self):
        """Returns a JSON safe representation of a batch"""
        return json.loads(json.dumps(self.to_dict()))