Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[14.0][ADD] attachment synchronize mixins #285

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file.
1 change: 1 addition & 0 deletions attachment_synchronize_mixins/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import models
18 changes: 18 additions & 0 deletions attachment_synchronize_mixins/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Copyright 2023 Akretion
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).

{
"name": "Attachment Synchronize exportable importable",
"summary": """
Base synchronizable mixins""",
"version": "14.0.1.0.0",
"license": "AGPL-3",
"author": "Akretion,Odoo Community Association (OCA)",
"website": "https://github.com/akretion/ak-odoo-incubator",
"depends": [
"base_sparse_field",
"attachment_synchronize",
],
"data": [],
"demo": [],
}
3 changes: 3 additions & 0 deletions attachment_synchronize_mixins/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from . import attachment_synchronize_task
from . import synchronize_importable_mixin
from . import synchronize_exportable_mixin
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Copyright 2023 Akretion
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).

from odoo import models


class AttachmentSynchronizeTask(models.Model):
_inherit = "attachment.synchronize.task"

def scheduler_export(self, model, domain=False):
if not domain:
recs = self.env[model].search()

Check warning on line 12 in attachment_synchronize_mixins/models/attachment_synchronize_task.py

View check run for this annotation

Codecov / codecov/patch

attachment_synchronize_mixins/models/attachment_synchronize_task.py#L12

Added line #L12 was not covered by tests
else:
recs = self.env[model].search(domain)

Check warning on line 14 in attachment_synchronize_mixins/models/attachment_synchronize_task.py

View check run for this annotation

Codecov / codecov/patch

attachment_synchronize_mixins/models/attachment_synchronize_task.py#L14

Added line #L14 was not covered by tests
if not recs:
return
attachments = recs.with_context(attachment_task=self).synchronize_export()
attachments.run()

Check warning on line 18 in attachment_synchronize_mixins/models/attachment_synchronize_task.py

View check run for this annotation

Codecov / codecov/patch

attachment_synchronize_mixins/models/attachment_synchronize_task.py#L16-L18

Added lines #L16 - L18 were not covered by tests
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# Copyright 2023 Akretion
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).

import base64
import csv
import datetime
import uuid
from io import StringIO

from odoo import fields, models


class SynchronizeExportableMixin(models.AbstractModel):
_name = "synchronize.exportable.mixin"
_description = "Synchronizable export mixin"
export_date = fields.Date()
export_attachment = fields.Char()
export_flag = fields.Boolean()

def synchronize_export(self):
data = self._prepare_export_data()

Check warning on line 21 in attachment_synchronize_mixins/models/synchronize_exportable_mixin.py

View check run for this annotation

Codecov / codecov/patch

attachment_synchronize_mixins/models/synchronize_exportable_mixin.py#L21

Added line #L21 was not covered by tests
if not data:
return self.env["attachment.queue"]
attachment = self._format_to_exportfile(data)
self.track_export(attachment)
self.export_flag = False
return attachment

Check warning on line 27 in attachment_synchronize_mixins/models/synchronize_exportable_mixin.py

View check run for this annotation

Codecov / codecov/patch

attachment_synchronize_mixins/models/synchronize_exportable_mixin.py#L23-L27

Added lines #L23 - L27 were not covered by tests

def track_export(self, attachment):
self.export_date = datetime.datetime.now()
self.export_attachment = attachment

Check warning on line 31 in attachment_synchronize_mixins/models/synchronize_exportable_mixin.py

View check run for this annotation

Codecov / codecov/patch

attachment_synchronize_mixins/models/synchronize_exportable_mixin.py#L30-L31

Added lines #L30 - L31 were not covered by tests

def _prepare_export_data(self) -> list:
raise NotImplementedError

Check warning on line 34 in attachment_synchronize_mixins/models/synchronize_exportable_mixin.py

View check run for this annotation

Codecov / codecov/patch

attachment_synchronize_mixins/models/synchronize_exportable_mixin.py#L34

Added line #L34 was not covered by tests

def _format_to_exportfile(self, data):
return self._format_to_exportfile_csv(data)

Check warning on line 37 in attachment_synchronize_mixins/models/synchronize_exportable_mixin.py

View check run for this annotation

Codecov / codecov/patch

attachment_synchronize_mixins/models/synchronize_exportable_mixin.py#L37

Added line #L37 was not covered by tests

def _format_to_exportfile_csv(self, data):
csv_file = StringIO()
delimiter = self.env.context.get("csv_delimiter") or ";"
writer = csv.DictWriter(

Check warning on line 42 in attachment_synchronize_mixins/models/synchronize_exportable_mixin.py

View check run for this annotation

Codecov / codecov/patch

attachment_synchronize_mixins/models/synchronize_exportable_mixin.py#L40-L42

Added lines #L40 - L42 were not covered by tests
csv_file, fieldnames=data[0].keys(), delimiter=delimiter
)
for row in data:
writer.writerow(row)
csv_file.seek(0)
ast = self.env.context.get("attachment_task")
vals = {

Check warning on line 49 in attachment_synchronize_mixins/models/synchronize_exportable_mixin.py

View check run for this annotation

Codecov / codecov/patch

attachment_synchronize_mixins/models/synchronize_exportable_mixin.py#L46-L49

Added lines #L46 - L49 were not covered by tests
"name": self._get_export_name(),
"datas": base64.b64encode(csv_file.getvalue().encode("utf-8")),
"task_id": ast.id,
"file_type": ast.file_type,
}
return self.env["attachment.queue"].create(vals)

Check warning on line 55 in attachment_synchronize_mixins/models/synchronize_exportable_mixin.py

View check run for this annotation

Codecov / codecov/patch

attachment_synchronize_mixins/models/synchronize_exportable_mixin.py#L55

Added line #L55 was not covered by tests

def _get_export_name(self):
return str(uuid.uuid4())

Check warning on line 58 in attachment_synchronize_mixins/models/synchronize_exportable_mixin.py

View check run for this annotation

Codecov / codecov/patch

attachment_synchronize_mixins/models/synchronize_exportable_mixin.py#L58

Added line #L58 was not covered by tests
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Copyright 2023 Akretion
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).

import datetime

from odoo import fields, models


class SynchronizeImportableMixin(models.AbstractModel):
_name = "synchronize.importable.mixin"
_description = "Synchronizable import mixin"

import_date = fields.Date()
import_file_id = fields.Many2one("attachment.queue")

def track_model_import(self):
self.write(

Check warning on line 17 in attachment_synchronize_mixins/models/synchronize_importable_mixin.py

View check run for this annotation

Codecov / codecov/patch

attachment_synchronize_mixins/models/synchronize_importable_mixin.py#L17

Added line #L17 was not covered by tests
{
"import_date": datetime.datetime.now(),
"import_file_id": self.env.context.get("attachment_queue"),
}
)
1 change: 1 addition & 0 deletions attachment_synchronize_mixins/readme/CONTRIBUTORS.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* Kevin Khao <[email protected]>
1 change: 1 addition & 0 deletions attachment_synchronize_mixins/readme/DESCRIPTION.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Base functionality for import and export of external systems with attachment_synchronize
Empty file.
Empty file.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading