from sqlalchemy import ForeignKey, event
from sqlalchemy.orm import backref, relationship
from sqlalchemy.ext.associationproxy import association_proxy
from geonature.utils.env import db
from ref_geo.models import BibAreasTypes, LAreas
from apptax.taxonomie.models import Taxref
from pypnnomenclature.models import BibNomenclaturesTypes, TNomenclatures
[docs]
cor_sensitivity_area = db.Table(
"cor_sensitivity_area",
db.Column(
"id_sensitivity",
db.Integer,
ForeignKey("gn_sensitivity.t_sensitivity_rules.id_sensitivity"),
primary_key=True,
),
db.Column("id_area", db.Integer, ForeignKey(LAreas.id_area), primary_key=True),
schema="gn_sensitivity",
)
[docs]
cor_sensitivity_area_type = db.Table(
"cor_sensitivity_area_type",
db.Column(
"id_nomenclature_sensitivity",
db.Integer,
ForeignKey("ref_nomenclatures.t_nomenclatures.id_nomenclature"),
primary_key=True,
),
db.Column("id_area_type", db.Integer, ForeignKey(BibAreasTypes.id_type), primary_key=True),
schema="gn_sensitivity",
)
[docs]
class SensitivityRule(db.Model):
[docs]
__tablename__ = "t_sensitivity_rules"
[docs]
__table_args__ = {"schema": "gn_sensitivity"}
[docs]
id = db.Column("id_sensitivity", db.Integer, primary_key=True)
[docs]
cd_nom = db.Column(db.Integer, ForeignKey(Taxref.cd_nom), nullable=False)
[docs]
nom_cite = db.Column(db.String(length=100))
[docs]
id_nomenclature_sensitivity = db.Column(
db.Integer, ForeignKey("ref_nomenclatures.t_nomenclatures.id_nomenclature"), nullable=False
)
[docs]
nomenclature_sensitivity = relationship(TNomenclatures)
[docs]
sensitivity_duration = db.Column(db.Integer, nullable=False)
[docs]
sensitivity_territory = db.Column(db.String(length=1000))
[docs]
id_territory = db.Column(db.String(length=50))
[docs]
date_min = db.Column(db.Date)
[docs]
date_max = db.Column(db.Date)
[docs]
source = db.Column(db.String(length=250))
[docs]
active = db.Column(db.Boolean, default=True)
[docs]
areas = relationship(LAreas, secondary=cor_sensitivity_area)
[docs]
criterias = association_proxy("sensitivity_criterias", "criteria")
[docs]
class CorSensitivityCriteria(db.Model):
[docs]
__tablename__ = "cor_sensitivity_criteria"
[docs]
__table_args__ = {"schema": "gn_sensitivity"}
[docs]
id_sensitivity_rule = db.Column(
"id_sensitivity", db.Integer, ForeignKey(SensitivityRule.id), primary_key=True
)
[docs]
sensitivity_rule = relationship(
SensitivityRule, backref=backref("sensitivity_criterias", cascade="all, delete-orphan")
)
[docs]
id_criteria = db.Column(
"id_criteria", db.Integer, ForeignKey(TNomenclatures.id_nomenclature), primary_key=True
)
[docs]
criteria = relationship(TNomenclatures)
[docs]
id_nomenclature_type = db.Column(
"id_type_nomenclature", db.Integer, ForeignKey(BibNomenclaturesTypes.id_type)
)
[docs]
nomenclature_type = relationship(BibNomenclaturesTypes)
def __init__(self, criteria=None, sensitivity_rule=None, nomenclature_type=None):
self.criteria = criteria
self.sensitivity_rule = sensitivity_rule
self.nomenclature_type = nomenclature_type
@event.listens_for(CorSensitivityCriteria, "before_insert")
@event.listens_for(CorSensitivityCriteria, "before_update")
[docs]
def before_insert_sensitivity_criteria(mapper, connection, target):
target.id_nomenclature_type = target.criteria.nomenclature_type.id_type