diff --git a/website_sale_partner_firstname/README.rst b/website_sale_partner_firstname/README.rst new file mode 100644 index 0000000000..67c5e5fc31 --- /dev/null +++ b/website_sale_partner_firstname/README.rst @@ -0,0 +1,91 @@ +============================= +First & Last Name at Checkout +============================= + +.. + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! This file is generated by oca-gen-addon-readme !! + !! changes will be overwritten. !! + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! source digest: sha256:2f71726af23fd6e4ec9092479bf6088ba852635f250840a2a17039c8858eec7b + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +.. |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%2Fwebsite-lightgray.png?logo=github + :target: https://github.com/OCA/website/tree/18.0/website_sale_partner_firstname + :alt: OCA/website +.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png + :target: https://translation.odoo-community.org/projects/website-18-0/website-18-0-website_sale_partner_firstname + :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/website&target_branch=18.0 + :alt: Try me on Runboat + +|badge1| |badge2| |badge3| |badge4| |badge5| + +This module extends the website checkout and portal to use separate +first name and last name fields, leveraging the ``partner_firstname`` +module for name splitting. + +**Table of contents** + +.. contents:: + :local: + +Usage +===== + +Once installed, the single "Name" field is automatically replaced by +separate "First name" and "Last name" fields on: + +- The checkout address form (``/shop/address``) +- The portal account details page (``/my/account``) + +Both fields are mandatory. Whitespace-only values are rejected. + +No configuration is required. + +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 +------- + +* Aaron Ngu + +Contributors +------------ + +- Aaron Ngu + +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/website `_ project on GitHub. + +You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute. diff --git a/website_sale_partner_firstname/__init__.py b/website_sale_partner_firstname/__init__.py new file mode 100644 index 0000000000..e046e49fbe --- /dev/null +++ b/website_sale_partner_firstname/__init__.py @@ -0,0 +1 @@ +from . import controllers diff --git a/website_sale_partner_firstname/__manifest__.py b/website_sale_partner_firstname/__manifest__.py new file mode 100644 index 0000000000..a3387a453e --- /dev/null +++ b/website_sale_partner_firstname/__manifest__.py @@ -0,0 +1,15 @@ +{ + "name": "First & Last Name at Checkout", + "summary": "Separate first and last name fields at checkout and portal", + "author": "Aaron Ngu, Odoo Community Association (OCA)", + "category": "Website/Website", + "version": "18.0.1.0.0", + "website": "https://github.com/OCA/website", + "license": "AGPL-3", + "images": [], + "depends": ["website_sale", "partner_firstname"], + "data": [ + "data/res_partner_data.xml", + "views/templates.xml", + ], +} diff --git a/website_sale_partner_firstname/controllers/__init__.py b/website_sale_partner_firstname/controllers/__init__.py new file mode 100644 index 0000000000..12a7e529b6 --- /dev/null +++ b/website_sale_partner_firstname/controllers/__init__.py @@ -0,0 +1 @@ +from . import main diff --git a/website_sale_partner_firstname/controllers/main.py b/website_sale_partner_firstname/controllers/main.py new file mode 100644 index 0000000000..2ef9c34138 --- /dev/null +++ b/website_sale_partner_firstname/controllers/main.py @@ -0,0 +1,63 @@ +from odoo.http import route + +from odoo.addons.website_sale.controllers.main import WebsiteSale + + +class WebsiteSaleFirstLastName(WebsiteSale): + """Replace 'name' with firstname/lastname on checkout/portal.""" + + # ------------------------------------------------------------------------- + # Shared (affects both checkout and portal) + # ------------------------------------------------------------------------- + + def _get_mandatory_fields(self): + """Swap 'name' for 'firstname' and 'lastname' in mandatory fields.""" + mandatory_fields = super()._get_mandatory_fields() + if "name" in mandatory_fields: + mandatory_fields.remove("name") + mandatory_fields.extend(["firstname", "lastname"]) + return mandatory_fields + + # ------------------------------------------------------------------------- + # Portal /my/account + # ------------------------------------------------------------------------- + + def details_form_validate(self, data, *args, **kwargs): + """Strip whitespace from name fields so blank strings fail validation.""" + for field in ("firstname", "lastname"): + if field in data and isinstance(data[field], str): + data[field] = data[field].strip() + return super().details_form_validate(data, *args, **kwargs) + + def on_account_update(self, values, partner): + """Ensure 'name' is present to prevent KeyError in bank-holder check.""" + result = super().on_account_update(values, partner) + # With partner_firstname, name is computed from firstname/lastname, + # so set the current name to prevent KeyError. + if "name" not in values: + values["name"] = partner.name or "" + return result + + # ------------------------------------------------------------------------- + # Checkout /shop/address + # ------------------------------------------------------------------------- + + def _get_mandatory_address_fields(self, country_sudo): + """Swap 'name' for 'firstname' and 'lastname' in mandatory address fields.""" + mandatory_fields = super()._get_mandatory_address_fields(country_sudo) + mandatory_fields.discard("name") + mandatory_fields |= {"firstname", "lastname"} + return mandatory_fields + + @route() + def shop_country_info(self, country, address_type, **kw): + """Update the JS required-fields list for firstname/lastname.""" + result = super().shop_country_info(country, address_type, **kw) + if "required_fields" in result: + result["required_fields"] = [ + f for f in result["required_fields"] if f != "name" + ] + for field in ("firstname", "lastname"): + if field not in result["required_fields"]: + result["required_fields"].append(field) + return result diff --git a/website_sale_partner_firstname/data/res_partner_data.xml b/website_sale_partner_firstname/data/res_partner_data.xml new file mode 100644 index 0000000000..8ef67178f6 --- /dev/null +++ b/website_sale_partner_firstname/data/res_partner_data.xml @@ -0,0 +1,7 @@ + + + + res.partner + + + diff --git a/website_sale_partner_firstname/oca_dependencies.txt b/website_sale_partner_firstname/oca_dependencies.txt new file mode 100644 index 0000000000..b0f1193f7d --- /dev/null +++ b/website_sale_partner_firstname/oca_dependencies.txt @@ -0,0 +1 @@ +partner-contact diff --git a/website_sale_partner_firstname/pyproject.toml b/website_sale_partner_firstname/pyproject.toml new file mode 100644 index 0000000000..4231d0cccb --- /dev/null +++ b/website_sale_partner_firstname/pyproject.toml @@ -0,0 +1,3 @@ +[build-system] +requires = ["whool"] +build-backend = "whool.buildapi" diff --git a/website_sale_partner_firstname/readme/CONTRIBUTORS.md b/website_sale_partner_firstname/readme/CONTRIBUTORS.md new file mode 100644 index 0000000000..5d4dd52766 --- /dev/null +++ b/website_sale_partner_firstname/readme/CONTRIBUTORS.md @@ -0,0 +1 @@ +- Aaron Ngu \ diff --git a/website_sale_partner_firstname/readme/DESCRIPTION.md b/website_sale_partner_firstname/readme/DESCRIPTION.md new file mode 100644 index 0000000000..5016c29b6f --- /dev/null +++ b/website_sale_partner_firstname/readme/DESCRIPTION.md @@ -0,0 +1,3 @@ +This module extends the website checkout and portal to use separate +first name and last name fields, leveraging the `partner_firstname` +module for name splitting. diff --git a/website_sale_partner_firstname/readme/USAGE.md b/website_sale_partner_firstname/readme/USAGE.md new file mode 100644 index 0000000000..b2927b427c --- /dev/null +++ b/website_sale_partner_firstname/readme/USAGE.md @@ -0,0 +1,9 @@ +Once installed, the single "Name" field is automatically replaced by +separate "First name" and "Last name" fields on: + +- The checkout address form (`/shop/address`) +- The portal account details page (`/my/account`) + +Both fields are mandatory. Whitespace-only values are rejected. + +No configuration is required. diff --git a/website_sale_partner_firstname/static/description/checkout.gif b/website_sale_partner_firstname/static/description/checkout.gif new file mode 100644 index 0000000000..8b967f9518 Binary files /dev/null and b/website_sale_partner_firstname/static/description/checkout.gif differ diff --git a/website_sale_partner_firstname/static/description/index.html b/website_sale_partner_firstname/static/description/index.html new file mode 100644 index 0000000000..c01ee327af --- /dev/null +++ b/website_sale_partner_firstname/static/description/index.html @@ -0,0 +1,437 @@ + + + + + +First & Last Name at Checkout + + + +
+

