Monday, 6 May 2019

Example: API to send teachers data from odoo

Example: API to send teachers data from odoo:

# -*- coding: utf-8 -*-
from odoo.tools.translate import _
from odoo import http
from odoo.http import request
import json
import sys

class odoo_public_data(http.Controller):
    @http.route('/get/teachers', type='http', methods=['GET'], auth="public")

    def get_teachers(self, **kwargs):
        teacher_model = request.env['dps.teacher']
        teacher_ids = teacher_model.sudo().search([])
        teacher_list = {'status': 1, 'data': []}
        try:
            if teacher_ids:
                for teacher in teacher_ids:
                    vals = {
                        'id': teacher.id,
                        'name': teacher.name,
                        'email': teacher.email,
                        'phone': teacher.phone,
                        'school': teacher.partner_id.name,
                    }
                    teacher_list['data'].append(vals)
            return json.dumps(teacher_list)
        except Exception as e:
            print str(e)
            return json.dumps({'status': 0, 'data': 'Some problem with API'})

No comments:

Post a Comment