Tornado Tryton connection Module

tornado-tryton

image

This is a simple-to-use connector to implement Tryton-based models in the Tornado web app. The realization is based on the flask-tryton module. Tornado β€” is a non-blocking I/O framework that is the most suitable for high-performance back-end applications.
Some discussion on Hackernoon about it.
Tornado is a well-suited powerful framework to build a lightweight Tryton-based backend. It is easy to deploy and debug.
Here you may find the module:

Currently, it is under active development.

Install:
pip install tornado-tryton

Use:
It is not recommended to use URL variables for password transmission as it may be recorded in server log files, and it is advised to move auth data into a request headers section. In the example, a simpler approach was used for simplicity, though.

#!python3
from tornado_tryton import Tryton # class to connect to Tryton DB

import json

## Tornado webserver modules
from tornado.web import RequestHandler
from tornado.gen import coroutine # used for async execution in earlier version of Python 
from tornado.options import define, options # to access to a server-wide configuration 

TRYTON_CONFIG = '/etc/trytond.conf' # Check Tryton's doc for Tryton configuration details, access to Tryton DB is configured here 

############## TRYTON INTEGRATION #################
define('config', default={"TRYTON_DATABASE" : "tryton", "TRYTON_CONFIG" : TRYTON_CONFIG}, help='app config path')
tryton = Tryton(options)
User = tryton.pool.get('res.user') # Important class type - User

@tryton.default_context # To create a default context of Tryton transactions
def default_context():
    return User.get_preferences(context_only=True)
## β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”

###########  RESPONDER FOR API REQUEST, HTML requests are handled in the same way.
class TrytonUser(RequestHandler):
    """Request for log in to Tryton"""
    SUPPORTED_METHODS = ("GET", "POST",)

    def jsonify(self, data, status=200):
        header = "Content-Type"
        body = "application/json"
        self.set_header(header, body)
        self.set_status(status)
        self.write(json.dumps(data))

    @tryton.transaction() ## To initiate tryton transaction and pass "local" request details 
    async def post(self, login, password):            

        # Check login and authorize
        user = User.search([('login', 'ilike', login)]) # Use `ilike` to ignore char case in login             
        if len(user)>0:
            # So, login is exist
            user, = User.search([('login', 'ilike', login)]) # to get the the first from the list if many
            parameters = {}
            parameters['password'] = password
            user_id = User.get_login(user.login, parameters) # bicrypt hash function
            if user_id:
                ## If user_id found β€” everyting is correct
                return self.jsonify(data={"result" : "success"}, status = 200)
            else:
                ## If none found β€” password is incorrect
                return self.jsonify({"result" : "wrong password"}, status=401)
        else:
            return self.jsonify({"result" : "unknown user"}, status=401)
2 Likes

Congrats, I am thinking to use FastApi for do the same, a new tryton connector.
Fastapi

Good!
Note: Tornado is a web-app framework that is very similar to Django and allows one to build a complex template-based web interface over API if you need that, but without its complexity.
Fast-API is amazing in creating JSON-based API backend in minutes. Like it, as well!
I chose Tornado because it is a non-blocking server, very simple and very fast, well-documented, and it even doesn’t require a Gunicorn atop.

1 Like