from flask import current_app from atlas_um.pgdb import pgdb from atlas_um.pgdb.associations import dna_account_claim_table from atlas_um.pgdb.queries.claim_value import ClaimValueQuery class ClaimValue(pgdb.Model): __tablename__ = "claim_value" query_class = ClaimValueQuery id = pgdb.Column(pgdb.Integer, primary_key=True) claim_name_id = pgdb.Column( pgdb.Integer, pgdb.ForeignKey("claim_name.id"), nullable=False ) friendly = pgdb.Column(pgdb.String(), nullable=False) components = pgdb.Column(pgdb.JSON, nullable=False) external_id = pgdb.Column(pgdb.String()) is_deleted = pgdb.Column(pgdb.Boolean(), nullable=False, default=False) parent_id = pgdb.Column( pgdb.Integer, pgdb.ForeignKey("claim_value.id"), nullable=True ) # relations claim_name = pgdb.relationship("ClaimName", back_populates="claim_values") dna_accounts = pgdb.relationship( "DNAAccount", secondary=dna_account_claim_table, back_populates="claim_values", ) parent = pgdb.relationship( "ClaimValue", remote_side=id, backref="children" ) @property def resource_group(self): return self.claim_name.resource_group def __str__(self): return self.friendly @property def image(self): url = "" host = current_app.config.get("CORE_IMAGES_HOST") schema = ( "" if host and any(item in host for item in ("http://", "https://")) else "https://" ) images_url = self.claim_name.images_url external_id = self.external_id if images_url and external_id and host: if images_url.startswith("/"): images_url = images_url[1:] if images_url.endswith("/"): images_url = images_url[:-1] if host.startswith("/"): host = host[1:] if external_id.startswith("GRAS_"): external_id = external_id[5:] url = f"{schema}{host}/{images_url}/{external_id}" return url