-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'sqlalchemy-support' into dev
- Loading branch information
Showing
11 changed files
with
298 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,3 +6,7 @@ mock | |
|
||
# Syntax checking | ||
flake8==2.4.0 | ||
|
||
# Soft requirements | ||
flask-sqlalchemy | ||
marshmallow-sqlalchemy |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# -*- coding: utf-8 -*- | ||
import flask | ||
import marshmallow as ma | ||
|
||
class Schema(ma.Schema): | ||
"""Base serializer with which to define custom serializers. | ||
See `marshmallow.Schema` for more details about the `Schema` API. | ||
""" | ||
|
||
def jsonify(self, obj, many=False, *args, **kwargs): | ||
"""Return a JSON response containing the serialized data. | ||
:param obj: Object to serialize. | ||
:param bool many: Set to `True` if `obj` should be serialized as a collection. | ||
:param kwargs: Additional keyword arguments passed to `flask.jsonify`. | ||
.. versionchanged:: 0.6.0 | ||
Takes the same arguments as `marshmallow.Schema.dump`. Additional | ||
keyword arguments are passed to `flask.jsonify`. | ||
""" | ||
data = self.dump(obj, many=many).data | ||
return flask.jsonify(data, *args, **kwargs) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# -*- coding: utf-8 -*- | ||
"""Integration with Flask-SQLAlchemy and marshmallow-sqlalchemy.""" | ||
|
||
import marshmallow_sqlalchemy as msqla | ||
from .schema import Schema | ||
|
||
class DummySession(object): | ||
"""Placeholder session object.""" | ||
pass | ||
|
||
class SchemaOpts(msqla.SchemaOpts): | ||
"""Schema options for `ModelSchema <flask_marshmallow.sqla.ModelSchema>`. | ||
Same as `marshmallow_sqlalchemy.SchemaOpts`, except that we add a | ||
placeholder `DummySession` if ``sqla_session`` is not defined on | ||
class Meta. The actual session from `flask_sqlalchemy` gets bound | ||
in `init_app`. | ||
""" | ||
session = DummySession() | ||
|
||
def __init__(self, meta): | ||
if not hasattr(meta, 'sqla_session'): | ||
meta.sqla_session = self.session | ||
super(SchemaOpts, self).__init__(meta) | ||
|
||
class ModelSchema(msqla.ModelSchema, Schema): | ||
"""ModelSchema that generates fields based on the | ||
`model` class Meta option, which should be a | ||
``db.Model`` class from `flask_sqlalchemy`. Uses the | ||
scoped session from Flask-SQLAlchemy by default. | ||
See `marshmallow_sqlalchemy.ModelSchema` for more details | ||
on the `ModelSchema` API. | ||
""" | ||
OPTIONS_CLASS = SchemaOpts |
Oops, something went wrong.