Skip to content

Commit

Permalink
correction d'erreurs de mises à jour des notes (#104)
Browse files Browse the repository at this point in the history
Co-authored-by: Julien Corny <[email protected]>
Co-authored-by: Etienne Delclaux <[email protected]>
  • Loading branch information
3 people authored Jan 17, 2025
1 parent 29e6762 commit 6f7b8fc
Show file tree
Hide file tree
Showing 14 changed files with 130 additions and 20 deletions.
36 changes: 32 additions & 4 deletions backend/gn_module_zh/blueprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
from utils_flask_sqla.response import json_resp_accept_empty_list, json_resp

from .api_error import ZHApiError
from . import tasks # noqa: F401

from .forms import (
create_zh,
post_file_info,
Expand All @@ -52,8 +52,7 @@
update_zh_tab6,
)

# from .forms import *
from .geometry import set_area, set_geom, get_main_rb
from .geometry import set_area, set_geom
from .hierarchy import Hierarchy, get_all_hierarchy_fields
from .model.cards import Card
from .model.repositories import ZhRepository
Expand Down Expand Up @@ -81,8 +80,8 @@
get_last_pdf_export,
get_main_picture_id,
get_user_cruved,
delete_notes,
)
import gn_module_zh.tasks

blueprint = Blueprint("pr_zh", __name__, "./static", template_folder="templates")

Expand Down Expand Up @@ -505,6 +504,35 @@ def delete_one_file(id_media):
DB.session.close()


@blueprint.route("notes/<int:id_zh>", methods=["DELETE"])
@json_resp
@permissions.check_cruved_scope("D", module_code="ZONES_HUMIDES")
def delete_one_zh_notes(id_zh):
"""delete all hierarchy notes for one zh"""
try:
delete_notes(id_zh)
except Exception as e:
DB.session.rollback()
if e.__class__.__name__ == "ZHApiError":
raise ZHApiError(message=str(e.message), details=str(e.details))
raise ZHApiError(message="delete_notes_error", details=str(e))
finally:
DB.session.close()


@blueprint.route("/all/hierarchy", methods=["GET"])
@json_resp
def generate_all_notes():
result = DB.session.scalars(select(TZH.id_zh)).all()
if result:
for id_zh in result:
try:
get_hierarchy(id_zh)
except Exception as e:
pass
return ("", 204)


@blueprint.route("files/<int:id_media>", methods=["GET"])
@permissions.check_cruved_scope("C", module_code="ZONES_HUMIDES")
def download_file(id_media):
Expand Down
30 changes: 25 additions & 5 deletions backend/gn_module_zh/tasks.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
import os

from datetime import datetime, timedelta
from pathlib import Path

from sqlalchemy import func, text
from celery.utils.log import get_task_logger
from celery.schedules import crontab
Expand All @@ -13,6 +8,8 @@
from geonature.utils.env import db
from geonature.utils.config import config

from .blueprint import generate_all_notes

logger = get_task_logger(__name__)


Expand Down Expand Up @@ -49,6 +46,22 @@ def setup_periodic_tasks_for_vm_rb_rules(sender, **kwargs):
)


@celery_app.on_after_finalize.connect
def setup_periodic_tasks_update_notes(sender, **kwargs):
minute, hour, day_of_month, month_of_year, day_of_week = "* * * * *".split(" ")
sender.add_periodic_task(
crontab(
minute=minute,
hour=hour,
day_of_week=day_of_week,
day_of_month=day_of_month,
month_of_year=month_of_year,
),
execute_generate_all_notes.s(),
name="Update_all_notes...",
)


@celery_app.task(bind=True)
def refresh_taxon_vm(self):
logger.info("Refresh taxon vms...")
Expand All @@ -63,3 +76,10 @@ def refresh_vm_rb_rules(self):
db.session.execute(text("REFRESH MATERIALIZED VIEW pr_zh.rb_notes_summary"))
db.session.commit()
logger.info("vm rb rules refreshed.")


@celery_app.task(bind=True)
def execute_generate_all_notes(self):
logger.info("Update_all_notes...")
generate_all_notes()
logger.info("all notes updated...")
11 changes: 10 additions & 1 deletion backend/gn_module_zh/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from sqlalchemy.sql import select, update, delete, func

from .api_error import ZHApiError
from .model.zh_schema import TZH, BibAreasTypes, LAreas
from .model.zh_schema import TZH, BibAreasTypes, LAreas, CorZhNotes


def get_main_picture_id(id_zh, media_list=None):
Expand Down Expand Up @@ -90,6 +90,15 @@ def delete_file(id_media):
)


def delete_notes(id_zh):
notes_to_delete = DB.session.execute(
select(CorZhNotes).where(CorZhNotes.id_zh == id_zh)
).scalars()
for note in notes_to_delete:
DB.session.delete(note)
DB.session.commit()


