在Flask中创建服务层的基类。
Flask-ServiceLayer的Python项目详细描述
#ldapom model
此包提供一些类来构建服务层并公开与模型交互的api。第一个想法是删除flask应用程序的路由和模型的所有逻辑,并将其放在服务层中。第二个目标是提供一个通用的api,不管它的存储后端是什么,都可以用来操作一个模型。
常见的烧瓶组织包括分离模型(例如,sqlalchemy类)和路由。当应用程序增长时,也可以使用蓝图。例如,一个按模型划分的蓝图:
```
app/>烧瓶主应用程序/#用户模板
…
产品/产品蓝图
config.py
filters.py
…
````
那太好了。但你把逻辑放在哪里呢?"是的…这取决于…有时在模型中,有时在视图中…。你明白我的意思了吧?问题是,随着应用程序的增长,它变得不可能维护,因为您只是不知道逻辑在哪里完成。
但把逻辑放在"视图层"中确实是个坏主意。如果要向应用程序中添加rest api怎么办?你只需要复制粘贴你的经典路由中的所有代码来创建新的api路由?
最后,如果您想用ldap替换sql数据库,您应该能够在不修改模型的情况下完成这项工作。所以你不能把任何逻辑放在"models.py"里。所以,请停止在您的视图中使用sqlalchemy方法。
这个包为您提供了一个解决方案。
一个是构建基于ldapom模型的服务。
\sqlalchemy service
sqlalchemyservice
from.models导入产品
from.导入db
class productservice(sqlalchemyservice):
products=products.all())
@product.route("/new",methods=['get','post'])
def new():
form=productform()
if form.validate嫒on嫒submit():
products.create(*{field.name:field.data for field in form})
返回重定向(url嫒for('.index'))
return呈现模板('product/new.html',form=form)
@product.route("/delete/<;int:id>;")
def delete(id):
products.delete(products.get_或_404(id))
return redirect(url_for('.index'))
```
ldapomservice
与产品视图一样,即使客户模型基于[ldapom model](https://github.com/sysnove/ldapom model)。您在视图中看到任何复杂的检索函数吗?
`app/customer/services.py`:
``python
从flask.ext.servicelayer导入ldapomservice
导入ldap
自.模型导入客户
类客户服务(ldapomservice):
/>``` python
customers=customerservice()
@customer.route("/"
def index():
返回呈现模板('customer/list.html',customers=customers.all())
@customer.show(/<;id>;)
def show(id):
customer=customers.get_或_404(id)
return render_模板("customer/show.html",customer=customer)
````
当您需要它时,或者在每个请求开始时实例化每个服务的对象,并将其存储在"g"中,以便在任何地方使用它。就我个人而言,我喜欢在蓝图的"before_request"方法中实例化每个服务。例如,在"app/customer/u init_uuu.py"中:
``python
from flask import blueprint,g
g.customers=客户服务(g.ldap)
导入视图
```
\ldap cache
包提供了一个"ldapomcachedservice"类。此类继承"ldapomservice",并且可以完全相同的方式使用。唯一的区别是"all()"、"get()"和"find()"方法缓存在服务对象中,以避免每次都执行新的LDAP请求。当您的服务对象放在全局变量"g"中,以便在请求期间在任何地方使用时,这是非常强大的。
\license
此代码位于[wtfpl]下(https://en.wikipedia.org/wiki/wtfpl)。你他妈的想怎么做就怎么做。
这个想法是基于[马特·赖特的文章《如何构造我的烧瓶应用程序》(http://mattupstate.com/python/2013/06/26/how-i-structure-my-flask-application s.html s2c)。马特,如果你读到了,谢谢!
此包提供一些类来构建服务层并公开与模型交互的api。第一个想法是删除flask应用程序的路由和模型的所有逻辑,并将其放在服务层中。第二个目标是提供一个通用的api,不管它的存储后端是什么,都可以用来操作一个模型。
常见的烧瓶组织包括分离模型(例如,sqlalchemy类)和路由。当应用程序增长时,也可以使用蓝图。例如,一个按模型划分的蓝图:
```
app/>烧瓶主应用程序/#用户模板
…
产品/产品蓝图
config.py
filters.py
…
````
那太好了。但你把逻辑放在哪里呢?"是的…这取决于…有时在模型中,有时在视图中…。你明白我的意思了吧?问题是,随着应用程序的增长,它变得不可能维护,因为您只是不知道逻辑在哪里完成。
但把逻辑放在"视图层"中确实是个坏主意。如果要向应用程序中添加rest api怎么办?你只需要复制粘贴你的经典路由中的所有代码来创建新的api路由?
最后,如果您想用ldap替换sql数据库,您应该能够在不修改模型的情况下完成这项工作。所以你不能把任何逻辑放在"models.py"里。所以,请停止在您的视图中使用sqlalchemy方法。
这个包为您提供了一个解决方案。
一个是构建基于ldapom模型的服务。
\sqlalchemy service
sqlalchemyservice
from.models导入产品
from.导入db
class productservice(sqlalchemyservice):
products=products.all())
@product.route("/new",methods=['get','post'])
def new():
form=productform()
if form.validate嫒on嫒submit():
products.create(*{field.name:field.data for field in form})
返回重定向(url嫒for('.index'))
return呈现模板('product/new.html',form=form)
@product.route("/delete/<;int:id>;")
def delete(id):
products.delete(products.get_或_404(id))
return redirect(url_for('.index'))
```
ldapomservice
与产品视图一样,即使客户模型基于[ldapom model](https://github.com/sysnove/ldapom model)。您在视图中看到任何复杂的检索函数吗?
`app/customer/services.py`:
``python
从flask.ext.servicelayer导入ldapomservice
导入ldap
自.模型导入客户
类客户服务(ldapomservice):
/>``` python
customers=customerservice()
@customer.route("/"
def index():
返回呈现模板('customer/list.html',customers=customers.all())
@customer.show(/<;id>;)
def show(id):
customer=customers.get_或_404(id)
return render_模板("customer/show.html",customer=customer)
````
当您需要它时,或者在每个请求开始时实例化每个服务的对象,并将其存储在"g"中,以便在任何地方使用它。就我个人而言,我喜欢在蓝图的"before_request"方法中实例化每个服务。例如,在"app/customer/u init_uuu.py"中:
``python
from flask import blueprint,g
导入视图
```
\ldap cache
包提供了一个"ldapomcachedservice"类。此类继承"ldapomservice",并且可以完全相同的方式使用。唯一的区别是"all()"、"get()"和"find()"方法缓存在服务对象中,以避免每次都执行新的LDAP请求。当您的服务对象放在全局变量"g"中,以便在请求期间在任何地方使用时,这是非常强大的。
\license
此代码位于[wtfpl]下(https://en.wikipedia.org/wiki/wtfpl)。你他妈的想怎么做就怎么做。
这个想法是基于[马特·赖特的文章《如何构造我的烧瓶应用程序》(http://mattupstate.com/python/2013/06/26/how-i-structure-my-flask-application s.html s2c)。马特,如果你读到了,谢谢!