3

I need to make a custom controller on Odoo for getting information from the particular task. And I can able to produce the result also. But now I'm facing an issue.

The client needs to retrieve the information with a particular field.

For example, The client needs to retrieve the information with the tracking number and the data must be JSON format also. If the tracking number is 15556456356, the url should be www.customurl.com/dataset/15556456356

Pablo Escobar
  • 679
  • 4
  • 20

1 Answers1

1

The route of that URL should be @http.route('/dataset/<string:tracking_number>', type='http or json', auth="user or public"), basically the method should be like this:

import json

from odoo import http
from odoo.http import Response, request

class tracking(http.Controller):
    # if user must be authenticated use auth="user"
    @http.route('/dataset/<string:tracking_number>', type='http', auth="public")
    def tracking(self, tracking_number):  # use the same variable name
        result = # compute the result with the given tracking_number and the result should be a dict to pass it json.dumps
        return Response(json.dumps(result), content_type='application/json;charset=utf-8',status=200)

This method accept http request and return a json response, if the client is sending a json requests you should change type='json'. don't forget to import the file in the __init___.py.

Lets take an example let say that I want to return some information about a sale.order by a giving ID in the URL:

import json

from odoo import http
from odoo.http import Response, request

 class Tracking(http.Controller):
    @http.route('/dataset/<int:sale_id>', type='http', auth="public")
    def tracking(self, sale_id):
        # get the information using the SUPER USER
        result = request.env['sale.order'].sudo().browse([sale_id]).read(['name', 'date_order'])
        return Response(json.dumps(result), content_type='application/json;charset=utf-8',status=200)

So when I enter this URL using my Browser: http://localhost:8069/dataset/1:

repose of my http request

Charif DZ
  • 14,415
  • 3
  • 21
  • 40
  • What is this 'int' in '/dataset/' ? Whether it is an integer or not? I need to search for it with a char field. – Pablo Escobar Oct 20 '19 at 12:19
  • 1
    use `` that's all I assumed that the field type is integer sorry about that – Charif DZ Oct 20 '19 at 12:22
  • 1
    Your just telling `werkzeug` to convert that value to a corresponding type, and pass it the method. – Charif DZ Oct 20 '19 at 12:23
  • Hi @CharifDZ, can you clear my doubt, so what is the use of and a route like /start/' (You will get notify about this, that's why i commented here.) – Ajmal JK Oct 21 '19 at 04:08
  • 1
    @AjmalJK Odoo define a custom convertor, that accepts this URL `/start/should_be_id_here` you can check `ModelConverter` defined in `base` and read about convertor in `werkzeug`, Odoo create a convertor using this class then pass the value captured by that part of url to retreive the record with that ID check the code of method `to_python` he is just using browse method. – Charif DZ Oct 21 '19 at 08:03