2018-04-27 17:16:43 +00:00
|
|
|
from enum import Enum
|
2019-02-03 16:12:53 +00:00
|
|
|
from typing import Any
|
2018-04-27 17:16:43 +00:00
|
|
|
|
2018-05-11 16:58:48 +00:00
|
|
|
from marshmallow import post_load
|
2018-10-05 15:13:23 +00:00
|
|
|
from marshmallow.fields import DateTime, List, String
|
2019-02-03 16:12:53 +00:00
|
|
|
from marshmallow.schema import SchemaMeta
|
2018-10-05 15:13:23 +00:00
|
|
|
from teal.marshmallow import URL
|
2018-09-07 10:38:02 +00:00
|
|
|
from teal.resource import Schema
|
2018-04-27 17:16:43 +00:00
|
|
|
|
2018-06-12 14:50:05 +00:00
|
|
|
from ereuse_devicehub.resources import models as m
|
2018-04-27 17:16:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
class UnitCodes(Enum):
|
|
|
|
mbyte = '4L'
|
|
|
|
mbps = 'E20'
|
|
|
|
mhz = 'MHZ'
|
|
|
|
gbyte = 'E34'
|
|
|
|
ghz = 'A86'
|
|
|
|
bit = 'A99'
|
|
|
|
kgm = 'KGM'
|
|
|
|
m = 'MTR'
|
|
|
|
|
2019-02-03 16:12:53 +00:00
|
|
|
def __str__(self):
|
|
|
|
return self.name
|
|
|
|
|
|
|
|
|
|
|
|
# The following SchemaMeta modifications allow us to generate
|
|
|
|
# documentation using our directive. This is their only purpose.
|
|
|
|
# Marshmallow's meta class removes variables from our defined
|
|
|
|
# classes, so we put some home made proxies in order to intercept
|
|
|
|
# those values and safe them in our classes.
|
|
|
|
# What we do is:
|
|
|
|
# 1. Make our ``Meta`` class be the superclass of Marshmallow's
|
|
|
|
# SchemaMeta and provide a new that stores in class, so we
|
|
|
|
# can save some vars.
|
|
|
|
# 2. Substitute SchemaMeta.get_declared_fields with our own method
|
|
|
|
# that saves more variables.
|
|
|
|
# Then the directive in our docs/config.py file reads these variables
|
|
|
|
# generating the documentation.
|
|
|
|
|
|
|
|
class Meta(type):
|
|
|
|
|
|
|
|
def __new__(cls, *args, **kw) -> Any:
|
|
|
|
base_name = args[1][0].__name__
|
|
|
|
y = super().__new__(cls, *args, **kw)
|
|
|
|
y._base_class = base_name
|
|
|
|
return y
|
|
|
|
|
|
|
|
|
|
|
|
SchemaMeta.__bases__ = Meta,
|
|
|
|
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def get_declared_fields(mcs, klass, cls_fields, inherited_fields, dict_cls):
|
|
|
|
klass._own = cls_fields
|
|
|
|
klass._inherited = inherited_fields
|
|
|
|
return dict_cls(inherited_fields + cls_fields)
|
|
|
|
|
|
|
|
|
|
|
|
SchemaMeta.get_declared_fields = get_declared_fields
|
|
|
|
|
|
|
|
_type_description = """The name of the type of Thing,
|
|
|
|
like "Device" or "Receive". This is the same as JSON-LD ``@type``.
|
|
|
|
|
|
|
|
This field is required when submitting values
|
|
|
|
so Devicehub knows the type of object. Devicehub always returns this
|
|
|
|
value.
|
|
|
|
"""
|
|
|
|
|
2018-04-27 17:16:43 +00:00
|
|
|
|
|
|
|
class Thing(Schema):
|
2019-02-03 16:12:53 +00:00
|
|
|
type = String(description=_type_description)
|
|
|
|
same_as = List(URL(dump_only=True),
|
|
|
|
dump_only=True,
|
|
|
|
data_key='sameAs')
|
2018-10-23 13:37:37 +00:00
|
|
|
updated = DateTime('iso', dump_only=True, description=m.Thing.updated.comment)
|
|
|
|
created = DateTime('iso', dump_only=True, description=m.Thing.created.comment)
|
2018-05-11 16:58:48 +00:00
|
|
|
|
|
|
|
@post_load
|
|
|
|
def remove_type(self, data: dict):
|
|
|
|
data.pop('type', None)
|