diff --git a/website_sale_partner_firstname/README.rst b/website_sale_partner_firstname/README.rst new file mode 100644 index 0000000000..b0046b7990 --- /dev/null +++ b/website_sale_partner_firstname/README.rst @@ -0,0 +1,95 @@ +.. image:: https://odoo-community.org/readme-banner-image + :target: https://odoo-community.org/get-involved?utm_source=readme + :alt: Odoo Community Association + +============================= +First & Last Name at Checkout +============================= + +.. + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! This file is generated by oca-gen-addon-readme !! + !! changes will be overwritten. !! + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! source digest: sha256:bf76c0ea622c9101ac6bd3d54ad0124b326577cd974d188dafc14c889b5d90de + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +.. |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/license-LGPL--3-blue.png + :target: http://www.gnu.org/licenses/lgpl-3.0-standalone.html + :alt: License: LGPL-3 +.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Fwebsite-lightgray.png?logo=github + :target: https://github.com/OCA/website/tree/19.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-19-0/website-19-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=19.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..c36bd48335 --- /dev/null +++ b/website_sale_partner_firstname/__init__.py @@ -0,0 +1,2 @@ +from . import controllers as controllers +from . import models as models diff --git a/website_sale_partner_firstname/__manifest__.py b/website_sale_partner_firstname/__manifest__.py new file mode 100644 index 0000000000..8ec5815bb4 --- /dev/null +++ b/website_sale_partner_firstname/__manifest__.py @@ -0,0 +1,14 @@ +{ + "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": "19.0.1.0.0", + "website": "https://github.com/OCA/website", + "license": "AGPL-3", + "images": [], + "depends": ["website_sale", "partner_firstname"], + "data": [ + "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..01df18619c --- /dev/null +++ b/website_sale_partner_firstname/controllers/__init__.py @@ -0,0 +1 @@ +from . import main as 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..ec0eac84a0 --- /dev/null +++ b/website_sale_partner_firstname/controllers/main.py @@ -0,0 +1,47 @@ +from odoo.http import request + +from odoo.addons.portal.controllers.portal import CustomerPortal + + +class CustomerPortalFirstLastName(CustomerPortal): + """Replace 'name' with firstname/lastname on checkout and portal.""" + + def _create_or_update_address(self, partner_sudo, **form_data): + """Synthesize 'name' from firstname/lastname so the base code's + address_values['name'] check doesn't raise a KeyError.""" + if "name" not in form_data and ( + "firstname" in form_data or "lastname" in form_data + ): + firstname = (form_data.get("firstname") or "").strip() + lastname = (form_data.get("lastname") or "").strip() + form_data["name"] = request.env["res.partner"]._get_computed_name( + lastname, + firstname, + ) + return super()._create_or_update_address(partner_sudo, **form_data) + + def _get_mandatory_billing_address_fields(self, country_sudo): + """Swap 'name' for 'firstname' and 'lastname' in mandatory billing fields.""" + fields = super()._get_mandatory_billing_address_fields(country_sudo) + fields.discard("name") + fields |= {"firstname", "lastname"} + return fields + + def _get_mandatory_delivery_address_fields(self, country_sudo): + """Swap 'name' for 'firstname' and 'lastname' in mandatory delivery fields.""" + fields = super()._get_mandatory_delivery_address_fields(country_sudo) + fields.discard("name") + fields |= {"firstname", "lastname"} + return fields + + def portal_address_country_info(self, country, address_type, **kw): + """Update the JS required-fields list for firstname/lastname.""" + result = super().portal_address_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/models/__init__.py b/website_sale_partner_firstname/models/__init__.py new file mode 100644 index 0000000000..10357f9cf3 --- /dev/null +++ b/website_sale_partner_firstname/models/__init__.py @@ -0,0 +1 @@ +from . import res_partner as res_partner diff --git a/website_sale_partner_firstname/models/res_partner.py b/website_sale_partner_firstname/models/res_partner.py new file mode 100644 index 0000000000..400f2fa867 --- /dev/null +++ b/website_sale_partner_firstname/models/res_partner.py @@ -0,0 +1,8 @@ +from odoo import models + + +class ResPartner(models.Model): + _inherit = "res.partner" + + def _get_frontend_writable_fields(self): + return super()._get_frontend_writable_fields() | {"firstname", "lastname"} 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..66e615dc2c --- /dev/null +++ b/website_sale_partner_firstname/static/description/index.html @@ -0,0 +1,443 @@ + + + + + +README.rst + + + +
+ + + +Odoo Community Association + +
+

First & Last Name at Checkout

+ +

