Use actions auth session get/set for sessions
This commit is contained in:
+40
-7
@@ -1,23 +1,48 @@
|
|||||||
import os
|
import subprocess
|
||||||
import requests
|
import requests
|
||||||
import random
|
import random
|
||||||
from typing import Any, TypeVar
|
from typing import Any, TypeVar
|
||||||
|
from urllib.parse import urlparse
|
||||||
from model import OdooModel
|
from model import OdooModel
|
||||||
|
|
||||||
|
|
||||||
T = TypeVar("T", bound=OdooModel)
|
T = TypeVar("T", bound=OdooModel)
|
||||||
|
|
||||||
|
|
||||||
|
def _get_odoo_session_id() -> str:
|
||||||
|
result = subprocess.run(
|
||||||
|
["actions", "auth", "session", "get", "odoo"],
|
||||||
|
capture_output=True,
|
||||||
|
text=True,
|
||||||
|
)
|
||||||
|
lines = result.stdout.splitlines()
|
||||||
|
if len(lines) > 1:
|
||||||
|
lines = lines[1:]
|
||||||
|
session_id = lines[0].strip() if lines else ""
|
||||||
|
if not session_id:
|
||||||
|
raise Exception(
|
||||||
|
"Failed to retrieve odoo session_id via `actions auth session get odoo`"
|
||||||
|
)
|
||||||
|
return session_id
|
||||||
|
|
||||||
|
|
||||||
|
def _set_odoo_session_id(session_id: str) -> None:
|
||||||
|
subprocess.run(
|
||||||
|
["actions", "auth", "session", "set", "odoo", session_id],
|
||||||
|
capture_output=True,
|
||||||
|
text=True,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
class OdooClient:
|
class OdooClient:
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.base_url = "https://www.odoo.com"
|
self.base_url = "https://www.odoo.com"
|
||||||
|
self._domain = urlparse(self.base_url).hostname
|
||||||
self.session = requests.Session()
|
self.session = requests.Session()
|
||||||
if session_id := os.getenv("ODOO_SESSION_ID"):
|
self._session_id = _get_odoo_session_id()
|
||||||
self.session.cookies.set("session_id", session_id)
|
self.session.cookies.set("session_id", self._session_id, domain=self._domain)
|
||||||
else:
|
self.session.cookies.set("cids", "1", domain=self._domain)
|
||||||
raise Exception("ODOO_SESSION_ID is not set")
|
self.session.cookies.set("frontend_lang", "en_US", domain=self._domain)
|
||||||
self.session.cookies.set("cids", "1")
|
|
||||||
self.session.cookies.set("frontend_lang", "en_US")
|
|
||||||
|
|
||||||
def rpc(
|
def rpc(
|
||||||
self, model: str, method: str, *args: Any, **kwargs: Any
|
self, model: str, method: str, *args: Any, **kwargs: Any
|
||||||
@@ -39,6 +64,14 @@ class OdooClient:
|
|||||||
json=body,
|
json=body,
|
||||||
)
|
)
|
||||||
response.raise_for_status()
|
response.raise_for_status()
|
||||||
|
|
||||||
|
new_session_id = self.session.cookies.get(
|
||||||
|
"session_id", domain=self._domain
|
||||||
|
)
|
||||||
|
if new_session_id and new_session_id != self._session_id:
|
||||||
|
_set_odoo_session_id(new_session_id)
|
||||||
|
self._session_id = new_session_id
|
||||||
|
|
||||||
return response
|
return response
|
||||||
|
|
||||||
def web_search_read(
|
def web_search_read(
|
||||||
|
|||||||
Reference in New Issue
Block a user