1- from flask_restplus import Namespace , Resource , abort , inputs
1+ from flask_restplus import Namespace , Resource , abort , inputs , fields
22from .projects_model import project_dao
33from time_tracker_api .errors import MissingResource
44
55ns = Namespace ('projects' , description = 'API for projects (clients)' )
66
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 ('' )
850class Projects (Resource ):
951 @ns .doc ('list_projects' )
52+ @ns .marshal_list_with (project_response , code = 200 )
1053 def get (self ):
1154 """List all projects"""
1255 return project_dao .get_all (), 200
1356
1457 @ns .doc ('create_project' )
58+ @ns .expect (project )
59+ @ns .marshal_with (project_response , code = 201 )
1560 def post (self ):
1661 """Create a project"""
1762 return project_dao .create (ns .payload ), 201
1863
64+ # TODO : fix, this parser is for a field that is not being used.
1965project_update_parser = ns .parser ()
2066project_update_parser .add_argument ('active' ,
2167 type = inputs .boolean ,
@@ -29,24 +75,14 @@ def post(self):
2975@ns .param ('uid' , 'The project identifier' )
3076class Project (Resource ):
3177 @ns .doc ('get_project' )
78+ @ns .marshal_with (project_response )
3279 def get (self , uid ):
3380 """Retrieve a project"""
3481 return project_dao .get (uid )
3582
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-
4883 @ns .doc ('update_project_status' )
4984 @ns .param ('uid' , 'The project identifier' )
85+ @ns .expect (project )
5086 @ns .response (204 , 'State of the project successfully updated' )
5187 def post (self , uid ):
5288 """Updates a project using form data"""
@@ -57,3 +93,17 @@ def post(self, uid):
5793 abort (code = 400 )
5894 except MissingResource as e :
5995 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