First & Last Name at Checkout

+ + +

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

+

This module extends the website checkout and portal to use separate +first name and last name fields, leveraging the partner_firstname +module for name splitting.

+

Table of contents

+ +
+

Usage

+

Once installed, the single “Name” field is automatically replaced by +separate “First name” and “Last name” fields on:

+
    +
  • The checkout address form (/shop/address)
  • +
  • The portal account details page (/my/account)
  • +
+

Both fields are mandatory. Whitespace-only values are rejected.

+

No configuration is required.

+
+
+

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

+
    +
  • Aaron Ngu
  • +
+
+ +
+

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

+

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

+
+
+
+ + diff --git a/website_sale_partner_firstname/static/description/portal.gif b/website_sale_partner_firstname/static/description/portal.gif new file mode 100644 index 0000000000..c272634aae Binary files /dev/null and b/website_sale_partner_firstname/static/description/portal.gif differ diff --git a/website_sale_partner_firstname/tests/__init__.py b/website_sale_partner_firstname/tests/__init__.py new file mode 100644 index 0000000000..b5a3200448 --- /dev/null +++ b/website_sale_partner_firstname/tests/__init__.py @@ -0,0 +1 @@ +from . import test_controller diff --git a/website_sale_partner_firstname/tests/test_controller.py b/website_sale_partner_firstname/tests/test_controller.py new file mode 100644 index 0000000000..c638f167d3 --- /dev/null +++ b/website_sale_partner_firstname/tests/test_controller.py @@ -0,0 +1,195 @@ +from unittest.mock import patch + +from odoo.tests import tagged + +from odoo.addons.website.tools import MockRequest +from odoo.addons.website_sale.tests.common import WebsiteSaleCommon +from odoo.addons.website_sale_partner_firstname.controllers.main import ( + WebsiteSaleFirstLastName, +) + + +@tagged("post_install", "-at_install") +class TestMandatoryFields(WebsiteSaleCommon): + @classmethod + def setUpClass(cls): + super().setUpClass() + cls.controller = WebsiteSaleFirstLastName() + + def test_get_mandatory_fields_removes_name(self): + """Portal mandatory fields should not contain 'name'.""" + with MockRequest(self.env, website=self.website): + fields = self.controller._get_mandatory_fields() + self.assertNotIn("name", fields) + + def test_get_mandatory_fields_has_firstname_lastname(self): + """Portal mandatory fields should contain 'firstname' and 'lastname'.""" + with MockRequest(self.env, website=self.website): + fields = self.controller._get_mandatory_fields() + self.assertIn("firstname", fields) + self.assertIn("lastname", fields) + + def test_get_mandatory_address_fields_removes_name(self): + """Checkout mandatory address fields should not contain 'name'.""" + with MockRequest(self.env, website=self.website): + fields = self.controller._get_mandatory_address_fields( + self.country_us, + ) + self.assertNotIn("name", fields) + + def test_get_mandatory_address_fields_has_firstname_lastname(self): + """Checkout address fields should contain firstname and lastname.""" + with MockRequest(self.env, website=self.website): + fields = self.controller._get_mandatory_address_fields( + self.country_us, + ) + self.assertIn("firstname", fields) + self.assertIn("lastname", fields) + + +@tagged("post_install", "-at_install") +class TestDetailsFormValidate(WebsiteSaleCommon): + @classmethod + def setUpClass(cls): + super().setUpClass() + cls.controller = WebsiteSaleFirstLastName() + + def _base_data(self, **overrides): + """Return a minimal valid form data dict for portal details.""" + data = { + "firstname": "John", + "lastname": "Doe", + "phone": "+1 555-555-5555", + "email": "john@example.com", + "street": "123 Main St", + "city": "Springfield", + "country_id": self.country_us.id, + "zipcode": "62701", + } + data.update(overrides) + return data + + def test_whitespace_firstname_triggers_error(self): + """A whitespace-only firstname should be stripped and flagged missing.""" + data = self._base_data(firstname=" ") + with MockRequest(self.env, website=self.website): + error, error_message = self.controller.details_form_validate(data) + self.assertEqual(error.get("firstname"), "missing") + + def test_whitespace_lastname_triggers_error(self): + """A whitespace-only lastname should be stripped and flagged missing.""" + data = self._base_data(lastname=" ") + with MockRequest(self.env, website=self.website): + error, error_message = self.controller.details_form_validate(data) + self.assertEqual(error.get("lastname"), "missing") + + def test_missing_name_fields_no_error(self): + """When firstname/lastname are absent from data, no strip occurs.""" + data = self._base_data() + del data["firstname"] + del data["lastname"] + data["name"] = "John Doe" + with MockRequest(self.env, website=self.website): + error, error_message = self.controller.details_form_validate(data) + self.assertNotIn("firstname", data) + + def test_valid_names_pass_validation(self): + """Normal firstname/lastname values should not produce errors.""" + data = self._base_data() + with MockRequest(self.env, website=self.website): + error, error_message = self.controller.details_form_validate(data) + self.assertNotIn("firstname", error) + self.assertNotIn("lastname", error) + + +@tagged("post_install", "-at_install") +class TestOnAccountUpdate(WebsiteSaleCommon): + @classmethod + def setUpClass(cls): + super().setUpClass() + cls.controller = WebsiteSaleFirstLastName() + cls.test_partner = cls.env["res.partner"].create( + { + "firstname": "Jane", + "lastname": "Smith", + } + ) + + def test_name_injected_when_missing(self): + """When 'name' is absent from values, it should be set from partner.""" + values = {"firstname": "Jane", "lastname": "Smith"} + with MockRequest(self.env, website=self.website): + self.controller.on_account_update(values, self.test_partner) + self.assertEqual(values["name"], self.test_partner.name) + + def test_name_not_overwritten_when_present(self): + """When 'name' is already in values, it should not be changed.""" + values = {"name": "Custom Name", "firstname": "Jane", "lastname": "Smith"} + with MockRequest(self.env, website=self.website): + self.controller.on_account_update(values, self.test_partner) + self.assertEqual(values["name"], "Custom Name") + + +@tagged("post_install", "-at_install") +class TestShopCountryInfo(WebsiteSaleCommon): + @classmethod + def setUpClass(cls): + super().setUpClass() + cls.controller = WebsiteSaleFirstLastName() + + def test_required_fields_removes_name(self): + """The required_fields list should not contain 'name'.""" + with MockRequest(self.env, website=self.website): + result = self.controller.shop_country_info( + self.country_us, + "billing", + ) + self.assertNotIn("name", result["required_fields"]) + + def test_required_fields_has_firstname_lastname(self): + """The required_fields list should contain 'firstname' and 'lastname'.""" + with MockRequest(self.env, website=self.website): + result = self.controller.shop_country_info( + self.country_us, + "billing", + ) + self.assertIn("firstname", result["required_fields"]) + self.assertIn("lastname", result["required_fields"]) + + def test_no_required_fields_key(self): + """When parent returns no required_fields, no error occurs.""" + with MockRequest(self.env, website=self.website): + parent_method = ( + "odoo.addons.website_sale.controllers.main" + ".WebsiteSale.shop_country_info" + ) + with patch(parent_method, return_value={"states": []}): + result = self.controller.shop_country_info( + self.country_us, + "billing", + ) + self.assertNotIn("required_fields", result) + + def test_fields_not_duplicated_when_already_present(self): + """When parent already includes firstname/lastname, skip append.""" + parent_result = { + "required_fields": ["firstname", "lastname", "country_id"], + } + with MockRequest(self.env, website=self.website): + parent_method = ( + "odoo.addons.website_sale.controllers.main" + ".WebsiteSale.shop_country_info" + ) + with patch(parent_method, return_value=parent_result): + result = self.controller.shop_country_info( + self.country_us, + "billing", + ) + self.assertEqual( + result["required_fields"].count("firstname"), + 1, + ) + self.assertEqual( + result["required_fields"].count("lastname"), + 1, + ) diff --git a/website_sale_partner_firstname/views/templates.xml b/website_sale_partner_firstname/views/templates.xml new file mode 100644 index 0000000000..423d867b7f --- /dev/null +++ b/website_sale_partner_firstname/views/templates.xml @@ -0,0 +1,81 @@ + + + + + + + + + + + + + +