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 Meta: model = InvoiceLine include_relationships = True
[docs] class InvoiceSchema(ModelResourceSchema):
[docs] class Meta: model = Invoice include_relationships = True
invoice_lines = fields.Nested("InvoiceLineSchema", many=True)
[docs] class EmployeeSchema(ModelResourceSchema):
[docs] class Meta: model = Employee include_relationships = True
[docs] class CustomerSchema(ModelResourceSchema):
[docs] class Meta: model = Customer include_relationships = True
phone = fields.String(load_only=True)
[docs] class PlaylistSchema(ModelResourceSchema):
[docs] class Meta: model = Playlist include_relationships = True
[docs] class MediaTypeSchema(ModelResourceSchema):
[docs] class Meta: model = MediaType include_relationships = True
[docs] class GenreSchema(ModelResourceSchema):
[docs] class Meta: model = Genre include_relationships = True
[docs] class TrackSchema(ModelResourceSchema):
[docs] class Meta: model = Track include_relationships = True
[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):
[docs] class Meta: model = Track include_relationships = True
album = Relationship( "AlbumResource", many=False, permissions_cls=DisallowAllOpPermissions)
[docs] class TrackStatsSchema(ModelResourceSchema):
[docs] class Meta: model = TrackStats include_relationships = True
[docs] class AlbumSchema(ModelResourceSchema):
[docs] class Meta: model = Album include_relationships = True id_keys = ["album_id"] error_messages = { "permission_denied": "Overrides original error." }
[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):
[docs] class Meta: model = Artist include_relationships = True
albums = Relationship( "AlbumResource", many=True, permissions_cls=DisallowAllOpPermissions)
[docs] class NodeSchema(ModelResourceSchema):
[docs] class Meta: model = Node include_relationships = True
[docs] class CompositeNodeSchema(ModelResourceSchema):
[docs] class Meta: model = CompositeNode include_relationships = True
[docs] class CompositeOneSchema(ModelResourceSchema):
[docs] class Meta: model = CompositeOne include_relationships = True
[docs] class CompositeManySchema(ModelResourceSchema):
[docs] class Meta: model = CompositeMany include_relationships = True
[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 Meta: model = InvoiceLine include_relationships = True model_converter = TestCamelModelResourceConverter
[docs] class InvoiceCamelSchema(ModelResourceSchema):
[docs] class Meta: model = Invoice include_relationships = True model_converter = TestCamelModelResourceConverter
invoice_lines = fields.Nested( "InvoiceLineCamelSchema", many=True, data_key="invoiceLines")
[docs] class EmployeeCamelSchema(ModelResourceSchema):
[docs] class Meta: model = Employee include_relationships = True model_converter = TestCamelModelResourceConverter
[docs] class CustomerCamelSchema(ModelResourceSchema):
[docs] class Meta: model = Customer include_relationships = True model_converter = TestCamelModelResourceConverter
phone = fields.String(load_only=True, data_key="phone")
[docs] class PlaylistCamelSchema(ModelResourceSchema):
[docs] class Meta: model = Playlist include_relationships = True model_converter = TestCamelModelResourceConverter
[docs] class MediaTypeCamelSchema(ModelResourceSchema):
[docs] class Meta: model = MediaType include_relationships = True model_converter = TestCamelModelResourceConverter
[docs] class GenreCamelSchema(ModelResourceSchema):
[docs] class Meta: model = Genre include_relationships = True model_converter = TestCamelModelResourceConverter
[docs] class TrackCamelSchema(ModelResourceSchema):
[docs] class Meta: model = Track include_relationships = True model_converter = TestCamelModelResourceConverter
[docs] class AlbumCamelSchema(ModelResourceSchema):
[docs] class Meta: model = Album include_relationships = True model_converter = TestCamelModelResourceConverter id_keys = ["album_id"]
[docs] class ArtistCamelSchema(ModelResourceSchema):
[docs] class Meta: model = Artist include_relationships = True model_converter = TestCamelModelResourceConverter
[docs] class NodeCamelSchema(ModelResourceSchema):
[docs] class Meta: model = Node include_relationships = True model_converter = TestCamelModelResourceConverter
[docs] class CompositeNodeCamelSchema(ModelResourceSchema):
[docs] class Meta: model = CompositeNode include_relationships = True model_converter = TestCamelModelResourceConverter
[docs] class CompositeOneCamelSchema(ModelResourceSchema):
[docs] class Meta: model = CompositeOne include_relationships = True model_converter = TestCamelModelResourceConverter
[docs] class CompositeManyCamelSchema(ModelResourceSchema):
[docs] class Meta: model = CompositeMany include_relationships = True model_converter = TestCamelModelResourceConverter
[docs] class MsAlbumSchema(SQLAlchemyAutoSchema):
[docs] class Meta: model = Album include_relationships = True load_instance = True
album_id = fields.Integer(data_key="albumId")
[docs] class AlbumBadIdKeysSchema(ModelResourceSchema):
[docs] class Meta: model = Album include_relationships = True id_keys = ["test"]