Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 84 additions & 0 deletions pricelist_cache_rest/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
====================
Pricelist Cache Rest
====================

.. !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! This file is generated by oca-gen-addon-readme !!
!! changes will be overwritten. !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

.. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png
:target: https://odoo-community.org/page/development-status
:alt: Beta
.. |badge2| image:: https://img.shields.io/badge/licence-AGPL--3-blue.png
:target: http://www.gnu.org/licenses/agpl-3.0-standalone.html
:alt: License: AGPL-3
.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Fsale--workflow-lightgray.png?logo=github
:target: https://github.com/OCA/sale-workflow/tree/14.0/pricelist_cache_rest
:alt: OCA/sale-workflow
.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png
:target: https://translation.odoo-community.org/projects/sale-workflow-14-0/sale-workflow-14-0-pricelist_cache_rest
:alt: Translate me on Weblate
.. |badge5| image:: https://img.shields.io/badge/runbot-Try%20me-875A7B.png
:target: https://runbot.odoo-community.org/runbot/167/14.0
:alt: Try me on Runbot

|badge1| |badge2| |badge3| |badge4| |badge5|

Provides an endpoint to get product prices for a given customer

**Table of contents**

.. contents::
:local:

Bug Tracker
===========

Bugs are tracked on `GitHub Issues <https://github.com/OCA/sale-workflow/issues>`_.
In case of trouble, please check there if your issue has already been reported.
If you spotted it first, help us smashing it by providing a detailed and welcomed
`feedback <https://github.com/OCA/sale-workflow/issues/new?body=module:%20pricelist_cache_rest%0Aversion:%2014.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**>`_.

Do not contact contributors directly about support or help with technical issues.

Credits
=======

Authors
~~~~~~~

* Camptocamp

Contributors
~~~~~~~~~~~~

* Simone Orsi <simone.orsi@camptocamp.com>
* Matthieu Méquignon <matthieu.mequignon@camptocamp.com>
* `Trobz <https://trobz.com>`_:
* Khoi Vo <khoivha@trobz.com>

Other credits
~~~~~~~~~~~~~

**Financial support**

* Cosanum
* Camptocamp R&D

Maintainers
~~~~~~~~~~~

This module is maintained by the OCA.

.. image:: https://odoo-community.org/logo.png
:alt: Odoo Community Association
:target: https://odoo-community.org

OCA, or the Odoo Community Association, is a nonprofit organization whose
mission is to support the collaborative development of Odoo features and
promote its widespread use.

This module is part of the `OCA/sale-workflow <https://github.com/OCA/sale-workflow/tree/14.0/pricelist_cache_rest>`_ project on GitHub.

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.
3 changes: 3 additions & 0 deletions pricelist_cache_rest/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from . import controllers
from . import models
from . import wizards
22 changes: 22 additions & 0 deletions pricelist_cache_rest/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Copyright 2021 Camptocamp SA
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
{
"name": "Pricelist Cache Rest",
"summary": "Provides an endpoint to get product prices for a given customer",
"version": "14.0.1.0.0",
"category": "Hidden",
"author": "Camptocamp",
"license": "AGPL-3",
"depends": [
"auth_api_key",
"base_jsonify",
"pricelist_cache",
],
"website": "https://github.com/OCA/sale-workflow",
"installable": True,
"data": [
"data/ir_exports_data.xml",
"wizards/res_config_settings.xml",
],
"demo": ["demo/auth_api_key_demo.xml"],
}
1 change: 1 addition & 0 deletions pricelist_cache_rest/controllers/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import main
57 changes: 57 additions & 0 deletions pricelist_cache_rest/controllers/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# Copyright 2021 Camptocamp SA
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html)

import json

from odoo import http
from werkzeug.exceptions import Unauthorized


class PricelistController(http.Controller):
"""Expose prices for pricelists.
"""

@http.route(
"/pricelist/<model('res.partner'):partner>",
type="http",
auth="api_key",
methods=["GET"],
csrf=False,
)
def partner_pricelist(self, partner):
"""Retrive all prices for given partner.

:return: TODO
"""
env = http.request.env
auth_api_key_id = getattr(http.request, "auth_api_key_id", None)
self._validate_api_key(env, auth_api_key_id)

cache_items = self._get_cache_items(partner)
return self._make_json_response(self._cache_to_json(cache_items))

def _validate_api_key(self, env, api_key_id):
if api_key_id is None:
raise Unauthorized("API key missing")
allowed_keys = self._get_authorized_api_keys(env)
if api_key_id not in allowed_keys:
raise Unauthorized("API key not valid")
return True

def _get_authorized_api_keys(self, env):
# TODO: what about multi company support?
return env.company.pricelist_cache_auhorize_apikey_ids.ids

def _get_cache_items(self, partner):
return partner._pricelist_cache_get_prices()

def _make_json_response(self, data):
headers = {}
headers["Content-Type"] = "application/json"
return http.request.make_response(json.dumps(data), headers=headers)

def _cache_to_json(self, cache_items):
exporter = cache_items.env.ref(
"pricelist_cache_rest.ir_exp_cache_item"
)
return cache_items.jsonify(exporter.get_json_parser())
21 changes: 21 additions & 0 deletions pricelist_cache_rest/data/ir_exports_data.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>
<odoo>

<record id="ir_exp_cache_item" model="ir.exports">
<field name="name">Pricelist Cache Parser</field>
<field name="resource">pricelist.cache</field>
</record>

<record id="ir_exp_cache_item_product_id" model="ir.exports.line">
<field name="name">product_id</field>
<field name="target">product_id:id</field>
<field name="instance_method_name">_jsonify_m2o_to_id</field>
<field name="export_id" ref="ir_exp_cache_item"/>
</record>

<record id="ir_exp_cache_item_product_price" model="ir.exports.line">
<field name="name">price</field>
<field name="export_id" ref="ir_exp_cache_item"/>
</record>

</odoo>
12 changes: 12 additions & 0 deletions pricelist_cache_rest/demo/auth_api_key_demo.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<odoo noupdate="1">
<record id="api_key_demo" model="auth.api.key">
<field name="name">Pricelist Cache demo key</field>
<field name="user_id" ref="base.user_demo" />
<field name="key">8E57rAPnZY6iS20w4XUQ</field>
</record>
<record id="api_key_demo_2" model="auth.api.key">
<field name="name">Pricelist Cache demo key 2</field>
<field name="user_id" ref="base.user_demo" />
<field name="key">8E57rAPnZY6iS20w4XUQ-2</field>
</record>
</odoo>
1 change: 1 addition & 0 deletions pricelist_cache_rest/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import res_company
14 changes: 14 additions & 0 deletions pricelist_cache_rest/models/res_company.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Copyright 2021 Camptocamp SA
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl)

from odoo import fields, models


class ResCompany(models.Model):

_inherit = "res.company"

pricelist_cache_auhorize_apikey_ids = fields.Many2many(
"auth.api.key",
help="API keys that can retrieve pricelist data via REST endpoints.",
)
4 changes: 4 additions & 0 deletions pricelist_cache_rest/readme/CONTRIBUTORS.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
* Simone Orsi <simone.orsi@camptocamp.com>
* Matthieu Méquignon <matthieu.mequignon@camptocamp.com>
* `Trobz <https://trobz.com>`_:
* Khoi Vo <khoivha@trobz.com>
4 changes: 4 additions & 0 deletions pricelist_cache_rest/readme/CREDITS.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
**Financial support**

* Cosanum
* Camptocamp R&D
1 change: 1 addition & 0 deletions pricelist_cache_rest/readme/DESCRIPTION.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Provides an endpoint to get product prices for a given customer
Loading