Beta License: LGPL-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..e497979d4d --- /dev/null +++ b/website_sale_partner_firstname/tests/__init__.py @@ -0,0 +1 @@ +from . import test_controller as 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..4369453e8e --- /dev/null +++ b/website_sale_partner_firstname/tests/test_controller.py @@ -0,0 +1,139 @@ +from odoo.tests import tagged + +from odoo.addons.website_sale.tests.common import MockRequest, WebsiteSaleCommon +from odoo.addons.website_sale_partner_firstname.controllers.main import ( + CustomerPortalFirstLastName, +) + + +@tagged("post_install", "-at_install") +class TestMandatoryFields(WebsiteSaleCommon): + @classmethod + def setUpClass(cls): + super().setUpClass() + cls.controller = CustomerPortalFirstLastName() + + def test_billing_fields_removes_name(self): + """Mandatory billing fields should not contain 'name'.""" + with MockRequest(self.env, website=self.website): + fields = self.controller._get_mandatory_billing_address_fields( + self.country_us, + ) + self.assertNotIn("name", fields) + + def test_billing_fields_has_firstname_lastname(self): + """Mandatory billing fields should contain 'firstname' and 'lastname'.""" + with MockRequest(self.env, website=self.website): + fields = self.controller._get_mandatory_billing_address_fields( + self.country_us, + ) + self.assertIn("firstname", fields) + self.assertIn("lastname", fields) + + def test_delivery_fields_removes_name(self): + """Mandatory delivery fields should not contain 'name'.""" + with MockRequest(self.env, website=self.website): + fields = self.controller._get_mandatory_delivery_address_fields( + self.country_us, + ) + self.assertNotIn("name", fields) + + def test_delivery_fields_has_firstname_lastname(self): + """Mandatory delivery fields should contain 'firstname' and 'lastname'.""" + with MockRequest(self.env, website=self.website): + fields = self.controller._get_mandatory_delivery_address_fields( + self.country_us, + ) + self.assertIn("firstname", fields) + self.assertIn("lastname", fields) + + +@tagged("post_install", "-at_install") +class TestCountryInfo(WebsiteSaleCommon): + @classmethod + def setUpClass(cls): + super().setUpClass() + cls.controller = CustomerPortalFirstLastName() + + 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.portal_address_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.portal_address_country_info( + self.country_us, + "billing", + ) + self.assertIn("firstname", result["required_fields"]) + self.assertIn("lastname", result["required_fields"]) + + +@tagged("post_install", "-at_install") +class TestCreateOrUpdateAddress(WebsiteSaleCommon): + @classmethod + def setUpClass(cls): + super().setUpClass() + cls.controller = CustomerPortalFirstLastName() + + def test_create_address_with_firstname_lastname(self): + """Creating an address with firstname/lastname (no 'name') should work.""" + with MockRequest(self.env, website=self.website): + partner, feedback = self.controller._create_or_update_address( + self.env["res.partner"], # empty recordset = create + address_type="billing", + verify_address_values=False, + firstname="John", + lastname="Doe", + email="john@example.com", + street="123 Main St", + city="Springfield", + country_id=str(self.country_us.id), + phone="555-1234", + ) + self.assertTrue(partner.exists()) + self.assertEqual(partner.firstname, "John") + self.assertEqual(partner.lastname, "Doe") + + def test_update_address_with_firstname_lastname(self): + """Updating an existing address with firstname/lastname should not KeyError.""" + partner = self.env["res.partner"].create( + { + "firstname": "Jane", + "lastname": "Smith", + "email": "jane@example.com", + "street": "456 Oak Ave", + "city": "Shelbyville", + "country_id": self.country_us.id, + } + ) + with MockRequest(self.env, website=self.website): + updated_partner, feedback = self.controller._create_or_update_address( + partner.sudo(), + address_type="billing", + verify_address_values=False, + firstname="Janet", + lastname="Smith", + email="jane@example.com", + street="456 Oak Ave", + city="Shelbyville", + country_id=str(self.country_us.id), + phone="555-5678", + ) + self.assertEqual(updated_partner.firstname, "Janet") + self.assertEqual(updated_partner.lastname, "Smith") + + +@tagged("post_install", "-at_install") +class TestFrontendWritableFields(WebsiteSaleCommon): + def test_firstname_lastname_writable(self): + """'firstname' and 'lastname' should be in frontend writable fields.""" + writable = self.env["res.partner"]._get_frontend_writable_fields() + self.assertIn("firstname", writable) + self.assertIn("lastname", writable) diff --git a/website_sale_partner_firstname/views/templates.xml b/website_sale_partner_firstname/views/templates.xml new file mode 100644 index 0000000000..b5f1055fcc --- /dev/null +++ b/website_sale_partner_firstname/views/templates.xml @@ -0,0 +1,42 @@ + + + + + + + + + +