from flask import current_app
from geoalchemy2 import Geometry
import sqlalchemy as sa
from sqlalchemy.dialects.postgresql import UUID
from sqlalchemy.ext.hybrid import hybrid_property
from sqlalchemy.sql.schema import ForeignKey
from sqlalchemy.orm import relationship, backref, deferred
from geoalchemy2 import Geometry
from utils_flask_sqla.serializers import serializable
from utils_flask_sqla_geo.serializers import geoserializable
from pypnnomenclature.models import TNomenclatures
from geonature.utils.env import DB, db
from geonature.core.gn_synthese.models import Synthese
from apptax.taxonomie.models import Taxref
@serializable
[docs]
class VmCorTaxonPhenology(DB.Model):
[docs]
__tablename__ = "vm_cor_taxon_phenology"
[docs]
__table_args__ = {"schema": "gn_profiles"}
[docs]
cd_ref = DB.Column(DB.Integer, primary_key=True)
[docs]
doy_min = DB.Column(DB.Integer, primary_key=True)
[docs]
doy_max = DB.Column(DB.Integer, primary_key=True)
[docs]
id_nomenclature_life_stage = DB.Column(
DB.Integer,
ForeignKey("ref_nomenclatures.t_nomenclatures.id_nomenclature"),
primary_key=True,
)
[docs]
extreme_altitude_min = DB.Column(DB.Integer)
[docs]
calculated_altitude_min = DB.Column(DB.Integer)
[docs]
extreme_altitude_max = DB.Column(DB.Integer)
[docs]
calculated_altitude_max = DB.Column(DB.Integer)
[docs]
nomenclature_life_stage = DB.relationship("TNomenclatures")
@serializable
@geoserializable(geoCol="valid_distribution", idCol="cd_ref")
[docs]
class VmValidProfiles(DB.Model):
[docs]
__tablename__ = "vm_valid_profiles"
[docs]
__table_args__ = {"schema": "gn_profiles"}
[docs]
cd_ref = DB.Column(DB.Integer, primary_key=True)
[docs]
valid_distribution = DB.Column(Geometry("GEOMETRY"))
[docs]
altitude_min = DB.Column(DB.Integer)
[docs]
altitude_max = DB.Column(DB.Integer)
[docs]
first_valid_data = DB.Column(DB.DateTime)
[docs]
last_valid_data = DB.Column(DB.DateTime)
[docs]
count_valid_data = DB.Column(DB.Integer)
[docs]
active_life_stage = DB.Column(DB.Boolean)
@serializable
[docs]
class VConsistancyData(DB.Model):
[docs]
__tablename__ = "v_consistancy_data"
[docs]
__table_args__ = {"schema": "gn_profiles"}
[docs]
id_synthese = DB.Column(DB.Integer, ForeignKey(Synthese.id_synthese), primary_key=True)
[docs]
synthese = relationship(Synthese, backref=backref("profile", uselist=False))
[docs]
id_sinp = DB.Column(UUID(as_uuid=True))
[docs]
cd_ref = DB.Column(DB.Integer)
[docs]
valid_name = DB.Column(DB.Unicode)
[docs]
valid_distribution = DB.Column(DB.Boolean)
[docs]
valid_phenology = DB.Column(DB.Boolean)
[docs]
valid_altitude = DB.Column(DB.Boolean)
# score = DB.Column(DB.Integer)
[docs]
valid_status = DB.Column(DB.Unicode)
@hybrid_property
[docs]
def score(self):
return int(
int(self.valid_distribution is True)
+ int(self.valid_phenology is True)
+ int(self.valid_altitude is True)
)
@score.expression
def score(cls):
return (
cls.valid_distribution.cast(sa.Integer)
+ cls.valid_phenology.cast(sa.Integer)
+ cls.valid_altitude.cast(sa.Integer)
)
[docs]
class VSyntheseForProfiles(db.Model):
[docs]
__tablename__ = "v_synthese_for_profiles"
[docs]
__table_args__ = {"schema": "gn_profiles"}
[docs]
id_synthese = db.Column(db.Integer, ForeignKey(Synthese.id_synthese), primary_key=True)
[docs]
synthese = relationship(Synthese)
[docs]
cd_nom = db.Column(db.Integer)
[docs]
nom_cite = db.Column(db.Unicode(length=1000))
[docs]
cd_ref = db.Column(db.Integer)
[docs]
nom_valide = db.Column(db.Unicode(length=500))
[docs]
id_rang = db.Column(db.Unicode(length=10))
[docs]
date_min = db.Column(db.DateTime)
[docs]
date_max = db.Column(db.DateTime)
[docs]
the_geom_local = deferred(db.Column(Geometry("GEOMETRY")))
[docs]
the_geom_4326 = deferred(db.Column(Geometry("GEOMETRY", 4326)))
[docs]
altitude_min = db.Column(db.Integer)
[docs]
altitude_max = db.Column(db.Integer)
[docs]
id_nomenclature_life_stage = db.Column(db.Integer, ForeignKey(TNomenclatures.id_nomenclature))
[docs]
nomenclature_life_stage = db.relationship(
TNomenclatures, foreign_keys=[id_nomenclature_life_stage]
)
[docs]
id_nomenclature_valid_status = db.Column(db.Integer, ForeignKey(TNomenclatures.id_nomenclature))
[docs]
nomenclature_valid_status = db.relationship(
TNomenclatures, foreign_keys=[id_nomenclature_valid_status]
)
[docs]
spatial_precision = db.Column(db.Integer)
[docs]
temporal_precision_days = db.Column(db.Integer)
[docs]
active_life_stage = db.Column(db.Boolean)
[docs]
distance = db.Column(db.Integer)
[docs]
class TParameters(DB.Model):
[docs]
__tablename__ = "t_parameters"
[docs]
__table_args__ = {"schema": "gn_profiles"}
[docs]
id_parameter = DB.Column(DB.Integer, primary_key=True)
[docs]
name = DB.Column(DB.String(100))
[docs]
desc = DB.Column(DB.Text)
[docs]
value = DB.Column(DB.Text)
[docs]
class CorTaxonParameters(DB.Model):
[docs]
__tablename__ = "cor_taxons_parameters"
[docs]
__table_args__ = {"schema": "gn_profiles"}
[docs]
cd_nom = DB.Column(DB.Integer, ForeignKey(Taxref.cd_nom), primary_key=True)
[docs]
spatial_precision = DB.Column(DB.Integer)
[docs]
temporal_precision_days = DB.Column(DB.Integer)
[docs]
active_life_stage = DB.Column(DB.Boolean)