Marshmallow subclasses with extended features
Project description
yModel
Schema is a Marshmallow subclass that saves the data and errors to private members (that you can access by using self.get_data() and self.get_errors())
The point is to not need a data class since, in an API context, all we need is json in json out
It highly uses decorators to avoid (because is automatic) the need of validation and post processing the response
Tree is the tree version with materialized paths and order in a object member (if needed)
MongoSchema and yMongoTree are the MongoDB versions for CRUD plain schemas and tree structures
It is part of the yRest framework (which includes ySanic and yAuth by the moment and yOpenSanic in the future)
Example
class Person(Schema):
name = fields.Str(required = True)
age = fields.Int()
@can_crash(Exception, ErrorSchema, 404)
@can_crash(ValidationError, ErrorSchema)
@produces(NameOnlyOkSchema, as_ = "name")
@consumes(NameOnlyRequestSchema)
async def set_name(self, request, schema):
if schema.get_data()["name"] == "Exception":
raise Exception("It can crash from a regular Exception")
if schema.get_data()["name"] == "MakeItCrash":
raise ValidationError("Crash test")
self.get_data()["name"] = schema.get_data()["name"]
return self.get_data["name"]
@deprecate("Reason to deprecate it")
async def to_deprecate(self, request):
return True
As you can see, the first part of the class is a regular marshmallow class with fields declaration
The interesting part is the set_name
and to_deprecate
members full of decorators
set_name
consumes a NameOnlyRequestSchema
(which is another yModel use for input validation) that will be filled with the request.json (by default)
With this decorator you will recieve an extra *args
item with the model already validated
Then it produces a NameOnlyOkSchema
with the return value from the function. So in this case will be something like
{
"ok": true,
"result": "the name of the person"
}
can_crash automates the error processing by returning a response like
{
"ok": false,
"message": "the raised error"
}
in the example two exception has been defined:
If the name passed is "Exception" will raise a regular python Exception which will be captured by the first can_crash
returning and ErrorSchema
and a 404 status
{
"ok": false,
"message": "It can crash from a regular Exception"
}
If the name passed is "MakeItCrash" it will be captured by the second can_crash
and will return an ErrorSchema
too but with the status 400
{
"ok": false,
"message": "Crash test"
}
yModel defines some basic schemas for returning ok OkSchema
and not ok ErrorSchema
that you can subclass with your own needs (or even use a schema build by you from scratch. It only needs to be a subclass of yModel)
The deprecated
decorator will be mostly useful with the yOpenSanic to mark an API endpoint as deprecated
This decorators will help later for introspection since the tree structures are tricky to instrospect
Installation
pip install yModel
Tree structures
Add a path
and name
fields to build the materialized path or add a @pre_load
function if you need a different source for the slug (it's using python-slugify but you can use any one else if you add your own @pre_load
function)
Check out the tests for examples
MongoDB
Use this class as a guide to create you own backend need (will be nice if you pull request it to this project if the backend is an open sourced one)
Help
Feel free to help if you think something is weird or incomplete by submiting a pull request
I've spend an indecent amount of time dealing with tree structures to know what's essential for that matter but will be no surprise if you have a nice idea to improve this code (at the end of the day I bet you are a smart person)
What is already needed
- [] Elasticsearch module
- [] Redis module
- [] More testing
- [] Continous integration
- [] Better help & documentation
I'm not a technical person but still want to help
You can tip the project with cryptos too:
BTC: 1GtKxwZGR65ar9V8xafxhMiniZyqXej2GC
ETH: 0x01bd478b8C07633D2f4E58AC553f72CE4E590d56
LTC: LYUzrFX6ck5uMhw5VqcD9piQHnX7oeSLdh
XMR: 49stcvbfjEkWLjb6mdG21zMJ3uRrLmN3bazGQ8cHjjsVHYYyY61N6P7emCXhpsvB2Vc8Uuz2FA1Qk6hkE8e4ADmJQQ64eyT
ADA: DdzFFzCqrhsoUF5UjGGAYUayV5uNCJZ17PJn9V8X9MTQ26m2wDVycme42gufKufPNWMazfJLg8RKHpc1iFvn6j8BTJjaozGtLPzCDx5t
NEM: NDGYO6X3NTD6CX3V7MCCYKQPBIOYGZRXEKDLCDW2
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file yModel-0.0.2.tar.gz
.
File metadata
- Download URL: yModel-0.0.2.tar.gz
- Upload date:
- Size: 12.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.20.1 setuptools/40.6.2 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.6.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | cb2af3e34c47aa3c96df30ab0241acf04652fd4917ccd80ab54c2761e0df6443 |
|
MD5 | 40b77bb91b20d648eeaad6593478b967 |
|
BLAKE2b-256 | ad16cf40b5e1e0eab07d69e5b646792c59e353c6d9a39ebc72290b7a095f993a |
File details
Details for the file yModel-0.0.2-py3-none-any.whl
.
File metadata
- Download URL: yModel-0.0.2-py3-none-any.whl
- Upload date:
- Size: 13.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.20.1 setuptools/40.6.2 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.6.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | c0f8e9336ebf6862c44a8814f2b1851d878c6af3f2f233a159dc55bf90b91ca9 |
|
MD5 | a3e263a8b56e462d58c8f497272b1a5f |
|
BLAKE2b-256 | ea318a354c4e36e3513a57b5f331e3d74f80b0b2bae50a9860bf6b4dd9fe638c |