diff --git a/product_download_feed/README.rst b/product_download_feed/README.rst new file mode 100644 index 00000000000..6b736890bd0 --- /dev/null +++ b/product_download_feed/README.rst @@ -0,0 +1,83 @@ +======================= +Product Feed CSV Export +======================= + +.. + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! This file is generated by oca-gen-addon-readme !! + !! changes will be overwritten. !! + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! source digest: sha256:f91fff29b8eb65573173388815219b940ca8c6e4f17c6e90226074816057b3c1 + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +.. |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%2Fproduct--attribute-lightgray.png?logo=github + :target: https://github.com/OCA/product-attribute/tree/18.0/product_download_feed + :alt: OCA/product-attribute +.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png + :target: https://translation.odoo-community.org/projects/product-attribute-18-0/product-attribute-18-0-product_download_feed + :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/product-attribute&target_branch=18.0 + :alt: Try me on Runboat + +|badge1| |badge2| |badge3| |badge4| |badge5| + +| This module provides an **HTTP endpoint** that allows authenticated + Odoo users to export the product feed as a **CSV file**. +| It automatically applies the user's assigned **pricelist**, so the + exported prices match what the user would see in sales quotations or + the portal. + +**Table of contents** + +.. contents:: + :local: + +Bug Tracker +=========== + +Bugs are tracked on `GitHub 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 `_. + +Do not contact contributors directly about support or help with technical issues. + +Credits +======= + +Authors +------- + +* Odoo S.A. +* Tecnativa + +Contributors +------------ + +- [Tecnativa](https://www.tecnativa.com): + + - Eduardo Ezerouali + +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/product-attribute `_ project on GitHub. + +You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute. diff --git a/product_download_feed/__init__.py b/product_download_feed/__init__.py new file mode 100644 index 00000000000..e42582f8dfe --- /dev/null +++ b/product_download_feed/__init__.py @@ -0,0 +1,3 @@ +# Copyright 2025 Tecnativa - Eduardo Ezerouali +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl) +from . import controllers diff --git a/product_download_feed/__manifest__.py b/product_download_feed/__manifest__.py new file mode 100644 index 00000000000..8f4f9000226 --- /dev/null +++ b/product_download_feed/__manifest__.py @@ -0,0 +1,15 @@ +# Copyright 2025 Tecnativa - Eduardo Ezerouali +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl) +{ + "name": "Product Feed CSV Export", + "version": "18.0.1.0.0", + "summary": "Download CSV with product feed from a pricelist using controller", + "category": "Product", + "license": "AGPL-3", + "website": "https://github.com/OCA/product-attribute", + "author": "Odoo S.A., Tecnativa, Odoo Community Association (OCA)", + "depends": ["sale_stock"], + "data": [], + "installable": True, + "application": False, +} diff --git a/product_download_feed/controllers/__init__.py b/product_download_feed/controllers/__init__.py new file mode 100644 index 00000000000..142e826ec9e --- /dev/null +++ b/product_download_feed/controllers/__init__.py @@ -0,0 +1,3 @@ +# Copyright 2025 Tecnativa - Eduardo Ezerouali +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl) +from . import main diff --git a/product_download_feed/controllers/main.py b/product_download_feed/controllers/main.py new file mode 100644 index 00000000000..3e0d45b8c88 --- /dev/null +++ b/product_download_feed/controllers/main.py @@ -0,0 +1,71 @@ +# Copyright 2025 Tecnativa - Eduardo Ezerouali +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl) +import csv +import io +from datetime import datetime + +from odoo import http +from odoo.http import request + + +class CatalogExportController(http.Controller): + @http.route( + ["/feed/export/products.csv"], + type="http", + auth="user", + methods=["GET"], + csrf=False, + ) + def export_products_csv(self, **kwargs): + """ + Export product feed to CSV applying pricelist. + Params: + pricelist_id: ID of the pricelist (or user default pricelist) + fields: comma-separated list of fields + active: true/false + """ + fields_param = ( + kwargs.get("fields") or "name,default_code,barcode,list_price,qty_available" + ) + fields = [f.strip() for f in fields_param.split(",") if f.strip()] + active = kwargs.get("active", "true").lower() != "false" + pricelist_id = kwargs.get("pricelist_id") + pricelist = None + if pricelist_id: + pricelist = ( + request.env["product.pricelist"].browse(int(pricelist_id)).exists() + ) + if not pricelist: + partner = request.env.user.partner_id + pricelist = partner.property_product_pricelist + domain = [("active", "=", active), ("sale_ok", "=", True)] + products = request.env["product.product"].search(domain) + buf = io.StringIO() + writer = csv.writer(buf, quoting=csv.QUOTE_MINIMAL) + writer.writerow(fields) + + def get_val(rec, field): + if field == "list_price": + return ( + pricelist._get_product_price(rec, 1.0) + if pricelist + else rec.list_price + ) + val = rec[field] if field in rec._fields else "" + if hasattr(val, "name") and val._name != "ir.attachment": + return val.name + if hasattr(val, "ids"): + return ",".join(str(x) for x in val.ids) + if isinstance(val, datetime): + return val.isoformat() + return val if val is not False else "" + + for rec in products: + writer.writerow([get_val(rec, f) for f in fields]) + + csv_bytes = buf.getvalue().encode("utf-8-sig") + headers = [ + ("Content-Type", "text/csv; charset=utf-8"), + ("Content-Disposition", 'attachment; filename="products.csv"'), + ] + return request.make_response(csv_bytes, headers=headers) diff --git a/product_download_feed/pyproject.toml b/product_download_feed/pyproject.toml new file mode 100644 index 00000000000..4231d0cccb3 --- /dev/null +++ b/product_download_feed/pyproject.toml @@ -0,0 +1,3 @@ +[build-system] +requires = ["whool"] +build-backend = "whool.buildapi" diff --git a/product_download_feed/readme/CONTRIBUTORS.md b/product_download_feed/readme/CONTRIBUTORS.md new file mode 100644 index 00000000000..e408c1e529e --- /dev/null +++ b/product_download_feed/readme/CONTRIBUTORS.md @@ -0,0 +1,2 @@ +- \[Tecnativa\](): + - Eduardo Ezerouali \ No newline at end of file diff --git a/product_download_feed/readme/DESCRIPTION.md b/product_download_feed/readme/DESCRIPTION.md new file mode 100644 index 00000000000..e465109eab3 --- /dev/null +++ b/product_download_feed/readme/DESCRIPTION.md @@ -0,0 +1,2 @@ +This module provides an **HTTP endpoint** that allows authenticated Odoo users to export the product feed as a **CSV file**. +It automatically applies the user's assigned **pricelist**, so the exported prices match what the user would see in sales quotations or the portal. diff --git a/product_download_feed/static/description/icon.png b/product_download_feed/static/description/icon.png new file mode 100644 index 00000000000..3a0328b516c Binary files /dev/null and b/product_download_feed/static/description/icon.png differ diff --git a/product_download_feed/static/description/index.html b/product_download_feed/static/description/index.html new file mode 100644 index 00000000000..e1f5e1f50bc --- /dev/null +++ b/product_download_feed/static/description/index.html @@ -0,0 +1,433 @@ + + + + + +Product Feed CSV Export + + + +
+

