Skip to content

Commit

Permalink
Fix PnX-SI/GeoNature/#2231 display AF chart int PDF
Browse files Browse the repository at this point in the history
display AF chart int PDF

#2034 - fix actors info in dataset PDF
  • Loading branch information
Gaetanbrl authored and bouttier committed Jan 4, 2023
1 parent ca59e55 commit 083e13f
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 66 deletions.
32 changes: 8 additions & 24 deletions backend/geonature/core/gn_meta/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,8 @@
import datetime as dt
import json
import logging
import threading
from pathlib import Path
from binascii import a2b_base64
import base64
from io import BytesIO, StringIO
from PIL import Image
from codecs import encode
import click
from lxml import etree as ET

from flask import (
Expand Down Expand Up @@ -192,19 +186,6 @@ def get_dataset(scope, id_dataset):
return dataset_schema.jsonify(dataset)


@routes.route("/upload_canvas", methods=["POST"])
def upload_canvas():
"""Upload the canvas as a temporary image used while generating the pdf file"""
data = request.data[22:]
filepath = str(Path(current_app.static_folder) / "images" / "taxa.png")
fm.remove_file(filepath)
if data:
binary_data = a2b_base64(data)
with open(filepath, "wb") as fd:
fd.write(binary_data)
return "", 204


@routes.route("/dataset/<int:ds_id>", methods=["DELETE"])
@permissions.check_cruved_scope("D", get_scope=True, module_code="METADATA")
def delete_dataset(scope, ds_id):
Expand Down Expand Up @@ -452,6 +433,7 @@ def update_dataset(id_dataset, scope):
# TODO: specify which fields may be updated
return DatasetSchema().jsonify(datasetHandler(dataset=dataset, data=request.get_json()))


@routes.route("/dataset/export_pdf", methods=["POST"])
@permissions.check_cruved_scope("E", get_scope=True, module_code="METADATA")
def get_export_pdf_dataset(scope):
Expand All @@ -462,17 +444,18 @@ def get_export_pdf_dataset(scope):
dataset = TDatasets.query.get_or_404(id_dataset)
if not dataset.has_instance_permission(scope=scope):
raise Forbidden("Vous n'avez pas les droits d'exporter ces informations")

dataset_schema = DatasetSchema(
only=[
"nomenclature_data_type",
"nomenclature_dataset_objectif",
"nomenclature_collecting_method",
"acquisition_framework",
"cor_dataset_actor.nomenclature_actor_role",
"cor_dataset_actor.organism",
"cor_dataset_actor.role",
]
)
dataset = dataset_schema.dump(dataset)

if len(dataset.get("dataset_desc")) > 240:
dataset["dataset_desc"] = dataset.get("dataset_desc")[:240] + "..."

Expand All @@ -490,8 +473,8 @@ def get_export_pdf_dataset(scope):
"date": date,
}
# chart
dataset["chart"] = request.json["chart"]

if request.is_json and request.json is not None:
dataset["chart"] = request.json["chart"]
# Appel de la methode pour generer un pdf
return fm.generate_pdf("dataset_template_pdf.html", dataset)

Expand Down Expand Up @@ -666,7 +649,8 @@ def get_export_pdf_acquisition_frameworks():
"nb_habitats": nb_habitat,
}

acquisition_framework["chart"] = request.data[22:]
if request.is_json and request.json is not None:
acquisition_framework["chart"] = request.json["chart"]

