Browse Source

[MIG][11.0] website_menu_by_user_status

Extending the controllers and the website xml templates is not required anymore because the
field is_visible on website.menu is computed.
pull/429/head
David Dufresne 1 year ago
parent
commit
e057c0fe50
12 changed files with 88 additions and 244 deletions
  1. +11
    -29
      website_menu_by_user_status/README.rst
  2. +0
    -3
      website_menu_by_user_status/__init__.py
  3. +3
    -5
      website_menu_by_user_status/__manifest__.py
  4. +0
    -1
      website_menu_by_user_status/controllers/__init__.py
  5. +0
    -122
      website_menu_by_user_status/controllers/main.py
  6. +0
    -34
      website_menu_by_user_status/i18n/website_menu_by_user_status.pot
  7. +0
    -2
      website_menu_by_user_status/models/__init__.py
  8. +14
    -2
      website_menu_by_user_status/models/website_menu.py
  9. +4
    -0
      website_menu_by_user_status/tests/__init__.py
  10. +56
    -0
      website_menu_by_user_status/tests/test_website_menu.py
  11. +0
    -0
      website_menu_by_user_status/views/website_menu.xml
  12. +0
    -46
      website_menu_by_user_status/views/website_templates.xml

+ 11
- 29
website_menu_by_user_status/README.rst View File

@@ -9,34 +9,17 @@ Website Menu By User Status
The module manages display website menu entries, depending if the user is
logged or not.
The selection of the display status can be chosen logged and/or not.
Extends features and view of website.menu model.
But for modules that install new routes like _your_wesite_/shop or _your_wesite_/event
the redirection will not work in such cases.

Installation
============

To install this module, just click to install butto

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

#. you must activate the developer mode

Usage
=====

To use this module, you need to edit website menu pages list view that can be found at :
Website Admin > Configuration > Settings > Configure website menus
To use this module, you need to:

#. remove default filter to edit website menu line
#. Go to Website > Configuration > Settings > Pages.
#. Select the page for which you would like to hide the menu.
#. In the `Related Menu Items` table, check whether the menu item is visible for logged/unlogged users.

The module inherit from website.menu to add 2 booleans fields, user_logged
and user_not_logged.
On top of that, website.layout template is extended to include a condition
that drive if the menu is built or not.
It has been choose to not only hide the menu to avoid to easily get around
by editing the html DOM.
#. remove default filter to edit website menu line

Bug Tracker
===========
@@ -46,23 +29,22 @@ Bugs are tracked on `GitHub Issues
check there if your issue has already been reported. If you spotted it first,
help us smash it by providing detailed and welcomed feedback.

Credits
=======

Images
------

* Odoo Community Association: `Icon <https://github.com/OCA/maintainer-tools/blob/master/template/module/static/description/icon.svg>`_.

* Odoo Community Association: `Icon <https://odoo-community.org/logo.png>`_.

Contributors
------------
* Bruno Joliveau <bruno.joliveau@savoirfairelinux.com>
* Jordi Riera <jordi.riera@savoirfairelinux.com>
* Meyomesse Gilles <meyomesse.gilles@gmail.com>
* David Dufresne

More information
----------------
Module developed and tested with Odoo version 10.0
For questions, please contact our support services
<support@savoirfairelinux.com>
Do not contact contributors directly about support or help with technical issues.

Maintainer
----------

+ 0
- 3
website_menu_by_user_status/__init__.py View File

@@ -1,6 +1,3 @@
# -*- coding: utf-8 -*-
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from . import models
from . import controllers


+ 3
- 5
website_menu_by_user_status/__manifest__.py View File

@@ -1,11 +1,10 @@
# -*- coding: utf-8 -*-
# Copyright 2013-2017 Savoir-faire Linux (<http://www.savoirfairelinux.com>)
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
{
'name': 'Website Menu By User Display',
'version': '10.0.1.0.0',
'version': '11.0.1.0.0',
'author': 'Savoir-faire Linux,Odoo Community Association (OCA)',
'website': 'http://www.savoirfairelinux.com',
'website': 'https://github.com/OCA/website',
'license': 'AGPL-3',
'category': 'Website',
'summary': 'Allow to manage the display of website.menus',
@@ -13,8 +12,7 @@
'website',
],
'data': [
'views/website_templates.xml',
'views/website_views.xml',
'views/website_menu.xml',
],
'installable': True,
}

+ 0
- 1
website_menu_by_user_status/controllers/__init__.py View File

@@ -1 +0,0 @@
from . import main

+ 0
- 122
website_menu_by_user_status/controllers/main.py View File

