Skip to content

Commit

Permalink
refactor config access
Browse files Browse the repository at this point in the history
  • Loading branch information
TheoLechemia committed Aug 14, 2023
1 parent f59dd0e commit 889b068
Show file tree
Hide file tree
Showing 26 changed files with 861 additions and 651 deletions.
20 changes: 13 additions & 7 deletions contrib/occtax/backend/occtax/blueprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
from geonature.utils.env import DB, db, ROOT_DIR

from geonature.utils import filemanager
from geonature.utils.config import config
from .models import (
TRelevesOccurrence,
TOccurrencesOccurrence,
Expand Down Expand Up @@ -65,6 +66,11 @@ def set_current_module(endpoint, values):
g.current_module = TModules.query.filter_by(module_code=requested_module).first_or_404(
f"No module name {requested_module}"
)
g.module_config = (
config["OCCTAX"]["DEFAULT"]
if requested_module == "OCCTAX"
else config["OCCTAX"][requested_module]
)


@blueprint.route("/<module_code>/releves", methods=["GET"])
Expand Down Expand Up @@ -562,12 +568,12 @@ def export(scope):
:query str format: format of the export ('csv', 'geojson', 'shapefile', 'gpkg')
"""
export_view_name = blueprint.config["export_view_name"]
export_geom_column = blueprint.config["export_geom_columns_name"]
export_columns = blueprint.config["export_columns"]
export_srid = blueprint.config["export_srid"]
export_view_name = g.module_config["export_view_name"]
export_geom_column = g.module_config["export_geom_columns_name"]
export_columns = g.module_config["export_columns"]
export_srid = g.module_config["export_srid"]
export_format = request.args["format"] if "format" in request.args else "geojson"
export_col_name_additional_data = blueprint.config["export_col_name_additional_data"]
export_col_name_additional_data = g.module_config["export_col_name_additional_data"]

export_view = GenericTableGeo(
tableName=export_view_name,
Expand All @@ -589,10 +595,10 @@ def export(scope):
export_view,
q,
from_generic_table=True,
obs_txt_column=blueprint.config["export_observer_txt_column"],
obs_txt_column=g.module_config["export_observer_txt_column"],
)

if current_app.config["OCCTAX"]["ADD_MEDIA_IN_EXPORT"]:
if g.module_config["ADD_MEDIA_IN_EXPORT"]:
q, columns = releve_repository.add_media_in_export(q, columns)
data = q.all()

Expand Down
14 changes: 5 additions & 9 deletions contrib/occtax/backend/occtax/conf_schema_toml.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,13 +204,9 @@ class OcctaxSchemaConf(Schema):
config_fields = OcctaxSchemaConf().fields


if len(submodules) > 0:
submodules.append("DEFAULT")
dict_nested = {}
for sub in submodules:
dict_nested[sub] = fields.Nested(config_fields, load_default=OcctaxSchemaConf().load({}))
submodules.append("DEFAULT")
dict_nested = {}
for sub in submodules:
dict_nested[sub] = fields.Nested(config_fields, load_default=OcctaxSchemaConf().load({}))

GnModuleSchemaConf = type("GnModuleSchemaConf", (Schema,), dict_nested)

else:
GnModuleSchemaConf = type("GnModuleSchemaConf", (Schema,), config_fields)
GnModuleSchemaConf = type("GnModuleSchemaConf", (Schema,), dict_nested)
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<div class="row">
<div
*ngIf="config.OCCTAX.form_fields.obj_count"
*ngIf="moduleConfig.form_fields.obj_count"
class="col-lg-4 col-sm-6"
>
<pnx-nomenclature
Expand All @@ -12,7 +12,7 @@
</pnx-nomenclature>
</div>
<div
*ngIf="config.OCCTAX.form_fields.type_count"
*ngIf="moduleConfig.form_fields.type_count"
class="col-lg-4 col-sm-6"
>
<pnx-nomenclature
Expand All @@ -37,7 +37,7 @@
<div class="row">

<div
*ngIf="config.OCCTAX.form_fields.count_min"
*ngIf="moduleConfig.form_fields.count_min"
class="col-3"
>
<small> {{ 'Counting.NumberMin' | translate }} </small>
Expand All @@ -52,7 +52,7 @@
</div>

<div
*ngIf="config.OCCTAX.form_fields.count_max"
*ngIf="moduleConfig.form_fields.count_max"
class="col-3"
>
<small> {{ 'Counting.NumberMax' | translate }} </small>
Expand All @@ -74,7 +74,7 @@
</div>

<div
*ngIf="config.OCCTAX.form_fields.life_stage"
*ngIf="moduleConfig.form_fields.life_stage"
class="col-3"
>
<pnx-nomenclature
Expand All @@ -93,7 +93,7 @@
</small>
</div>
<div
*ngIf="config.OCCTAX.form_fields.sex"
*ngIf="moduleConfig.form_fields.sex"
class="col-3"
>
<pnx-nomenclature
Expand Down Expand Up @@ -125,7 +125,7 @@
<div class="row">
<div
id='occurrence-medias'
*ngIf="config.OCCTAX.ENABLE_MEDIAS"
*ngIf="moduleConfig.ENABLE_MEDIAS"
class="col-sm-12"
>

Expand All @@ -143,7 +143,7 @@ <h6 style="margin-top: 15px;">
[parentFormControl]="form.controls.medias"
[sizeMax]="config.MEDIAS.MEDIAS_SIZE_MAX"
[default]="defaultsMedia()"
[details]="config.OCCTAX.MEDIA_FIELDS_DETAILS"
[details]="moduleConfig.MEDIA_FIELDS_DETAILS"
[disabled]="!taxref"
disabledTxt="Veuillez choisir un taxon avant de renseigner les médias"
></pnx-medias>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,30 +1,39 @@
import { Component, OnInit, Input, OnDestroy, Output, EventEmitter } from '@angular/core';
import { UntypedFormGroup, UntypedFormArray } from '@angular/forms';
import { Subscription } from 'rxjs';
import { filter } from 'rxjs/operators';
import {
Component,
OnInit,
Input,
OnDestroy,
Output,
EventEmitter,
} from "@angular/core";
import { UntypedFormGroup, UntypedFormArray } from "@angular/forms";
import { Subscription } from "rxjs";
import { filter } from "rxjs/operators";

import { OcctaxFormService } from '../occtax-form.service';
import { OcctaxFormOccurrenceService } from '../occurrence/occurrence.service';
import { OcctaxFormCountingService } from './counting.service';
import { ConfigService } from '@geonature/services/config.service';
import { OcctaxFormService } from "../occtax-form.service";
import { OcctaxFormOccurrenceService } from "../occurrence/occurrence.service";
import { OcctaxFormCountingService } from "./counting.service";
import { ConfigService } from "@geonature/services/config.service";
import { OcctaxDataService } from "../../services/occtax-data.service";

@Component({
selector: 'pnx-occtax-form-counting',
templateUrl: './counting.component.html',
styleUrls: ['./counting.component.scss'],
selector: "pnx-occtax-form-counting",
templateUrl: "./counting.component.html",
styleUrls: ["./counting.component.scss"],
providers: [OcctaxFormCountingService],
})
export class OcctaxFormCountingComponent implements OnInit, OnDestroy {
public data: any;

@Input('value')
@Input("value")
set counting(value: any) {
this.occtaxFormCountingService.counting.next(value);
}
public sub: Subscription;
@Output() lifeStageChange = new EventEmitter();

form: UntypedFormGroup;
public moduleConfig;
get additionalFieldsForm(): any[] {
return this.occtaxFormCountingService.additionalFieldsForm;
}
Expand All @@ -33,27 +42,37 @@ export class OcctaxFormCountingComponent implements OnInit, OnDestroy {
public occtaxFormService: OcctaxFormService,
public occtaxFormOccurrenceService: OcctaxFormOccurrenceService,
private occtaxFormCountingService: OcctaxFormCountingService,
public config: ConfigService
public config: ConfigService,
private _ds: OcctaxDataService,
) {}

ngOnInit() {
this.moduleConfig = this._ds.moduleConfig;
this.form = this.occtaxFormCountingService.form;
this.sub = this.form
.get('id_nomenclature_life_stage')
.valueChanges.pipe(filter((idNomenclatureLifeStage) => idNomenclatureLifeStage !== null))
.get("id_nomenclature_life_stage")
.valueChanges.pipe(
filter((idNomenclatureLifeStage) => idNomenclatureLifeStage !== null),
)
.subscribe((idNomenclatureLifeStage) => {
this.occtaxFormOccurrenceService.lifeStage.next(idNomenclatureLifeStage);
this.occtaxFormOccurrenceService.lifeStage.next(
idNomenclatureLifeStage,
);
});
}

ngOnDestroy() {
//delete elem from form.get('cor_counting_occtax')
const idx = (
this.occtaxFormOccurrenceService.form.get('cor_counting_occtax') as UntypedFormArray
this.occtaxFormOccurrenceService.form.get(
"cor_counting_occtax",
) as UntypedFormArray
).controls.findIndex((elem) => elem === this.form);
if (idx !== -1) {
(
this.occtaxFormOccurrenceService.form.get('cor_counting_occtax') as UntypedFormArray
this.occtaxFormOccurrenceService.form.get(
"cor_counting_occtax",
) as UntypedFormArray
).removeAt(idx);
}
}
Expand All @@ -73,20 +92,26 @@ export class OcctaxFormCountingComponent implements OnInit, OnDestroy {
};
}

const observers = (occtaxData && occtaxData.releve.properties.observers) || [];
const author = observers.map((o) => o.nom_complet).join(', ');
const observers =
(occtaxData && occtaxData.releve.properties.observers) || [];
const author = observers.map((o) => o.nom_complet).join(", ");

const date_min = (occtaxData && occtaxData.releve.properties.date_min) || null;
const date_min =
(occtaxData && occtaxData.releve.properties.date_min) || null;

const cd_nom = String(taxref && taxref.cd_nom) || '';
const lb_nom = (taxref && `${taxref.lb_nom}`) || '';
const date_txt = date_min ? `${date_min.year}_${date_min.month}_${date_min.day}` : '';
const date_txt2 = date_min ? `${date_min.day}/${date_min.month}/${date_min.year}` : '';
const cd_nom = String(taxref && taxref.cd_nom) || "";
const lb_nom = (taxref && `${taxref.lb_nom}`) || "";
const date_txt = date_min
? `${date_min.year}_${date_min.month}_${date_min.day}`
: "";
const date_txt2 = date_min
? `${date_min.day}/${date_min.month}/${date_min.year}`
: "";

return {
displayDetails: false,
author: author,
title_fr: `${date_txt}_${lb_nom.replace(' ', '_')}_${cd_nom}`,
title_fr: `${date_txt}_${lb_nom.replace(" ", "_")}_${cd_nom}`,
description_fr: `${lb_nom} observé le ${date_txt2}`,
};
}
Expand Down
Loading

0 comments on commit 889b068

Please sign in to comment.