Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Split functionality into modules #10

Closed
daniel-j-h opened this issue Mar 26, 2016 · 5 comments
Closed

Split functionality into modules #10

daniel-j-h opened this issue Mar 26, 2016 · 5 comments

Comments

@daniel-j-h
Copy link
Contributor

Currently you have all the "business logic" in the pegasus.py file. What you normally do is split logical parts into their own modules. For example views.py module for all of your views (functions that run on a specific route endpoint) and an errorhandlers.py module for custom error handlers. You then hoist your app in the __init__.py file, import both modules and register them with your Flask app like this:

routes = [
    ('/', views.index, ['GET', 'POST']),
    ...]

for rule, view, methods in routes:
    app.add_url_rule(rule, view_func=view, methods=methods)

handlers = [
    (400, errorhandlers.bad_request),
    (401, errorhandlers.unauthorized),
    (404, errorhandlers.page_not_found),
    (403, errorhandlers.page_forbidden),
    (410, errorhandlers.page_gone),
    (500, errorhandlers.not_found),
]

for errno, handler in handlers:
    app.register_error_handler(errno, handler)

The logical structure can be something similar to: views.py for all of your view functions, models.py for classes that abstract away entities and how you store and query them in your database, and then other modules as needs be, e.g. a decorators.py with helpers such as http://flask.pocoo.org/docs/0.10/patterns/viewdecorators/#templating-decorator (good time to learn about decorators!).

Your view.py then could look similar to this:

@templated('index.html')
def index():
    return dict(userName='someUser')
@mariamrf
Copy link
Owner

I split the functionality into modules, but what I don't understand is the part where I have to register them with the Flask app? They import app from pegasus and __init__.py imports all modules after it initializes app, and it works, then what? Why do I need to register them like this also?

@daniel-j-h
Copy link
Contributor Author

If you keep the route decorators on the view functions, you don't have to register them (again). @app.route('/') for example is fine.

Often times you want to separate the view functions (what they do, how they are implemented) and their route (is it /register or /reg or something different). If you want to do this, the common way to "link" view functions with their route by manually registering them in one place instead of using route decorators.

If you want to stay with the route decorator that's fine 👍 I just wanted to bring this to your attention.

@mariamrf
Copy link
Owner

Oh okay. That makes sense. Thank you!

@daniel-j-h
Copy link
Contributor Author

Just today I added a little Flask server to my project and this is a perfect example of a situation where you can not use @app.route:

https://github.com/daniel-j-h/classyshoes/blob/eb717cec1ec35c54d0fa6f668005331210bd20a2/classyshoes/httpd.py

See how the Flask app (here named httpd) is only created locally in a function, so it can not be used as a decorator for the index function.

Just noted that while implementing it and thought you may be interested :)

@mariamrf
Copy link
Owner

That's an interesting take on it. Thank you for explaining! 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants