|
@@ -1,22 +1,63 @@
|
|
|
# -*- coding: utf-8 -*-
|
|
|
-# from odoo import http
|
|
|
+import logging
|
|
|
+from odoo import exceptions, http, models
|
|
|
+from odoo.http import request
|
|
|
|
|
|
+_logger = logging.getLogger(__name__)
|
|
|
|
|
|
-# class SetCustomerGroups(http.Controller):
|
|
|
-# @http.route('/set_customer_groups/set_customer_groups', auth='public')
|
|
|
-# def index(self, **kw):
|
|
|
-# return "Hello, world"
|
|
|
+class RestrictedPageController(http.Controller):
|
|
|
+ @http.route(
|
|
|
+ '/restricted-content/<string:slug>',
|
|
|
+ type='http',
|
|
|
+ auth='user',
|
|
|
+ website=True,
|
|
|
+ sitemap=False,
|
|
|
+ priority=99
|
|
|
+ )
|
|
|
|
|
|
-# @http.route('/set_customer_groups/set_customer_groups/objects', auth='public')
|
|
|
-# def list(self, **kw):
|
|
|
-# return http.request.render('set_customer_groups.listing', {
|
|
|
-# 'root': '/set_customer_groups/set_customer_groups',
|
|
|
-# 'objects': http.request.env['set_customer_groups.set_customer_groups'].search([]),
|
|
|
-# })
|
|
|
+ def restricted_page(self, slug, **kw):
|
|
|
+ # Find the website.page that matches the slug
|
|
|
+ _logger.debug("restricted_page: slug=%s | user=%s (%s)",
|
|
|
+ slug, request.env.user.login, request.env.user.id)
|
|
|
+ page = request.env['website.page'].sudo().search([
|
|
|
+ ('url', '=', '/restricted-content/%s' % slug)
|
|
|
+ ], limit=1)
|
|
|
+ if not page:
|
|
|
+ return request.not_found()
|
|
|
|
|
|
-# @http.route('/set_customer_groups/set_customer_groups/objects/<model("set_customer_groups.set_customer_groups"):obj>', auth='public')
|
|
|
-# def object(self, obj, **kw):
|
|
|
-# return http.request.render('set_customer_groups.object', {
|
|
|
-# 'object': obj
|
|
|
-# })
|
|
|
+ # Load the product_page_access mapping for this page
|
|
|
+ mapping = request.env['product_page_access.page_product_mapping'].sudo().search([
|
|
|
+ ('page', '=', page.id)
|
|
|
+ ], limit=1)
|
|
|
+ if not mapping:
|
|
|
+ # no allowlist defined → redirect to shop
|
|
|
+ _logger.debug("restricted_page: no mapping for page_id=%s, redirecting", page.id)
|
|
|
+ return request.redirect('/')
|
|
|
|
|
|
+ _logger.debug("restricted_page: allowed_product=%s", mapping.products.ids)
|
|
|
+
|
|
|
+ if not mapping.products:
|
|
|
+ # empty list → redirect
|
|
|
+ _logger.debug("restricted_page: empty product for page_id=%s, redirecting",
|
|
|
+ page.id)
|
|
|
+ return request.redirect('/')
|
|
|
+
|
|
|
+ # Check if the user ever bought at least one of them
|
|
|
+ user = request.env.user
|
|
|
+ has_access = request.env['sale.order.line'].sudo().search_count([
|
|
|
+ ('order_id.partner_id', 'child_of', user.partner_id.id),
|
|
|
+ ('order_id.state', 'in', ('sale', 'done')),
|
|
|
+ ('product_id', 'in', mapping.products.ids),
|
|
|
+ ], limit=1)
|
|
|
+
|
|
|
+ _logger.debug("restricted_page: user_id=%s has_access=%s", user.id, has_access)
|
|
|
+
|
|
|
+ if not has_access:
|
|
|
+ # Redirect to the shop so the user can buy
|
|
|
+ _logger.debug("restricted_page: user_id=%s has_access=%s", user.id, has_access)
|
|
|
+
|
|
|
+ return request.redirect(mapping.link_out_product.website_url)
|
|
|
+
|
|
|
+ # 5. Everything fine – render the page
|
|
|
+ _logger.debug("restricted_page: rendering page.key=%s", page.key)
|
|
|
+ return request.render(page.key, {})
|