Skip to main content

Base classes to create a Service Layer in Flask.

Project description

# LDAPOM Model

This package provides some classes to build a Service layer and expose an API that interacts with the model. The first idea is to remove all logic of the routes and model of the Flask application, and put it in the service layer. The second goal is to provide a common API that can be use to manipulate a model regardless of its storage backend.

## Sorry… What?

A common Flask organisation consists in separating model (SQLAlchemy classes, for instance), and routes. When the application grows, you can also use Blueprints. One Blueprint by model for instance :

app/ # Flask main application
customer/ # Customer Blueprint # User forms # User model # User views
templates/ # User templates
product/ # Product Blueprint
templates/ # Common templates

That's great. But where do you put the logic ? "Yeah… It depends…". "Sometimes in the model, sometimes in the views…". You see what I mean, right ? Problem is, as the application grows, it became impossible to maintain, because you just doesn't know where the logic is done.

First, "model layer" is supposed to be only data storage manipulation. But putting the logic in the "view layer" really is bad idea. What if you want to add a REST API to you application ? You just copy paste all the code from your classic route to make the new API routes ?

Finally, if you want to replace SQL database by LDAP, you should be able to do it without modifying anything but the model. So you can't put any logic in "". So, please, stop using SQLAlchemy methods in your views.

This package gives you a solution.

## Installation

pip install flask-servicelayer

## Example

The package provides two main classes, one to build a Flask SQLAlchemy-based service, and one to build a LDAPom-model-based service.

### SQLAlchemyService

See how the route became clean when all the logic and the SQLAlchemy specifics code are put in a service layer.

`app/product/` :

from flask.ext.servicelayer import SQLAlchemyService

from .models import Product
from .. import db

class ProductService(SQLAlchemyService):
__model__ = Product
__db__ = db


products = ProductService()

def index():
return render_template('product/list.html', products=products.all())

@product.route("/new", methods=['GET', 'POST'])
def new():
form = ProductForm()
if form.validate_on_submit():
products.create(**{ for field in form})
return redirect(url_for('.index'))
return render_template('product/new.html', form=form)

def delete(id):
return redirect(url_for('.index'))

### LDAPOMService

And see how customer views are really looking like product views, even if the Customer model is based on [LDAPOM-model]( Do you see any complex retrieve functions in the views?


from flask.ext.servicelayer import LDAPOMService

from .. import ldap
from .models import Customer

class CustomerService(LDAPOMService):
__model__ = Customer
__ldap__ = ldap


customers = CustomerService()

def index():
return render_template('customer/list.html', customers=customers.all())<id>)
def show(id):
customer = customers.get_or_404(id)
return render_template("customer/show.html", customer=customer)

## Tips

### Instantiation

Depending on how you have structure the rest of your application, you can instantiate a service object when you need it, or instantiate on object of each service at the beginning of each request, and store it in `g` to use it everywhere. Personally, I like to instantiate each service in the `before_request` method of its Blueprint. For instance, in `app/customer/`:

from flask import Blueprint, g

domain = Blueprint("customer", __name__, template_folder='templates')

from .services import CustomerService

def before_request():
if 'ldap' in g:
g.customers = CustomerService(g.ldap)

from . import views

### LDAP Cache

The package provides an `LDAPOMCachedService` class. This class inherits `LDAPOMService` and can be use exactly the same way. The only difference is that `all()`, `get()` and `find()` methods are cached within the service object to avoid doing a new LDAP request every time. This is very powerful when your service object is put in the global var `g` to be used everywhere during your request.

## Licence

This code is under [WTFPL]( Just do what the fuck you want with it.

The idea is based on [Matt Wright's article How do I Structure My Flask Application]( Matt, if you read this, thank you!

Project details

Release history Release notifications

This version
History Node


History Node


History Node


History Node


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Filename, size & hash SHA256 hash help File type Python version Upload date
Flask-ServiceLayer-0.0.4.tar.gz (7.3 kB) Copy SHA256 hash SHA256 Source None Jan 6, 2016

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging CloudAMQP CloudAMQP RabbitMQ AWS AWS Cloud computing Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page