Class based views for Flask
Clone or download
Pull request Compare This branch is 207 commits ahead of apiguy:master.
hoatle Merge pull request #86 from LucasG2000/init-parameter-84
Add additional init_argument to register method
Latest commit 5eb9ed7 Apr 19, 2018
Permalink
Failed to load latest commit information.
.travis build docs for X.X.x branches Feb 24, 2016
dev-setup @ #67 | should create virtualbox sync host path if it does not exists Sep 23, 2017
docs Adding unit tests for both scenarios, adding new init parameter on re… Apr 14, 2018
test_classful Adding unit tests for both scenarios, adding new init parameter on re… Apr 14, 2018
test_classful_py3 @ #47 | the behavior of the trailing slash should be more intuitive a… Sep 9, 2017
.coveragerc fix coveralls report Feb 15, 2016
.gitignore @ #67 | should create virtualbox sync host path if it does not exists Sep 23, 2017
.pylintrc add coveralls, style check Aug 21, 2015
.travis.yml @ #61 | should fix docs publishing Sep 21, 2017
AUTHORS.md @ #62 | should add and update contributor list Sep 21, 2017
CHANGELOG.md @ #79 | release v0.14.1 Oct 19, 2017
CONTRIBUTORS.md @ #62 | update contributor list: add @hieptranquoc Sep 26, 2017
LICENSE @ #51 | should keep the original license Sep 7, 2017
Makefile @ #47 | get coveralls work Sep 9, 2017
README.rst @ #32 | add teracy-dev as dev-setup, add docker dev Sep 9, 2017
docker-compose.yml @ #32 | add teracy-dev as dev-setup, add docker dev Sep 9, 2017
flask_classful.py Sometimes it is needed to pass additional information to the inherite… Apr 9, 2018
requirements.txt @ #50 | base_args should not be overriden when route_base is set Sep 10, 2017
run-dev.sh @ #32 | add teracy-dev as dev-setup, add docker dev Sep 9, 2017
setup.cfg @ #25 | Special methods should work with Python 3.5 type hints Jul 5, 2016
setup.py @ #32 | travis-ci test with Python 3.3 and 3.6 so it should cover 3.4… Sep 10, 2017
tox.ini fix test for python 3.3 Aug 18, 2015

README.rst

Flask-Classful

https://travis-ci.org/teracyhq/flask-classful.svg?branch=develop Join the chat at https://gitter.im/teracyhq/flask-classful https://coveralls.io/repos/teracyhq/flask-classful/badge.svg?branch=develop&service=github Code Climate 'Latest PyPI release' 'Wheel Supported' 'Supported Python versions'

Code Issues Open Issues 'Current Sprint' 'In Progress' 'Under Review' 'Throughput Graph'

Note:

This is a fork of original `Flask-Classy` for continuing its development since the original
project was not updated for a long time. For more information, see:
https://github.com/apiguy/flask-classy/issues/80

Flask-Classful is an extension that adds class-based views to Flask. But why?

I ❤ Flask. Like a lot. But sometimes projects get a little big and I need some way of managing and organizing all the different pieces. I know what you're saying: "But what about Blueprints?"

You're right. Blueprints are pretty awesome. But I found that they aren't always enough to encapsulate a specific context the way I need. What I wanted, no what I needed was to be able to group my views into relevant classes each with their own context and behavior. It's also made testing really nifty too.

"OK, I see your point. But can't I just use the base classes in flask.views to do that?"

Well, yes and no. While flask.views.MethodView does provide some of the functionality of flask_classful.FlaskView it doesn't quite complete the picture by supporting methods that aren't part of the typical CRUD operations for a given resource, or make it easy for me to override the route rules for particular view. And while flask.views.View does add some context, it requires a class for each view instead of letting me group very similar views for the same resource into a single class.

"But my projects aren't that big. Can Flask-Classful do anything else for me besides making a big project easier to manage?"

Why yes. It does help a bit with some other things.

For example, Flask-Classful will automatically generate routes based on the methods in your views, and makes it super simple to override those routes using Flask's familiar decorator syntax.

Installation

Install the latest extension with:

$ pip install flask-classful

Or install the bleeding edge development version with:

$ pip install git+https://github.com/teracyhq/flask-classful.git@develop#egg=flask-classful

Let's see how it works

If you're like me, you probably get a better idea of how to use something when you see it being used. Let's go ahead and create a little app to see how Flask-Classful works:

from flask import Flask
from flask_classful import FlaskView

# we'll make a list to hold some quotes for our app
quotes = [
    "A noble spirit embiggens the smallest man! ~ Jebediah Springfield",
    "If there is a way to do it better... find it. ~ Thomas Edison",
    "No one knows what he can do till he tries. ~ Publilius Syrus"
]

app = Flask(__name__)

class QuotesView(FlaskView):
    def index(self):
        return "<br>".join(quotes)

QuotesView.register(app)

if __name__ == '__main__':
    app.run()

Run this app and open your web browser to: http://localhost:5000/quotes/

As you can see, it returns the list of quotes. But what if we just wanted one quote? What would we do then?

class QuotesView(FlaskView):
    def index(self):
        ...

    def get(self, id):
        id = int(id)
        if id < len(quotes) - 1:
            return quotes[id]
        else:
            return "Not Found", 404

Now direct your browser to: http://localhost:5000/quotes/1/ and you should see the very poignant quote from the esteemed Mr. Edison.

That's cool and all, but what if we just wanted a random quote? What then? Let's add a random view to our FlaskView:

from random import choice
class QuotesView(FlaskView):
    def index(self):
        ...

    def get(self, id):
        ...

    def random(self):
        return choice(quotes)

And point your browser to: http://localhost:5000/quotes/random/ and see that a random quote is returned each time. Voilà!

So by now you must be keenly aware of the fact that you have not defined a single route, but yet routing is obviously taking place. "Is this voodoo?" you ask?

Not at all. Flask-Classful will automatically create routes for any method in a FlaskView that doesn't begin with an underscore character. You can still define your own routes of course, and we'll look at that next.

Questions?

Feel free to ping me on twitter @teracyhq, or head on over to the github repo at http://github.com/teracyhq/flask-classful so you can join the fun.

How to develop

Follow the dev-setup/README.md file (https://github.com/teracyhq/flask-classful/blob/develop/dev-setup/README.md)

License

BSD License

Copyright (c) 2012 by Freedom Dumlao.

Some rights reserved.

Redistribution and use in source and binary forms of the software as well
as documentation, with or without modification, are permitted provided
that the following conditions are met:

* Redistributions of source code must retain the above copyright
  notice, this list of conditions and the following disclaimer.

* Redistributions in binary form must reproduce the above
  copyright notice, this list of conditions and the following
  disclaimer in the documentation and/or other materials provided
  with the distribution.

* The names of the contributors may not be used to endorse or
  promote products derived from this software without specific
  prior written permission.

THIS SOFTWARE AND DOCUMENTATION IS PROVIDED BY THE COPYRIGHT HOLDERS AND
CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE AND DOCUMENTATION, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
DAMAGE.