Skip to content
Draft
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
6 changes: 6 additions & 0 deletions setup/spreadsheet_oca_upload_base/setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import setuptools

setuptools.setup(
setup_requires=['setuptools-odoo'],
odoo_addon=True,
)
168 changes: 168 additions & 0 deletions spreadsheet_oca_upload_base/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
===========================
Spreadsheet OCA Upload Base
===========================

..
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! This file is generated by oca-gen-addon-readme !!
!! changes will be overwritten. !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! source digest: sha256:6495e77fa5a250c4e672af7e3a502bc571a4df9c527461a1031f4f2bfc593dc6
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

.. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png
:target: https://odoo-community.org/page/development-status
:alt: Beta
.. |badge2| image:: https://img.shields.io/badge/licence-AGPL--3-blue.png
:target: http://www.gnu.org/licenses/agpl-3.0-standalone.html
:alt: License: AGPL-3
.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Fspreadsheet-lightgray.png?logo=github
:target: https://github.com/OCA/spreadsheet/tree/16.0/spreadsheet_oca_upload_base
:alt: OCA/spreadsheet
.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png
:target: https://translation.odoo-community.org/projects/spreadsheet-16-0/spreadsheet-16-0-spreadsheet_oca_upload_base
:alt: Translate me on Weblate
.. |badge5| image:: https://img.shields.io/badge/runboat-Try%20me-875A7B.png
:target: https://runboat.odoo-community.org/builds?repo=OCA/spreadsheet&target_branch=16.0
:alt: Try me on Runboat

|badge1| |badge2| |badge3| |badge4| |badge5|

- This module extends the integration with Odoo Spreadsheet to allow uploading XLSX files directly from any record.

- It simplifies spreadsheet management by automatically generating attachments, validating file types, and linking the spreadsheet to the record, enabling quick and efficient access and updates.

**Table of contents**

.. contents::
:local:

Usage
=====

The SpreadsheetUploadMixin mixin can be used when a model needs the ability to upload an XLSX file and automatically create or link a spreadsheet document in Odoo.
It allows users to manage spreadsheets directly from the form view of a record.

Let's consider the following models:

.. code-block:: python

class MyModelA(models.Model):
_name = "my.model.a"
_inherit = ["spreadsheet.upload.mixin"]

By inheriting from spreadsheet.upload.mixin, the model gains the following:

- upload_file: binary field to upload an XLSX file.

- file_name: helper field to store the filename.

- spreadsheet_id: link to the generated spreadsheet.spreadsheet record.

- action_create_spreadsheet(): creates a new spreadsheet from the uploaded file.

- action_open_spreadsheet(): opens the linked spreadsheet directly from the form.

A corresponding form view can then be defined to expose these fields and buttons:

.. code-block:: xml

<record id="my_model_a_form_view" model="ir.ui.view">
<field name="name">my.model.a.form.view</field>
<field name="model">my.model.a</field>
<field name="arch" type="xml">
<form>
<sheet>
<group>
<div class="o_row">
<label for="spreadsheet_id"
attrs="{'invisible': [('spreadsheet_id', '=', False)]}" />
<div attrs="{'invisible': [('spreadsheet_id', '=', False)]}"
style="margin-bottom: 15px;">
<field name="spreadsheet_id"
readonly="1"
class="oe_inline"
style="margin-right:7px;"/>
<button name="action_open_spreadsheet"
type="object"
string="Edit"
icon="fa-pencil"
class="btn btn-primary"/>
</div>
</div>

<div class="o_row">
<label for="upload_file"
class="o_form_label"
style="font-weight: bold;"
attrs="{'invisible': ['|', ('upload_file', '=', False), ('spreadsheet_id', '!=', False)]}"/>
<field name="upload_file"
filename="file_name"
nolabel="1"
attrs="{'invisible': [('spreadsheet_id', '!=', False)]}"/>
<div style="margin-right: 400px; margin-bottom: 15px;">
<button name="action_create_spreadsheet"
type="object"
string="Create Spreadsheet From File"
class="btn btn-primary"
attrs="{'invisible': ['|', ('upload_file', '=', False), ('spreadsheet_id', '!=', False)]}"/>
</div>
</div>

<field name="file_name" invisible="1"/>
</group>
</sheet>
</form>
</field>
</record>

By using this mixin and view definition, you can upload an XLSX file from any record form, create a linked Odoo Spreadsheet, and open it for editing directly from the form view.

Bug Tracker
===========

Bugs are tracked on `GitHub Issues <https://github.com/OCA/spreadsheet/issues>`_.
In case of trouble, please check there if your issue has already been reported.
If you spotted it first, help us to smash it by providing a detailed and welcomed
`feedback <https://github.com/OCA/spreadsheet/issues/new?body=module:%20spreadsheet_oca_upload_base%0Aversion:%2016.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**>`_.

Do not contact contributors directly about support or help with technical issues.

Credits
=======

Authors
~~~~~~~

* BizzAppDev Systems Pvt. Ltd.

Contributors
~~~~~~~~~~~~

* `BizzAppDev Systems <https://www.bizzappdev.com>`_:

* Ruchir Shukla <ruchir@bizzappdev.com>

Other credits
~~~~~~~~~~~~~

The development of this module has been financially supported by:

- Agent ERP GmbH

Maintainers
~~~~~~~~~~~

This module is maintained by the OCA.

.. image:: https://odoo-community.org/logo.png
:alt: Odoo Community Association
:target: https://odoo-community.org

OCA, or the Odoo Community Association, is a nonprofit organization whose
mission is to support the collaborative development of Odoo features and
promote its widespread use.

This module is part of the `OCA/spreadsheet <https://github.com/OCA/spreadsheet/tree/16.0/spreadsheet_oca_upload_base>`_ project on GitHub.

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.
1 change: 1 addition & 0 deletions spreadsheet_oca_upload_base/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import models
11 changes: 11 additions & 0 deletions spreadsheet_oca_upload_base/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"name": "Spreadsheet OCA Upload Base",
"summary": "Base module for uploading spreadsheets to the model",
"version": "16.0.1.0.0",
"license": "AGPL-3",
"author": "BizzAppDev Systems Pvt. Ltd., Odoo Community Association (OCA)",
"website": "https://github.com/OCA/spreadsheet",
"depends": ["spreadsheet_oca"],
"installable": True,
"auto_install": False,
}
1 change: 1 addition & 0 deletions spreadsheet_oca_upload_base/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import spreadsheet_upload_mixin
64 changes: 64 additions & 0 deletions spreadsheet_oca_upload_base/models/spreadsheet_upload_mixin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
from odoo import _, api, fields, models
from odoo.exceptions import ValidationError


class SpreadsheetUploadMixin(models.AbstractModel):
_name = "spreadsheet.upload.mixin"
_description = "Mixin to upload XLSX and create linked spreadsheet"

spreadsheet_id = fields.Many2one(
comodel_name="spreadsheet.spreadsheet",
string="Spreadsheet",
help="Linked Spreadsheet",
copy=False,
)
upload_file = fields.Binary(
string="Upload XLSX", copy=False, help="XLSX file to upload"
)
file_name = fields.Char(copy=False, help="Name of the uploaded file")

@api.constrains("file_name")
def _check_xlsx_file_type(self):
"""New constraint to check uploaded file type is XLSX."""
invalid_files = self.filtered(
lambda spreadsheet: not spreadsheet.file_name.lower().endswith(".xlsx")
)
if invalid_files:
raise ValidationError(_("Please upload a valid XLSX file."))

def _get_attachment(self):
"""New method to search for the attachment of the uploaded file."""
self.ensure_one()
attachment = self.env["ir.attachment"].search(
[
("res_model", "=", self._name),
("res_id", "=", self.id),
("res_field", "=", "upload_file"),
],
limit=1,
)
if attachment:
attachment.write(
{
"name": self.file_name or "uploaded_file",
}
)
return attachment

