Skip to content

Commit

Permalink
Intégration de taxhub v2 à GeoNature (#3150)
Browse files Browse the repository at this point in the history
* TaxHub integration:
- load of blueprint
- add Babel for traductions
- Infer TAXHUB API URL from GN API
- remove taxhub from installation : systemd + apache 
* Taxhub integration (medias + suppression de l'installation standalone de TaxHub
* doc on taxhub
* compat 2.14
* fix multiple heads of migration ebbe0f7ed866
* add endpoint for taxhub media
* Centralisation des parametères relatif à taxhub sous section TAXHUB + API_PREFIX for taxhub api url - retrocompatibily occtax-mobile GeoNature-atlas
* Ajout paramètre taxref_region  
* update requirements
* Correction test_synthese::test_get_observations_for_web
* doc changelog

---------

Co-authored-by: TheoLechemia <[email protected]>
Co-authored-by: jacquesfize <[email protected]>
Co-authored-by: amandine-sahl <[email protected]>
  • Loading branch information
4 people committed Aug 9, 2024
1 parent dbb8797 commit 3adc7b4
Show file tree
Hide file tree
Showing 30 changed files with 487 additions and 151 deletions.
9 changes: 2 additions & 7 deletions .github/workflows/cypress.yml
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ jobs:
GEONATURE_CONFIG_FILE: config/test_config.toml
srid_local: 2154
install_bdc_statuts: true
taxref_region: fr
add_sample_data: true
install_sig_layers: true
install_grid_layer_5: true
Expand All @@ -100,7 +101,7 @@ jobs:
cp ./config/settings.ini.sample ./config/settings.ini
./install/05_install_frontend.sh --ci
env:
GEONATURE_CONFIG_FILE: "${{ github.workspace }}/config/test_config.toml"
GEONATURE_CONFIG_FILE: '${{ github.workspace }}/config/test_config.toml'
- name: Install core modules
run: |
geonature install-gn-module contrib/occtax OCCTAX --build=false
Expand All @@ -115,12 +116,6 @@ jobs:
run: geonature dev_back &
env:
GEONATURE_CONFIG_FILE: config/test_config.toml
- name: Run TaxHub backend
run: flask run --host=0.0.0.0 &
working-directory: ./backend/dependencies/TaxHub/
env:
TAXHUB_SETTINGS: test_config.py
TAXHUB_SQLALCHEMY_DATABASE_URI: "postgresql://geonatadmin:[email protected]:5432/geonature2db"
- name: Cypress run
uses: cypress-io/github-action@v5
with:
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/eval.yml
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ jobs:
GEONATURE_CONFIG_FILE: config/test_config.toml
srid_local: 2154
install_bdc_statuts: true
taxref_region: fr
add_sample_data: true
install_sig_layers: true
install_grid_layer_5: true
Expand Down
21 changes: 11 additions & 10 deletions .github/workflows/eval_perf.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@ jobs:
strategy:
fail-fast: false
matrix:
debian-version: ["11", "12"]
debian-version: ['11', '12']
include:
- debian-version: "11"
python-version: "3.9"
postgres-version: "13"
postgis-version: "3.2"
- debian-version: "12"
python-version: "3.11"
postgres-version: "15"
postgis-version: "3.3"
- debian-version: '11'
python-version: '3.9'
postgres-version: '13'
postgis-version: '3.2'
- debian-version: '12'
python-version: '3.11'
postgres-version: '15'
postgis-version: '3.3'

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

Expand Down Expand Up @@ -50,7 +50,7 @@ jobs:
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}
cache: "pip"
cache: 'pip'
- name: Install GDAL
run: |
sudo apt update
Expand Down Expand Up @@ -94,6 +94,7 @@ jobs:
GEONATURE_CONFIG_FILE: config/test_config.toml
srid_local: 2154
install_bdc_statuts: true
taxref_region: fr
add_sample_data: true
install_sig_layers: true
install_grid_layer_5: true
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/pytest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ jobs:
GEONATURE_CONFIG_FILE: config/test_config.toml
srid_local: 2154
install_bdc_statuts: true
taxref_region: fr
add_sample_data: true
install_sig_layers: true
install_grid_layer_5: true
Expand Down
2 changes: 1 addition & 1 deletion backend/dependencies/TaxHub
Submodule TaxHub updated 181 files
42 changes: 42 additions & 0 deletions backend/geonature/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from flask import Flask, g, request, current_app, send_from_directory
from flask.json.provider import DefaultJSONProvider
from flask_mail import Message
from flask_babel import Babel
from flask_cors import CORS
from flask_login import current_user
from flask_sqlalchemy.track_modifications import before_models_committed
Expand Down Expand Up @@ -82,6 +83,17 @@ def default(o):
return DefaultJSONProvider.default(o)


def get_locale():
# if a user is logged in, use the locale from the user settings
user = getattr(g, "user", None)
if user is not None:
return user.locale
# otherwise try to guess the language from the user accept
# header the browser transmits. We support de/fr/en in this
# example. The best match wins.
return request.accept_languages.best_match(["de", "fr", "en"])


def create_app(with_external_mods=True):
app = Flask(
__name__.split(".")[0],
Expand Down Expand Up @@ -175,12 +187,22 @@ def set_sentry_context():

admin.init_app(app)

# babel
babel = Babel(app, locale_selector=get_locale)

# Enable serving of media files
app.add_url_rule(
f"{config['MEDIA_URL']}/<path:filename>",
view_func=lambda filename: send_from_directory(config["MEDIA_FOLDER"], filename),
endpoint="media",
)
app.add_url_rule(
f"{config['MEDIA_URL']}/taxhub/<path:filename>",
view_func=lambda filename: send_from_directory(
config["MEDIA_FOLDER"] + "/taxhub", filename
),
endpoint="media_taxhub",
)

for blueprint_path, url_prefix in [
("pypn_habref_api.routes:routes", "/habref"),
Expand All @@ -202,6 +224,26 @@ def set_sentry_context():
app.register_blueprint(blueprint, url_prefix=url_prefix)

with app.app_context():
# taxhub api
from apptax import taxhub_api_routes

base_api_prefix = app.config["TAXHUB"].get("API_PREFIX")

for blueprint_path, url_prefix in taxhub_api_routes:
module_name, blueprint_name = blueprint_path.split(":")
blueprint = getattr(import_module(module_name), blueprint_name)
app.register_blueprint(blueprint, url_prefix="/taxhub" + base_api_prefix + url_prefix)

# taxhub admin
from apptax.admin.admin import adresses

app.register_blueprint(adresses, url_prefix="/taxhub")

# register taxhub admin view which need app context
from geonature.core.taxonomie.admin import load_admin_views

load_admin_views(app, admin)

# register errors handlers
import geonature.core.errors

Expand Down
3 changes: 3 additions & 0 deletions backend/geonature/core/admin/admin.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import os

from flask import g
from werkzeug.exceptions import Unauthorized
from flask_admin import Admin, AdminIndexView, expose
Expand Down Expand Up @@ -152,4 +154,5 @@ class ProtectedTNomenclaturesAdmin(
)
)


flask_admin = admin # for retro-compatibility, usefull for export module for instance
1 change: 0 additions & 1 deletion backend/geonature/core/command/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,6 @@ def default_config():
required_fields = (
"URL_APPLICATION",
"API_ENDPOINT",
"API_TAXHUB",
"SECRET_KEY",
"SQLALCHEMY_DATABASE_URI",
)
Expand Down
1 change: 1 addition & 0 deletions backend/geonature/core/gn_permissions/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from markupsafe import Markup
from sqlalchemy.orm import contains_eager, joinedload
from sqlalchemy import select
from markupsafe import Markup

from geonature.utils.env import db
from geonature.utils.config import config
Expand Down
92 changes: 92 additions & 0 deletions backend/geonature/core/taxonomie/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import os
from apptax.admin.admin_view import (
BibListesView,
TaxrefView,
TMediasView,
BibAttributsView,
BibThemesView,
)
from geonature.utils.env import db

from apptax.admin.admin import adresses
from apptax.taxonomie.models import Taxref, BibListes, TMedias, BibAttributs, BibThemes
from geonature.core.admin.utils import CruvedProtectedMixin


class CruvedProtectedBibListesView(CruvedProtectedMixin, BibListesView):
module_code = "TAXHUB"
object_code = "LISTE"
extra_actions_perm = {".import_cd_nom_view": "C"}


class CruvedProtectedTaxrefView(CruvedProtectedMixin, TaxrefView):
module_code = "TAXHUB"
object_code = "TAXON"


class CruvedProtectedTMediasView(CruvedProtectedMixin, TMediasView):
module_code = "TAXHUB"
object_code = "TAXON"


class CruvedProtectedBibAttributsView(CruvedProtectedMixin, BibAttributsView):
module_code = "TAXHUB"
object_code = "ATTRIBUT"


class CruvedProtectedBibThemes(CruvedProtectedMixin, BibThemesView):
module_code = "TAXHUB"
object_code = "THEME"


def load_admin_views(app, admin):
static_folder = os.path.join(adresses.root_path, "static")

admin.add_view(
CruvedProtectedTaxrefView(
Taxref,
db.session,
name="Taxref",
endpoint="taxons",
category="TaxHub",
static_folder=static_folder,
)
)
admin.add_view(
CruvedProtectedBibListesView(
BibListes,
db.session,
name="Listes",
category="TaxHub",
static_folder=static_folder,
)
)

admin.add_view(
CruvedProtectedBibAttributsView(
BibAttributs,
db.session,
name="Attributs",
category="TaxHub",
static_folder=static_folder,
)
)
admin.add_view(
CruvedProtectedBibThemes(
BibThemes,
db.session,
name="Thèmes",
category="TaxHub",
static_folder=static_folder,
)
)

admin.add_view(
CruvedProtectedTMediasView(
TMedias,
db.session,
name="Médias",
category="TaxHub",
static_folder=static_folder,
)
)
Loading

0 comments on commit 3adc7b4

Please sign in to comment.