Skip to content
Snippets Groups Projects
Commit 32a53dec9881 authored by Houzefa Abbasbhay's avatar Houzefa Abbasbhay :slight_smile:
Browse files

Merge 13.0 (13.0.3.2.0 with fastjsonschema)

No related branches found
Tags 15.0.3.2.0
2 merge requests!37Merge 15.0 (15.0.3.2.0 with fastjsonschema),!36Merge 13.0 (13.0.3.2.0 with fastjsonschema)
......@@ -11,3 +11,4 @@
b9202f7d5dc1fe8c3d532c8c441c9f11b5911acb 13.0.3.0.2
bc4a32eb10ad712d229fed2017dde115cdb1d432 13.0.3.1.0
1ec96eae270f2bac3b80666e469d67d24b27874b 15.0.3.1.0
da9a0fe039b65f5cfd3d5a5a60eaac2665026a91 13.0.3.2.0
Changelog
=========
15.0.3.2.0
----------
(port from 13.0.3.2.0)
~~~~~~~~~~~~~~~~~~~~~~
Export constants from ``.base``.
Used by other modules; these exports got removed when I replaced an ``import *`` earlier.
``jsonschema`` ➔ ``fastjsonschema``.
15.0.3.1.0
----------
......
......@@ -20,6 +20,11 @@
from . import models
from .base import (
OPERATION_CREATION,
OPERATION_UPDATE,
PHASE_POSTCREATE,
PHASE_PRECREATE,
PHASE_UPDATE,
Computed,
Constant,
ContextBuilder,
......
......@@ -22,6 +22,6 @@
"name": "Converter",
"license": "AGPL-3",
"summary": "Convert odoo records to/from plain data structures.",
"version": "15.0.3.1.0",
"version": "15.0.3.2.0",
"category": "Hidden",
"author": "XCG Consulting",
......@@ -26,7 +26,7 @@
"category": "Hidden",
"author": "XCG Consulting",
"website": "https://odoo.consulting/",
"website": "https://orbeet.io/",
"depends": ["base", "mail"],
"data": [],
"installable": True,
# These dependencies are in the "requirements" file.
......@@ -29,6 +29,6 @@
"depends": ["base", "mail"],
"data": [],
"installable": True,
# These dependencies are in the "requirements" file.
"external_dependencies": {"python": ["jsonschema"]},
"external_dependencies": {"python": ["fastjsonschema"]},
}
......@@ -23,8 +23,6 @@
from collections.abc import Iterable, Mapping
from typing import Any, Optional
import jsonschema
from odoo import api, models
from .base import (
......@@ -97,7 +95,7 @@
if self.validation != VALIDATION_SKIP and self._jsonschema is not None:
try:
self.validator.validate(self._jsonschema, message_data)
except jsonschema.exceptions.ValidationError as exception:
except Exception as exception:
_logger.warning("Validation failed", exc_info=1)
if self.validation == VALIDATION_STRICT:
raise exception
......
# Used by converter module
jsonschema==3.2.0
fastjsonschema
......@@ -19,6 +19,5 @@
##############################################################################
import json
import math
import os
......@@ -23,6 +22,6 @@
import os
import jsonschema
import fastjsonschema
import odoo.addons
......@@ -46,5 +45,4 @@
self.repository = repository
# exemple "https://annonces-legales.fr/xbus/schemas/v1/{}.schema.json"
self.default_url_pattern = default_url_pattern
self.schemastore = {}
self.validators = {}
......@@ -50,5 +48,4 @@
self.validators = {}
self.search_path = "not initialized, please call initialize()"
self.initialized = False
self.encoding = "UTF-8"
......@@ -56,6 +53,9 @@
repo_module_basepath = os.path.dirname(
getattr(odoo.addons, self.repository_module_name).__file__
)
# Read local schema definitions.
schemas = {}
schema_search_path = os.path.abspath(
os.path.join(repo_module_basepath, self.repository)
)
......@@ -59,12 +59,10 @@
schema_search_path = os.path.abspath(
os.path.join(repo_module_basepath, self.repository)
)
self.search_path = schema_search_path
for root, _dirs, files in os.walk(self.search_path):
for root, _dirs, files in os.walk(schema_search_path):
for fname in files:
fpath = os.path.join(root, fname)
if fpath.endswith((".json",)):
with open(fpath, "r", encoding=self.encoding) as schema_fd:
schema = json.load(schema_fd)
if "$id" in schema:
......@@ -65,9 +63,18 @@
for fname in files:
fpath = os.path.join(root, fname)
if fpath.endswith((".json",)):
with open(fpath, "r", encoding=self.encoding) as schema_fd:
schema = json.load(schema_fd)
if "$id" in schema:
self.schemastore[schema["$id"]] = schema
schemas[schema["$id"]] = schema
# Prepare validators for each schema. We add an HTTPS handler that
# points back to our schema definition cache built above.
for schema_id, schema in schemas.items():
self.validators[schema_id] = fastjsonschema.compile(
schema,
handlers={"https": lambda uri: schemas[uri]},
use_default=False,
)
self.initialized = True
......@@ -72,6 +79,6 @@
self.initialized = True
def validate(self, schema_id, doc) -> None:
def validate(self, schema_id, payload) -> None:
if not self.initialized:
raise NotInitialized("please call the initialize() method")
......@@ -75,34 +82,4 @@
if not self.initialized:
raise NotInitialized("please call the initialize() method")
uri = "file://{}/{}.schema.json".format(self.search_path, schema_id)
validator = self.validators.get(uri)
if validator is None:
schema = self.schemastore[self.default_url_pattern.format(schema_id)]
resolver = jsonschema.RefResolver(uri, schema, self.schemastore)
validator = jsonschema.Draft7Validator(schema, resolver=resolver)
self.validators[uri] = validator
validator.validate(doc)
def multiple_of(validator, db, instance, schema):
"""Fix rounding which fails when comparing e.g. 66.6 vs 0.01"""
if not validator.is_type(instance, "number"):
return
if isinstance(db, float):
quotient = instance / db
failed = not math.isclose(round(quotient), quotient)
else:
failed = instance % db
if failed:
yield jsonschema.ValidationError("%r is not a multiple of %r" % (instance, db))
# Fix rounding which fails when comparing e.g. 66.6 vs 0.01
jsonschema.Draft7Validator.VALIDATORS["multipleOf"] = multiple_of
self.validators[self.default_url_pattern.format(schema_id)](payload)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment