Skip to content

Commit

Permalink
[IMP] website_sale_require_legal: document, fix and test new module s…
Browse files Browse the repository at this point in the history
…cope

This is what the module does now:

- Add the posibility to configure legal terms acceptance requirement before saving a new partner.
- Log that acceptance as a note in the partner.
- Extend upstream's legal requirement before payment and log that acceptance as a note in the sale order.

@Tecnativa TT25963
  • Loading branch information
Jairo Llopis committed Jul 13, 2021
1 parent 0dd4da5 commit 6b9c2ba
Show file tree
Hide file tree
Showing 14 changed files with 223 additions and 42 deletions.
36 changes: 31 additions & 5 deletions website_sale_require_legal/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,12 @@ Require accepting legal terms to checkout

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

This module was written to extend the functionality of your website shop to
support forcing the user to accept your legal advice, terms of use and privacy
policy, and allow you to comply with some countries' laws.
This module extends your e-commerce legal compliance options:

#. Require accepting legal terms before submitting a new address.
#. Log a note in the partner when such terms are accepted.
#. Log a note in the sale order when terms are accepted before payment
(done for every online payment, it is an upstream feature).

**Table of contents**

Expand All @@ -39,14 +42,34 @@ Installation

To install this module, you need to:

* Install repository `OCA/website <https://github.com/OCA/website>`_.
* Install ``website_legal_page`` from repository `OCA/website <https://github.com/OCA/website>`_.

Configuration
=============

To configure this module, you need to:

* Set up your legal pages.
#. Install it.

#. Set up `your legal pages </legal>`__.

#. Go to your e-commerce and make a sample checkout.

#. Visit `/shop/address </shop/address>`__ and enable *Customize > Require
Legal Terms Acceptance*.

.. figure:: https://raw.githubusercontent.com/OCA/e-commerce/13.0/website_sale_require_legal/static/description/address-enable.png

This will require acceptance before recording a new address, and log visitor's
acceptance.

#. Visit `/shop/payment </shop/payment>`__ and enable *Customize > Accept Terms
& Conditions* (upstream Odoo feature).

.. figure:: https://raw.githubusercontent.com/OCA/e-commerce/13.0/website_sale_require_legal/static/description/payment-enable.png

This will require acceptance before paying the sale order, and log visitor's
acceptance.

Usage
=====
Expand All @@ -63,6 +86,9 @@ Known issues / Roadmap
notification should be made. An implicit acceptance could be printed in the
payment screen to solve this. Maybe that could be a work to develop in
another module.
* If you enable both acceptance views as explained in the configuration section,
first-time buyers will have to accept the legal terms between 2 and 3 times
to buy.

Bug Tracker
===========
Expand Down
4 changes: 2 additions & 2 deletions website_sale_require_legal/__manifest__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2015, 2017 Tecnativa - Jairo Llopis
# Copyright 2015, 2017, 2021 Tecnativa - Jairo Llopis
# Copyright 2016 Tecnativa - Vicent Cubells
# Copyright 2019 Tecnativa - Cristina Martin R.
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
Expand All @@ -13,5 +13,5 @@
"application": False,
"installable": True,
"depends": ["website_legal_page", "website_sale"],
"data": ["views/website_sale.xml", "views/assets.xml"],
"data": ["templates/website_sale.xml", "templates/assets.xml"],
}
70 changes: 56 additions & 14 deletions website_sale_require_legal/controllers/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
# Copyright 2016 Tecnativa - Vicent Cubells
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).

from odoo import SUPERUSER_ID
from odoo.http import request
from odoo import _
from odoo.http import request, route

from odoo.addons.website_sale.controllers import main

