Skip to content

Commit

Permalink
Add nb_attributs, nb_medias
Browse files Browse the repository at this point in the history
  • Loading branch information
amandine-sahl committed Aug 1, 2023
1 parent df2ddab commit ad7d29c
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
11 changes: 11 additions & 0 deletions apptax/admin/admin_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from flask_admin.model.ajax import AjaxModelLoader, DEFAULT_PAGE_SIZE



from flask_admin.base import expose
from flask_admin.model.helpers import get_mdict_item_or_list

Expand All @@ -33,6 +34,8 @@
from flask_admin.contrib.sqla.filters import BaseSQLAFilter

from sqlalchemy import or_
from sqlalchemy.orm import undefer

from wtforms import Form, BooleanField, SelectField, PasswordField, SubmitField, StringField

from wtforms.validators import ValidationError, DataRequired, Email, EqualTo, Length
Expand Down Expand Up @@ -297,6 +300,9 @@ class TaxrefView(
"classe",
"ordre",
"famille",
"liste",
"nb_attributs",
"nb_medias",
)

column_searchable_list = ["nom_complet", "cd_nom"]
Expand Down Expand Up @@ -340,6 +346,11 @@ def _get_theme_attributes(self, taxon_name):
.all()
)

def get_query(self):
return self.session.query(self.model).options(
undefer("nb_attributs"), undefer("nb_medias")
)

def _get_attributes_value(self, taxon_name, theme_attributs_def):
attributes_val = {}
for a in [a for attrs in [t.attributs for t in theme_attributs_def] for a in attrs]:
Expand Down
17 changes: 16 additions & 1 deletion apptax/taxonomie/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

from flask import current_app
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy import ForeignKey, Sequence
from sqlalchemy import ForeignKey, select, func

from sqlalchemy.ext.hybrid import hybrid_property
from sqlalchemy.schema import FetchedValue
from sqlalchemy.orm import deferred

from flask_sqlalchemy import BaseQuery
from utils_flask_sqla.serializers import serializable
Expand Down Expand Up @@ -481,3 +483,16 @@ class TMetaTaxref(db.Model):
referencial_name = db.Column(db.Integer, primary_key=True)
version = db.Column(db.Integer)
update_date = db.Column(db.DateTime, default=db.func.now(), nullable=False)


# Taxref deffered properties

Taxref.nb_medias = deferred(
select([func.count(TMedias.id_media)]).where(TMedias.cd_ref == Taxref.cd_ref)
)

Taxref.nb_attributs = deferred(
select([func.count(CorTaxonAttribut.id_attribut)])
.where(CorTaxonAttribut.cd_ref == Taxref.cd_ref)
.correlate_except(CorTaxonAttribut)
)

0 comments on commit ad7d29c

Please sign in to comment.