Skip to content

Commit

Permalink
Rework model schema subclass code
Browse files Browse the repository at this point in the history
  • Loading branch information
rbw committed Nov 13, 2020
1 parent dbe838e commit 4be1585
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 24 deletions.
17 changes: 10 additions & 7 deletions aiosnow/models/_base/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,6 @@

class BaseModelMeta(type):
def __new__(mcs, name: str, bases: tuple, attrs: dict) -> Any:
if "schema_cls" in attrs and attrs["schema_cls"]:
return super().__new__(mcs, name, bases, attrs)

fields = {}
base_members = {}

Expand All @@ -44,8 +41,14 @@ def __new__(mcs, name: str, bases: tuple, attrs: dict) -> Any:
if not isinstance(v, (BaseField, Nested, ModelSchemaMeta))
}
)
inherited_fields = getattr(base.schema_cls, "_declared_fields")
fields.update(inherited_fields)

if hasattr(base, "schema_cls"):
inherited_fields = getattr(base.schema_cls, "_declared_fields")
fields.update(inherited_fields)
else:
for k, v in base.__dict__.items():
if isinstance(v, (BaseField, Nested, ModelSchemaMeta)):
fields[k] = v

for k, v in attrs.items():
if isinstance(v, (BaseField, Nested, ModelSchemaMeta)):
Expand Down Expand Up @@ -103,11 +106,11 @@ async def request(self, method: str, *args: Any, **kwargs: Any) -> Response:

return response

async def _close_self(self):
async def _close_self(self) -> None:
self.log.debug(f"Closing session {self.session} of {self}")
await self.session.close()

async def _close_session(self):
async def _close_session(self) -> None:
await self._close_self()

async def __aenter__(self) -> BaseModel:
Expand Down
4 changes: 2 additions & 2 deletions aiosnow/models/attachment/schema.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from .._schema import ModelSchema, fields
from .._schema import fields


class AttachmentModelSchema(ModelSchema):
class AttachmentModelSchema:
"""Attachment API model schema"""

sys_id = fields.String(is_primary=True)
Expand Down
8 changes: 4 additions & 4 deletions aiosnow/models/table/declared/incident.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from aiosnow.models import ModelSchema, TableModel, fields
from aiosnow.models import TableModel, fields


class IncidentModelSchema(ModelSchema):
class IncidentModelSchema:
sys_id = fields.String(is_primary=True)
impact = fields.IntegerMap()
number = fields.String()
Expand Down Expand Up @@ -87,5 +87,5 @@ class IncidentModelSchema(ModelSchema):
category = fields.String()


class IncidentModel(TableModel):
schema_cls = IncidentModelSchema
class IncidentModel(TableModel, IncidentModelSchema):
"""Incident API Model"""
8 changes: 4 additions & 4 deletions aiosnow/models/table/declared/journal.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from aiosnow.models import ModelSchema, TableModel, fields
from aiosnow.models import TableModel, fields


class JournalModelSchema(ModelSchema):
class JournalModelSchema:
sys_id = fields.String(is_primary=True)
sys_created_on = fields.DateTime()
sys_created_by = fields.String()
Expand All @@ -12,5 +12,5 @@ class JournalModelSchema(ModelSchema):
sys_tags = fields.String()


class JournalModel(TableModel):
schema_cls = JournalModelSchema
class JournalModel(TableModel, JournalModelSchema):
"""Journal API Model"""
2 changes: 1 addition & 1 deletion docs/reference/models/attachment/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Attachment
==========

The *AttachmentModel* is typically used in context of another API,
e.g. via the :class:`~aiosnow.models.table.TableModel`, but can be used directly in a global context as well.
e.g. via a :class:`~aiosnow.models.table.TableModel` type, but can be used directly in a global context as well.

*Reference*

Expand Down
12 changes: 6 additions & 6 deletions docs/usage/model/declare.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Declare
-------

Model declaration is done using or or more :ref:`aiosnow.fields <fields-root>` assigned directly
to members of the Model class, or using a separate *ModelSchema* associated via the `model_cls` member.
to members of the Model class.

Model
*****
Expand All @@ -28,13 +28,13 @@ Assign fields directly on the *BaseModel* derived class.
ModelSchema
***********

Assign fields using a *ModelSchema* derived class.
Assign fields in separate class, which is then subclassed.

.. code-block:: python
from aiosnow import ModelSchema, TableModel, fields
from aiosnow import TableModel, fields
class IncidentModelSchema(ModelSchema):
class IncidentModelSchema:
sys_id = fields.String(is_primary=True)
number = fields.String()
impact = fields.IntegerMap()
Expand All @@ -44,5 +44,5 @@ Assign fields using a *ModelSchema* derived class.
made_sla = fields.Boolean()
class IncidentModel(TableModel):
schema_cls = IncidentModelSchema
class IncidentModel(TableModel, IncidentModelSchema):
"""Incident API Model"""

0 comments on commit 4be1585

Please sign in to comment.