Skip to content
This repository has been archived by the owner on Jan 5, 2019. It is now read-only.

Commit

Permalink
Updated examples
Browse files Browse the repository at this point in the history
  • Loading branch information
rbw committed Mar 14, 2018
1 parent ab6bfa3 commit 7fa3788
Show file tree
Hide file tree
Showing 9 changed files with 79 additions and 73 deletions.
16 changes: 11 additions & 5 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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, '/<user_id>', methods=['PUT'], _body=user, marshal_with=user)
def update_user(user_id, _body):
return update_user(user_id, _body.data)
BlueprintBundle
Expand Down Expand Up @@ -115,5 +121,5 @@ Created by Robert Wikman <rbw@vault13.org> in 2018

JetBrains
---------
Thank you `Jetbrains <http://www.jetbrains.com>`_ for creating pycharm and for providing me with free licenses
Thank you `Jetbrains <http://www.jetbrains.com>`_ for creating pycharm and providing me with free licenses

23 changes: 11 additions & 12 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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, '/<user_id>', methods=['PUT'], _body=user, marshal_with=user)
def update_user(user_id, _body):
return update_user(user_id, _body.data)
Blueprints
==========
Expand All @@ -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
Expand All @@ -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)
Expand Down
11 changes: 4 additions & 7 deletions examples/full/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
============================== ============== ================
Expand Down
14 changes: 14 additions & 0 deletions examples/full/app/api/pilots/services.py
Original file line number Diff line number Diff line change
@@ -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)), {})
3 changes: 1 addition & 2 deletions examples/full/app/api/pilots/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
37 changes: 37 additions & 0 deletions examples/full/app/api/planes/services.py
Original file line number Diff line number Diff line change
@@ -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}
3 changes: 1 addition & 2 deletions examples/full/app/api/planes/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
11 changes: 0 additions & 11 deletions examples/full/app/fake_data/pilots.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)), {})
34 changes: 0 additions & 34 deletions examples/full/app/fake_data/planes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}

0 comments on commit 7fa3788

Please sign in to comment.