@@ -1,122 +0,0 @@
# -*- coding: utf-8 -*-
# Copyright 2017 ACSONE SA/NV
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

import logging

from odoo import http
from odoo.http import request

from odoo.addons.web.controllers.main import Home
from odoo import api

logger = logging.getLogger(__name__)


class Website(Home):

@api.model
def _get_pages(self):
"""
All pages width url
"""
return request.env['website.menu'].search([]).filtered('url')

@api.multi
def _check_user_access(self):
"""
Is user logged
"""
return request.env.user == request.website.user_id or False

@api.multi
def _get_suggest_page(self):
"""
All pages accessible by the user (logged or not)
"""
if self._check_user_access():
return self._get_pages().filtered(lambda r: r.user_not_logged)
else:
return self._get_pages().filtered(lambda r: r.user_logged)

@api.multi
def _get_page_access(self, url):
# active pages
active_urls = self._get_pages()

# compose access page url
base_url = u'/page/'
base_url += url

# check users (Public or Logged)
if self._check_user_access():
return active_urls.filtered(
lambda r: r.user_not_logged and r.url == base_url)
else:
return active_urls.filtered(
lambda r: r.user_logged and r.url == base_url)

@api.model
def _check_template(self, response, page):
template = request.env['ir.ui.view']
try:
template = http.request.env.ref(response.template,
raise_if_not_found=False)
except Exception:
logger.exception("Failed to load local template %r", page)
return template

@api.multi
def _default_response(self, page, **opt):
"""
This is for custom rendering page
:param page: requested by user
:param opt: parameters
:return: custom 404 or page requested
"""

# website response
default_response = super(Website, self).page(page, **opt)

# accessible pages
to_display = self._get_page_access(page)

# bad request or access denied
bad_request = request.render('website.404', {
'suggest_page': self._get_suggest_page()})

# managed pages for users are unique string
simple_page = True
if len(page.split('.')) > 1:
simple_page = False

# 404 page
page_404 = u'page_404'

# check template exists (avoid render 404 for page added by another
# module like contactus_thanks page in website_crm module)
template = self._check_template(default_response, page)

if template and template.name == page_404 or not template:
default_response = bad_request
elif template and template.name == page_404 and not to_display:
default_response = bad_request
elif template and template.name != \
page_404 and simple_page and not to_display:
default_response = bad_request
return default_response

@http.route('/', type='http', auth="public", website=True)
def index(self, **kw):
# Do not return 404 for odoo backend menu
main_menu = request.env.ref('website.main_menu',
raise_if_not_found=False)
if not main_menu:
url = request.httprequest.referrer.split('/')[-1]
return self._default_response(url, **kw)
return super(Website, self).index(**kw)

@http.route('/page/<page:page>', type='http', auth="public", website=True,
cache=300)
def page(self, page, **opt):
return self._default_response(page, **opt)

+ 0
- 34
website_menu_by_user_status/i18n/website_menu_by_user_status.pot View File

@@ -1,34 +0,0 @@
# Translation of Odoo Server.
# This file contains the translation of the following modules:
# * website_menu_by_user_status
#
msgid ""
msgstr ""
"Project-Id-Version: Odoo Server 8.0\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2015-02-02 14:37+0000\n"
"PO-Revision-Date: 2015-02-02 14:37+0000\n"
"Last-Translator: <>\n"
"Language-Team: \n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: \n"
"Plural-Forms: \n"

#. module: website_menu_by_user_status
#: code:addons/website_menu_by_user_status/models/website_menu.py:36
#, python-format
msgid "If checked, the menu will be displayed when the user is logged."
msgstr ""

#. module: website_menu_by_user_status
#: code:addons/website_menu_by_user_status/models/website_menu.py:43
#, python-format
msgid "If checked, the menu will be displayed when the user is not logged."
msgstr ""

#. module: website_menu_by_user_status
#: model:ir.model,name:website_menu_by_user_status.model_website_menu
msgid "Website Menu"
msgstr ""


+ 0
- 2
website_menu_by_user_status/models/__init__.py View File

@@ -1,5 +1,3 @@
# -*- coding: utf-8 -*-
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from . import website_menu


+ 14
- 2
website_menu_by_user_status/models/website_menu.py View File

@@ -1,14 +1,14 @@
# -*- coding: utf-8 -*-
# Copyright 2013-2017 Savoir-faire Linux (<http://www.savoirfairelinux.com>)
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from odoo import models, fields, _
from odoo import api, fields, models, _