if acquisition_framework:
acquisition_framework[
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@
<p class="repartition-especes">Répartition des espèces</p>
<img
class="taxons"
src="{{url_for('static', filename='images/taxa.svg')}}"
src="{{data['chart']}}"
alt="Pas d'espèces à afficher."
>
</div>
Expand Down
4 changes: 3 additions & 1 deletion backend/geonature/templates/dataset_template_pdf.html
Original file line number Diff line number Diff line change
Expand Up @@ -205,14 +205,16 @@ <h5> {{data.title}} </h5>
<div class="map"></div>
</div> -->
<!-- A modifier lorsque la carte sera prete -->
{% if data['chart']: %}
<div class="repartition">
<p class="repartition-especes">Répartition des espèces</p>
<img
class="taxons test"
class="taxons"
alt="Pas d'espèces à afficher."
src="{{data['chart']}}"
>
</div>
{% endif %}
<div class="ca">
<hr class="ligne-titre main-color">
<p class="ca-initial">Cadre d'acquisition</p>
Expand Down
33 changes: 17 additions & 16 deletions backend/geonature/tests/test_gn_meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -388,22 +388,19 @@ def test_get_export_pdf_acquisition_frameworks(self, users, acquisition_framewor

set_logged_user_cookie(self.client, users["user"])

response = self.client.get(
url_for(
"gn_meta.get_export_pdf_acquisition_frameworks",
id_acquisition_framework=af_id,
)
response = self.client.post(
url_for("gn_meta.get_export_pdf_acquisition_frameworks", af=af_id, json={})
)

assert response.status_code == 200

def test_get_export_pdf_acquisition_frameworks_unauthorized(self, acquisition_frameworks):
af_id = acquisition_frameworks["own_af"].id_acquisition_framework

response = self.client.get(
response = self.client.post(
url_for(
"gn_meta.get_export_pdf_acquisition_frameworks",
id_acquisition_framework=af_id,
af=af_id,
)
)

Expand Down Expand Up @@ -621,27 +618,31 @@ def test_dataset_pdf_export(self, users, datasets):
unexisting_id = db.session.query(func.max(TDatasets.id_dataset)).scalar() + 1
ds = datasets["own_dataset"]

response = self.client.get(
url_for("gn_meta.get_export_pdf_dataset", id_dataset=ds.id_dataset)
response = self.client.post(
url_for("gn_meta.get_export_pdf_dataset", dataset=ds.id_dataset, json={})
)
assert response.status_code == Unauthorized.code

set_logged_user_cookie(self.client, users["self_user"])

response = self.client.get(
url_for("gn_meta.get_export_pdf_dataset", id_dataset=unexisting_id)
response = self.client.post(
url_for("gn_meta.get_export_pdf_dataset", dataset=unexisting_id, json={})
)
assert response.status_code == NotFound.code

response = self.client.get(
url_for("gn_meta.get_export_pdf_dataset", id_dataset=ds.id_dataset)
response = self.client.post(
url_for("gn_meta.get_export_pdf_dataset", dataset=ds.id_dataset, json={})
)
assert response.status_code == Forbidden.code

set_logged_user_cookie(self.client, users["user"])

response = self.client.get(
url_for("gn_meta.get_export_pdf_dataset", id_dataset=ds.id_dataset)
print("LAST")
response = self.client.post(
url_for(
"gn_meta.get_export_pdf_dataset",
dataset=ds.id_dataset
# json chart is not required
)
)
assert response.status_code == 200

Expand Down
1 change: 0 additions & 1 deletion backend/geonature/utils/filemanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import logging
import datetime
import re
from io import BytesIO
from pathlib import Path

from werkzeug.utils import secure_filename
Expand Down
16 changes: 0 additions & 16 deletions frontend/src/app/app.module.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Angular core
import { BrowserModule } from '@angular/platform-browser';
import { NgModule, APP_INITIALIZER, Injector } from '@angular/core';
import { Observable } from "rxjs";

import {
HttpClientModule,
Expand Down Expand Up @@ -82,15 +81,6 @@ export function getModulesAndInitRouting(injector) {
};
}

function initializeAppFactory(httpClient: HttpClient): () => Observable<any> {
return () => httpClient.get("https://dummyjson.com/products")
.pipe(
tap(user => {
console.log(user);
return user
})
);
}
@NgModule({
imports: [
BrowserModule,
Expand Down Expand Up @@ -143,12 +133,6 @@ function initializeAppFactory(httpClient: HttpClient): () => Observable<any> {
{ provide: APP_CONFIG_TOKEN, useValue: AppConfig },
{ provide: HTTP_INTERCEPTORS, useClass: MyCustomInterceptor, multi: true },
{ provide: HTTP_INTERCEPTORS, useClass: UnauthorizedInterceptor, multi: true },
{
provide: APP_INITIALIZER,
useFactory: initializeAppFactory,
deps: [HttpClient],
multi: true
},
// { provide: APP_INITIALIZER, useFactory: get_cruved, deps: [CruvedStoreService], multi: true},
{
provide: APP_INITIALIZER,
Expand Down
7 changes: 3 additions & 4 deletions frontend/src/app/metadataModule/af/af-card.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,12 +143,11 @@ export class AfCardComponent implements OnInit {
}

getPdf() {
const dataUrl = this.chart ? this.chart.ctx['canvas'].toDataURL('image/svg') : '';
this._dfs.exportPDF(
dataUrl,
this.chart ? this.chart.toBase64Image() : '',
{ af: this.af.id_acquisition_framework },
`${AppConfig.API_ENDPOINT}/meta/acquisition_frameworks/export_pdf`,
"af"
)
'af'
);
}
}
6 changes: 3 additions & 3 deletions frontend/src/app/metadataModule/af/af-form.component.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Component, OnInit } from '@angular/core';
import { FormArray, FormGroup } from '@angular/forms';
import { HttpClient, HttpParams } from '@angular/common/http';
import { HttpClient } from '@angular/common/http';
import { ToastrService } from 'ngx-toastr';
import { Router, ActivatedRoute } from '@angular/router';
import { NgbDateParserFormatter } from '@ng-bootstrap/ng-bootstrap';
Expand Down Expand Up @@ -132,7 +132,7 @@ export class AfFormComponent implements OnInit {
null,
{ af: this.afFormS.acquisition_framework.getValue().id_acquisition_framework },
`${AppConfig.API_ENDPOINT}/meta/dataset/export_pdf`,
"af"
)
'af'
);
}
}

0 comments on commit 083e13f

Please sign in to comment.