Thursday, September 14, 2023

portal view for model in odoo

In this blog we will define the steps to create portal view for model in odoo

So

Step 1 : inherit "portal.mixin" model inside your model as bellow 

    class ClassName(models.Model):
        _name = "model.name"
        _inherit = ['model.name','portal.mixin']
        //for old models

    class ClassName(models.Model):
        _name = "model.name"
        _inherit = ['model.name','portal.mixin']
        //for new models

Step 2 : set value for access_url field inside your model by inheriting the get_portal_url() function as bellow 

    def _compute_access_url(self):
        super(productTemplate, self)._compute_access_url()
        for rec in self:
            rec.access_url = '/model/name/%s' % rec.id

Step 3 : add another function to define the portal url for each record from your model as bellow

    def get_portal_url(self, suffix=None, report_type=None, download=None, query_string=None, anchor=None, view_name=None):
        self.ensure_one()
        url = self.access_url + '%s?access_token=%s%s%s%s%s' % (
            suffix if suffix else '',
            self._portal_ensure_token(),
            '&report_type=%s' % report_type if report_type else '',
            '&download=true' if download else '',
            query_string if query_string else '',
            '#%s' % anchor if anchor else ''
        )
        return url

Step 4 : create your controller which must inherit the "CustomerPortal" controller as bellow 

    # -*- coding: utf-8 -*-

    from odoo import http, _
    from odoo.exceptions import AccessError, MissingError
    from odoo.http import request
    from odoo.addons.portal.controllers.portal import CustomerPortal, pager as               portal_pager
    from odoo.tools import groupby as groupbyelem
    from odoo.osv.expression import OR

    class CustomerPortal(CustomerPortal):

        @http.route(['/model/name/<int:record_id>'], type='http', auth="public", website=True)
        def my_model_portal(self, record_id=None, access_token=None, **kw):
            try:
                model_name_sudo = self._document_check_access('model.name', record_id, access_token)
            except (AccessError, MissingError):
               return request.redirect('/my')
            values = {}
            return request.render("module_name.template_id", values)

Step 5 : create the template view as bellow

    <?xml version="1.0" encoding="UTF-8"?>
    <odoo>
        <template id="template_id" name="Template Name">
            <t t-call="website.layout">
                <form action='/form/name'>
                </form>
            </t>
        </template>
    </odoo>

Step 6 : call the portal view for specific model record as bellow

    <a t-att-href="record_id.get_portal_url()"></a>

No comments:

Post a Comment

Odoo Invoice Qr code issues

There are two main issues must of us facing with the QR code in Odoo invoice & these issues are 1/ QR code displayed as broken image w...