-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathproject.py
executable file
·154 lines (125 loc) · 3.83 KB
/
project.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
153
154
from flask import abort, Blueprint, jsonify, request
from flask_jwt_extended import jwt_required
from auth import requires_auth
from custom_types import ApiResponse
from models.project import ProjectModel
from resources.message import (
CREATED,
DELETED,
ERROR_404,
ERROR_404_LIST,
ERROR_409,
MODIFIED,
)
from schemas.project import ProjectSchema
projects = Blueprint("projects", __name__)
project_schema = ProjectSchema()
project_list_schema = ProjectSchema(many=True)
@projects.route("/<int:project_id>")
def get_project(project_id: int) -> ApiResponse:
project = ProjectModel.find_by_id(project_id)
if not project:
abort(
404,
description=ERROR_404.format("Project", "id", project_id),
)
return (
jsonify(
{
"project": project_schema.dump(project),
}
),
200,
)
@projects.route("", methods=["POST"])
@jwt_required()
@requires_auth("post:project")
def post_project() -> ApiResponse:
project_json = request.get_json()
if ProjectModel.find_by_title(project_json.get("title")):
abort(
409,
description=ERROR_409.format(
"Project",
"title",
project_json.get("title"),
),
)
project = project_schema.load(project_json)
project.save_to_db()
return (
jsonify(
{
"message": CREATED.format("Project"),
"project": project_schema.dump(project),
}
),
201,
)
@projects.route("/<int:project_id>", methods=["PUT"])
@jwt_required()
@requires_auth("post:project")
def put_project(project_id: int) -> ApiResponse:
project = ProjectModel.find_by_id(project_id)
if not project:
abort(
404,
description=ERROR_404.format("Project", "id", project_id),
)
project_json = request.get_json()
project_by_title = ProjectModel.find_by_title(project_json.get("title"))
if project_by_title and project_by_title.id != project_id:
abort(
409,
description=ERROR_409.format(
"Project",
"title",
project_json.get("title"),
),
)
project.start_date = project_json.get("start_date")
project.end_date = project_json.get("end_date")
project.title = project_json.get("title")
project.description = project_json.get("description")
project.goals = project_json.get("goals")
project.status = project_json.get("status")
project.admin_id = project_json.get("admin_id")
project.save_to_db()
return (
jsonify(
{
"message": MODIFIED.format("Project"),
"project": project_schema.dump(project),
}
),
200,
)
@projects.route("/<int:project_id>", methods=["DELETE"])
@jwt_required()
@requires_auth("post:project")
def delete_project(project_id: int) -> ApiResponse:
project = ProjectModel.find_by_id(project_id)
if not project:
abort(
404,
description=ERROR_404.format("Project", "id", project_id),
)
project.delete_from_db()
return (
jsonify(
{
"message": DELETED.format("project"),
"project": project_schema.dump(project),
}
),
200,
)
@projects.route("")
def get_projects() -> ApiResponse:
project_list = ProjectModel.find_all()
if not project_list:
abort(404, description=ERROR_404_LIST.format("projects"))
return (
jsonify({"projects": project_list_schema.dump(project_list)}),
200,
)