def action_create_spreadsheet(self):
"""New method to create spredsheet from the upload file."""
for record in self:
attachment = record._get_attachment()
if not attachment:
continue
spreadsheet = self.env[
"spreadsheet.spreadsheet"
].create_document_from_attachment([attachment.id])
record.spreadsheet_id = spreadsheet.get("res_id") if spreadsheet else False

def action_open_spreadsheet(self):
"""New method to open linked spredsheet."""
self.ensure_one()
if not self.spreadsheet_id:
return
return self.spreadsheet_id.open_spreadsheet()
3 changes: 3 additions & 0 deletions spreadsheet_oca_upload_base/readme/CONTRIBUTORS.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
* `BizzAppDev Systems <https://www.bizzappdev.com>`_:

* Ruchir Shukla <ruchir@bizzappdev.com>
3 changes: 3 additions & 0 deletions spreadsheet_oca_upload_base/readme/CREDITS.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
The development of this module has been financially supported by:

- Agent ERP GmbH
3 changes: 3 additions & 0 deletions spreadsheet_oca_upload_base/readme/DESCRIPTION.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
- This module extends the integration with Odoo Spreadsheet to allow uploading XLSX files directly from any record.

- It simplifies spreadsheet management by automatically generating attachments, validating file types, and linking the spreadsheet to the record, enabling quick and efficient access and updates.
77 changes: 77 additions & 0 deletions spreadsheet_oca_upload_base/readme/USAGE.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
The SpreadsheetUploadMixin mixin can be used when a model needs the ability to upload an XLSX file and automatically create or link a spreadsheet document in Odoo.
It allows users to manage spreadsheets directly from the form view of a record.

Let's consider the following models:

.. code-block:: python

class MyModelA(models.Model):
_name = "my.model.a"
_inherit = ["spreadsheet.upload.mixin"]

By inheriting from spreadsheet.upload.mixin, the model gains the following:

- upload_file: binary field to upload an XLSX file.

- file_name: helper field to store the filename.

- spreadsheet_id: link to the generated spreadsheet.spreadsheet record.

- action_create_spreadsheet(): creates a new spreadsheet from the uploaded file.

- action_open_spreadsheet(): opens the linked spreadsheet directly from the form.

A corresponding form view can then be defined to expose these fields and buttons:

.. code-block:: xml

<record id="my_model_a_form_view" model="ir.ui.view">
<field name="name">my.model.a.form.view</field>
<field name="model">my.model.a</field>
<field name="arch" type="xml">
<form>
<sheet>
<group>
<div class="o_row">
<label for="spreadsheet_id"
attrs="{'invisible': [('spreadsheet_id', '=', False)]}" />
<div attrs="{'invisible': [('spreadsheet_id', '=', False)]}"
style="margin-bottom: 15px;">
<field name="spreadsheet_id"
readonly="1"
class="oe_inline"
style="margin-right:7px;"/>
<button name="action_open_spreadsheet"
type="object"
string="Edit"
icon="fa-pencil"
class="btn btn-primary"/>
</div>
</div>

<div class="o_row">
<label for="upload_file"
class="o_form_label"
style="font-weight: bold;"
attrs="{'invisible': ['|', ('upload_file', '=', False), ('spreadsheet_id', '!=', False)]}"/>
<field name="upload_file"
filename="file_name"
nolabel="1"
attrs="{'invisible': [('spreadsheet_id', '!=', False)]}"/>
<div style="margin-right: 400px; margin-bottom: 15px;">
<button name="action_create_spreadsheet"
type="object"
string="Create Spreadsheet From File"
class="btn btn-primary"
attrs="{'invisible': ['|', ('upload_file', '=', False), ('spreadsheet_id', '!=', False)]}"/>
</div>
</div>

<field name="file_name" invisible="1"/>
</group>
</sheet>
</form>
</field>
</record>

By using this mixin and view definition, you can upload an XLSX file from any record form, create a linked Odoo Spreadsheet, and open it for editing directly from the form view.
Loading