Skip to content

TinKurbatoff/tornado-tryton

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

31 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

PyPI - Python Version GitHub PyPI GitHub repo size PyPI - Wheel

Tornado-tryton

Adds Tryton support to the Tornado application.

Links: Tryton ERP | Tornado webserver

Please, add stars to this project on github if this was helpful for your project. Share your questions and thoughts. Always welcome!

Django webserver note

It is possible to use this module with a Django web-server with some minor edits. I will add such a support or create a new module. But you may try to implement it manualy.

Table of Contents

General Information

  • This module helps to connect to tryton back-end from Tornado web application
  • I created a Mobile App that used data from Tryton DB to operate. There is a flask_tryton module, but I don't want to use flask as it is a "blocking server", and wasn't intended to use in multi-user multitasking operations. I use Tornado as API back-end, hence I need a new module to connect a Tornado back-end to Tryton DB and its models.

Technologies Used

  • trytond - version 5.8.1
  • tornado - version 6.1

Features

  • Supports async operations (the main advantage of using Tornado instead of 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.

Screenshots

TODO: Example screenshot

Setup

$pip3 install tornado-tryton

Usage

By default transactions are read-only except for PUT, POST, DELETE and PATCH request methods. You need trytond with all your user modules installed, and a proper trytond.conf database configuration set:

#/etc/trytond.conf
[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 Status

Project is: in progress .

Room for Improvement

It is tested with only one server configuration and may have some issues in production.

Room for improvement:

  • Test with more than 10 concurrent connections
  • Add a large file-handling support

To do:

  • TODO: Switch context if there are MANY Tryton databases/accounts available.
  • TODO: e-mailing support

Acknowledgements

  • This project was inspired by Tryton community...
  • This project is based on the flask_tryton module at PyPi...
  • Many thanks to Cédric Krier for his support of the Tryton project and detailed answers on the forum.

Contact

Feel free to contact me with any questions!

License

Project is maintained under GNU GPL v.3

About

Adds Tryton support to Tornado webserver application.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages