Source code for tests.schemas
"""
tests.schemas
~~~~~~~~~~~~~
Schemas used for test purposes.
"""
# :copyright: (c) 2016-2020 by Nicholas Repole and contributors.
# See AUTHORS for more details.
# :license: MIT - See LICENSE for more details.
from drowsy.convert import CamelModelResourceConverter
from drowsy.fields import Relationship
from drowsy.permissions import DisallowAllOpPermissions
from drowsy.schema import ModelResourceSchema
from .models import (
Album, Artist, CompositeOne, CompositeMany, CompositeNode, Customer,
Employee, Genre, Invoice, InvoiceLine, MediaType, Node, Playlist, Track,
TrackStats
)
from marshmallow import fields
from marshmallow_sqlalchemy.schema import SQLAlchemyAutoSchema
[docs]
class InvoiceLineSchema(ModelResourceSchema):
[docs]
class InvoiceSchema(ModelResourceSchema):
invoice_lines = fields.Nested("InvoiceLineSchema", many=True)
[docs]
class EmployeeSchema(ModelResourceSchema):
[docs]
class CustomerSchema(ModelResourceSchema):
phone = fields.String(load_only=True)
[docs]
class PlaylistSchema(ModelResourceSchema):
[docs]
class GenreSchema(ModelResourceSchema):
[docs]
class TrackSchema(ModelResourceSchema):
[docs]
def check_permission(self, data, instance, action):
"""Checks if this action is permissible to attempt.
:param dict data: The user supplied data to be deserialized.
:param instance: A pre-existing instance the data is to be
deserialized into. Should be ``None`` if not updating an
existing object.
:param str action: Either ``"create"``, ``"update"``, or
``"delete"``.
:return: None
:raise PermissionValidationError: If the action being taken is
not allowed.
"""
if (data.get("track_id") == 1 or (instance is not None and
instance.track_id == 1) and
data.get("name") == "Denied"):
raise self.make_error("permission_denied")
[docs]
class TrackPermissionsSchema(ModelResourceSchema):
album = Relationship(
"AlbumResource", many=False, permissions_cls=DisallowAllOpPermissions)
[docs]
class TrackStatsSchema(ModelResourceSchema):
[docs]
class AlbumSchema(ModelResourceSchema):
[docs]
def check_permission(self, data, instance, action):
"""Checks if this action is permissible to attempt.
:param dict data: The user supplied data to be deserialized.
:param instance: A pre-existing instance the data is to be
deserialized into. Should be ``None`` if not updating an
existing object.
:param str action: Either ``"create"``, ``"update"``, or
``"delete"``.
:return: None
:raise PermissionValidationError: If the action being taken is
not allowed.
"""
if data.get("title") == "Denied" or (
instance is not None and instance.album_id == 340 and
action == "delete"):
raise self.make_error("permission_denied")
[docs]
class ArtistSchema(ModelResourceSchema):
albums = Relationship(
"AlbumResource", many=True, permissions_cls=DisallowAllOpPermissions)
[docs]
class NodeSchema(ModelResourceSchema):
[docs]
class CompositeNodeSchema(ModelResourceSchema):
[docs]
class CompositeOneSchema(ModelResourceSchema):
[docs]
class CompositeManySchema(ModelResourceSchema):
[docs]
class TestCamelModelResourceConverter(CamelModelResourceConverter):
"""Convert a model's fields for use in a `ModelResourceSchema`."""
def _add_relationship_kwargs(self, kwargs, prop):
"""Update the provided kwargs based on the relationship given.
:param dict kwargs: A dictionary of kwargs to pass to the
eventual field constructor. This argument is modified
in place.
:param prop: A relationship property used to determine how
``kwargs`` should be updated.
:type prop:
:class:`~sqlalchemy.orm.properties.RelationshipProperty`
"""
super(TestCamelModelResourceConverter, self)._add_relationship_kwargs(
kwargs, prop)
kwargs.update({
"nested": prop.mapper.class_.__name__ + 'CamelResource'
})
[docs]
class InvoiceLineCamelSchema(ModelResourceSchema):
[docs]
class InvoiceCamelSchema(ModelResourceSchema):
invoice_lines = fields.Nested(
"InvoiceLineCamelSchema", many=True, data_key="invoiceLines")
[docs]
class EmployeeCamelSchema(ModelResourceSchema):
[docs]
class CustomerCamelSchema(ModelResourceSchema):
phone = fields.String(load_only=True, data_key="phone")
[docs]
class PlaylistCamelSchema(ModelResourceSchema):
[docs]
class GenreCamelSchema(ModelResourceSchema):
[docs]
class TrackCamelSchema(ModelResourceSchema):
[docs]
class AlbumCamelSchema(ModelResourceSchema):
[docs]
class ArtistCamelSchema(ModelResourceSchema):
[docs]
class NodeCamelSchema(ModelResourceSchema):
[docs]
class CompositeNodeCamelSchema(ModelResourceSchema):
[docs]
class CompositeOneCamelSchema(ModelResourceSchema):
[docs]
class CompositeManyCamelSchema(ModelResourceSchema):
[docs]
class MsAlbumSchema(SQLAlchemyAutoSchema):
album_id = fields.Integer(data_key="albumId")
[docs]
class AlbumBadIdKeysSchema(ModelResourceSchema):