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

Use the route() decorator is used to bind a function to a URL #178

Closed
sharoonthomas opened this issue Apr 28, 2014 · 1 comment
Closed

Comments

@sharoonthomas
Copy link

Using a decorator to bind URLs to view functions the canonical way to route URLs in flask. This patch implements the same for nereid and this issue will be used as a reference to why this change is done and the implementation detail.

New design

It is now possible to use a route decorator to bind your URLs to view functions. Example below::

class Product:
    __name__ = 'product.product'

    @classmethod
    @route('/product/<uri>')
    def render_product(cls, uri):
        ...
        return 'Product Information'

Caveats

Methods need to be decorated again if they are implemented in subclasses (mostly in downstream modules). This is easier to explain with an example.

If the code in the above example was from nereid_catalog module and the nereid_webshop module implements this method to make a change in the behavior, then the decorator needs to be applied again.

Example code:

# File: nereid_webshop/product.py

class Product:
    __name__ = 'product.product'

    @classmethod
    @route('/product/<uri>')
    def render_product(cls, uri):
        # Do something here
        # may be call the super function to get the response
        # return the final result
       return render_template('...')

Before

Nereid, until version 3.0.7.0 used xml records which created the rules in the database to bind URLs to view functions which were methods in tryton models. This was required because the URLs (and view functions) change depending on the database (more precisely the modules installed in the database). Since XML records in a module are only created in the database when a module is installed for the specific database, it was an easy way to bind URLs to the corresponding views. However, the approach had its flaws:

  1. URL creation was more complicated than it should have been (define XML, upgrade DB and so on).
  2. Non flaskish - URL routing with decorators is indeed considered a major feature of Flask and nereid should not miss out on it.
sharoonthomas pushed a commit that referenced this issue Apr 28, 2014
@sharoonthomas
Copy link
Author

Implemented

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

No branches or pull requests

1 participant