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

move import match to base #8

Merged
merged 4 commits into from
Sep 7, 2023
Merged
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
68 changes: 68 additions & 0 deletions spp_import_match/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
====================
OpenSPP Import Match
====================

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

.. |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-openspp-project%2Fopenspp--base-lightgray.png?logo=github
:target: https://github.com/openspp/openspp-base/tree/15.0/spp_import_match
:alt: openspp/openspp-base

|badge1| |badge2| |badge3|

OpenSPP Import Match

.. 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:

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

Bugs are tracked on `GitHub Issues <https://github.com/openspp/openspp-base/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/openspp/openspp-base/issues/new?body=module:%20spp_import_match%0Aversion:%2015.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
~~~~~~~

* OpenSPP.org

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

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

Current maintainers:

|maintainer-jeremi| |maintainer-gonzalesedwin1123|

This module is part of the `openspp/openspp-base <https://github.com/openspp/openspp-base/tree/15.0/spp_import_match>`_ project on GitHub.

You are welcome to contribute.
3 changes: 3 additions & 0 deletions spp_import_match/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Part of OpenSPP. See LICENSE file for full copyright and licensing details.

from . import models
24 changes: 24 additions & 0 deletions spp_import_match/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Part of OpenSPP. See LICENSE file for full copyright and licensing details.

{
"name": "OpenSPP Import Matching",
"category": "OpenSPP",
"version": "15.0.0.0.0",
"sequence": 1,
"author": "OpenSPP.org",
"website": "https://github.com/openspp/openspp-registry",
"license": "LGPL-3",
"development_status": "Beta",
"maintainers": ["jeremi", "gonzalesedwin1123"],
"depends": ["base", "g2p_registry_base"],
"data": [
"security/ir.model.access.csv",
"views/import_match_view.xml",
],
"assets": {},
"demo": [],
"images": [],
"application": True,
"installable": True,
"auto_install": False,
}
4 changes: 4 additions & 0 deletions spp_import_match/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Part of OpenSPP. See LICENSE file for full copyright and licensing details.

from . import base
from . import import_match
64 changes: 64 additions & 0 deletions spp_import_match/models/base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# Part of OpenSPP. See LICENSE file for full copyright and licensing details.
import re
from odoo import api, models

class Base(models.AbstractModel):
_inherit = "base"

@api.model
def load(self, fields, data):
usable, field_to_match = self.env["spp.import.match"]._usable_rules(self._name, fields)
import_match = self.env["spp.import.match"].search([("model_name", "=", self._name)])
overwrite_match = import_match[0].overwrite_match if import_match else False

if usable:
newdata = list()
if ".id" in fields:
column = fields.index(".id")
fields[column] = "id"
for values in data:
dbid = int(values[column])
values[column] = self.browse(dbid).get_external_id().get(dbid)
import_fields = list(map(models.fix_import_export_id_paths, fields))
converted_data = self._convert_records(
self._extract_records(import_fields, data)
)

if "id" not in fields:
fields.append("id")
import_fields.append(["id"])

clean_fields = [f[0] for f in import_fields]
for dbid, xmlid, record, info in converted_data:
row = dict(zip(clean_fields, data[info["record"]]))

match = self
if xmlid:
row["id"] = xmlid
newdata.append(tuple(row[f] for f in clean_fields))
continue
elif dbid:
match = self.browse(dbid)
else:
match = self.env["spp.import.match"]._match_find(self, record, row)

flat_fields_to_remove = [item for sublist in field_to_match for item in sublist]
for fields_pop in flat_fields_to_remove:
if fields_pop in fields:
fields.remove(fields_pop)

match.export_data(fields)

ext_id = match.get_external_id()
add_row = False
if ext_id:
if overwrite_match:
add_row = True
else:
add_row = True

if add_row:
row["id"] = ext_id[match.id] if match else row.get("id", "")
newdata.append(tuple(row[f] for f in fields))
data = newdata
return super(Base, self).load(fields, data)
162 changes: 162 additions & 0 deletions spp_import_match/models/import_match.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
# Part of OpenSPP. See LICENSE file for full copyright and licensing details.

import logging
from odoo import _, api, fields, models, tools
from odoo.exceptions import ValidationError

_logger = logging.getLogger(__name__)


class SPPImportMatch(models.Model):
_name = "spp.import.match"
_description = "Import Matching"
_order = "sequence, name"

