From 7fa3788e6c0ded7aaafe38cc1c2bd6a6a2b77b44 Mon Sep 17 00:00:00 2001 From: Robert Wikman Date: Wed, 14 Mar 2018 02:05:22 +0100 Subject: [PATCH] Updated examples --- README.rst | 16 ++++++---- docs/index.rst | 23 +++++++-------- examples/full/README.rst | 11 +++---- examples/full/app/api/pilots/services.py | 14 +++++++++ examples/full/app/api/pilots/views.py | 3 +- examples/full/app/api/planes/services.py | 37 ++++++++++++++++++++++++ examples/full/app/api/planes/views.py | 3 +- examples/full/app/fake_data/pilots.py | 11 ------- examples/full/app/fake_data/planes.py | 34 ---------------------- 9 files changed, 79 insertions(+), 73 deletions(-) create mode 100644 examples/full/app/api/pilots/services.py create mode 100644 examples/full/app/api/planes/services.py diff --git a/README.rst b/README.rst index f83d158..565005d 100644 --- a/README.rst +++ b/README.rst @@ -45,19 +45,25 @@ Some examples of ``@route`` and ``BlueprintBundle`` + ``Journey`` from flask import Blueprint from flask_journey import route - from .db import create_user, get_user + + from .services import create_user, get_user, update_user from .schemas import user, users, query bp = Blueprint('users', __name__) @route(bp, '/', methods=['GET'], _query=query, marshal_with=users) - def get_many(_query): + def get_users(_query): return get_users(_query.data) @route(bp, '/', methods=['POST'], _body=user, marshal_with=user) - def create(_body): - return create_user(_body.data) + def create_user(_body): + return create_user(_body.data) + + + @route(bp, '/', methods=['PUT'], _body=user, marshal_with=user) + def update_user(user_id, _body): + return update_user(user_id, _body.data) BlueprintBundle @@ -115,5 +121,5 @@ Created by Robert Wikman in 2018 JetBrains --------- -Thank you `Jetbrains `_ for creating pycharm and for providing me with free licenses +Thank you `Jetbrains `_ for creating pycharm and providing me with free licenses diff --git a/docs/index.rst b/docs/index.rst index cfe481b..f0a74bb 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -87,23 +87,25 @@ However, functions decorated with ``flask_journey.route`` can of course, just as from flask import Blueprint from flask_journey import route - from db import create_user, get_user + from .services import create_user, get_user, update_user from .schemas import user, users, query bp = Blueprint('users', __name__) @route(bp, '/', methods=['GET'], _query=query, marshal_with=users) - def get_many(_query=None): + def get_users(_query): return get_users(_query.data) @route(bp, '/', methods=['POST'], _body=user, marshal_with=user) - def create(_body=None): + def create_user(_body): return create_user(_body.data) - + @route(bp, '/', methods=['PUT'], _body=user, marshal_with=user) + def update_user(user_id, _body): + return update_user(user_id, _body.data) Blueprints ========== @@ -127,18 +129,15 @@ There are various benefits of using the Journey BlueprintBundle, and in most cas from .users import bp as users from .groups import bp as groups from .companies import bp as companies - from .new_feature import bp as new_feature + from .stuff import bp as stuff v1 = BlueprintBundle(path='/api/v1', description="API v1, stable") v1.attach_bp(users, description='Users CRUD') v1.attach_bp(groups) v1.attach_bp(companies, description='Companies API') - v2 = BlueprintBundle(path='/api/v2', description="API v2, beta") - v2.attach_bp(users, description='Users CRUD') - v2.attach_bp(groups) - v2.attach_bp(companies, description='Companies API') - v2.attach_bp(new_feature) + other = BlueprintBundle(path='/other') + other.attach_bp(stuff) Importing bundles @@ -151,12 +150,12 @@ Importing and registering bundles (along with blueprints) is easy as pie: # file: api/__init__.py from flask import Flask - from .bundles import v1, v2 + from .bundles import v1, other app = Flask(__name__) journey = Journey() journey.attach_bundle(v1) - journey.attach_bundle(v2) + journey.attach_bundle(other) journey.init_app(app) diff --git a/examples/full/README.rst b/examples/full/README.rst index 833a9a0..28459b4 100644 --- a/examples/full/README.rst +++ b/examples/full/README.rst @@ -3,13 +3,12 @@ Full example This example shows how a structured Flask application can utilize the Flask-Journey extension for: -- Simple route / blueprint management -- Deserialization -- Validation -- Marshalling +- blueprint management +- (de)serialization +- validation -Note that the **routes** endpoint uses the regular ``route`` decorator from ``flask.Blueprint``. This is intentional to show that Flask-Journey works seamlessly with vanilla Flask blueprints. +Note that the **routes view** uses the regular ``route`` decorator from ``flask.Blueprint``. This is intentional to show that Flask-Journey works seamlessly with vanilla Flask blueprints. Preparations @@ -28,8 +27,6 @@ Start the server using the manager:: $ python manage.py runserver -Test it out *now* with a user-agent! - ============================== ============== ================ Name Default value Description ============================== ============== ================ diff --git a/examples/full/app/api/pilots/services.py b/examples/full/app/api/pilots/services.py new file mode 100644 index 0000000..1de7c91 --- /dev/null +++ b/examples/full/app/api/pilots/services.py @@ -0,0 +1,14 @@ +# -*- coding: utf-8 -*- + +from app.fake_data.pilots import data + + +def get_pilots(name=None): + if name: + return [x for x in data if x['name'] == name] + + return data + + +def get_pilot(pilot_id): + return next((x for x in data if x['id'] == int(pilot_id)), {}) diff --git a/examples/full/app/api/pilots/views.py b/examples/full/app/api/pilots/views.py index 7fa2ba4..c44f44e 100644 --- a/examples/full/app/api/pilots/views.py +++ b/examples/full/app/api/pilots/views.py @@ -3,8 +3,7 @@ from flask import Blueprint from flask_journey import route -from app.fake_data.pilots import get_pilots, get_pilot - +from .services import get_pilots, get_pilot from .schemas import pilot, pilots, query diff --git a/examples/full/app/api/planes/services.py b/examples/full/app/api/planes/services.py new file mode 100644 index 0000000..c56bd4d --- /dev/null +++ b/examples/full/app/api/planes/services.py @@ -0,0 +1,37 @@ +# -*- coding: utf-8 -*- + +from app.fake_data.planes import data + + +def get_planes(min_wings=None): + return [x for x in data if x['wings'] >= min_wings] + + +def get_plane(plane_id): + return next((x for x in data if x['id'] == int(plane_id)), {}) + + +def create_plane(plane): + data.append(plane) + return plane + + +def update_plane(plane_id, plane): + if not get_plane(plane_id): + return {} + else: + for i, item in enumerate(data): + if item['id'] == int(plane_id): + data[i] = plane + + return plane + + +def delete_plane(plane_id): + deleted = False + for i, item in enumerate(data): + if item['id'] == int(plane_id): + deleted = True + del data[i] + + return {'success': deleted} diff --git a/examples/full/app/api/planes/views.py b/examples/full/app/api/planes/views.py index a9f2aeb..eb90197 100644 --- a/examples/full/app/api/planes/views.py +++ b/examples/full/app/api/planes/views.py @@ -3,8 +3,7 @@ from flask import Blueprint, jsonify from flask_journey import route -from app.fake_data.planes import get_plane, get_planes, create_plane, update_plane, delete_plane - +from .services import get_plane, get_planes, create_plane, update_plane, delete_plane from .schemas import plane, planes, query diff --git a/examples/full/app/fake_data/pilots.py b/examples/full/app/fake_data/pilots.py index 4a9761e..95ba13a 100644 --- a/examples/full/app/fake_data/pilots.py +++ b/examples/full/app/fake_data/pilots.py @@ -12,14 +12,3 @@ 'name': 'Jerry', }, ] - - -def get_pilots(name=None): - if name: - return [x for x in data if x['name'] == name] - - return data - - -def get_pilot(pilot_id): - return next((x for x in data if x['id'] == int(pilot_id)), {}) diff --git a/examples/full/app/fake_data/planes.py b/examples/full/app/fake_data/planes.py index 91ce837..e6fa7cf 100644 --- a/examples/full/app/fake_data/planes.py +++ b/examples/full/app/fake_data/planes.py @@ -15,37 +15,3 @@ 'wings': 2, }, ] - - -def get_planes(min_wings=None): - return [x for x in data if x['wings'] >= min_wings] - - -def get_plane(plane_id): - return next((x for x in data if x['id'] == int(plane_id)), {}) - - -def create_plane(plane): - data.append(plane) - return plane - - -def update_plane(plane_id, plane): - if not get_plane(plane_id): - return {} - else: - for i, item in enumerate(data): - if item['id'] == int(plane_id): - data[i] = plane - - return plane - - -def delete_plane(plane_id): - deleted = False - for i, item in enumerate(data): - if item['id'] == int(plane_id): - deleted = True - del data[i] - - return {'success': deleted}