Expand All @@ -28,7 +28,12 @@ def checkout_form_validate(self, mode, all_form_values, data):
"""Require accepting legal terms to validate form."""
# Patch context
old_context = request.context
request.context = dict(request.context, needs_legal=True)
request.context = dict(
request.context,
needs_legal=request.website.viewref(
"website_sale_require_legal.address_require_legal"
).active,
)
result = super().checkout_form_validate(mode, all_form_values, data)
# Unpatch context
request.context = old_context
Expand All @@ -37,15 +42,52 @@ def checkout_form_validate(self, mode, all_form_values, data):
def _checkout_form_save(self, mode, checkout, all_values):
res = super()._checkout_form_save(mode, checkout, all_values)
if all_values.get("submitted") and all_values.get("accepted_legal_terms"):
environ = request.httprequest.headers.environ
metadata = "Website legal terms acceptance metadata: "
metadata += "<br/>".join(
"{}: {}".format(val, environ.get(val))
for val in ("REMOTE_ADDR", "HTTP_USER_AGENT", "HTTP_ACCEPT_LANGUAGE",)
)
partner_id = request.env["res.partner"].browse(res)
website_user = request.website.salesperson_id.id or SUPERUSER_ID
partner_id.with_user(website_user).message_post(
body=metadata, message_type="notification"
)
partner = request.env["res.partner"].browse(res)
self._log_acceptance_metadata(partner)
return res

def _log_acceptance_metadata(self, record):
"""Log legal terms acceptance metadata."""
environ = request.httprequest.headers.environ
message = _("Website legal terms acceptance metadata: %s")
metadata = "<br/>".join(
"{}: {}".format(val, environ.get(val))
for val in ("REMOTE_ADDR", "HTTP_USER_AGENT", "HTTP_ACCEPT_LANGUAGE",)
)
record.sudo().message_post(body=message % metadata, message_type="notification")

@route()
def payment_transaction(
self,
acquirer_id,
save_token=False,
so_id=None,
access_token=None,
token=None,
**kwargs
):
"""Record sale order payment legal terms acceptance.
If the "Accept Terms & Conditions" upstream view is enabled in the
website, to get here, user must have accepted legal terms.
"""
result = super().payment_transaction(
acquirer_id, save_token, so_id, access_token, token, **kwargs
)
# If the "Accept Terms & Conditions" view is disabled, we log nothing
if not request.website.viewref("website_sale.payment_sale_note").active:
return result
# Retrieve the sale order
if so_id:
env = request.env["sale.order"]
domain = [("id", "=", so_id)]
if access_token:
env = env.sudo()
domain.append(("access_token", "=", access_token))
order = env.search(domain, limit=1)
else:
order = request.website.sale_get_order()
# Log metadata in the sale order
if order:
self._log_acceptance_metadata(order)
return result
22 changes: 21 additions & 1 deletion website_sale_require_legal/readme/CONFIGURE.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,23 @@
To configure this module, you need to:

* Set up your legal pages.
#. Install it.

#. Set up `your legal pages </legal>`__.

#. Go to your e-commerce and make a sample checkout.

#. Visit `/shop/address </shop/address>`__ and enable *Customize > Require
Legal Terms Acceptance*.

.. figure:: ../static/description/address-enable.png

This will require acceptance before recording a new address, and log visitor's
acceptance.

#. Visit `/shop/payment </shop/payment>`__ and enable *Customize > Accept Terms
& Conditions* (upstream Odoo feature).

.. figure:: ../static/description/payment-enable.png

This will require acceptance before paying the sale order, and log visitor's
acceptance.
9 changes: 6 additions & 3 deletions website_sale_require_legal/readme/DESCRIPTION.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
This module was written to extend the functionality of your website shop to
support forcing the user to accept your legal advice, terms of use and privacy
policy, and allow you to comply with some countries' laws.
This module extends your e-commerce legal compliance options:

#. Require accepting legal terms before submitting a new address.
#. Log a note in the partner when such terms are accepted.
#. Log a note in the sale order when terms are accepted before payment
(done for every online payment, it is an upstream feature).
2 changes: 1 addition & 1 deletion website_sale_require_legal/readme/INSTALL.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
To install this module, you need to:

* Install repository `OCA/website <https://github.com/OCA/website>`_.
* Install ``website_legal_page`` from repository `OCA/website <https://github.com/OCA/website>`_.
3 changes: 3 additions & 0 deletions website_sale_require_legal/readme/ROADMAP.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,6 @@
notification should be made. An implicit acceptance could be printed in the
payment screen to solve this. Maybe that could be a work to develop in
another module.
* If you enable both acceptance views as explained in the configuration section,
first-time buyers will have to accept the legal terms between 2 and 3 times
to buy.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
44 changes: 36 additions & 8 deletions website_sale_require_legal/static/description/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="generator" content="Docutils 0.15.1: http://docutils.sourceforge.net/" />
<meta name="generator" content="Docutils: http://docutils.sourceforge.net/" />
<title>Require accepting legal terms to checkout</title>
<style type="text/css">

Expand Down Expand Up @@ -368,9 +368,13 @@ <h1 class="title">Require accepting legal terms to checkout</h1>
!! changes will be overwritten. !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! -->
<p><a class="reference external" href="https://odoo-community.org/page/development-status"><img alt="Beta" src="https://img.shields.io/badge/maturity-Beta-yellow.png" /></a> <a class="reference external" href="http://www.gnu.org/licenses/agpl-3.0-standalone.html"><img alt="License: AGPL-3" src="https://img.shields.io/badge/licence-AGPL--3-blue.png" /></a> <a class="reference external" href="https://github.com/OCA/e-commerce/tree/13.0/website_sale_require_legal"><img alt="OCA/e-commerce" src="https://img.shields.io/badge/github-OCA%2Fe--commerce-lightgray.png?logo=github" /></a> <a class="reference external" href="https://translation.odoo-community.org/projects/e-commerce-13-0/e-commerce-13-0-website_sale_require_legal"><img alt="Translate me on Weblate" src="https://img.shields.io/badge/weblate-Translate%20me-F47D42.png" /></a> <a class="reference external" href="https://runbot.odoo-community.org/runbot/113/13.0"><img alt="Try me on Runbot" src="https://img.shields.io/badge/runbot-Try%20me-875A7B.png" /></a></p>
<p>This module was written to extend the functionality of your website shop to
support forcing the user to accept your legal advice, terms of use and privacy
policy, and allow you to comply with some countries’ laws.</p>
<p>This module extends your e-commerce legal compliance options:</p>
<ol class="arabic simple">
<li>Require accepting legal terms before submitting a new address.</li>
<li>Log a note in the partner when such terms are accepted.</li>
<li>Log a note in the sale order when terms are accepted before payment
(done for every online payment, it is an upstream feature).</li>
</ol>
<p><strong>Table of contents</strong></p>
<div class="contents local topic" id="contents">
<ul class="simple">
Expand All @@ -391,15 +395,36 @@ <h1 class="title">Require accepting legal terms to checkout</h1>
<h1><a class="toc-backref" href="#id1">Installation</a></h1>
<p>To install this module, you need to:</p>
<ul class="simple">
<li>Install repository <a class="reference external" href="https://github.com/OCA/website">OCA/website</a>.</li>
<li>Install <tt class="docutils literal">website_legal_page</tt> from repository <a class="reference external" href="https://github.com/OCA/website">OCA/website</a>.</li>
</ul>
</div>
<div class="section" id="configuration">
<h1><a class="toc-backref" href="#id2">Configuration</a></h1>
<p>To configure this module, you need to:</p>
<ul class="simple">
<li>Set up your legal pages.</li>
</ul>
<ol class="arabic">
<li><p class="first">Install it.</p>
</li>
<li><p class="first">Set up <a class="reference external" href="/legal">your legal pages</a>.</p>
</li>
<li><p class="first">Go to your e-commerce and make a sample checkout.</p>
</li>
<li><p class="first">Visit <a class="reference external" href="/shop/address">/shop/address</a> and enable <em>Customize &gt; Require
Legal Terms Acceptance</em>.</p>
<div class="figure">
<img alt="https://raw.githubusercontent.com/OCA/e-commerce/13.0/website_sale_require_legal/static/description/address-enable.png" src="https://raw.githubusercontent.com/OCA/e-commerce/13.0/website_sale_require_legal/static/description/address-enable.png" />
</div>
<p>This will require acceptance before recording a new address, and log visitor’s
acceptance.</p>
</li>
<li><p class="first">Visit <a class="reference external" href="/shop/payment">/shop/payment</a> and enable <em>Customize &gt; Accept Terms
&amp; Conditions</em> (upstream Odoo feature).</p>
<div class="figure">
<img alt="https://raw.githubusercontent.com/OCA/e-commerce/13.0/website_sale_require_legal/static/description/payment-enable.png" src="https://raw.githubusercontent.com/OCA/e-commerce/13.0/website_sale_require_legal/static/description/payment-enable.png" />
</div>
<p>This will require acceptance before paying the sale order, and log visitor’s
acceptance.</p>
</li>
</ol>
</div>
<div class="section" id="usage">
<h1><a class="toc-backref" href="#id3">Usage</a></h1>
Expand All @@ -416,6 +441,9 @@ <h1><a class="toc-backref" href="#id4">Known issues / Roadmap</a></h1>
notification should be made. An implicit acceptance could be printed in the
payment screen to solve this. Maybe that could be a work to develop in
another module.</li>
<li>If you enable both acceptance views as explained in the configuration section,
first-time buyers will have to accept the legal terms between 2 and 3 times
to buy.</li>
</ul>
</div>
<div class="section" id="bug-tracker">
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
38 changes: 33 additions & 5 deletions website_sale_require_legal/static/tests/tours/tour.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
odoo.define("website_sale_require_legal.tour", function(require) {
"use strict";

var base = require("web_editor.base");
var tour = require("web_tour.tour");

var steps = [
Expand All @@ -17,11 +16,32 @@ odoo.define("website_sale_require_legal.tour", function(require) {
{
trigger: ".btn:contains('Process Checkout')",
},

// Fill all required fields except legal terms acceptance
{
run: "text Super Mario",
trigger: ".checkout_autoformat input[name=name]",
},
{
run: "text mario@example.com",
trigger: ".checkout_autoformat input[name=email]",
},
{
run: "text 000 000 000",
trigger: ".checkout_autoformat input[name=phone]",
},
{
run: "text Castle St., 1",
trigger: ".checkout_autoformat input[name=street]",
},
{
run: "text Mushroom Kingdom",
trigger: ".checkout_autoformat input[name=city]",
},
{
run: "text Japan",
trigger: ".checkout_autoformat select[name=country_id]",
},
// Submit, to check the lack of acceptance is a failure
{
trigger: ".btn-primary:contains('Next')",
Expand All @@ -33,12 +53,21 @@ odoo.define("website_sale_require_legal.tour", function(require) {
{
trigger: ".btn-primary:contains('Next')",
},
// If I can proceed to payment, it's because the form validated fine
{
trigger: ".btn-primary:contains('Confirm')",
trigger: "#checkbox_cgv",
},
{
trigger: '#payment_method label:contains("Wire Transfer")',
},
{
extra_trigger:
'#payment_method label:contains("Wire Transfer") input:checked,#payment_method:not(:has("input:radio:visible"))',
trigger: 'button[id="o_payment_form_pay"]:visible:not(:disabled)',
},
// If I can proceed to payment, it's because the form validated fine
{
trigger: ".btn-primary:contains('Pay Now')",
// HACK https://github.com/odoo/odoo/blob/0a57e10accfa12dae5fea5bb8cb65b90bdf6e839/addons/website_sale/static/tests/tours/website_sale_buy.js#L76-L85
trigger: '.oe_website_sale:contains("Please make a payment to:")',
},
];

Expand All @@ -47,7 +76,6 @@ odoo.define("website_sale_require_legal.tour", function(require) {
{
url: "/shop",
test: true,
wait_for: base.ready(),
},
steps
);
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,13 @@
</label>
</div>
</template>
<template id="address" inherit_id="website_sale.address">
<template
id="address_require_legal"
inherit_id="website_sale.address"
name="Require Legal Terms Acceptance"
customize_show="True"
active="False"
>
<xpath expr="//div[hasclass('w-100')][last()]" position="after">
<t t-call="website_sale_require_legal.accept_input" />
</xpath>
Expand Down
Loading

0 comments on commit 6b9c2ba

Please sign in to comment.