Skip to content

Commit

Permalink
feat: get information when edit site
Browse files Browse the repository at this point in the history
- Get all fields from specific site.json into editform
- Fix problem redirection if edit object site

- WIP : check how to update specific fields from object
- WIP : check how to manage listOption types site when reload or when
  come back into component after first init

Reviewed-by: andriac
[Refs_ticket]: #5 , #6
  • Loading branch information
andriacap committed Apr 18, 2023
1 parent 7bc382b commit 11ba905
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 76 deletions.
10 changes: 8 additions & 2 deletions backend/gn_module_monitoring/monitoring/objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,17 @@ class MonitoringSite(MonitoringObjectGeom):

def preprocess_data(self, data):
type_site_ids = [type_site.id_nomenclature_type_site for type_site in self._model.types_site]
if len(data['types_site']) >0 :
if len(data['types_site']) >0 and all(isinstance(x,int) for x in data['types_site']):
for id_type_site in data['types_site']:
if int(id_type_site) not in type_site_ids:
type_site_ids.append(id_type_site)
#TODO: A enlever une fois qu'on aura enelever le champ "id_nomenclature_type_site" du model et de la bdd
data["id_nomenclature_type_site"]=data["types_site"][0]
else :
for item in data['types_site']:
if int(item['id_nomenclature_type_site']) not in type_site_ids:
type_site_ids.append(id_type_site)
data["id_nomenclature_type_site"]=data["types_site"][0]['id_nomenclature_type_site']
#TODO: A enlever une fois qu'on aura enelever le champ "id_nomenclature_type_site" du model et de la bdd


data['types_site'] = type_site_ids
13 changes: 13 additions & 0 deletions backend/gn_module_monitoring/routes/site.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,3 +144,16 @@ def post_sites():
customConfig.update(post_data["dataComplement"][keys]["config"])
get_config(module_code, force=True, customSpecConfig=customConfig)
return create_or_update_object_api_sites_sites_group(module_code, object_type), 201


@blueprint.route("/sites/<int:_id>", methods=["PATCH"])
def pacth_sites(_id):
module_code = "generic"
object_type = "site"
customConfig = dict()
post_data = dict(request.get_json())
for keys in post_data["dataComplement"].keys():
if "config" in post_data["dataComplement"][keys]:
customConfig.update(post_data["dataComplement"][keys]["config"])
get_config(module_code, force=True, customSpecConfig=customConfig)
return create_or_update_object_api_sites_sites_group(module_code, object_type, _id), 201
44 changes: 0 additions & 44 deletions backend/gn_module_monitoring/routes/sites_groups.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,47 +103,3 @@ def handle_validation_error(error):
status_code=422,
payload=error.data,
).to_dict()


# TODO: OPTIMIZE in order to adapt to new monitoring module (entry by sites_groups)


def create_or_update_object_api(module_code, object_type, id=None):
"""
route pour la création ou la modification d'un objet
si id est renseigné, c'est une création (PATCH)
sinon c'est une modification (POST)
:param module_code: reference le module concerne
:param object_type: le type d'object (site, visit, obervation)
:param id : l'identifiant de l'object (de id_base_site pour site)
:type module_code: str
:type object_type: str
:type id: int
:return: renvoie l'object crée ou modifié
:rtype: dict
"""
depth = to_int(request.args.get("depth", 1))

# recupération des données post
post_data = dict(request.get_json())
if module_code != "generic":
module = get_module("module_code", module_code)
else:
module = {"id_module": "generic"}
#TODO : A enlever une fois que le post_data contiendra geometry et type depuis le front
if object_type == "site":
post_data["geometry"]={'type':'Point', 'coordinates':[2.5,50]}
post_data["type"]='Feature'
# on rajoute id_module s'il n'est pas renseigné par défaut ??
if "id_module" not in post_data["properties"]:
module["id_module"] = "generic"
post_data["properties"]["id_module"] = module["id_module"]
else:
post_data["properties"]["id_module"] = module["id_module"]

return (
monitoring_definitions.monitoring_object_instance(module_code, object_type, id)
.create_or_update(post_data)
.serialize(depth)
)
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ export class MonitoringFormComponentG implements OnInit {
) {}

ngOnInit() {
// TODO: Avoid two subscribes one inside other (code test above doesn't work. When add type site the observable currentdata is not recall)
this._formService.currentData
.pipe(
tap((data) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
></btn-select>
<pnx-monitoring-form-g
[objForm]="form"
#subscritionObjConfig
[obj]="site"
(objChanged)="onObjChanged($event)"
*ngIf="bEdit"
Expand All @@ -25,7 +24,6 @@
*ngIf="!bEdit"
[(bEdit)]="bEdit"
[newParentType]="objParent"
[objectType]="objectType"
[(bEdit)]="bEdit"
[selectedObj]="site"
></pnx-monitoring-properties-g>
Expand All @@ -39,4 +37,6 @@
(onSetPage)="setPage($event)"
[obj]="visits"
(onDetailsRow)="seeDetails($event)"
></pnx-monitoring-datatable-g>

> <option-list-btn add-button placeholder="Sélectionner le protocole" label="Ajouter une visite" [optionList]="modules"
(onDeployed)="getModules()" (onSaved)="addNewVisit($event)"></option-list-btn></pnx-monitoring-datatable-g>
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Component, Input, OnInit, ViewChild } from '@angular/core';
import { Component, Input, OnInit, QueryList, ViewChild, ViewChildren } from '@angular/core';
import { FormBuilder, FormGroup } from '@angular/forms';
import { ActivatedRoute, Router } from '@angular/router';
import { Observable, forkJoin } from 'rxjs';
Expand All @@ -15,21 +15,17 @@ import { JsonData } from '../../types/jsondata';
import { SelectObject } from '../../interfaces/object';
import { Module } from '../../interfaces/module';
import { ConfigService } from '../../services/config.service';
import { MonitoringFormComponentG } from '../monitoring-form-g/monitoring-form.component-g';

import { FormService } from "../../services/form.service";
@Component({
selector: 'monitoring-visits',
templateUrl: './monitoring-visits.component.html',
styleUrls: ['./monitoring-visits.component.css'],
})
export class MonitoringVisitsComponent extends MonitoringGeomComponent implements OnInit {
@ViewChild('subscritionObjConfig')
monitoringFormComponentG: MonitoringFormComponentG;
site: ISite;
@Input() visits: IVisit[];
@Input() page: IPage;
// colsname: typeof columnNameVisit = columnNameVisit;
objectType: string;
@Input() bEdit: boolean;
form: FormGroup;
colsname: {};
Expand All @@ -44,7 +40,7 @@ export class MonitoringVisitsComponent extends MonitoringGeomComponent implement
placeholderText: string = 'Sélectionnez les types de site';
id_sites_group: number;
types_site: string[];

config: JsonData;

constructor(
private _sites_service: SitesService,
Expand All @@ -54,12 +50,12 @@ export class MonitoringVisitsComponent extends MonitoringGeomComponent implement
private router: Router,
private _Activatedroute: ActivatedRoute,
private _formBuilder: FormBuilder,
private _formService: FormService,
private _configService: ConfigService,
private siteService: SitesService,
) {
super();
this.getAllItemsCallback = this.getVisits;
this.objectType = 'sites';
}

ngOnInit() {
Expand All @@ -70,24 +66,30 @@ export class MonitoringVisitsComponent extends MonitoringGeomComponent implement
this._objService.currentObjectTypeParent.subscribe((objParent) => (this.objParent = objParent));

this._objService.changeObjectType(this._visits_service.objectObs);
this.initSiteVisit()

}

initSiteVisit(){
this._Activatedroute.params
.pipe(
map((params) => params['id'] as number),
mergeMap((id: number) =>
forkJoin({
site: this._sites_service.getById(id),
visits: this._visits_service.get(1, this.limit, {
id_base_site: id,
}),
})
)
.pipe(
map((params) => params['id'] as number),
mergeMap((id: number) =>
forkJoin({
site: this._sites_service.getById(id),
visits: this._visits_service.get(1, this.limit, {
id_base_site: id,
}),
})
)
.subscribe((data: { site: ISite; visits: IPaginated<IVisit> }) => {
this.site = data.site;
this.setVisits(data.visits);
this.baseFilters = { id_base_site: this.site.id_base_site };
});
this.isInitialValues = true;
)
.subscribe((data: { site: ISite; visits: IPaginated<IVisit> }) => {
this._objService.changeSelectedObj(data.site, true);
this.site = data.site;
this.setVisits(data.visits);
this.baseFilters = { id_base_site: this.site.id_base_site };
});
this.isInitialValues = true;
}

getVisits(page: number, filters: JsonData) {
Expand Down Expand Up @@ -143,8 +145,9 @@ export class MonitoringVisitsComponent extends MonitoringGeomComponent implement
}

onSendConfig(config: JsonData): void {
config = this.addTypeSiteListIds(config);
this.monitoringFormComponentG.getConfigFromBtnSelect(config);
this.config = this.addTypeSiteListIds(config);
this.updateForm()
// this.monitoringFormComponentG.getConfigFromBtnSelect(this.config);
}

addTypeSiteListIds(config: JsonData): JsonData {
Expand All @@ -162,4 +165,30 @@ export class MonitoringVisitsComponent extends MonitoringGeomComponent implement
initValueToSend(){
return this.site['types_site']
}

updateForm(){
this.site['specific'] = {};
this.site['dataComplement'] = {};
for (const key in this.config) {
if (this.config[key].config != undefined) {
if (Object.keys(this.config[key].config).length !== 0) {
Object.assign(this.site['specific'], this.config[key].config.specific);
}
}
}
for(var k in this.site.data) this.site[k]=this.site.data[k];
Object.assign(this.site['dataComplement'].dataComplement, this.config);

this._formService.changeDataSub(this.site,
this.objParent.objectType,
this.objParent.endPoint);
}

// removeLastPart(url: string): string {
// return url.slice(0, url.lastIndexOf('/'));
// }

onObjChanged($event) {
this.initSiteVisit();
}
}

0 comments on commit 11ba905

Please sign in to comment.