forked from yezyilomo/odoo-rest-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
373 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
#Mifos Integration Module |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
from . import controllers | ||
from . import models |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# -*- coding: utf-8 -*- | ||
{ | ||
'name': "mifos_integration", | ||
|
||
'summary': """ | ||
Short (1 phrase/line) summary of the module's purpose, used as | ||
subtitle on modules listing or apps.openerp.com""", | ||
|
||
'description': """ | ||
Long description of module's purpose | ||
""", | ||
|
||
'author': "My Company", | ||
'website': "http://www.yourcompany.com", | ||
|
||
# Categories can be used to filter modules in modules listing | ||
# Check https://github.com/odoo/odoo/blob/12.0/odoo/addons/base/data/ir_module_category_data.xml | ||
# for the full list | ||
'category': 'Uncategorized', | ||
'version': '0.1', | ||
|
||
# any module necessary for this one to work correctly | ||
'depends': ['base'], | ||
|
||
# always loaded | ||
'data': [ | ||
# 'security/ir.model.access.csv', | ||
'views/views.xml', | ||
'views/templates.xml', | ||
], | ||
# only loaded in demonstration mode | ||
'demo': [ | ||
'demo/demo.xml', | ||
], | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
from . import controllers |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,198 @@ | ||
# -*- coding: utf-8 -*- | ||
import json | ||
import math | ||
import logging | ||
import functools | ||
import werkzeug | ||
from collections import Counter | ||
from datetime import datetime | ||
from odoo import http, api, _ | ||
from odoo.http import request | ||
from odoo.addons.http_routing.models.ir_http import slug | ||
|
||
|
||
class Error(Exception): | ||
"""Base class for exceptions in this module.""" | ||
pass | ||
|
||
class FormatError(Error): | ||
"""Exception raised for errors in the input. | ||
Attributes: | ||
message -- explanation of the error | ||
""" | ||
|
||
def __init__(self, message): | ||
self.message = message | ||
|
||
|
||
def check_if_instanceof(against, faulty_node): | ||
|
||
def wraper(node): | ||
result = isinstance(node, against) | ||
if not result and isinstance(faulty_node, list): | ||
faulty_node.append(str(node)) | ||
|
||
return result | ||
return wraper | ||
|
||
|
||
def isqueryable(obj, query, faulty_node=None): | ||
flat_or_nested = all( | ||
map(check_if_instanceof((str, dict), faulty_node), query) | ||
) | ||
|
||
iterable = (len(query) <= 1) and \ | ||
all( | ||
map(check_if_instanceof((list, tuple), faulty_node), query) | ||
) | ||
|
||
if not (flat_or_nested or iterable): | ||
faulty_node = "".join(faulty_node) | ||
return False | ||
else: | ||
return True | ||
|
||
|
||
def _jsonify_obj(obj, query, call_callable, not_found_create, fields=None): | ||
faulty_node = [] | ||
|
||
if not isqueryable(obj, query, faulty_node=faulty_node): | ||
raise FormatError(f"Wrong formating of Query on \"{faulty_node}\" field") | ||
|
||
for field in query: | ||
if isinstance(field, str): | ||
# Flat field | ||
if fields is None: | ||
fields = {} | ||
if callable( getattr(obj, field) ) and call_callable: | ||
fields.update({field: getattr(obj, field)()}) | ||
else: | ||
fields.update({field: getattr(obj, field)}) | ||
|
||
|
||
elif isinstance(field, dict): | ||
# Nested field | ||
for sub_field in field: | ||
found = hasattr(obj, sub_field) | ||
|
||
# This cond below is for dict fields only | ||
# bcuz it's the only way to create new fields | ||
if not_found_create and not found: | ||
# Create new field | ||
fields.update({sub_field: field[sub_field]}) | ||
continue | ||
elif not found: | ||
# Throw NotFound Error [FIXME] | ||
getattr(obj, sub_field) | ||
continue | ||
|
||
|
||
if len(field[sub_field]) < 1: | ||
# Nested empty object, | ||
# Empty dict is the default value for empty nested objects. | ||
# Comment the line below to remove empty objects in results. [FIXME] | ||
fields.update({sub_field: {}}) | ||
continue | ||
|
||
if isinstance(field[sub_field][0], (list, tuple)): | ||
# Nested object is iterable | ||
fields.update({sub_field: []}) | ||
else: | ||
# Nested object is flat | ||
fields.update({sub_field: {}}) | ||
|
||
obj_field = getattr(obj, sub_field) | ||
if callable( obj_field ) and call_callable: | ||
obj_field = obj_field() | ||
|
||
_jsonify_obj( | ||
obj_field, | ||
field[sub_field], | ||
call_callable, | ||
not_found_create, | ||
fields=fields[sub_field], | ||
) | ||
|
||
elif isinstance(field, (list, tuple)): | ||
# Nested object | ||
if fields is None: | ||
fields = [] | ||
for sub_obj in obj: | ||
sub_field={} | ||
fields.append(sub_field) | ||
_jsonify_obj( | ||
sub_obj, | ||
field, | ||
call_callable, | ||
not_found_create, | ||
fields=sub_field | ||
) | ||
else: | ||
raise Exception( | ||
f""" | ||
Wrong formating of Query on '{field}' field, | ||
It seems like the Query was mutated on run time, | ||
Use 'tuple' instead of 'list' to avoid mutating Query accidentally. | ||
""" | ||
) | ||
|
||
return fields | ||
|
||
|
||
def jsonify(data, query, call_callable=False, not_found_create=False): | ||
return _jsonify_obj(data, query, call_callable, not_found_create) | ||
|
||
|
||
|
||
|
||
class MifosIntegration(http.Controller): | ||
@http.route('/api/users/', auth='public') | ||
def sys_users(self, **kw): | ||
users = request.env['res.users'].sudo().search([]) | ||
query = [[ | ||
"id", | ||
"name", | ||
{ | ||
"groups_id": [ | ||
[ | ||
"id", | ||
"name" | ||
] | ||
] | ||
} | ||
]] | ||
data = jsonify(users, query) | ||
return http.Response( | ||
json.dumps(data), | ||
status=200, | ||
mimetype='application/json' | ||
) | ||
|
||
@http.route('/api/journals/', auth='public') | ||
def account_journals(self, **kw): | ||
journals = request.env['account.journal'].sudo().search([]) | ||
query = [[ | ||
"id", | ||
"name" | ||
]] | ||
data = jsonify(journals, query) | ||
return http.Response( | ||
json.dumps(data), | ||
status=200, | ||
mimetype='application/json' | ||
) | ||
|
||
@http.route('/api/account-charts/', auth='public') | ||
def chart_of_account(self, **kw): | ||
accounts = request.env['account.account'].sudo().search([]) | ||
query = [[ | ||
"id", | ||
"name" | ||
]] | ||
data = jsonify(accounts, query) | ||
return http.Response( | ||
json.dumps(data), | ||
status=200, | ||
mimetype='application/json' | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<odoo> | ||
<data> | ||
<!-- --> | ||
<!-- <record id="object0" model="mifos_integration.mifos_integration"> --> | ||
<!-- <field name="name">Object 0</field> --> | ||
<!-- <field name="value">0</field> --> | ||
<!-- </record> --> | ||
<!-- --> | ||
<!-- <record id="object1" model="mifos_integration.mifos_integration"> --> | ||
<!-- <field name="name">Object 1</field> --> | ||
<!-- <field name="value">10</field> --> | ||
<!-- </record> --> | ||
<!-- --> | ||
<!-- <record id="object2" model="mifos_integration.mifos_integration"> --> | ||
<!-- <field name="name">Object 2</field> --> | ||
<!-- <field name="value">20</field> --> | ||
<!-- </record> --> | ||
<!-- --> | ||
<!-- <record id="object3" model="mifos_integration.mifos_integration"> --> | ||
<!-- <field name="name">Object 3</field> --> | ||
<!-- <field name="value">30</field> --> | ||
<!-- </record> --> | ||
<!-- --> | ||
<!-- <record id="object4" model="mifos_integration.mifos_integration"> --> | ||
<!-- <field name="name">Object 4</field> --> | ||
<!-- <field name="value">40</field> --> | ||
<!-- </record> --> | ||
<!-- --> | ||
</data> | ||
</odoo> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
from . import models |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
from odoo import models, fields, api | ||
|
||
# class mifos_integration(models.Model): | ||
# _name = 'mifos_integration.mifos_integration' | ||
|
||
# name = fields.Char() | ||
# value = fields.Integer() | ||
# value2 = fields.Float(compute="_value_pc", store=True) | ||
# description = fields.Text() | ||
# | ||
# @api.depends('value') | ||
# def _value_pc(self): | ||
# self.value2 = float(self.value) / 100 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink | ||
access_mifos_integration_mifos_integration,mifos_integration.mifos_integration,model_mifos_integration_mifos_integration,,1,0,0,0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<odoo> | ||
<data> | ||
<!-- <template id="listing"> --> | ||
<!-- <ul> --> | ||
<!-- <li t-foreach="objects" t-as="object"> --> | ||
<!-- <a t-attf-href="#{ root }/objects/#{ object.id }"> --> | ||
<!-- <t t-esc="object.display_name"/> --> | ||
<!-- </a> --> | ||
<!-- </li> --> | ||
<!-- </ul> --> | ||
<!-- </template> --> | ||
<!-- <template id="object"> --> | ||
<!-- <h1><t t-esc="object.display_name"/></h1> --> | ||
<!-- <dl> --> | ||
<!-- <t t-foreach="object._fields" t-as="field"> --> | ||
<!-- <dt><t t-esc="field"/></dt> --> | ||
<!-- <dd><t t-esc="object[field]"/></dd> --> | ||
<!-- </t> --> | ||
<!-- </dl> --> | ||
<!-- </template> --> | ||
</data> | ||
</odoo> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<odoo> | ||
<data> | ||
<!-- explicit list view definition --> | ||
<!-- | ||
<record model="ir.ui.view" id="mifos_integration.list"> | ||
<field name="name">mifos_integration list</field> | ||
<field name="model">mifos_integration.mifos_integration</field> | ||
<field name="arch" type="xml"> | ||
<tree> | ||
<field name="name"/> | ||
<field name="value"/> | ||
<field name="value2"/> | ||
</tree> | ||
</field> | ||
</record> | ||
--> | ||
|
||
<!-- actions opening views on models --> | ||
<!-- | ||
<record model="ir.actions.act_window" id="mifos_integration.action_window"> | ||
<field name="name">mifos_integration window</field> | ||
<field name="res_model">mifos_integration.mifos_integration</field> | ||
<field name="view_mode">tree,form</field> | ||
</record> | ||
--> | ||
|
||
<!-- server action to the one above --> | ||
<!-- | ||
<record model="ir.actions.server" id="mifos_integration.action_server"> | ||
<field name="name">mifos_integration server</field> | ||
<field name="model_id" ref="model_mifos_integration_mifos_integration"/> | ||
<field name="state">code</field> | ||
<field name="code"> | ||
action = { | ||
"type": "ir.actions.act_window", | ||
"view_mode": "tree,form", | ||
"res_model": self._name, | ||
} | ||
</field> | ||
</record> | ||
--> | ||
|
||
<!-- Top menu item --> | ||
<!-- | ||
<menuitem name="mifos_integration" id="mifos_integration.menu_root"/> | ||
--> | ||
<!-- menu categories --> | ||
<!-- | ||
<menuitem name="Menu 1" id="mifos_integration.menu_1" parent="mifos_integration.menu_root"/> | ||
<menuitem name="Menu 2" id="mifos_integration.menu_2" parent="mifos_integration.menu_root"/> | ||
--> | ||
<!-- actions --> | ||
<!-- | ||
<menuitem name="List" id="mifos_integration.menu_1_list" parent="mifos_integration.menu_1" | ||
action="mifos_integration.action_window"/> | ||
<menuitem name="Server to list" id="mifos_integration" parent="mifos_integration.menu_2" | ||
action="mifos_integration.action_server"/> | ||
--> | ||
</data> | ||
</odoo> |