Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[17.0][MIG] edi_record_metadata_oca #119

Open
wants to merge 18 commits into
base: 17.0
Choose a base branch
from
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
121 changes: 121 additions & 0 deletions edi_record_metadata_oca/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
===================
EDI record metadata
===================

..
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! This file is generated by oca-gen-addon-readme !!
!! changes will be overwritten. !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! source digest: sha256:25c9d000f3b88b2582c20ba7433885252f58c75a38b839b3b21a538d6d9a5298
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

.. |badge1| image:: https://img.shields.io/badge/maturity-Alpha-red.png
:target: https://odoo-community.org/page/development-status
:alt: Alpha
.. |badge2| image:: https://img.shields.io/badge/licence-LGPL--3-blue.png
:target: http://www.gnu.org/licenses/lgpl-3.0-standalone.html
:alt: License: LGPL-3
.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Fedi--framework-lightgray.png?logo=github
:target: https://github.com/OCA/edi-framework/tree/17.0/edi_record_metadata_oca
:alt: OCA/edi-framework
.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png
:target: https://translation.odoo-community.org/projects/edi-framework-17-0/edi-framework-17-0-edi_record_metadata_oca
:alt: Translate me on Weblate
.. |badge5| image:: https://img.shields.io/badge/runboat-Try%20me-875A7B.png
:target: https://runboat.odoo-community.org/builds?repo=OCA/edi-framework&target_branch=17.0
:alt: Try me on Runboat

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

Technical module for the EDI suite to provide a way to store some
technical data.

For instance, you receive a sale order via EDI and you want to store
some original values from the incoming file without having to parse
again the whole file or compute values that you need later to compite
something else.

.. IMPORTANT::
This is an alpha version, the data model and design can change at any time without warning.
Only for development or testing purpose, do not use in production.
`More details on development status <https://odoo-community.org/page/development-status>`_

**Table of contents**

.. contents::
:local:

Usage
=====

Get / set metadata
------------------

On a \`edi.exchange.record\`:

exc_record.set_metadata({...}) exc_record.get_metadata()

Automatically store metadata from consumer records
--------------------------------------------------

- Make sure your model inherits from edi.exchange.consumer.mixin
- Override \_edi_get_metadata_to_store

NOTE: automatic storage happens only when create gets called in an EDI
framework session (edi_framework_action is in ctx).

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

Bugs are tracked on `GitHub Issues <https://github.com/OCA/edi-framework/issues>`_.
In case of trouble, please check there if your issue has already been reported.
If you spotted it first, help us to smash it by providing a detailed and welcomed
`feedback <https://github.com/OCA/edi-framework/issues/new?body=module:%20edi_record_metadata_oca%0Aversion:%2017.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>
- Duong (Tran Quoc) <duongtq@trobz.com>

Other credits
-------------

The migration of this module from 14.0 to 16.0 was financially supported
by Camptocamp.

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.

.. |maintainer-simahawk| image:: https://github.com/simahawk.png?size=40px
:target: https://github.com/simahawk
:alt: simahawk

Current `maintainer <https://odoo-community.org/page/maintainer-role>`__:

|maintainer-simahawk|

This module is part of the `OCA/edi-framework <https://github.com/OCA/edi-framework/tree/17.0/edi_record_metadata_oca>`_ project on GitHub.

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.
1 change: 1 addition & 0 deletions edi_record_metadata_oca/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import models
20 changes: 20 additions & 0 deletions edi_record_metadata_oca/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Copyright 2023 Camptocamp SA
# @author: Simone Orsi <simahawk@gmail.com>
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl).

{
"name": "EDI record metadata",
"summary": """
Allow to store metadata for related records.
""",
"version": "17.0.1.0.0",
"development_status": "Alpha",
"license": "LGPL-3",
"website": "https://github.com/OCA/edi-framework",
"author": "Camptocamp, Odoo Community Association (OCA)",
"maintainers": ["simahawk"],
"depends": ["edi_oca", "base_sparse_field"],
"data": [
"views/edi_exchange_record.xml",
],
}
47 changes: 47 additions & 0 deletions edi_record_metadata_oca/fields.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# Copyright 2023 Camptocamp SA
# @author Simone Orsi <simahawk@gmail.com>
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl).

import datetime
import json
from functools import singledispatch

from odoo import fields

from odoo.addons.base_sparse_field.models.fields import Serialized


@singledispatch
def convert(obj):
raise TypeError(

Check warning on line 16 in edi_record_metadata_oca/fields.py

View check run for this annotation

Codecov / codecov/patch

edi_record_metadata_oca/fields.py#L16

Added line #L16 was not covered by tests
f"Object of type {obj.__class__.__name__} " f"is not JSON serializable"
)


@convert.register(datetime.date)
def convert_date(obj):
return fields.Date.to_string(obj)


@convert.register(datetime.datetime)
def convert_datetime(obj):
return fields.Datetime.to_string(obj)


class ExtendedJSONEncoder(json.JSONEncoder):
def default(self, obj):
return convert(obj)


# ATM there's no control over the encoder of Serialized field.
# Hence, we must use our own field.
# TODO: propose configurable param on odoo master.
class BetterSerialized(Serialized):
"""Serialize fields w/ proper JSON encoder/decoder"""

def convert_to_cache(self, value, record, validate=True):
return (
json.dumps(value, cls=ExtendedJSONEncoder)
if isinstance(value, dict)
else (value or None)
)
45 changes: 45 additions & 0 deletions edi_record_metadata_oca/i18n/edi_record_metadata_oca.pot
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# Translation of Odoo Server.
# This file contains the translation of the following modules:
# * edi_record_metadata_oca
#
msgid ""
msgstr ""
"Project-Id-Version: Odoo Server 16.0\n"
"Report-Msgid-Bugs-To: \n"
"Last-Translator: \n"
"Language-Team: \n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: \n"
"Plural-Forms: \n"

#. module: edi_record_metadata_oca
#: model:ir.model,name:edi_record_metadata_oca.model_edi_exchange_consumer_mixin
msgid "Abstract record where exchange records can be assigned"
msgstr ""

#. module: edi_record_metadata_oca
#: model:ir.model,name:edi_record_metadata_oca.model_edi_exchange_record
msgid "EDI exchange Record"
msgstr ""

#. module: edi_record_metadata_oca
#: model:ir.model.fields,help:edi_record_metadata_oca.field_edi_exchange_record__metadata_display
msgid "Enable debug mode to be able to inspect data."
msgstr ""

#. module: edi_record_metadata_oca
#: model:ir.model.fields,help:edi_record_metadata_oca.field_edi_exchange_record__metadata
msgid "JSON-like metadata used for technical purposes."
msgstr ""

#. module: edi_record_metadata_oca
#: model:ir.model.fields,field_description:edi_record_metadata_oca.field_edi_exchange_record__metadata
#: model_terms:ir.ui.view,arch_db:edi_record_metadata_oca.edi_exchange_record_view_form
msgid "Metadata"
msgstr ""

#. module: edi_record_metadata_oca
#: model:ir.model.fields,field_description:edi_record_metadata_oca.field_edi_exchange_record__metadata_display
msgid "Metadata Display"
msgstr ""
48 changes: 48 additions & 0 deletions edi_record_metadata_oca/i18n/it.po
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# Translation of Odoo Server.
# This file contains the translation of the following modules:
# * edi_record_metadata_oca
#
msgid ""
msgstr ""
"Project-Id-Version: Odoo Server 16.0\n"
"Report-Msgid-Bugs-To: \n"
"PO-Revision-Date: 2024-07-29 08:58+0000\n"
"Last-Translator: mymage <stefano.consolaro@mymage.it>\n"
"Language-Team: none\n"
"Language: it\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: \n"
"Plural-Forms: nplurals=2; plural=n != 1;\n"
"X-Generator: Weblate 5.6.2\n"

#. module: edi_record_metadata_oca
#: model:ir.model,name:edi_record_metadata_oca.model_edi_exchange_consumer_mixin
msgid "Abstract record where exchange records can be assigned"
msgstr "Record astratto dove i recod di scambio possono essere assegnati"

#. module: edi_record_metadata_oca
#: model:ir.model,name:edi_record_metadata_oca.model_edi_exchange_record
msgid "EDI exchange Record"
msgstr "Record di scambio EDI"

#. module: edi_record_metadata_oca
#: model:ir.model.fields,help:edi_record_metadata_oca.field_edi_exchange_record__metadata_display
msgid "Enable debug mode to be able to inspect data."
msgstr "Abilitare la modalità debug per poter ispezionare i dati."

#. module: edi_record_metadata_oca
#: model:ir.model.fields,help:edi_record_metadata_oca.field_edi_exchange_record__metadata
msgid "JSON-like metadata used for technical purposes."
msgstr "Metadati simili a JSON utilizzati per motivi tecnici."

#. module: edi_record_metadata_oca
#: model:ir.model.fields,field_description:edi_record_metadata_oca.field_edi_exchange_record__metadata
#: model_terms:ir.ui.view,arch_db:edi_record_metadata_oca.edi_exchange_record_view_form
msgid "Metadata"
msgstr "Metadati"

#. module: edi_record_metadata_oca
#: model:ir.model.fields,field_description:edi_record_metadata_oca.field_edi_exchange_record__metadata_display
msgid "Metadata Display"
msgstr "Metadata visualizzati"
2 changes: 2 additions & 0 deletions edi_record_metadata_oca/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from . import edi_exchange_record
from . import edi_exchange_consumer_mixin
46 changes: 46 additions & 0 deletions edi_record_metadata_oca/models/edi_exchange_consumer_mixin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Copyright 2023 Camptocamp SA
# @author Simone Orsi <simahawk@gmail.com>
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl).

from odoo import api, models


class EDIMetadataConsumerMixin(models.AbstractModel):
"""Add API to handle EDI stored metadata to exchange consumers."""

_inherit = "edi.exchange.consumer.mixin"

@api.model_create_multi
def create(self, vals_list):
do_store_metadata = self.env.context.get("edi_framework_action")
if do_store_metadata:
# If an origin record is already given at creation
# let's store metadata immediately before the creation really happens.
# This way, if you have logic relying on metadata to be present at create
# you will be able to make it work.
# If the origin is set later, you'll need to handle it by yourself.
for vals in vals_list:
origin_id = vals.get("origin_exchange_record_id")
metadata = self._edi_get_metadata_to_store(vals)
if origin_id and metadata:
self._edi_store_metadata_before_create(origin_id, metadata)
return super().create(vals_list)

@api.model
def _edi_get_metadata_to_store(self, orig_vals):
"""Hook here to customize which values will be stored.

:param `orig_vals`: values received by `create` method.
"""
return {}

def _edi_store_metadata(self, metadata):
if self.origin_exchange_record_id:
self.origin_exchange_record_id.set_metadata(metadata)

Check warning on line 39 in edi_record_metadata_oca/models/edi_exchange_consumer_mixin.py

View check run for this annotation

Codecov / codecov/patch

edi_record_metadata_oca/models/edi_exchange_consumer_mixin.py#L39

Added line #L39 was not covered by tests

@api.model
def _edi_store_metadata_before_create(self, origin_id, metadata):
self.env["edi.exchange.record"].browse(origin_id).set_metadata(metadata)

def _edi_get_metadata(self):
return self.origin_exchange_record_id.get_metadata()
32 changes: 32 additions & 0 deletions edi_record_metadata_oca/models/edi_exchange_record.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Copyright 2023 Camptocamp SA
# @author Simone Orsi <simahawk@gmail.com>
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl).
import json

from odoo import api, fields, models

from ..fields import BetterSerialized


class EDIExchangeRecord(models.Model):
_inherit = "edi.exchange.record"

metadata = BetterSerialized(
help="JSON-like metadata used for technical purposes.",
default={},
)
metadata_display = fields.Text(
compute="_compute_metadata_display",
help="Enable debug mode to be able to inspect data.",
)

@api.depends("metadata")
def _compute_metadata_display(self):
for rec in self:
rec.metadata_display = json.dumps(rec.metadata, sort_keys=True, indent=4)

def set_metadata(self, data):
self.metadata = data

def get_metadata(self):
return self.metadata
3 changes: 3 additions & 0 deletions edi_record_metadata_oca/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[build-system]
requires = ["whool"]
build-backend = "whool.buildapi"
2 changes: 2 additions & 0 deletions edi_record_metadata_oca/readme/CONTRIBUTORS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- Simone Orsi \<<simone.orsi@camptocamp.com>\>
- Duong (Tran Quoc) \<<duongtq@trobz.com>\>
2 changes: 2 additions & 0 deletions edi_record_metadata_oca/readme/CREDITS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
The migration of this module from 14.0 to 16.0 was financially supported
by Camptocamp.
7 changes: 7 additions & 0 deletions edi_record_metadata_oca/readme/DESCRIPTION.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Technical module for the EDI suite to provide a way to store some
technical data.

For instance, you receive a sale order via EDI and you want to store
some original values from the incoming file without having to parse
again the whole file or compute values that you need later to compite
something else.
Loading
Loading