-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathutils.py
71 lines (54 loc) · 1.74 KB
/
utils.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
"""
Utility module for Flask resource handlers and models.
"""
import dotenv
import os
from flask import Response
dotenv.load()
IMAGE_FOLDER = 'images'
def get_project_root():
return os.path.dirname(__file__)
def get_image_folder():
return os.path.join(get_project_root(), IMAGE_FOLDER)
def save_image(file, picture_uuid, extension):
"""
Create an image folder if not exist and then save in the
folder the image passed with its extension
"""
if not os.path.exists(get_image_folder()):
os.makedirs(get_image_folder())
file.save(image_fullpath(picture_uuid, extension))
def remove_image(picture_uuid, extension):
"""
Remove a specified picture by picture_uuid from folder
"""
if os.path.isdir(get_image_folder()) and os.path.isfile(
image_fullpath(picture_uuid, extension)
):
os.remove(image_fullpath(picture_uuid, extension))
# TODO log in case file or folder not found
def image_fullpath(picture_uuid, extension):
"""
Return a path for the image in the image folder,
with picture_uuid and extension
"""
return os.path.join(
get_image_folder(),
'{}.{}'.format(str(picture_uuid), extension))
def generate_response(data, status, mimetype='application/vnd.api+json'):
"""
Given a resource model that extends from `BaseModel` generate a Reponse
object to be returned from the application endpoints'
"""
return Response(
response=data,
status=status,
mimetype=mimetype
)
def non_empty_str(val, name):
"""
Check if a string is empty. If not, raise a ValueError exception.
"""
if not str(val).strip():
raise ValueError('The argument {} is not empty'.format(name))
return str(val)