Package: tornado-tryton
Tornado-trytonAdds Tryton support to the Tornado application.Please, add stars to this project ongithubif this was helpful for your project. Share your questions and thoughts. Always welcome!Table of ContentsGeneral InfoTechnologies UsedFeaturesScreenshotsSetupUsageProject StatusRoom for ImprovementAcknowledgementsContactGeneral InformationThis module helps to connect to tryton back-end from Tornado web applicationI created a Mobile App that used data from Tryton DB to operate, there is aflask_trytonmodule, but I don't want to use flask as it blocking a server and wasn't intended to use in multi-user multitasking operations. As I use Tornado as API back-end I needed to connect a Tornado back-end to Tryton DB and its models.Technologies Usedtrytond - version 5.8.1tornado - version 6.1FeaturesList the ready features here:Supports async operations (the main advantage of using Tornado over Flask)Reads/writes to a Tryton database in a non-blocking manner.Supports all data models, including user-defined, from the Pool of the installed Tryton ERP.ScreenshotsSetup$pip3 install tornado_trytonUsageBy default transactions are read-only except for PUT, POST, DELETE and PATCH request methods.
You need trytond with all your user modules installed and proper trytond.conf database configuration set:[database]
uri =  postgresql://tryton:<my_secret_password>@postgres:5432/#!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)Project StatusProject is:in progress.Room for ImprovementIt is tested with only one server configuration and may have some issues in production.Room for improvement:Test with more than 10 concurrent connectionsAdd a large file-handling supportTo do:TODO: Switch context if there are MANY Tryton databases/accounts available.TODO: e-mailing supportAcknowledgementsGive credit here.This project was inspired byTryton community...This project is based on the flask_tryton module atPyPi...Many thanks to Cédric Krier for support of the Tryton project and prompt answers on the forum.ContactFeel free to contact me with any questions!LicenseProject is maintained under GNU GPL v.3