Skip to content

Commit

Permalink
feat: add type site - categorie relation
Browse files Browse the repository at this point in the history
WIP - add selectfield to get type site in admin module

Reviewed-by: andriac
[Refs ticket]: #3
  • Loading branch information
andriacap committed Dec 21, 2022
1 parent e55ad46 commit 26917f1
Show file tree
Hide file tree
Showing 3 changed files with 162 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
"""create_cor_site_type_category
Revision ID: e64bafb13ce8
Revises:
Create Date: 2022-12-06 16:18:24.512562
"""
from alembic import op
import sqlalchemy as sa

# revision identifiers, used by Alembic.
revision = "e64bafb13ce8"
down_revision = "a54bafb13ce8"
branch_labels = None
depends_on = None

monitorings_schema = "gn_monitoring"
referent_schema = "ref_nomenclatures"


def upgrade():
op.create_table(
"cor_site_type_categorie",
sa.Column(
"id_categorie",
sa.Integer(),
sa.ForeignKey(
f"{monitorings_schema}.bib_categorie_site.id_categorie",
name="fk_cor_site_type_categorie_id_categorie",
ondelete="CASCADE",
onupdate="CASCADE",
),
nullable=False,
),
sa.Column("id_nomenclature", sa.Integer(),sa.ForeignKey(
f"{referent_schema}.t_nomenclatures.id_nomenclature",
name="fk_cor_site_type_categorie_id_type",
ondelete="CASCADE",
onupdate="CASCADE",
), nullable=False),
sa.PrimaryKeyConstraint("id_categorie", "id_nomenclature", name="pk_cor_site_type_categorie"),
schema=monitorings_schema,
)


def downgrade():
op.drop_table("cor_site_type_categorie", schema=monitorings_schema)
88 changes: 86 additions & 2 deletions backend/gn_module_monitoring/monitoring/admin.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,28 @@
from flask_admin.contrib.sqla import ModelView
from flask_admin.contrib.sqla import ModelView, fields

from geonature.core.admin.admin import CruvedProtectedMixin

from wtforms import (
StringField,
PasswordField,
BooleanField,
SubmitField,
HiddenField,
SelectField,
RadioField,
SelectMultipleField,
widgets,
Form,
)
from wtforms.validators import DataRequired, Email
from wtforms.widgets import TextArea
from wtforms_sqlalchemy.fields import QuerySelectField

from gn_module_monitoring.monitoring.models import BibCategorieSite

from geonature.core.gn_permissions.models import TFilters, BibFiltersType, TActions
from geonature.core.gn_commons.models import TModules,TNomenclatures
from gn_module_monitoring.monitoring.models import BibCategorieSite
from geonature.utils.env import DB

class BibCategorieSiteView(CruvedProtectedMixin, ModelView):
"""
Expand All @@ -12,7 +31,72 @@ class BibCategorieSiteView(CruvedProtectedMixin, ModelView):

module_code = "MONITORINGS"
object_code = None
# site_type = SelectMultipleField('', choices=["test1"], coerce=int)

def __init__(self, session, **kwargs):
# Référence au model utilisé
super(BibCategorieSiteView, self).__init__(BibCategorieSite, session, **kwargs)
# site_type = SelectMultipleField('', choices=[c.id for c in BibView.site_type], coerce=int)
def get_only_type_site_desc():
return DB.session.query(TNomenclatures).order_by(
TNomenclatures.mnemonique.desc()
).filter(TNomenclatures.id_type == 116)

# Nom de colonne user friendly
column_labels = dict(site_type="Type de site")
# Description des colonnes
column_descriptions = dict(site_type="Type de site à choisir")
# Surcharge du formulaure
# form_overrides = dict(
# site_type = SelectMultipleField
# )
# form_choices = {
# 'site_type': DB.session.query(TNomenclatures.mnemonique).order_by(
# TNomenclatures.mnemonique.desc()
# ).filter(TNomenclatures.id_type == 116).all()
# }
# form_args = {
# "site_type": {
# "query_factory": lambda: DB.session.query(TNomenclatures).order_by(
# TNomenclatures.mnemonique.desc()
# ).filter(TNomenclatures.id_type == 116)
# }
# }

column_hide_backrefs = False
# form_extra_fields = {
# 'type_site_2': fields.QuerySelectField(
# label='Type de site 2',
# query_factory=get_only_type_site_desc,
# widget=SelectMultipleField()
# )
# }
form_args = dict(
site_type=dict(
query_factory=get_only_type_site_desc,
)
)
# form_columns = ('label','config')
# inline_models = ((
# BibCategorieSite,
# {
# 'form_columns': ('site_type'),
# }
# ),)
form_choices = {
'title':DB.session.query(TNomenclatures.mnemonique).order_by(
TNomenclatures.mnemonique.desc()
).filter(TNomenclatures.id_type == 116).all()
}
column_list=["label","config","site_type"]
# form_choices = {
# 'site_type': DB.session.query(TNomenclatures.mnemonique).order_by(
# TNomenclatures.mnemonique.desc()
# ).filter(TNomenclatures.id_type == 116).all()
# }
wait = True





32 changes: 29 additions & 3 deletions backend/gn_module_monitoring/monitoring/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

from sqlalchemy.ext.hybrid import hybrid_property


from pypnnomenclature.models import TNomenclatures, BibNomenclaturesTypes
from geonature.core.gn_commons.models import TMedias
from geonature.core.gn_monitoring.models import TBaseSites, TBaseVisits
from geonature.core.gn_meta.models import TDatasets
Expand All @@ -35,15 +35,41 @@
primary_key=True,
), schema="gn_monitoring")

cor_site_type_categorie = DB.Table(
"cor_site_type_categorie",
DB.Column(
"id_nomenclature",
DB.Integer,
DB.ForeignKey("ref_nomenclatures.t_nomenclatures.id_nomenclature"),
primary_key=True,
),
DB.Column(
"id_categorie",
DB.Integer,
DB.ForeignKey("gn_monitoring.bib_categorie_site.id_categorie"),
primary_key=True,
), schema="gn_monitoring")

@serializable
class BibCategorieSite(DB.Model):
__tablename__ = "bib_categorie_site"
__table_args__ = {"schema": "gn_monitoring"}
__mapper_args__ = {'polymorphic_identity': 'categorie_site'}
id_categorie = DB.Column(DB.Integer, primary_key=True, nullable=False, unique=True)
label = DB.Column(DB.String, nullable=False)
config = DB.Column(JSONB)


site_type = DB.relationship(
"TNomenclatures",
secondary=cor_site_type_categorie,
lazy="joined",
backref="categorie_site"
)
# site_type = DB.relation(
# BibNomenclaturesTypes,
# primaryjoin=(id_categorie == BibNomenclaturesTypes.mnemonique=="TYPE_SITE"),
# foreign_keys=[BibNomenclaturesTypes.id_type],
# cascade="all,delete")

@serializable
class TMonitoringObservationDetails(DB.Model):
__tablename__ = "t_observation_details"
Expand Down

0 comments on commit 26917f1

Please sign in to comment.