Product Feed CSV Export

+ + +

Beta License: AGPL-3 OCA/product-attribute Translate me on Weblate Try me on Runboat

+
+
This module provides an HTTP endpoint that allows authenticated +Odoo users to export the product feed as a CSV file.
+
It automatically applies the user’s assigned pricelist, so the +exported prices match what the user would see in sales quotations or +the portal.
+
+

Table of contents

+ +
+

Bug Tracker

+

Bugs are tracked on GitHub 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.

+

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

+
+
+

Credits

+
+

Authors

+
    +
  • Odoo S.A.
  • +
  • Tecnativa
  • +
+
+
+

Contributors

+ +
+
+

Maintainers

+

This module is maintained by the OCA.

+ +Odoo Community Association + +

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/product-attribute project on GitHub.

+

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.

+
+
+
+ + diff --git a/product_download_feed/tests/__init__.py b/product_download_feed/tests/__init__.py new file mode 100644 index 00000000000..2fb2633b4f4 --- /dev/null +++ b/product_download_feed/tests/__init__.py @@ -0,0 +1,3 @@ +# Copyright 2025 Tecnativa - Eduardo Ezerouali +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl) +from . import test_export_products diff --git a/product_download_feed/tests/test_export_products.py b/product_download_feed/tests/test_export_products.py new file mode 100644 index 00000000000..bd98cac19a4 --- /dev/null +++ b/product_download_feed/tests/test_export_products.py @@ -0,0 +1,105 @@ +# Copyright 2025 Tecnativa - Eduardo Ezerouali +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl) +import csv +import io + +from odoo import Command +from odoo.tests import HttpCase, new_test_user, tagged + + +@tagged("-at_install", "post_install") +class TestCatalogExportController(HttpCase): + @classmethod + def setUpClass(cls): + super().setUpClass() + # Create a user, partner, product, and pricelist for testing + cls.user = new_test_user(cls.env, login="csv_user") + cls.user.groups_id = [Command.link(cls.env.ref("stock.group_stock_user").id)] + cls.product = cls.env["product.product"].create( + { + "name": "Test Product", + "default_code": "TP001", + "list_price": 100.0, + } + ) + cls.pricelist = cls.env["product.pricelist"].create( + { + "name": "Special Pricelist", + "currency_id": cls.env.ref("base.USD").id, + } + ) + cls.pricelist_deafult = cls.env["product.pricelist"].create( + { + "name": "Deafult Pricelist", + "currency_id": cls.env.ref("base.USD").id, + } + ) + cls.env["product.pricelist.item"].create( + { + "pricelist_id": cls.pricelist.id, + "applied_on": "1_product", + "product_tmpl_id": cls.product.product_tmpl_id.id, + "product_id": cls.product.id, + "compute_price": "fixed", + "fixed_price": 80.0, + } + ) + cls.env["product.pricelist.item"].create( + { + "pricelist_id": cls.pricelist_deafult.id, + "applied_on": "1_product", + "product_tmpl_id": cls.product.product_tmpl_id.id, + "product_id": cls.product.id, + "compute_price": "formula", + "base": "list_price", + } + ) + + def _find_product_row(self, rows, product_name): + """Find the row in the CSV corresponding to the given product name.""" + headers = rows[0] + name_index = headers.index("name") + for row in rows[1:]: + if row[name_index] == product_name: + return row + self.fail(f"Product {product_name} not found in CSV export") + + def _read_csv(self, content): + """Helper to parse CSV response into rows.""" + decoded = content.decode("utf-8-sig") + buf = io.StringIO(decoded) + reader = csv.reader(buf) + return list(reader) + + def test_csv_export_with_pricelist(self): + """Export should apply pricelist and generate correct filename.""" + + self.user.partner_id.property_product_pricelist = self.pricelist + self.authenticate("csv_user", "csv_user") + response = self.url_open("/feed/export/products.csv") + self.assertEqual(response.status_code, 200) + dispo = response.headers.get("Content-Disposition") + self.assertIn("products.csv", dispo) + self.assertTrue(dispo.endswith('.csv"')) + rows = self._read_csv(response.content) + headers = rows[0] + product_row = self._find_product_row(rows, self.product.name) + price_index = headers.index("list_price") + price = float(product_row[price_index]) + self.assertEqual(price, 80.0) + + def test_csv_export_without_pricelist(self): + """Export should fall back to list_price when no pricelist is assigned.""" + + self.authenticate("csv_user", "csv_user") + self.user.partner_id.property_product_pricelist = self.pricelist_deafult + self.user.partner_id.flush_model() + self.user.partner_id.invalidate_model() + response = self.url_open("/feed/export/products.csv") + self.assertEqual(response.status_code, 200) + rows = self._read_csv(response.content) + headers = rows[0] + product_row = self._find_product_row(rows, self.product.name) + price_index = headers.index("list_price") + price = float(product_row[price_index]) + self.assertEqual(price, 100.0)