1
- from flask_restplus import Namespace , Resource , abort , inputs
1
+ from flask_restplus import Namespace , Resource , abort , inputs , fields
2
2
from .projects_model import project_dao
3
3
from time_tracker_api .errors import MissingResource
4
4
5
5
ns = Namespace ('projects' , description = 'API for projects (clients)' )
6
6
7
- @ns .route ('/' )
7
+ # Project Model
8
+ project = ns .model ('Project' , {
9
+ 'name' : fields .String (
10
+ required = True ,
11
+ title = 'Name' ,
12
+ max_length = 50 ,
13
+ description = 'Name of the project'
14
+ ),
15
+ 'description' : fields .String (
16
+ title = 'Description' ,
17
+ description = 'Description about the project'
18
+ ),
19
+ 'type' : fields .String (
20
+ required = True ,
21
+ title = 'Type' ,
22
+ max_length = 30 ,
23
+ description = 'If it is `Costumer`, `Training` or other type' ,
24
+ ),
25
+ })
26
+
27
+
28
+ project_response = ns .inherit ('ProjectResponse' , project , {
29
+ 'id' : fields .String (
30
+ readOnly = True ,
31
+ required = True ,
32
+ title = 'Identifier' ,
33
+ description = 'The unique identifier' ,
34
+ ),
35
+ 'created_at' : fields .Date (
36
+ readOnly = True ,
37
+ title = 'Created' ,
38
+ description = 'Date of creation'
39
+ ),
40
+ 'tenant_id' : fields .String (
41
+ readOnly = True ,
42
+ title = 'Tenant' ,
43
+ max_length = 64 ,
44
+ description = 'The tenant this belongs to' ,
45
+ ),
46
+ })
47
+
48
+
49
+ @ns .route ('' )
8
50
class Projects (Resource ):
9
51
@ns .doc ('list_projects' )
52
+ @ns .marshal_list_with (project_response , code = 200 )
10
53
def get (self ):
11
54
"""List all projects"""
12
55
return project_dao .get_all (), 200
13
56
14
57
@ns .doc ('create_project' )
58
+ @ns .expect (project )
59
+ @ns .marshal_with (project_response , code = 201 )
15
60
def post (self ):
16
61
"""Create a project"""
17
62
return project_dao .create (ns .payload ), 201
18
63
64
+ # TODO : fix, this parser is for a field that is not being used.
19
65
project_update_parser = ns .parser ()
20
66
project_update_parser .add_argument ('active' ,
21
67
type = inputs .boolean ,
@@ -29,24 +75,14 @@ def post(self):
29
75
@ns .param ('uid' , 'The project identifier' )
30
76
class Project (Resource ):
31
77
@ns .doc ('get_project' )
78
+ @ns .marshal_with (project_response )
32
79
def get (self , uid ):
33
80
"""Retrieve a project"""
34
81
return project_dao .get (uid )
35
82
36
- @ns .doc ('delete_project' )
37
- @ns .response (204 , 'Project deleted' )
38
- def delete (self , uid ):
39
- """Deletes a project"""
40
- project_dao .delete (uid )
41
- return None , 204
42
-
43
- @ns .doc ('put_project' )
44
- def put (self , uid ):
45
- """Create or replace a project"""
46
- return project_dao .update (uid , ns .payload )
47
-
48
83
@ns .doc ('update_project_status' )
49
84
@ns .param ('uid' , 'The project identifier' )
85
+ @ns .expect (project )
50
86
@ns .response (204 , 'State of the project successfully updated' )
51
87
def post (self , uid ):
52
88
"""Updates a project using form data"""
@@ -57,3 +93,17 @@ def post(self, uid):
57
93
abort (code = 400 )
58
94
except MissingResource as e :
59
95
abort (message = str (e ), code = 404 )
96
+
97
+ @ns .doc ('put_project' )
98
+ @ns .expect (project )
99
+ @ns .marshal_with (project_response )
100
+ def put (self , uid ):
101
+ """Create or replace a project"""
102
+ return project_dao .update (uid , ns .payload )
103
+
104
+ @ns .doc ('delete_project' )
105
+ @ns .response (204 , 'Project deleted successfully' )
106
+ def delete (self , uid ):
107
+ """Deletes a project"""
108
+ project_dao .delete (uid )
109
+ return None , 204
0 commit comments