forked from virtool/virtool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.py
153 lines (111 loc) · 3.45 KB
/
api.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import virtool.http.routes
import virtool.utils
import virtool.validators
import virtool.labels.checks
import virtool.db.utils
from virtool.api.response import bad_request, json_response, no_content, not_found
routes = virtool.http.routes.Routes()
@routes.get("/api/labels")
async def find(req):
"""
Get a list of all label documents in the database.
"""
db = req.app["db"]
document = db.labels.find()
return json_response([virtool.utils.base_processor(d) async for d in document])
@routes.get("/api/labels/{label_id}")
async def get(req):
"""
Get a complete label document.
"""
document = await req.app["db"].labels.find_one(req.match_info["label_id"])
if not document:
return not_found()
return json_response(virtool.utils.base_processor(document))
@routes.post("/api/labels", schema={
"name": {
"type": "string",
"coerce": virtool.validators.strip,
"required": True,
"empty": False
},
"color": {
"type": "string",
"coerce": virtool.validators.strip,
},
"description": {
"type": "string",
"coerce": virtool.validators.strip,
"default": ""
}
})
async def create(req):
"""
Add a new label to the labels database.
"""
db = req.app["db"]
data = req["data"]
valid_color = await virtool.labels.checks.check_hex_color(req)
if not valid_color:
return bad_request("This is not a valid Hexadecimal color")
name_exist = await db.labels.count_documents({'name': data['name']})
if name_exist:
return bad_request("Label name already exists")
label_id = await virtool.db.utils.get_new_id(db.labels)
document = {
"_id": label_id,
"name": data["name"],
"color": data["color"],
"description": data["description"]
}
await db.labels.insert_one(document)
headers = {
"Location": "/api/labels/" + label_id
}
return json_response(virtool.utils.base_processor(document), status=201, headers=headers)
@routes.patch("/api/labels/{label_id}", schema={
"name": {
"type": "string",
"coerce": virtool.validators.strip,
},
"color": {
"type": "string",
"coerce": virtool.validators.strip,
},
"description": {
"type": "string",
"coerce": virtool.validators.strip,
}
})
async def edit(req):
"""
Edit an existing label.
"""
db = req.app["db"]
data = req["data"]
label_id = req.match_info["label_id"]
if data["name"]:
name_exist = await db.labels.count_documents({"_id": {"$ne": label_id}, "name": data["name"]})
if name_exist:
return bad_request("Label name already exists")
if data["color"]:
valid_color = await virtool.labels.checks.check_hex_color(req)
if not valid_color:
return bad_request("This is not a valid Hexadecimal color")
document = await db.labels.find_one_and_update({"_id": label_id}, {
"$set": data
})
if document is None:
return not_found()
return json_response(virtool.utils.base_processor(document))
@routes.delete("/api/labels/{label_id}")
async def remove(req):
"""
Remove a label.
"""
db = req.app["db"]
label_id = req.match_info["label_id"]
delete_result = await db.labels.delete_one({"_id": label_id})
if delete_result.deleted_count == 0:
return not_found()
return no_content()