forked from CircleCI-Public/sample-python-cfd
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimage_controller.py
More file actions
59 lines (42 loc) · 1.51 KB
/
image_controller.py
File metadata and controls
59 lines (42 loc) · 1.51 KB
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
import connexion
from openapi_server.models.error import Error # noqa: E501
from openapi_server.models.inline_response200 import InlineResponse200 # noqa: E501
from openapi_server.database import models
from sqlalchemy.exc import SQLAlchemyError
def add_image(): # noqa: E501
"""Add an image to the restaraunt
Creates an image. Duplicates are allowed. Returns the image id # noqa: E501
:param file_name:
:type file_name: str
:rtype: InlineResponse200
"""
uploaded_file = connexion.request.files["fileName"]
# save the file to the path and then save the path to the 'db'
try:
image = models.Image.add(uploaded_file.read())
return InlineResponse200(image_id=image.id)
except (SQLAlchemyError, TypeError):
models.db.session.rollback()
return Error(400), 400
def delete_image(image_id): # noqa: E501
"""Remove image
The imageId must exist # noqa: E501
:param image_id: The imageId to delete
:type image_id: int
:rtype: None
"""
try:
models.Image.delete_image(image_id)
except (SQLAlchemyError, TypeError):
models.db.session.rollback()
return Error(400), 400
def get_image(image_id): # noqa: E501
"""Get image
Returns the image as image/png # noqa: E501
:param image_id: The imageId of the image to retrieve
:type image_id: int
:rtype: file
"""
if not (image := models.Image.get_image(image_id)):
return Error(404, "image not found"), 404
return image.data