def check_ref_geo_schema():
try:
id_type_com = DB.session.scalar(
Expand Down
15 changes: 15 additions & 0 deletions frontend/app/services/hierarchy.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,16 @@ export class HierarchyService {
this.isLoading = false;
}

clear(): void {
this.warning = '';
}

getHierarchyFromZh(zh) {
if (zh.properties.main_id_rb) {
this.getHierarchy(zh.properties.id_zh);
}
}

// get current zone humides
getHierarchy(zhId) {
this.isLoading = true;
Expand All @@ -59,13 +69,18 @@ export class HierarchyService {
(i) => error.error['message'] === i.api
)[0].front;
}
this.deleteNotes(zhId);
},
() => {
this.isLoading = false;
}
);
}

deleteNotes(idZH: number) {
return this._dataService.deleteNotes(idZH).subscribe();
}

// set list of hierarchy items
setItems(data) {
if (data == null) {
Expand Down
4 changes: 4 additions & 0 deletions frontend/app/services/zh-data.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,10 @@ export class ZhDataService {
});
}

deleteNotes(zhId: number) {
return this._api.delete(`${this.config.API_ENDPOINT}/zones_humides/notes/${zhId}`);
}

getPdf(zhId: number) {
return this._api.get(`${this.config.API_ENDPOINT}/zones_humides/export_pdf/${zhId}`, {
responseType: 'blob',
Expand Down
6 changes: 5 additions & 1 deletion frontend/app/zh-forms/tabs/tab0/zh-form-tab0.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { ZhDataService } from '../../../services/zh-data.service';
import { TabsService } from '../../../services/tabs.service';
import { ErrorTranslatorService } from '../../../services/error-translator.service';
import { PbfService } from '../../../services/pbf.service';
import { HierarchyService } from '../../../services/hierarchy.service';

const GEOM_CONTAINED_ID = 1;

Expand Down Expand Up @@ -47,7 +48,8 @@ export class ZhFormTab0Component implements OnInit {
private _router: Router,
private _toastr: ToastrService,
private _error: ErrorTranslatorService,
private _pbfService: PbfService
private _pbfService: PbfService,
public hierarchy: HierarchyService
) {}

ngOnInit() {
Expand Down Expand Up @@ -209,6 +211,7 @@ export class ZhFormTab0Component implements OnInit {
closeButton: true,
positionClass: 'toast-top-right',
});
this.hierarchy.getHierarchyFromZh(this._currentZh);
this.nextTab.emit(1);
});
},
Expand Down Expand Up @@ -281,6 +284,7 @@ export class ZhFormTab0Component implements OnInit {
ngOnDestroy() {
if (this.$_geojsonSub) this.$_geojsonSub.unsubscribe();
if (this.$_currentZhSub) this.$_currentZhSub.unsubscribe();
this.hierarchy.clear();
}

featureCollectionToMultipolygon(featureCollection) {
Expand Down
6 changes: 5 additions & 1 deletion frontend/app/zh-forms/tabs/tab1/zh-form-tab1.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { ZhDataService } from '../../../services/zh-data.service';
import { TabsService } from '../../../services/tabs.service';
import { ErrorTranslatorService } from '../../../services/error-translator.service';
import { ConfigService } from '@geonature/services/config.service';
import { HierarchyService } from '../../../services/hierarchy.service';

@Component({
selector: 'zh-form-tab1',
Expand Down Expand Up @@ -47,7 +48,8 @@ export class ZhFormTab1Component implements OnInit {
private _tabService: TabsService,
private _error: ErrorTranslatorService,
public ngbModal: NgbModal,
private _config: ConfigService
private _config: ConfigService,
public hierarchy: HierarchyService
) {}

ngOnInit() {
Expand Down Expand Up @@ -159,6 +161,7 @@ export class ZhFormTab1Component implements OnInit {
this._toastr.success('Vos données sont bien enregistrées', '', {
positionClass: 'toast-top-right',
});
this.hierarchy.getHierarchyFromZh(this.currentZh);
this.nextTab.emit(2);
});
},
Expand Down Expand Up @@ -196,5 +199,6 @@ export class ZhFormTab1Component implements OnInit {
ngOnDestroy() {
if (this.$_currentZhSub) this.$_currentZhSub.unsubscribe();
this.ngbModal.dismissAll();
this.hierarchy.clear();
}
}
6 changes: 5 additions & 1 deletion frontend/app/zh-forms/tabs/tab2/zh-form-tab2.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { Subscription } from 'rxjs';
import { ErrorTranslatorService } from '../../../services/error-translator.service';
import { TabsService } from '../../../services/tabs.service';
import { ZhDataService } from '../../../services/zh-data.service';
import { HierarchyService } from '../../../services/hierarchy.service';

@Component({
selector: 'zh-form-tab2',
Expand All @@ -29,7 +30,8 @@ export class ZhFormTab2Component implements OnInit, AfterViewInit {
private _dataService: ZhDataService,
private _toastr: ToastrService,
private _error: ErrorTranslatorService,
private _tabService: TabsService
private _tabService: TabsService,
public hierarchy: HierarchyService
) {}

ngOnInit() {
Expand Down Expand Up @@ -121,6 +123,7 @@ export class ZhFormTab2Component implements OnInit, AfterViewInit {
this._toastr.success('Vos données sont bien enregistrées', '', {
positionClass: 'toast-top-right',
});
this.hierarchy.getHierarchyFromZh(this.currentZh);
this.nextTab.emit(3);
});
},
Expand All @@ -138,5 +141,6 @@ export class ZhFormTab2Component implements OnInit, AfterViewInit {
ngOnDestroy() {
if (this.$_currentZhSub) this.$_currentZhSub.unsubscribe();
if (this.$_fromChangeSub) this.$_fromChangeSub.unsubscribe();
this.hierarchy.clear();
}
}
6 changes: 5 additions & 1 deletion frontend/app/zh-forms/tabs/tab3/zh-form-tab3.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { ToastrService } from 'ngx-toastr';
import { TabsService } from '../../../services/tabs.service';
import { ModalService } from '../../../services/modal.service';
import { ErrorTranslatorService } from '../../../services/error-translator.service';
import { HierarchyService } from '../../../services/hierarchy.service';

@Component({
selector: 'zh-form-tab3',
Expand Down Expand Up @@ -91,7 +92,8 @@ export class ZhFormTab3Component implements OnInit {
public ngbModal: NgbModal,
private _modalService: ModalService,
private _error: ErrorTranslatorService,
private _toastr: ToastrService
private _toastr: ToastrService,
public hierarchy: HierarchyService
) {}

ngOnInit() {
Expand Down Expand Up @@ -423,6 +425,7 @@ export class ZhFormTab3Component implements OnInit {
this._toastr.success('Vos données sont bien enregistrées', '', {
positionClass: 'toast-top-right',
});
this.hierarchy.getHierarchyFromZh(this.currentZh);
this.nextTab.emit(4);
});
},
Expand Down Expand Up @@ -468,5 +471,6 @@ export class ZhFormTab3Component implements OnInit {
ngOnDestroy() {
if (this.$_currentZhSub) this.$_currentZhSub.unsubscribe();
if (this.$_fromChangeSub) this.$_fromChangeSub.unsubscribe();
this.hierarchy.clear();
}
}
10 changes: 8 additions & 2 deletions frontend/app/zh-forms/tabs/tab4/zh-form-tab4.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import { ZhDataService } from '../../../services/zh-data.service';
import { TabsService } from '../../../services/tabs.service';
import { ModalService } from '../../../services/modal.service';
import { ErrorTranslatorService } from '../../../services/error-translator.service';
import { HierarchyService } from '../../../services/hierarchy.service';

@Component({
selector: 'zh-form-tab4',
templateUrl: './zh-form-tab4.component.html',
Expand Down Expand Up @@ -99,7 +101,8 @@ export class ZhFormTab4Component implements OnInit {
private _dataService: ZhDataService,
private _modalService: ModalService,
private _error: ErrorTranslatorService,
private _tabService: TabsService
private _tabService: TabsService,
public hierarchy: HierarchyService
) {}

ngOnInit() {
Expand Down Expand Up @@ -456,6 +459,7 @@ export class ZhFormTab4Component implements OnInit {
this._toastr.success('Vos données sont bien enregistrées', '', {
positionClass: 'toast-top-right',
});
this.hierarchy.getHierarchyFromZh(this.currentZh);
this.nextTab.emit(5);
});
},
Expand All @@ -470,5 +474,7 @@ export class ZhFormTab4Component implements OnInit {
}
}

ngOnDestroy() {}
ngOnDestroy() {
this.hierarchy.clear();
}
}
6 changes: 5 additions & 1 deletion frontend/app/zh-forms/tabs/tab5/zh-form-tab5.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { ModalService } from '../../../services/modal.service';
import { TaxaFile } from './zh-form-tab5.models';
import { ErrorTranslatorService } from '../../../services/error-translator.service';
import { FilesService } from '../../../services/files.service';
import { HierarchyService } from '../../../services/hierarchy.service';

@Component({
selector: 'zh-form-tab5',
Expand Down Expand Up @@ -192,7 +193,8 @@ export class ZhFormTab5Component implements OnInit {
private _modalService: ModalService,
private _error: ErrorTranslatorService,
private _tabService: TabsService,
public _filesService: FilesService
public _filesService: FilesService,
public hierarchy: HierarchyService
) {}

ngOnInit() {
Expand Down Expand Up @@ -1143,6 +1145,7 @@ export class ZhFormTab5Component implements OnInit {
this._toastr.success('Vos données sont bien enregistrées', '', {
positionClass: 'toast-top-right',
});
this.hierarchy.getHierarchyFromZh(this.currentZh);
this.nextTab.emit(6);
});
},
Expand Down Expand Up @@ -1177,5 +1180,6 @@ export class ZhFormTab5Component implements OnInit {
ngOnDestroy() {
if (this.$_getTabChangeSub) this.$_getTabChangeSub.unsubscribe();
if (this.$_currentZhSub) this.$_currentZhSub.unsubscribe();
this.hierarchy.clear();
}
}
Loading

0 comments on commit 6f7b8fc

Please sign in to comment.