Skip to content

Commit 0b25b30

Browse files
committed
[ADD] spreadsheet_oca_upload_base
1 parent 75d1365 commit 0b25b30

File tree

16 files changed

+930
-0
lines changed

16 files changed

+930
-0
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../../../spreadsheet_oca_upload_base
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import setuptools
2+
3+
setuptools.setup(
4+
setup_requires=['setuptools-odoo'],
5+
odoo_addon=True,
6+
)
Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
===========================
2+
Spreadsheet OCA Upload Base
3+
===========================
4+
5+
..
6+
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
7+
!! This file is generated by oca-gen-addon-readme !!
8+
!! changes will be overwritten. !!
9+
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
10+
!! source digest: sha256:6495e77fa5a250c4e672af7e3a502bc571a4df9c527461a1031f4f2bfc593dc6
11+
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
12+
13+
.. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png
14+
:target: https://odoo-community.org/page/development-status
15+
:alt: Beta
16+
.. |badge2| image:: https://img.shields.io/badge/licence-AGPL--3-blue.png
17+
:target: http://www.gnu.org/licenses/agpl-3.0-standalone.html
18+
:alt: License: AGPL-3
19+
.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Fspreadsheet-lightgray.png?logo=github
20+
:target: https://github.com/OCA/spreadsheet/tree/16.0/spreadsheet_oca_upload_base
21+
:alt: OCA/spreadsheet
22+
.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png
23+
:target: https://translation.odoo-community.org/projects/spreadsheet-16-0/spreadsheet-16-0-spreadsheet_oca_upload_base
24+
:alt: Translate me on Weblate
25+
.. |badge5| image:: https://img.shields.io/badge/runboat-Try%20me-875A7B.png
26+
:target: https://runboat.odoo-community.org/builds?repo=OCA/spreadsheet&target_branch=16.0
27+
:alt: Try me on Runboat
28+
29+
|badge1| |badge2| |badge3| |badge4| |badge5|
30+
31+
- This module extends the integration with Odoo Spreadsheet to allow uploading XLSX files directly from any record.
32+
33+
- 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.
34+
35+
**Table of contents**
36+
37+
.. contents::
38+
:local:
39+
40+
Usage
41+
=====
42+
43+
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.
44+
It allows users to manage spreadsheets directly from the form view of a record.
45+
46+
Let's consider the following models:
47+
48+
.. code-block:: python
49+
50+
class MyModelA(models.Model):
51+
_name = "my.model.a"
52+
_inherit = ["spreadsheet.upload.mixin"]
53+
54+
By inheriting from spreadsheet.upload.mixin, the model gains the following:
55+
56+
- upload_file: binary field to upload an XLSX file.
57+
58+
- file_name: helper field to store the filename.
59+
60+
- spreadsheet_id: link to the generated spreadsheet.spreadsheet record.
61+
62+
- action_create_spreadsheet(): creates a new spreadsheet from the uploaded file.
63+
64+
- action_open_spreadsheet(): opens the linked spreadsheet directly from the form.
65+
66+
A corresponding form view can then be defined to expose these fields and buttons:
67+
68+
.. code-block:: xml
69+
70+
<record id="my_model_a_form_view" model="ir.ui.view">
71+
<field name="name">my.model.a.form.view</field>
72+
<field name="model">my.model.a</field>
73+
<field name="arch" type="xml">
74+
<form>
75+
<sheet>
76+
<group>
77+
<div class="o_row">
78+
<label for="spreadsheet_id"
79+
attrs="{'invisible': [('spreadsheet_id', '=', False)]}" />
80+
<div attrs="{'invisible': [('spreadsheet_id', '=', False)]}"
81+
style="margin-bottom: 15px;">
82+
<field name="spreadsheet_id"
83+
readonly="1"
84+
class="oe_inline"
85+
style="margin-right:7px;"/>
86+
<button name="action_open_spreadsheet"
87+
type="object"
88+
string="Edit"
89+
icon="fa-pencil"
90+
class="btn btn-primary"/>
91+
</div>
92+
</div>
93+
94+
<div class="o_row">
95+
<label for="upload_file"
96+
class="o_form_label"
97+
style="font-weight: bold;"
98+
attrs="{'invisible': ['|', ('upload_file', '=', False), ('spreadsheet_id', '!=', False)]}"/>
99+
<field name="upload_file"
100+
filename="file_name"
101+
nolabel="1"
102+
attrs="{'invisible': [('spreadsheet_id', '!=', False)]}"/>
103+
<div style="margin-right: 400px; margin-bottom: 15px;">
104+
<button name="action_create_spreadsheet"
105+
type="object"
106+
string="Create Spreadsheet From File"
107+
class="btn btn-primary"
108+
attrs="{'invisible': ['|', ('upload_file', '=', False), ('spreadsheet_id', '!=', False)]}"/>
109+
</div>
110+
</div>
111+
112+
<field name="file_name" invisible="1"/>
113+
</group>
114+
</sheet>
115+
</form>
116+
</field>
117+
</record>
118+
119+
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.
120+
121+
Bug Tracker
122+
===========
123+
124+
Bugs are tracked on `GitHub Issues <https://github.com/OCA/spreadsheet/issues>`_.
125+
In case of trouble, please check there if your issue has already been reported.
126+
If you spotted it first, help us to smash it by providing a detailed and welcomed
127+
`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**>`_.
128+
129+
Do not contact contributors directly about support or help with technical issues.
130+
131+
Credits
132+
=======
133+
134+
Authors
135+
~~~~~~~
136+
137+
* BizzAppDev Systems Pvt. Ltd.
138+
139+
Contributors
140+
~~~~~~~~~~~~
141+
142+
* `BizzAppDev Systems <https://www.bizzappdev.com>`_:
143+
144+
* Ruchir Shukla <[email protected]>
145+
146+
Other credits
147+
~~~~~~~~~~~~~
148+
149+
The development of this module has been financially supported by:
150+
151+
- Agent ERP GmbH
152+
153+
Maintainers
154+
~~~~~~~~~~~
155+
156+
This module is maintained by the OCA.
157+
158+
.. image:: https://odoo-community.org/logo.png
159+
:alt: Odoo Community Association
160+
:target: https://odoo-community.org
161+
162+
OCA, or the Odoo Community Association, is a nonprofit organization whose
163+
mission is to support the collaborative development of Odoo features and
164+
promote its widespread use.
165+
166+
This module is part of the `OCA/spreadsheet <https://github.com/OCA/spreadsheet/tree/16.0/spreadsheet_oca_upload_base>`_ project on GitHub.
167+
168+
You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from . import models
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"name": "Spreadsheet OCA Upload Base",
3+
"summary": "Base module for uploading spreadsheets to the model",
4+
"version": "16.0.1.0.0",
5+
"license": "AGPL-3",
6+
"author": "BizzAppDev Systems Pvt. Ltd., Odoo Community Association (OCA)",
7+
"website": "https://github.com/OCA/spreadsheet",
8+
"depends": ["spreadsheet_oca"],
9+
"installable": True,
10+
"auto_install": False,
11+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from . import spreadsheet_upload_mixin
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
from odoo import _, api, fields, models
2+
from odoo.exceptions import ValidationError
3+
4+
5+
class SpreadsheetUploadMixin(models.AbstractModel):
6+
_name = "spreadsheet.upload.mixin"
7+
_description = "Mixin to upload XLSX and create linked spreadsheet"
8+
9+
spreadsheet_id = fields.Many2one(
10+
comodel_name="spreadsheet.spreadsheet",
11+
string="Spreadsheet",
12+
help="Linked Spreadsheet",
13+
copy=False,
14+
)
15+
upload_file = fields.Binary(
16+
string="Upload XLSX", copy=False, help="XLSX file to upload"
17+
)
18+
file_name = fields.Char(copy=False, help="Name of the uploaded file")
19+
20+
@api.constrains("file_name")
21+
def _check_xlsx_file_type(self):
22+
"""New constraint to check uploaded file type is XLSX."""
23+
invalid_files = self.filtered(
24+
lambda spreadsheet: not spreadsheet.file_name.lower().endswith(".xlsx")
25+
)
26+
if invalid_files:
27+
raise ValidationError(_("Please upload a valid XLSX file."))
28+
29+
def _get_attachment(self):
30+
"""New method to search for the attachment of the uploaded file."""
31+
self.ensure_one()
32+
attachment = self.env["ir.attachment"].search(
33+
[
34+
("res_model", "=", self._name),
35+
("res_id", "=", self.id),
36+
("res_field", "=", "upload_file"),
37+
],
38+
limit=1,
39+
)
40+
if attachment:
41+
attachment.write(
42+
{
43+
"name": self.file_name or "uploaded_file",
44+
}
45+
)
46+
return attachment
47+
48+
def action_create_spreadsheet(self):
49+
"""New method to create spredsheet from the upload file."""
50+
for record in self:
51+
attachment = record._get_attachment()
52+
if not attachment:
53+
continue
54+
spreadsheet = self.env[
55+
"spreadsheet.spreadsheet"
56+
].create_document_from_attachment([attachment.id])
57+
record.spreadsheet_id = spreadsheet.get("res_id") if spreadsheet else False
58+
59+
def action_open_spreadsheet(self):
60+
"""New method to open linked spredsheet."""
61+
self.ensure_one()
62+
if not self.spreadsheet_id:
63+
return
64+
return self.spreadsheet_id.open_spreadsheet()
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
* `BizzAppDev Systems <https://www.bizzappdev.com>`_:
2+
3+
* Ruchir Shukla <[email protected]>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
The development of this module has been financially supported by:
2+
3+
- Agent ERP GmbH
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
- This module extends the integration with Odoo Spreadsheet to allow uploading XLSX files directly from any record.
2+
3+
- 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.

0 commit comments

Comments
 (0)