Skip to content

Commit 4893f2e

Browse files
committed
Fixes #18 - create common structure and use it across models
1 parent 5153909 commit 4893f2e

File tree

5 files changed

+64
-75
lines changed

5 files changed

+64
-75
lines changed

time_tracker_api/activities/activities_api.py

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from flask_restplus import fields, Resource, Namespace
2+
from time_tracker_api.api import audit_fields
23

34
ns = Namespace('activities', description='API for activities')
45

@@ -16,31 +17,21 @@
1617
),
1718
})
1819

19-
activity_response = ns.inherit('ActivityResponse', activity, {
20+
activity_response_fields = {
2021
'id': fields.String(
2122
readOnly=True,
2223
required=True,
2324
title='Identifier',
2425
description='The unique identifier',
25-
),
26-
'created_at': fields.Date(
27-
readOnly=True,
28-
title='Created',
29-
description='Date of creation'
30-
),
31-
'created_by': fields.String(
32-
readOnly=True,
33-
title='Creator',
34-
max_length=64,
35-
description='User that created it',
36-
),
37-
'tenant_id': fields.String(
38-
readOnly=True,
39-
title='Tenant',
40-
max_length=64,
41-
description='The tenant this belongs to',
42-
),
43-
})
26+
)
27+
}
28+
activity_response_fields.update(audit_fields)
29+
30+
activity_response = ns.inherit(
31+
'ActivityResponse',
32+
activity,
33+
activity_response_fields
34+
)
4435

4536

4637
@ns.route('')

time_tracker_api/api.py

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,29 @@
1-
2-
from flask_restplus import Api
1+
from flask_restplus import Api, fields
32

43
api = Api(version='1.0.1', title="TimeTracker API",
54
description="API for the TimeTracker project")
65

6+
# Common models structure
7+
audit_fields = {
8+
'created_at': fields.Date(
9+
readOnly=True,
10+
title='Created',
11+
description='Date of creation'
12+
),
13+
'tenant_id': fields.String(
14+
readOnly=True,
15+
title='Tenant',
16+
max_length=64,
17+
description='The tenant this belongs to',
18+
),
19+
'created_by': fields.String(
20+
readOnly=True,
21+
title='Creator',
22+
max_length=64,
23+
description='User that created it',
24+
),
25+
}
26+
727
# APIs
828
from time_tracker_api.projects import projects_api
929
api.add_namespace(projects_api.ns)

time_tracker_api/projects/projects_api.py

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from flask_restplus import Namespace, Resource, abort, inputs, fields
22
from .projects_model import project_dao
33
from time_tracker_api.errors import MissingResource
4+
from time_tracker_api.api import audit_fields
45

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

@@ -24,26 +25,21 @@
2425
),
2526
})
2627

27-
28-
project_response = ns.inherit('ProjectResponse', project, {
28+
project_response_fields = {
2929
'id': fields.String(
3030
readOnly=True,
3131
required=True,
3232
title='Identifier',
3333
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-
})
34+
)
35+
}
36+
project_response_fields.update(audit_fields)
37+
38+
project_response = ns.inherit(
39+
'ProjectResponse',
40+
project,
41+
project_response_fields
42+
)
4743

4844

4945
@ns.route('')

time_tracker_api/technologies/technologies_api.py

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from flask_restplus import Namespace, Resource, fields
2+
from time_tracker_api.api import audit_fields
23

34
ns = Namespace('technologies', description='API for technologies used')
45

@@ -12,31 +13,21 @@
1213
),
1314
})
1415

15-
technology_response = ns.inherit('TechnologyResponse', technology, {
16+
technology_response_fields = {
1617
'id': fields.String(
1718
readOnly=True,
1819
required=True,
1920
title='Identifier',
2021
description='The unique identifier',
2122
),
22-
'created_at': fields.Date(
23-
readOnly=True,
24-
title='Created',
25-
description='Date of creation'
26-
),
27-
'created_by': fields.String(
28-
readOnly=True,
29-
title='Creator',
30-
max_length=64,
31-
description='User that created it',
32-
),
33-
'tenant_id': fields.String(
34-
readOnly=True,
35-
title='Tenant',
36-
max_length=64,
37-
description='The tenant this belongs to',
38-
),
39-
})
23+
}
24+
technology_response_fields.update(audit_fields)
25+
26+
technology_response = ns.inherit(
27+
'TechnologyResponse',
28+
technology,
29+
technology_response_fields
30+
)
4031

4132

4233
@ns.route('')

time_tracker_api/time_entries/time_entry_api.py

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from flask_restplus import fields, Resource, Namespace
2+
from time_tracker_api.api import audit_fields
23

34
ns = Namespace('time-entries', description='API for time entries')
45

@@ -38,7 +39,7 @@
3839
),
3940
})
4041

41-
time_entry_response = ns.inherit('TimeEntryResponse', time_entry, {
42+
time_entry_response_fields = {
4243
'id': fields.String(
4344
readOnly=True,
4445
required=True,
@@ -50,24 +51,14 @@
5051
title='Is it running?',
5152
description='Whether this time entry is currently running or not'
5253
),
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-
})
54+
}
55+
time_entry_response_fields.update(audit_fields)
56+
57+
time_entry_response = ns.inherit(
58+
'TimeEntryResponse',
59+
time_entry,
60+
time_entry_response_fields,
61+
)
7162

7263

7364
@ns.route('')

0 commit comments

Comments
 (0)