class WebsiteMenu(models.Model):
"""Improve website.menu with adding booleans that drive
if the menu is displayed when the user is logger or not.
"""

_inherit = 'website.menu'

user_logged = fields.Boolean(
@@ -26,3 +26,15 @@ class WebsiteMenu(models.Model):
"the menu will be displayed when the user is not logged "
"and give access.")
)

@api.one
def _compute_visible(self):
"""Display the menu item whether the user is logged or not."""
super()._compute_visible()
if not self.is_visible:
return

if self.env.user == self.env.ref('base.public_user'):
self.is_visible = self.user_not_logged
else:
self.is_visible = self.user_logged

+ 4
- 0
website_menu_by_user_status/tests/__init__.py View File

@@ -0,0 +1,4 @@
# Copyright 2018 David Dufresne
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from . import test_website_menu

+ 56
- 0
website_menu_by_user_status/tests/test_website_menu.py View File

@@ -0,0 +1,56 @@
# Copyright 2018 David Dufresne
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from odoo.tests import SavepointCase


class TestWebsiteMenu(SavepointCase):

@classmethod
def setUpClass(cls):
super().setUpClass()
cls.page = cls.env.ref('website.contactus_page')
cls.page.is_visible = False

cls.menu = cls.env.ref('website.menu_contactus')
cls.menu.user_logged = False
cls.menu.user_not_logged = False

cls.public_user = cls.env.ref('base.public_user')
cls.demo_user = cls.env.ref('base.user_demo')

def test_visible_user_logged_demo(self):
self.page.is_visible = True
self.menu.user_logged = True
self.assertTrue(self.menu.sudo(self.demo_user).is_visible)

def test_visible_user_logged_public(self):
self.page.is_visible = True
self.menu.user_logged = True
self.assertFalse(self.menu.sudo(self.public_user).is_visible)

def test_visible_user_not_logged_demo(self):
self.page.is_visible = True
self.user_not_logged = True
self.assertFalse(self.menu.sudo(self.demo_user).is_visible)

def test_visible_user_not_logged_public(self):
self.page.is_visible = True
self.user_not_logged = True
self.assertFalse(self.menu.sudo(self.public_user).is_visible)

def test_not_visible_user_logged_demo(self):
self.menu.user_logged = True
self.assertTrue(self.menu.sudo(self.demo_user).is_visible)

def test_not_visible_user_logged_public(self):
self.menu.user_logged = True
self.assertFalse(self.menu.sudo(self.public_user).is_visible)

def test_not_visible_user_not_logged_demo(self):
self.user_not_logged = True
self.assertFalse(self.menu.sudo(self.demo_user).is_visible)

def test_not_visible_user_not_logged_public(self):
self.user_not_logged = True
self.assertFalse(self.menu.sudo(self.public_user).is_visible)

website_menu_by_user_status/views/website_views.xml → website_menu_by_user_status/views/website_menu.xml View File


+ 0
- 46
website_menu_by_user_status/views/website_templates.xml View File

@@ -1,46 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright 2013-2017 Savoir-faire Linux (<http://www.savoirfairelinux.com>)
Copyright 2017 ACSONE SA/NV
License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). -->
<odoo>
<data>
<template id="website_display_menu" name="Conditional display menu"
inherit_id="website.layout"
active="True" customize_show="True">
<xpath expr="//ul[@id='top_menu']/t/t" position="replace">
<t t-if="(submenu.user_logged and request.uid != %(base.public_user)d) or (submenu.user_not_logged and request.uid == %(base.public_user)d)">
<t t-call="website.submenu"/>
</t>
</xpath>
</template>
</data>

<template id="404" name="Display pages" inherit_id="website.404">
<xpath expr="//div[@id='wrap']" position="replace">
<div id="wrap">
<t t-raw="0"/>
<div class="oe_structure oe_empty">
<div class="container">
<h1 class="mt32">404: Page not found!</h1>
<p>
The page you were looking for could not be found; it is possible you have
typed the address incorrectly, but it has most probably been removed due
to the recent website reorganisation.
</p>
<p t-if="not suggest_page">No page active</p>
<p t-if="suggest_page">Maybe you were looking for one of these popular pages ?</p>
<ul t-if="suggest_page">
<t t-foreach="suggest_page or []" t-as="page">
<li><a t-attf-href="#{page.url}"><t t-esc="page.name"/></a></li>
</t>
</ul>
</div>
</div>

<t t-if="request.debug">
<t t-call="website.http_error_debug"/>
</t>
</div>
</xpath>
</template>
</odoo>

Loading…
Cancel
Save