-
Notifications
You must be signed in to change notification settings - Fork 102
/
routes.py
134 lines (101 loc) · 3.31 KB
/
routes.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
"""
Route permettant de manipuler les fichiers
contenus dans gn_media
"""
import json
from flask import Blueprint, request, current_app, redirect, jsonify
from werkzeug.exceptions import NotFound
from geonature.core.gn_commons.repositories import TMediaRepository, TMediumRepository
from geonature.core.gn_commons.models import TMedias
from geonature.utils.env import DB
from utils_flask_sqla.response import json_resp, json_resp_accept_empty_list
from geonature.utils.errors import (
GeoNatureError,
GeonatureApiError,
)
from ..routes import routes
@routes.route("/medias/<string:uuid_attached_row>", methods=["GET"])
@json_resp_accept_empty_list
def get_medias(uuid_attached_row):
"""
Retourne des medias
.. :quickref: Commons;
"""
res = DB.session.query(TMedias).filter(TMedias.uuid_attached_row == uuid_attached_row).all()
return [r.as_dict() for r in (res or [])]
@routes.route("/media/<int:id_media>", methods=["GET"])
def get_media(id_media):
"""
Retourne un media
.. :quickref: Commons;
"""
m = TMediaRepository(id_media=id_media).media
if not m:
raise NotFound
return jsonify(m.as_dict())
@routes.route("/media", methods=["POST", "PUT"])
@routes.route("/media/<int:id_media>", methods=["POST", "PUT"])
@json_resp
def insert_or_update_media(id_media=None):
"""
Insertion ou mise à jour d'un média
avec prise en compte des fichiers joints
.. :quickref: Commons;
"""
# gestion des parametres de route
if request.files:
file = request.files["file"]
else:
file = None
data = {}
# Useful ?
if request.form:
formData = dict(request.form)
for key in formData:
data[key] = formData[key]
if data[key] in ["null", "undefined"]:
data[key] = None
if isinstance(data[key], list):
data[key] = data[key][0]
if (
key in ["id_table_location", "id_nomenclature_media_type", "id_media"]
and data[key] is not None
):
data[key] = int(data[key])
if data[key] == "true":
data[key] = True
if data[key] == "false":
data[key] = False
else:
data = request.get_json(silent=True)
try:
m = TMediaRepository(data=data, file=file, id_media=id_media).create_or_update_media()
except GeoNatureError as e:
return str(e), 400
TMediumRepository.sync_medias()
return m.as_dict()
@routes.route("/media/<int:id_media>", methods=["DELETE"])
@json_resp
def delete_media(id_media):
"""
Suppression d'un media
.. :quickref: Commons;
"""
TMediaRepository(id_media=id_media).delete()
TMediumRepository.sync_medias()
return {"resp": "media {} deleted".format(id_media)}
@routes.route("/media/thumbnails/<int:id_media>/<int:size>", methods=["GET"])
def get_media_thumb(id_media, size):
"""
Retourne le thumbnail d'un media
.. :quickref: Commons;
"""
media_repo = TMediaRepository(id_media=id_media)
m = media_repo.media
if not m:
return {"msg": "Media introuvable"}, 404
try:
url_thumb = media_repo.get_thumbnail_url(size)
except GeoNatureError as e:
return {"msg": str(e)}, 500
return redirect(url_thumb)