Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 64 additions & 14 deletions time_tracker_api/projects/projects_api.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,67 @@
from flask_restplus import Namespace, Resource, abort, inputs
from flask_restplus import Namespace, Resource, abort, inputs, fields
from .projects_model import project_dao
from time_tracker_api.errors import MissingResource

ns = Namespace('projects', description='API for projects (clients)')

@ns.route('/')
# Project Model
project = ns.model('Project', {
'name': fields.String(
required=True,
title='Name',
max_length=50,
description='Name of the project'
),
'description': fields.String(
title='Description',
description='Description about the project'
),
'type': fields.String(
required=True,
title='Type',
max_length=30,
description='If it is `Costumer`, `Training` or other type',
),
})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would give you the same recommendations as in #12 (review). Add the audit fields (tenant_id, created_at and created_by) and create a project_response to show those.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, let's move id to the response model ;)



project_response = ns.inherit('ProjectResponse', project, {
'id': fields.String(
readOnly=True,
required=True,
title='Identifier',
description='The unique identifier',
),
'created_at': fields.Date(
readOnly=True,
title='Created',
description='Date of creation'
),
'tenant_id': fields.String(
readOnly=True,
title='Tenant',
max_length=64,
description='The tenant this belongs to',
),
})


@ns.route('')
class Projects(Resource):
@ns.doc('list_projects')
@ns.marshal_list_with(project_response, code=200)
def get(self):
"""List all projects"""
return project_dao.get_all(), 200

@ns.doc('create_project')
@ns.expect(project)
@ns.marshal_with(project_response, code=201)
def post(self):
"""Create a project"""
return project_dao.create(ns.payload), 201

# TODO : fix, this parser is for a field that is not being used.
project_update_parser = ns.parser()
project_update_parser.add_argument('active',
type=inputs.boolean,
Expand All @@ -29,24 +75,14 @@ def post(self):
@ns.param('uid', 'The project identifier')
class Project(Resource):
@ns.doc('get_project')
@ns.marshal_with(project_response)
def get(self, uid):
"""Retrieve a project"""
return project_dao.get(uid)

@ns.doc('delete_project')
@ns.response(204, 'Project deleted')
def delete(self, uid):
"""Deletes a project"""
project_dao.delete(uid)
return None, 204

@ns.doc('put_project')
def put(self, uid):
"""Create or replace a project"""
return project_dao.update(uid, ns.payload)

@ns.doc('update_project_status')
@ns.param('uid', 'The project identifier')
@ns.expect(project)
@ns.response(204, 'State of the project successfully updated')
def post(self, uid):
"""Updates a project using form data"""
Expand All @@ -57,3 +93,17 @@ def post(self, uid):
abort(code=400)
except MissingResource as e:
abort(message=str(e), code=404)

@ns.doc('put_project')
@ns.expect(project)
@ns.marshal_with(project_response)
def put(self, uid):
"""Create or replace a project"""
return project_dao.update(uid, ns.payload)

@ns.doc('delete_project')
@ns.response(204, 'Project deleted successfully')
def delete(self, uid):
"""Deletes a project"""
project_dao.delete(uid)
return None, 204