Merged in enhancement/reponse-dates-standardization (pull request #49)

Add created_at and modified_at timestamps to all responses
develop
Ensar Sarajcic 2018-11-03 14:55:02 +00:00
commit 2b34466589
4 changed files with 23 additions and 12 deletions

View File

@ -9,11 +9,11 @@ from app.accounts.tasks import send_email_task
from app.api.auth_protection import ProtectedResource from app.api.auth_protection import ProtectedResource
from app.api.permission_protection import (requires_permission, from app.api.permission_protection import (requires_permission,
valid_permissions) valid_permissions)
from app.api.schemas import BaseResourceSchema from app.api.schemas import BaseTimestampedResourceSchema
from flask import current_app as app from flask import current_app as app
class UserSchema(BaseResourceSchema): class UserSchema(BaseTimestampedResourceSchema):
username = fields.Str(required=True) username = fields.Str(required=True)
email = fields.Email(required=True) email = fields.Email(required=True)
password = fields.Str(required=True, load_only=True) password = fields.Str(required=True, load_only=True)
@ -27,7 +27,7 @@ def validate_role_permissions(permissions_list):
return set(permissions_list).issubset(valid_permissions) return set(permissions_list).issubset(valid_permissions)
class RoleSchema(BaseResourceSchema): class RoleSchema(BaseTimestampedResourceSchema):
id = fields.Integer(required=True, location='json') id = fields.Integer(required=True, location='json')
display_name = fields.String(required=True, location='json') display_name = fields.String(required=True, location='json')
permissions = fields.List(fields.String, required=True, permissions = fields.List(fields.String, required=True,

View File

@ -6,10 +6,12 @@ from flasgger import swag_from
import app.dashboards.api as dashboard import app.dashboards.api as dashboard
import app.devices.api as device import app.devices.api as device
from app.api.auth_protection import ProtectedResource from app.api.auth_protection import ProtectedResource
from app.api.schemas import BaseResourceSchema from app.api.schemas import (BaseResourceSchema,
BaseTimestampedSchema,
BaseTimestampedResourceSchema)
class BasicDashboardWidgetSchema(Schema): class BasicDashboardWidgetSchema(BaseTimestampedSchema):
id = fields.Integer(dump_only=True) id = fields.Integer(dump_only=True)
device_id = fields.Integer(required=True) device_id = fields.Integer(required=True)
name = fields.String(required=True) name = fields.String(required=True)
@ -25,7 +27,7 @@ class DashboardWidgetSchema(BaseResourceSchema, BasicDashboardWidgetSchema):
pass pass
class DashboardSchema(BaseResourceSchema): class DashboardSchema(BaseTimestampedResourceSchema):
id = fields.Integer(dump_only=True) id = fields.Integer(dump_only=True)
active = fields.Boolean(required=False) active = fields.Boolean(required=False)
dashboard_data = fields.Raw(required=True) dashboard_data = fields.Raw(required=True)

View File

@ -6,11 +6,13 @@ from flask import g, request, redirect
from app.api.blueprint import api from app.api.blueprint import api
import app.devices.api as devices import app.devices.api as devices
from app.api.auth_protection import ProtectedResource from app.api.auth_protection import ProtectedResource
from app.api.schemas import BaseResourceSchema from app.api.schemas import (BaseResourceSchema,
BaseTimestampedSchema,
BaseTimestampedResourceSchema)
from flask import current_app as app from flask import current_app as app
class BasicDeviceTypeSchema(Schema): class BasicDeviceTypeSchema(BaseTimestampedSchema):
id = fields.Integer(dump_only=True) id = fields.Integer(dump_only=True)
name = fields.Str(required=True) name = fields.Str(required=True)
@ -19,13 +21,11 @@ class DeviceTypeSchema(BaseResourceSchema, BasicDeviceTypeSchema):
pass pass
class DeviceSchema(BaseResourceSchema): class DeviceSchema(BaseTimestampedResourceSchema):
id = fields.Integer(dump_only=True) id = fields.Integer(dump_only=True)
name = fields.Str(required=True) name = fields.Str(required=True)
device_type = fields.Nested(BasicDeviceTypeSchema, dump_only=True) device_type = fields.Nested(BasicDeviceTypeSchema, dump_only=True)
device_type_id = fields.Integer(load_only=True, missing=1) device_type_id = fields.Integer(load_only=True, missing=1)
created_at = fields.DateTime(dump_only=True)
modified_at = fields.DateTime(dump_only=True)
class DeviceWithConfigurationSchema(DeviceSchema): class DeviceWithConfigurationSchema(DeviceSchema):

View File

@ -1,7 +1,16 @@
from marshmallow import Schema, post_dump from marshmallow import Schema, post_dump, fields
class BaseResourceSchema(Schema): class BaseResourceSchema(Schema):
@post_dump(pass_many=True) @post_dump(pass_many=True)
def wrap_with_envelope(self, data, many): def wrap_with_envelope(self, data, many):
return {'content': data} return {'content': data}
class BaseTimestampedSchema(Schema):
created_at = fields.DateTime(dump_only=True)
modified_at = fields.DateTime(dump_only=True)
class BaseTimestampedResourceSchema(BaseResourceSchema, BaseTimestampedSchema):
pass