name = fields.Char(compute="_compute_name", store=True, index=True)
sequence = fields.Integer(index=True)
overwrite_match = fields.Boolean(Default=False)
model_id = fields.Many2one(
"ir.model",
"Model",
required=True,
ondelete="cascade",
domain=[("transient", "=", False)],
help="Model for Import Matching",
)
model_name = fields.Char(related="model_id.model")
model_description = fields.Char(related="model_id.name")
field_ids = fields.One2many(
"spp.import.match.fields",
"match_id",
string="Fields",
required=True,
help="Fields to Match in Importing",
)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
_sql_constraints = [
(
"model_id_uniq",
"UNIQUE(model_id)",
"Model should be unique!",
)
]

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will be removed, as we can have multiple Import Match Configuration for a single model.

_sql_constraints = [
(
"model_id_unique",
"unique (model_id)",
"Model must be unique",
),
]

@api.onchange("model_id")
def _onchange_model_id(self):
for rec in self:
rec.field_ids = None

@api.depends("model_id")
def _compute_name(self):
for rec in self:
name = "New"
if rec.model_id:
name = rec.model_description

rec.name = name

@api.model
def _match_find(self, model, converted_row, imported_row):
usable, field_to_match = self._usable_rules(model._name, converted_row)
usable = self.browse(usable)
for combination in usable:
combination_valid = True
domain = list()
for field in combination.field_ids:
if field.conditional:
if imported_row[field.name] != field.imported_value:
combination_valid = False
break
if field.field_id.name in converted_row:
row_value = converted_row[field.field_id.name]
field_value = field.field_id.name
add_to_domain = True
if field.sub_field_id:
tuple_val = row_value[0][2]
add_to_domain = False
if field.sub_field_id.name in tuple_val:
row_value = tuple_val[field.sub_field_id.name]
add_to_domain = True
field_value = field.field_id.name + "." + field.sub_field_id.name
if add_to_domain:
domain.append((field_value, "=", row_value))
if not combination_valid:
continue
match = model.search(domain)
if len(match) == 1:
return match

return model

@api.model
@tools.ormcache("model_name", "frozenset(fields)")
def _usable_rules(self, model_name, fields):
result = self
available = self.search([("model_name", "=", model_name)])
field_to_match = []
for record in available:
for field in record.field_ids:
if not field.sub_field_id:
field_to_match.append(field.name)
for f in record.field_ids:
if f.name in fields or f.field_id.name in fields:
result |= record

return result.ids, field_to_match


class SPPImportMatchFields(models.Model):
_name = "spp.import.match.fields"
_description = "Fields for Import Matching"

name = fields.Char(compute="_compute_name")
field_id = fields.Many2one(
"ir.model.fields",
string="Field",
required=True,
ondelete="cascade",
domain="[('model_id', '=', model_id)]",
help="Fields to Match in Importing",
)
relation = fields.Char(related="field_id.relation")
sub_field_id = fields.Many2one(
"ir.model.fields",
string="Sub-Field",
ondelete="cascade",
help="Sub Fields to Match in Importing",
)
match_id = fields.Many2one("spp.import.match", string="Match", ondelete="cascade")
model_id = fields.Many2one(related="match_id.model_id")
conditional = fields.Boolean()
imported_value = fields.Char(
help="This will be used as a condition to disregard this field if matched"
)

def _compute_name(self):
for rec in self:
name = rec.field_id.name
if rec.sub_field_id:
name = rec.field_id.name + "/" + rec.sub_field_id.name
rec.name = name

@api.onchange("field_id")
def _onchange_field_id(self):
for rec in self:
field_id = rec.field_id.id
field_type = rec.field_id.ttype
fields_list = []
if field_type not in ('many2many', 'one2many', 'many2one'):
for field in rec.match_id.field_ids:
new_id_str = str(field.id)
new_id_str_2 = ''.join(letter for letter in new_id_str if letter.isalnum())
if 'NewIdvirtual' not in new_id_str_2:
fields_list.append(field.field_id.id)

duplicate_counter = 0
for duplicate_field in fields_list:
if duplicate_field == field_id:
duplicate_counter += 1

if duplicate_counter > 1:
raise ValidationError(_("Field '%s', already exists!") % rec.field_id.field_description)

3 changes: 3 additions & 0 deletions spp_import_match/security/ir.model.access.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink
spp_import_match_admin,SPP Import Matching Admin Access,spp_import_match.model_spp_import_match,g2p_registry_base.group_g2p_admin,1,1,1,1
spp_import_match_fields_admin,SPP Import Matching Fields Admin Access,spp_import_match.model_spp_import_match_fields,g2p_registry_base.group_g2p_admin,1,1,1,1
Binary file added spp_import_match/static/description/icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions spp_import_match/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import test_res_partner_import_match
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Renaud,[email protected]
1 change: 1 addition & 0 deletions spp_import_match/tests/import_data/res_partner_name.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Renaud,Rufino,Renaud Rufino,[email protected]
Loading
Loading