Repository navigation
Expand file tree
/
Copy pathimage.py
More file actions
48 lines (43 loc) · 1.38 KB
/
Copy pathimage.py
File metadata and controls
48 lines (43 loc) · 1.38 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
from flask import make_response, abort, request
from config import db
from models import Image, image_schema, images_schema, Document
def read_all():
images = Image.query.all()
return images_schema.dump(images)
def read_all_document(document_id):
images = Image.query.filter_by(document_id=document_id).all()
return images_schema.dump(images)
def read_one(image_id):
image = Image.query.get(image_id)
if image:
return image_schema.dump(image)
else:
abort(
404,
f"Error: image with name = {image_id} not found"
)
def create(image):
document_id = image.get("document_id")
document = Document.query.get(document_id)
if document:
new_image = image_schema.load(image,
session=db.session)
document.image.append(new_image)
db.session.commit()
return image_schema.dump(new_image), 201
else:
abort(
404,
f"Error: document with ID = {document_id} not found"
)
def delete(image_id):
existing_image = Image.query.get(image_id)
if existing_image:
db.session.delete(existing_image)
db.session.commit()
return make_response(f"Success: image with ID = {image_id} deleted")
else:
abort(
404,
f"Error: image with ID = {image_id} not found"
)