Skip to content

Commit

Permalink
[Add] New Variables - v0.1.2
Browse files Browse the repository at this point in the history
- Version
```python
print(odoo.version)
#17.0
```
- User Context
```python
print(odoo.env.context)
#{'lang': 'en_US', 'tz': 'Europe/Brussels', 'uid': 2}
```
- User Info
```python
print(odoo.env.user_info)
#{'uid': 2, 'is_admin': True, 'name': 'Mitchell Admin', 'username': 'admin', 'partner_id': 3}
```
- Settings
```python
print(odoo.env.settings)
#{'web_base_url': 'https://demo.odoo.com', 'localization': {'lang': 'en_US', 'tz': 'Europe/Brussels'}, 'company_details': {'current_company': 1, 'allowed_companies': {'2': {'id': 2, 'name': 'My Company (Chicago)', 'sequence': 10, 'child_ids': [], 'parent_id': False, 'timesheet_uom_id': 4, 'timesheet_uom_factor': 1.0}, '1': {'id': 1, 'name': 'My Company (San Francisco)', 'sequence': 0, 'child_ids': [], 'parent_id': False, 'timesheet_uom_id': 4, 'timesheet_uom_factor': 1.0}}, 'disallowed_ancestor_companies': {}}}
```
  • Loading branch information
fasilwdr committed May 9, 2024
1 parent e033df8 commit ae2dee7
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 4 deletions.
26 changes: 25 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,17 +59,21 @@ partner.update({'mobile': '12345678'})
```python
partner_ids = odoo.env['res.partner'].search(domain=[('name', '=', 'Abigail Peterson')])
print(partner_ids)
#[50]
```
- Read Records
```python
print(partner.name)
records = odoo.env['res.partner'].read(ids=search_check, fields=['name', 'email'])
records = odoo.env['res.partner'].read(ids=partner_ids, fields=['name', 'email'])
print(records)
#Wood Corner
#[{'id': 50, 'name': 'Abigail Peterson', 'email': 'abigail.peterson39@example.com'}]
```
- Create a New Record
```python
new_partner_id = odoo.env['res.partner'].create({'name': 'New Partner', 'email': 'new@partner.com', 'is_company': True})
print(new_partner_id)
#100
```
- Update Records
```python
Expand All @@ -83,6 +87,26 @@ odoo.env['res.partner'].unlink(ids=new_partner_id)
```python
odoo.download_report(report_name='sale.report_saleorder', record_ids=[52], file_name='Sales Report')
```
- Version
```python
print(odoo.version)
#17.0
```
- User Context
```python
print(odoo.env.context)
#{'lang': 'en_US', 'tz': 'Europe/Brussels', 'uid': 2}
```
- User Info
```python
print(odoo.env.user_info)
#{'uid': 2, 'is_admin': True, 'name': 'Mitchell Admin', 'username': 'admin', 'partner_id': 3}
```
- Settings
```python
print(odoo.env.settings)
#{'web_base_url': 'https://demo.odoo.com', 'localization': {'lang': 'en_US', 'tz': 'Europe/Brussels'}, 'company_details': {'current_company': 1, 'allowed_companies': {'2': {'id': 2, 'name': 'My Company (Chicago)', 'sequence': 10, 'child_ids': [], 'parent_id': False, 'timesheet_uom_id': 4, 'timesheet_uom_factor': 1.0}, '1': {'id': 1, 'name': 'My Company (San Francisco)', 'sequence': 0, 'child_ids': [], 'parent_id': False, 'timesheet_uom_id': 4, 'timesheet_uom_factor': 1.0}}, 'disallowed_ancestor_companies': {}}}
```

## Contributing
Contributions are welcome! Please feel free to submit pull requests, report bugs, or suggest features.
Expand Down
37 changes: 36 additions & 1 deletion pyodoo_connect/odoo.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,28 @@ class Environment:
def __init__(self, odoo_instance):
self.odoo = odoo_instance
self._model_cache = {}
self.context = {} # Initializes an empty context
self.user_info = {}
self.settings = {}

def update_context(self, result):
"""Update the environment with new context and other relevant data."""
self.context = result.get('user_context', {})
self.user_info = {
'uid': result.get('uid'),
'is_admin': result.get('is_admin'),
'name': result.get('name'),
'username': result.get('username'),
'partner_id': result.get('partner_id'),
}
self.settings = {
'web_base_url': result.get('web.base.url'),
'localization': {
'lang': self.context.get('lang'),
'tz': self.context.get('tz')
},
'company_details': result.get('user_companies', {})
}

def __getitem__(self, model_name):
if model_name not in self._model_cache:
Expand All @@ -37,6 +59,7 @@ def __init__(self, url, db, username, password):
self.jsonrpc_url = f"{url}jsonrpc" if url[-1] == '/' else f"{url}/jsonrpc"
self.session = None
self.uid = None
self.version = None
self.env = Environment(self)
self.login() # Automatically login on initialization

Expand Down Expand Up @@ -72,16 +95,28 @@ def login(self):
""" Authenticate and establish a session with the Odoo server. """
login_url = f"{self.url}/web/session/authenticate"
headers = {'Content-Type': 'application/json'}
data = json.dumps({"jsonrpc": "2.0", "params": {"db": self.db, "login": self.username, "password": self.password}}).encode('utf-8')
data = json.dumps({
"jsonrpc": "2.0",
"params": {
"db": self.db,
"login": self.username,
"password": self.password
}
}).encode('utf-8')

req = urllib.request.Request(login_url, data=data, headers=headers)
self.session = urllib.request.build_opener(urllib.request.HTTPCookieProcessor())

try:
with self.session.open(req) as response:
result = json.loads(response.read().decode())
if result.get('result'):
self.uid = result.get('result').get('uid')
self.version = result.get('result').get('server_version')
if self.uid is None:
raise Exception("Failed to obtain user ID.")
# Update context
self.env.update_context(result.get('result'))
else:
raise Exception("Login failed: " + str(result.get('error')))
except HTTPError as e:
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"

[project]
name = "pyodoo_connect"
version = "0.1.1"
version = "0.1.2"
description = "A Python package to interact with Odoo via JSON-RPC."
authors = [{ name = "Fasil", email = "fasilwdr@hotmail.com" }]
license = { file = "LICENSE" }
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[metadata]
name = pyodoo_connect
version = 0.1.1
version = 0.1.2
author = Fasil
author_email = fasilwdr@hotmail.com
description = A Python package to interact with Odoo via JSON-RPC.
Expand Down

0 comments on commit ae2dee7

Please sign in to comment.