Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
117 changes: 61 additions & 56 deletions .github/workflows/install_taxhub.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,59 +3,64 @@ name: Test the installation of TaxHub
on: [pull_request]

jobs:
setup:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
submodules: true
- name: Set up Python 3.11
uses: actions/setup-python@v5
with:
python-version: "3.11"
cache: "pip"
- name: Install system dependencies
run: |
sudo apt-get update
sudo apt-get install -y apache2 libpq-dev libgdal-dev unzip gettext-base postgresql postgresql-postgis postgresql-contrib
- name: Start PostgreSQL
run: |
sudo systemctl enable --now postgresql
for i in $(seq 1 30); do
sudo -u postgres pg_isready && break
sleep 1
done
sudo -u postgres pg_isready
- name: Create PostgreSQL role
run: |
sudo -u postgres psql -c "CREATE ROLE taxhubadmin LOGIN PASSWORD 'taxhubpwd';"
- name: Fill settings file
run: |
cp settings.ini.sample settings.ini
sed -i 's|db_name=.*|db_name=taxhub|' settings.ini
sed -i 's|user_pg=.*|user_pg=taxhubadmin|' settings.ini
sed -i 's|user_pg_pass=.*|user_pg_pass=taxhubpwd|' settings.ini
sed -i 's|db_host=.*|db_host=localhost|' settings.ini
sed -i 's|db_port=.*|db_port=5432|' settings.ini
sed -i 's|insert_geonatureatlas_data=.*|insert_geonatureatlas_data=false|' settings.ini
echo "users_schema=local" >> settings.ini
cat settings.ini
- name: Install TaxHub application
run: |
./install_app.sh
- name: Install TaxHub database
run: |
./install_db.sh < /dev/null
- name: Run TaxHub
run: |
sudo systemctl start taxhub
sleep 5
sudo systemctl status taxhub --no-pager
- name: Check if TaxHub is running
run: |
CODE=$(curl -s -o /dev/null -w "%{http_code}" http://127.0.0.1:5000/bdc_statuts/status_types)
if [ "$CODE" != "200" ]; then
echo "Error: TaxHub is not running properly. Status code is $CODE"
sudo journalctl -u taxhub --no-pager
exit 1
fi
setup:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
submodules: true
- name: Set up Python 3.11
uses: actions/setup-python@v5
with:
python-version: "3.11"
cache: "pip"
- name: Install system dependencies
run: |
sudo apt-get update
sudo apt-get install -y apache2 libpq-dev libgdal-dev unzip gettext-base postgresql postgresql-postgis postgresql-contrib
- name: Start PostgreSQL
run: |
sudo systemctl enable --now postgresql
for i in $(seq 1 30); do
sudo -u postgres pg_isready && break
sleep 1
done
sudo -u postgres pg_isready
- name: Create PostgreSQL role
run: |
sudo -u postgres psql -c "CREATE ROLE taxhubadmin LOGIN PASSWORD 'taxhubpwd';"
- name: Fill settings file
run: |
cp settings.ini.sample settings.ini
sed -i 's|db_name=.*|db_name=taxhub|' settings.ini
sed -i 's|user_pg=.*|user_pg=taxhubadmin|' settings.ini
sed -i 's|user_pg_pass=.*|user_pg_pass=taxhubpwd|' settings.ini
sed -i 's|db_host=.*|db_host=localhost|' settings.ini
sed -i 's|db_port=.*|db_port=5432|' settings.ini
sed -i 's|insert_geonatureatlas_data=.*|insert_geonatureatlas_data=false|' settings.ini
echo "users_schema=local" >> settings.ini
cat settings.ini
- name: Install TaxHub application
run: |
./install_app.sh
- name: Setup python dev dependencies
if: github.base_ref != 'master'
run: |
source venv/bin/activate
pip install -r requirements-dev.txt
- name: Install TaxHub database
run: |
./install_db.sh < /dev/null
- name: Run TaxHub
run: |
sudo systemctl start taxhub
sleep 5
sudo systemctl status taxhub --no-pager
- name: Check if TaxHub is running
run: |
CODE=$(curl -s -o /dev/null -w "%{http_code}" http://127.0.0.1:5000/bdc_statuts/status_types)
if [ "$CODE" != "200" ]; then
echo "Error: TaxHub is not running properly. Status code is $CODE"
sudo journalctl -u taxhub --no-pager
exit 1
fi
11 changes: 5 additions & 6 deletions .github/workflows/pytest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ on:
- master
- hotfixes
- develop
- chore/drop-py39
paths-ignore:
- "docs/**"

Expand All @@ -24,7 +25,7 @@ jobs:
fail-fast: false
matrix:
debian-version: ["12", "13"]
sqlalchemy-version: ["1.4"]
sqlalchemy-version: ["2.0"]
include:
- debian-version: "12"
python-version: "3.11"
Expand All @@ -35,7 +36,8 @@ jobs:
postgres-version: "17"
postgis-version: "3.5"

name: Debian ${{ matrix.debian-version }} - SQLAlchemy ${{ matrix.sqlalchemy-version }}
name: Debian ${{ matrix.debian-version }} - SQLAlchemy ${{
matrix.sqlalchemy-version }}

services:
postgres:
Expand All @@ -47,9 +49,7 @@ jobs:
ports:
- 5432:5432
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-cmd pg_isready --health-interval 10s --health-timeout 5s
--health-retries 5

steps:
Expand Down Expand Up @@ -79,7 +79,6 @@ jobs:
sudo apt update
sudo apt install -y libgdal-dev
- name: Install dependencies
if: ${{ matrix.sqlalchemy-version == '1.4' }}
run: |
python -m pip install --upgrade pip
python -m pip install \
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.3.1
2.4.0
62 changes: 32 additions & 30 deletions apptax/admin/admin_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -430,20 +430,23 @@ def _apply_search(self, query, count_query, joins, count_joins, search):

def _get_theme_attributes(self, taxon):
return (
db.session.query(BibThemes)
.join(
BibAttributs,
and_(
BibAttributs.id_theme == BibThemes.id_theme,
or_(BibAttributs.regne == taxon.regne, BibAttributs.regne == None),
or_(
BibAttributs.group2_inpn == taxon.group2_inpn,
BibAttributs.group2_inpn == None,
db.session.scalars(
select(BibThemes)
.join(
BibAttributs,
and_(
BibAttributs.id_theme == BibThemes.id_theme,
or_(BibAttributs.regne == taxon.regne, BibAttributs.regne == None),
or_(
BibAttributs.group2_inpn == taxon.group2_inpn,
BibAttributs.group2_inpn == None,
),
),
),
)
.options(contains_eager(BibThemes.attributs))
.order_by(BibAttributs.ordre)
)
.options(contains_eager(BibThemes.attributs))
.order_by(BibAttributs.ordre)
.unique()
.all()
)

Expand Down Expand Up @@ -497,9 +500,9 @@ def render(self, template, **kwargs):
@expose("/details/", methods=("GET",))
def details_view(self):
id = get_mdict_item_or_list(request.args, "id")
taxon_name = db.session.query(Taxref).get(id)
taxon_name = db.session.get(Taxref, id)
if not taxon_name.cd_nom == taxon_name.cd_ref:
taxon_valid = db.session.query(Taxref).get(taxon_name.cd_ref)
taxon_valid = db.session.get(Taxref, taxon_name.cd_ref)
else:
taxon_valid = taxon_name

Expand Down Expand Up @@ -536,14 +539,8 @@ def edit_form(self, obj=None):
def edit_view(self):
# Get Taxon data
id = get_mdict_item_or_list(request.args, "id")
taxon_name = db.session.query(Taxref).get(id)

# Get attributes only if cd_nom is cd_ref
taxon_name = db.session.get(Taxref, id)
if taxon_name.cd_nom == taxon_name.cd_ref:
theme_attributs_def = self._get_theme_attributes(taxon_name)
attributes_val = self._get_attributes_value(taxon_name, theme_attributs_def)
self._template_args["theme_attributs_def"] = theme_attributs_def
self._template_args["attributes_val"] = attributes_val
if request.method == "POST":
for f in request.form:
if request.form.getlist(f) and f.startswith("attr."):
Expand All @@ -569,6 +566,10 @@ def edit_view(self):
)
db.session.add(model)
db.session.commit()
theme_attributs_def = self._get_theme_attributes(taxon_name)
attributes_val = self._get_attributes_value(taxon_name, theme_attributs_def)
self._template_args["theme_attributs_def"] = theme_attributs_def
self._template_args["attributes_val"] = attributes_val
self._template_args["url_cancel"] = request.referrer or url_for("taxons.index_view")

return super(TaxrefView, self).edit_view()
Expand All @@ -584,15 +585,12 @@ def format(self, model):
return None

def get_one(self, pk):
return Taxref.query.filter(Taxref.cd_nom == pk).first()
return db.session.scalars(select(Taxref).where(Taxref.cd_nom == pk)).first()

def get_list(self, query, offset=0, limit=DEFAULT_PAGE_SIZE):
results = (
Taxref.query.filter(Taxref.nom_complet.ilike(f"{query}%"))
.limit(limit)
.offset(offset)
.all()
)
results = db.session.scalars(
select(Taxref).where(Taxref.nom_complet.ilike(f"{query}%")).limit(limit).offset(offset)
).all()
return results


Expand Down Expand Up @@ -657,10 +655,14 @@ def format(self, model):
return None

def get_one(self, pk):
return Taxref.query.with_entities(Taxref.regne).filter(Taxref.regne == pk).distinct().one()
from apptax.taxonomie import db

return db.session.execute(select(Taxref.regne).where(Taxref.regne == pk).distinct()).one()

def get_list(self, query, offset=0, limit=DEFAULT_PAGE_SIZE):
return Taxref.query.with_entities(Taxref.regne).distinct().all()
from apptax.taxonomie import db

return db.session.execute(select(Taxref.regne).distinct()).all()


class BibAttributsView(FlaskAdminProtectedMixin, RegneAndGroupFormMixin, ModelView):
Expand Down
17 changes: 11 additions & 6 deletions apptax/admin/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@
from flask_admin.contrib.sqla.filters import FilterEqual
from flask_admin.babel import lazy_gettext

from sqlalchemy import select
from sqlalchemy.orm import aliased

from apptax.database import db

from apptax.taxonomie.models import (
Taxref,
BibAttributs,
Expand Down Expand Up @@ -39,8 +42,10 @@ class TaxrefDistinctFilter(DynamicOptionsMixin, FilterEqual):
def get_dynamic_options(self, view):
if has_app_context():
yield from [
(getattr(row, self.column.key), getattr(row, self.column.key))
for row in Taxref.query.distinct(self.column).order_by(self.column).all()
(row[0], row[0])
for row in db.session.execute(
select(self.column).distinct().order_by(self.column)
).all()
]


Expand All @@ -53,9 +58,9 @@ def operation(self):

def get_dynamic_options(self, view):
if has_app_context():
yield from [
(attr.id_attribut, attr.label_attribut) for attr in BibAttributs.query.all()
]
yield from db.session.execute(
select(BibAttributs.id_attribut, BibAttributs.label_attribut)
).all()


class FilterBiblist(DynamicOptionsMixin, BaseFilter):
Expand All @@ -67,7 +72,7 @@ def operation(self):

def get_dynamic_options(self, view):
if has_app_context():
yield from [(list.id_liste, list.nom_liste) for list in BibListes.query.all()]
yield from db.session.execute(select(BibListes.id_liste, BibListes.nom_liste)).all()


class FilterIsValidName(BaseFilter):
Expand Down
5 changes: 4 additions & 1 deletion apptax/migrations/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@
from flask import current_app

config.set_main_option(
"sqlalchemy.url", str(current_app.extensions["migrate"].db.engine.url).replace("%", "%%")
"sqlalchemy.url",
current_app.extensions["migrate"]
.db.engine.url.render_as_string(hide_password=False)
.replace("%", "%%"),
)
target_metadata = current_app.extensions["migrate"].db.metadata

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@
def upgrade():
# Suppression des fichiers médias
conn = op.get_bind()
res = conn.execute("""SELECT id_media, chemin
res = conn.execute(sa.text("""SELECT id_media, chemin
FROM taxonomie.t_medias
WHERE NOT NULLIF(chemin , '') IS NULL AND supprime = TRUE;""")
WHERE NOT NULLIF(chemin , '') IS NULL AND supprime = TRUE;"""))

if "MEDIA_FOLDER" in current_app.config:
media_path = Path(current_app.config["MEDIA_FOLDER"], "taxhub").absolute()
Expand Down
6 changes: 3 additions & 3 deletions apptax/taxonomie/commands/taxref.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import csv

from flask.cli import with_appcontext
from sqlalchemy import select, func
from sqlalchemy import select, func, text
from sqlalchemy.orm.exc import NoResultFound


Expand Down Expand Up @@ -81,7 +81,7 @@ def delete():
) as bar:
for i, table in enumerate(tables):
bar.update(n_steps=i, current_item=table)
db.session.execute(f"DELETE FROM {table}")
db.session.execute(text(f"DELETE FROM {table}"))

db.session.commit()

Expand Down Expand Up @@ -118,7 +118,7 @@ def import_inpn_media(file):

# Get Taxon
try:
taxon = Taxref.query.get(int(value))
taxon = db.session.get(Taxref, int(value))
except (NoResultFound, ValueError):
logger.error(f"{value} is not a valid cd_ref")
continue
Expand Down
Loading
Loading