|
| 1 | +from flask_restplus import fields, Resource, Namespace |
| 2 | + |
| 3 | +ns = Namespace('time-entries', description='API for time entries') |
| 4 | + |
| 5 | +# TimeEntry Model |
| 6 | +time_entry = ns.model('TimeEntry', { |
| 7 | + 'project_id': fields.String( |
| 8 | + required=True, |
| 9 | + title='Project', |
| 10 | + max_length=64, |
| 11 | + description='The id of the selected project', |
| 12 | + ), |
| 13 | + 'activity_id': fields.String( |
| 14 | + required=False, |
| 15 | + title='Activity', |
| 16 | + max_length=64, |
| 17 | + description='The id of the selected activity', |
| 18 | + ), |
| 19 | + 'technologies': fields.String( |
| 20 | + required=True, |
| 21 | + title='Technologies', |
| 22 | + max_length=64, |
| 23 | + description='Canonical names of the used technologies during this period', |
| 24 | + ), |
| 25 | + 'description': fields.String( |
| 26 | + title='Comments', |
| 27 | + description='Comments about the time entry' |
| 28 | + ), |
| 29 | + 'start_date': fields.DateTime( |
| 30 | + required=True, |
| 31 | + title='Start date', |
| 32 | + description='When the user started doing this activity', |
| 33 | + ), |
| 34 | + 'end_date': fields.DateTime( |
| 35 | + required=True, |
| 36 | + title='End date', |
| 37 | + description='When the user ended doing this activity', |
| 38 | + ), |
| 39 | +}) |
| 40 | + |
| 41 | +time_entry_response = ns.inherit('TimeEntryResponse', time_entry, { |
| 42 | + 'id': fields.String( |
| 43 | + readOnly=True, |
| 44 | + required=True, |
| 45 | + title='Identifier', |
| 46 | + description='The unique identifier', |
| 47 | + ), |
| 48 | + 'running': fields.Boolean( |
| 49 | + readOnly=True, |
| 50 | + title='Is it running?', |
| 51 | + description='Whether this time entry is currently running or not' |
| 52 | + ), |
| 53 | + 'created_at': fields.Date( |
| 54 | + readOnly=True, |
| 55 | + title='Created', |
| 56 | + description='Date of creation' |
| 57 | + ), |
| 58 | + 'created_by': fields.String( |
| 59 | + readOnly=True, |
| 60 | + title='Creator', |
| 61 | + max_length=64, |
| 62 | + description='User that created it', |
| 63 | + ), |
| 64 | + 'tenant_id': fields.String( |
| 65 | + readOnly=True, |
| 66 | + title='Tenant', |
| 67 | + max_length=64, |
| 68 | + description='The tenant this belongs to', |
| 69 | + ), |
| 70 | +}) |
| 71 | + |
| 72 | + |
| 73 | +@ns.route('') |
| 74 | +class TimeEntries(Resource): |
| 75 | + @ns.doc('list_time_entries') |
| 76 | + @ns.marshal_list_with(time_entry_response, code=200) |
| 77 | + def get(self): |
| 78 | + """List all available time entries""" |
| 79 | + return [] |
| 80 | + |
| 81 | + @ns.doc('create_time_entry') |
| 82 | + @ns.expect(time_entry) |
| 83 | + @ns.marshal_with(time_entry_response, code=201) |
| 84 | + @ns.response(400, 'Invalid format of the attributes of the time entry') |
| 85 | + def post(self): |
| 86 | + """Starts a time entry by creating it""" |
| 87 | + return ns.payload, 201 |
| 88 | + |
| 89 | + |
| 90 | +@ns.route('/<string:id>') |
| 91 | +@ns.response(404, 'Time entry not found') |
| 92 | +@ns.param('id', 'The unique identifier of the time entry') |
| 93 | +class TimeEntry(Resource): |
| 94 | + @ns.doc('get_time_entry') |
| 95 | + @ns.marshal_with(time_entry_response) |
| 96 | + def get(self, id): |
| 97 | + """Retrieve all the data of a single time entry""" |
| 98 | + return {} |
| 99 | + |
| 100 | + @ns.doc('delete_time_entry') |
| 101 | + @ns.response(204, 'The time entry was deleted successfully (No content is returned)') |
| 102 | + def delete(self, id): |
| 103 | + """Deletes a time entry""" |
| 104 | + return None, 204 |
| 105 | + |
| 106 | + @ns.doc('put_time_entry') |
| 107 | + @ns.response(400, 'Invalid format of the attributes of the time entry.') |
| 108 | + @ns.expect(time_entry) |
| 109 | + @ns.marshal_with(time_entry_response) |
| 110 | + def put(self, id): |
| 111 | + """Updates a time entry""" |
| 112 | + return ns.payload |
| 113 | + |
| 114 | + |
| 115 | +@ns.route('<string:id>/stop') |
| 116 | +@ns.response(404, 'Running time entry not found') |
| 117 | +@ns.param('id', 'The unique identifier of a running time entry') |
| 118 | +class StopTimeEntry(Resource): |
| 119 | + @ns.doc('stop_time_entry') |
| 120 | + @ns.response(204, 'The time entry was stopped successfully (No content is returned)') |
| 121 | + def post(self, id): |
| 122 | + """Stops a running time entry""" |
| 123 | + return None, 204 |
| 124 | + |
| 125 | + |
| 126 | +@ns.route('<string:id>/continue') |
| 127 | +@ns.response(404, 'Stopped time entry not found') |
| 128 | +@ns.param('id', 'The unique identifier of a stopped time entry') |
| 129 | +class ContinueTimeEntry(Resource): |
| 130 | + @ns.doc('continue_time_entry') |
| 131 | + @ns.response(204, 'The time entry was continued successfully (No content is returned)') |
| 132 | + def post(self, id): |
| 133 | + """Restart an stopped time entry""" |
| 134 | + return None, 204 |
0 commit comments