-
Notifications
You must be signed in to change notification settings - Fork 137
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ff49dfb
commit e9b5d07
Showing
7 changed files
with
118 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
# Copyright 2024 Ivan Yelizariev <https://twitter.com/yelizariev> | ||
import base64 | ||
import csv | ||
import json | ||
from io import StringIO | ||
|
||
import yaml | ||
|
||
from odoo import fields, models | ||
|
||
|
||
class SyncData(models.Model): | ||
_name = "sync.data" | ||
_description = "Sync Data File" | ||
|
||
name = fields.Char("Technical name") | ||
project_id = fields.Many2one("sync.project", ondelete="cascade") | ||
file_name = fields.Char("File Name") | ||
file_content = fields.Binary("File Content") | ||
|
||
def csv(self, *args, **kwargs): | ||
"""Parse CSV file from binary field.""" | ||
if self.file_content: | ||
file_content = base64.b64decode(self.file_content) | ||
file_content = file_content.decode("utf-8") | ||
file_like_object = StringIO(file_content) | ||
reader = csv.DictReader(file_like_object, *args, **kwargs) | ||
return [row for row in reader] | ||
return [] | ||
|
||
def json(self): | ||
"""Parse JSON file from binary field.""" | ||
if self.file_content: | ||
file_content = base64.b64decode(self.file_content) | ||
file_content = file_content.decode("utf-8") | ||
return json.loads(file_content) | ||
return {} | ||
|
||
def yaml(self): | ||
"""Parse YAML file from binary field.""" | ||
if self.file_content: | ||
file_content = base64.b64decode(self.file_content) | ||
file_content = file_content.decode("utf-8") | ||
return yaml.safe_load(file_content) | ||
return None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters