import datetime
from sqlalchemy.orm import relationship
from sqlalchemy import ForeignKey
from sqlalchemy.ext.hybrid import hybrid_property
import marshmallow as ma
from pypnnomenclature.models import TNomenclatures
from pypnusershub.db.models import User, Organisme
from utils_flask_sqla.serializers import serializable
from geonature.utils.env import DB, db
[docs]
class DateFilterSchema(ma.Schema):
[docs]
year = ma.fields.Integer()
[docs]
month = ma.fields.Integer()
[docs]
day = ma.fields.Integer()
[docs]
cor_acquisition_framework_objectif = db.Table(
"cor_acquisition_framework_objectif",
db.Column(
"id_acquisition_framework",
db.Integer,
ForeignKey("gn_meta.t_acquisition_frameworks.id_acquisition_framework"),
primary_key=True,
),
db.Column(
"id_nomenclature_objectif",
db.Integer,
ForeignKey(TNomenclatures.id_nomenclature),
primary_key=True,
),
schema="gn_meta",
)
[docs]
cor_acquisition_framework_voletsinp = db.Table(
"cor_acquisition_framework_voletsinp",
db.Column(
"id_acquisition_framework",
db.Integer,
ForeignKey("gn_meta.t_acquisition_frameworks.id_acquisition_framework"),
primary_key=True,
),
db.Column(
"id_nomenclature_voletsinp",
db.Integer,
ForeignKey(TNomenclatures.id_nomenclature),
primary_key=True,
),
schema="gn_meta",
)
[docs]
cor_acquisition_framework_territory = db.Table(
"cor_acquisition_framework_territory",
db.Column(
"id_acquisition_framework",
db.Integer,
ForeignKey("gn_meta.t_acquisition_frameworks.id_acquisition_framework"),
primary_key=True,
),
db.Column(
"id_nomenclature_territory",
db.Integer,
ForeignKey(TNomenclatures.id_nomenclature),
primary_key=True,
),
schema="gn_meta",
)
@serializable
[docs]
class CorAcquisitionFrameworkActor(DB.Model):
[docs]
__tablename__ = "cor_acquisition_framework_actor"
[docs]
__table_args__ = {"schema": "gn_meta"}
[docs]
id_cafa = DB.Column(DB.Integer, primary_key=True)
[docs]
id_acquisition_framework = DB.Column(
DB.Integer,
ForeignKey("gn_meta.t_acquisition_frameworks.id_acquisition_framework"),
)
[docs]
id_role = DB.Column(DB.Integer, ForeignKey(User.id_role))
[docs]
id_organism = DB.Column(DB.Integer, ForeignKey(Organisme.id_organisme))
[docs]
id_nomenclature_actor_role = DB.Column(
DB.Integer,
ForeignKey("ref_nomenclatures.t_nomenclatures.id_nomenclature"),
default=lambda: TNomenclatures.get_default_nomenclature("ROLE_ACTEUR"),
)
[docs]
nomenclature_actor_role = DB.relationship(
TNomenclatures,
lazy="joined",
primaryjoin=(TNomenclatures.id_nomenclature == id_nomenclature_actor_role),
)
[docs]
role = DB.relationship(
User,
lazy="joined",
)
[docs]
organism = relationship(
Organisme,
lazy="joined",
)
@serializable(exclude=["actor"])
[docs]
class CorDatasetActor(DB.Model):
[docs]
__tablename__ = "cor_dataset_actor"
[docs]
__table_args__ = {"schema": "gn_meta"}
[docs]
id_cda = DB.Column(DB.Integer, primary_key=True)
[docs]
id_dataset = DB.Column(DB.Integer, ForeignKey("gn_meta.t_datasets.id_dataset"))
[docs]
id_role = DB.Column(DB.Integer, ForeignKey(User.id_role))
[docs]
id_organism = DB.Column(DB.Integer, ForeignKey(Organisme.id_organisme))
[docs]
id_nomenclature_actor_role = DB.Column(
DB.Integer,
ForeignKey("ref_nomenclatures.t_nomenclatures.id_nomenclature"),
default=lambda: TNomenclatures.get_default_nomenclature("ROLE_ACTEUR"),
)
[docs]
nomenclature_actor_role = DB.relationship(
TNomenclatures,
lazy="joined",
foreign_keys=[id_nomenclature_actor_role],
)
[docs]
role = DB.relationship(User, lazy="joined")
[docs]
organism = DB.relationship(Organisme, lazy="joined")
@hybrid_property
[docs]
def actor(self):
if self.role is not None:
return self.role
else:
return self.organism
@hybrid_property
[docs]
def display(self):
if self.role:
actor = self.role.nom_complet
else:
actor = self.organism.nom_organisme
return "{} ({})".format(actor, self.nomenclature_actor_role.label_default)
@serializable
[docs]
class CorDatasetProtocol(DB.Model):
# TODO: replace with table used as secondary in relationships
[docs]
__tablename__ = "cor_dataset_protocol"
[docs]
__table_args__ = {"schema": "gn_meta"}
[docs]
id_cdp = DB.Column(DB.Integer, primary_key=True)
[docs]
id_dataset = DB.Column(DB.Integer, ForeignKey("gn_meta.t_datasets.id_dataset"))
[docs]
id_protocol = DB.Column(DB.Integer, ForeignKey("gn_meta.sinp_datatype_protocols.id_protocol"))
[docs]
cor_dataset_territory = db.Table(
"cor_dataset_territory",
db.Column(
"id_dataset",
db.Integer,
ForeignKey("gn_meta.t_datasets.id_dataset"),
primary_key=True,
),
db.Column(
"id_nomenclature_territory",
db.Integer,
ForeignKey(TNomenclatures.id_nomenclature),
primary_key=True,
),
schema="gn_meta",
)
@serializable
[docs]
class TBibliographicReference(db.Model):
[docs]
__tablename__ = "t_bibliographical_references"
[docs]
__table_args__ = {"schema": "gn_meta"}
[docs]
id_bibliographic_reference = DB.Column(DB.Integer, primary_key=True)
[docs]
id_acquisition_framework = DB.Column(
DB.Integer,
ForeignKey("gn_meta.t_acquisition_frameworks.id_acquisition_framework"),
)
[docs]
publication_url = DB.Column(DB.Unicode)
[docs]
publication_reference = DB.Column(DB.Unicode)