From a5bd965cdeda940b52247318a55151826badee36 Mon Sep 17 00:00:00 2001 From: Fasil | Python/Odoo Developer Date: Sat, 11 May 2024 00:13:35 +0300 Subject: [PATCH] [Add] New Feature with_context in v0.1.3 - With Context record_id.with_context({'send_rfq':True}).action_rfq_send() #or record_id.with_context(send_rfq=True).action_rfq_send() --- README.md | 7 ++++ pyodoo_connect/proxies.py | 76 +++++++++++++++++++++------------------ pyproject.toml | 2 +- setup.cfg | 2 +- 4 files changed, 50 insertions(+), 37 deletions(-) diff --git a/README.md b/README.md index 04df660..030578a 100644 --- a/README.md +++ b/README.md @@ -103,6 +103,13 @@ odoo.download_report(report_name='sale.report_saleorder', record_ids=[52], file_ print(odoo.version) #17.0 ``` +- With Context +```python +record_id = odoo.env['purchase.order'].browse(14) +record_id.with_context({'send_rfq':True}).action_rfq_send() +#or +record_id.with_context(send_rfq=True).action_rfq_send() +``` - UID ```python print(odoo.uid) diff --git a/pyodoo_connect/proxies.py b/pyodoo_connect/proxies.py index 46fc799..5e0dba5 100644 --- a/pyodoo_connect/proxies.py +++ b/pyodoo_connect/proxies.py @@ -6,53 +6,37 @@ # Facebook: https://www.facebook.com/fasilwdr # Instagram: https://www.instagram.com/fasilwdr ############################################################################# - -class MethodCaller: - def __init__(self, model_proxy, record_id, method_name): - self.model_proxy = model_proxy - self.record_id = record_id - self.method_name = method_name - - def __call__(self, *args, **kwargs): - if self.method_name == 'write': - self.method_name = 'update' - # If there are positional arguments, assume the first is a dictionary and reformat as needed - if args and isinstance(args[0], dict): - kwargs = {'values': args[0]} # Override kwargs with values from args - elif args: - # If args are not a dictionary, raise an error or handle appropriately - raise ValueError( - "Positional arguments must be a single dictionary containing the parameters for the method.") - return self.model_proxy.odoo.execute_function( - self.model_proxy.model_name, - [self.record_id], - self.method_name, - **kwargs - ) +from typing import Dict, Any class RecordProxy: - def __init__(self, model_proxy, record_id): + def __init__(self, model_proxy, record_id, context=None): self.model_proxy = model_proxy self.record_id = record_id + self.context = context if context is not None else {} self.values = {} self.loaded_fields = set() + def with_context(self, *args, **kwargs): + if args and isinstance(args[0], dict): + kwargs = args[0] + # Return a new instance of RecordProxy with the updated context + new_context = self.context.copy() + new_context.update(kwargs) + return RecordProxy(self.model_proxy, self.record_id, new_context) + def __getattr__(self, name): if name in self.values: return self.values[name] elif name not in self.loaded_fields: - # Attempt to fetch the field first. If not present, treat as method try: self.fetch_field(name) return self.values.get(name, None) except KeyError: pass - # If it's not a field, treat it as a method call - return MethodCaller(self.model_proxy, self.record_id, name) + return MethodCaller(self.model_proxy, self.record_id, name, self.context) def fetch_field(self, field_name): - # Fetch single field and check if it exists fields_info = self.model_proxy.fields_get([field_name], ['name', 'type']) if field_name not in fields_info: raise KeyError(f"No such field '{field_name}' on model.") @@ -62,18 +46,40 @@ def fetch_field(self, field_name): self.loaded_fields.add(field_name) def __setattr__(self, name, value): - if name in ['model_proxy', 'record_id', 'values', 'loaded_fields'] or name.startswith('_'): + if name in ['model_proxy', 'record_id', 'values', 'loaded_fields', 'context'] or name.startswith('_'): super().__setattr__(name, value) else: self.model_proxy.write(ids=[self.record_id], values={name: value}) - self.values[name] = value # Update the local cache after the write operation + self.values[name] = value - def _fetch_all(self): - # Optional: Method to manually fetch all fields if needed - fields = self.model_proxy.fields_get([], ['name']) - self.values = self.model_proxy.read([self.record_id], list(fields.keys()))[0] - self.loaded_fields.update(fields.keys()) +# MethodCaller class modification to include context in RPC calls +class MethodCaller: + def __init__(self, model_proxy, record_id, method_name, context=None): + self.model_proxy = model_proxy + self.record_id = record_id + self.method_name = method_name + self.context = context if context is not None else {} + + def __call__(self, *args, **kwargs): + if self.method_name == 'write': + self.method_name = 'update' + if args and isinstance(args[0], dict): + kwargs = {'values': args[0]} + elif args: + raise ValueError( + "Positional arguments must be a single dictionary containing the parameters for the method.") + + # Merging context into kwargs for RPC call + if self.context: + kwargs['context'] = self.context + + return self.model_proxy.odoo.execute_function( + self.model_proxy.model_name, + [self.record_id], + self.method_name, + **kwargs + ) class ModelProxy: diff --git a/pyproject.toml b/pyproject.toml index e9e88e2..0be423d 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta" [project] name = "pyodoo_connect" -version = "0.1.2" +version = "0.1.3" description = "A Python package to interact with Odoo via JSON-RPC." authors = [{ name = "Fasil", email = "fasilwdr@hotmail.com" }] license = { file = "LICENSE" } diff --git a/setup.cfg b/setup.cfg index aa01ad1..ed50c30 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,6 +1,6 @@ [metadata] name = pyodoo_connect -version = 0.1.2 +version = 0.1.3 author = Fasil author_email = fasilwdr@hotmail.com description = A Python package to interact with Odoo via JSON-RPC.