Tuesday, October 18, 2022

domain many2many fields in odoo 14


In this blog we will explain how to set domain many2many fields in Odoo 14

So let us say that we have four models (restaurants model, food category model, food model and orders model) the restaurant contains many categories, the category can contain many foods , as bellow 

 

class restaurants(models.Model):
    _name = "res.restaurants"
    category_ids = fields.Many2many('res.category', string="categories")
 

 

class Foods(models.Model):
    _name = "res.foods"
    category_id = fields.Many2one('res.category', string="Category")


class Orders(models.Model):
    _name = "res.orders"
    restaurant_id = fields.Many2one('res.restaurants', string="Restaurant")
    foods_ids = fields.Many2many('res.foods', string="Foods")


To create order the user must select the restaurant then select foods , but the system must show the food with category_id in  order.restaurant_id.category_ids

So we can do it by creating function in order model called on-change restaurant_id and this function will set the domain for foods , just as bellow

 

@api.onchange('restaurant_id')
    def _set_foods_domain(self):
        res = {}
        if self.restaurant_id:
            ids_lis = []
    
        food_ids = self.env['res.foods'].search([])
            for food_id in self.category_ids:
                if food_id.id in self.restaurant_id.mapped("category_ids").ids:
                    ids_lis.append(food_id.id)
            res['domain'] = {'food_ids': [('id', 'in', ids_lis)]}
        return res

ValueError: odoo.addons.web.__spec__ is None

 

I was trying to install Odoo 15 in Ubuntu 18.4, but when I was trying to run the odoo server I faced this error

 ValueError: odoo.addons.web.__spec__ is None

and to fix it just I run the command bellow 

sudo pip3 install Jinja2==2.10.1

 

amount in words in odoo 14

 


In this blog we will crate function to convert number amount to words in Odoo 14 

so the input of this function will be (amount as numbers, language code), and the output will be the amount as words , now let us create the function with the name amount_as_text()

def amount_as_text(self,amount,lang):
        text = _(str(self.currency_id.with_context(lang=lang).amount_to_text(amount)))
        return text

 

We can call the function in report templates with Arabic language as bellow

<t t-set="amount_text" t-value="o.amount_as_text(o.amount_total,'ar_001')"/>

Sunday, September 25, 2022

KSA E.Invoice Templates


This module provides 

(1) Six optional templates for KSA invoice
(2) custom options for the default demplate
(3) ability to design new template using GUI




Sunday, August 14, 2022

ModuleNotFoundError: No module named 'pdfkit'


When I was trying to run odoo server it was returning internal server error so I checked the log file and I found this error

ModuleNotFoundError: No module named 'pdfkit'

so to fix it install pdfkit by running the command bellow

sudo pip3 install pdfkit

odoo server wide modules

Odoo server wide modules are modules are supposed to provide features not necessarily tied to a particular database and they are loaded after running the server just as base and web modules , we can use this feature for example if we want to create login API with HTTP type , or API to return the databases list ...etc

How to define custom module as server wide module ?

First : if we are running the server from command line directly we must use --load with the running command 

eg : ./odoo-bin --addons-path='/opt/test/odoo-14.0/addons' --load=base,web,custom_web

Second : if we add odoo as service and created configuration file we can add server_wide_modules inside the configuration file 

eg :
[options]
; This is the password that allows database operations:
admin_passwd = master_admin@ssc
db_host = False
server_wide_modules = base,web,custom_web
db_port = False
db_user = test
db_password = False
addons_path = /opt/test/odoo/addons
logfile = /var/log/test/odoo-test.log
http_port = 8080
limit_request = 8192
limit_time_cpu = 600
limit_time_real = 120000

Tuesday, July 12, 2022

Bearer token authentication in odoo 14

 

In this blog we will explain how to create bearer token authentication in odoo 14 

first we have to know that we have 2 types of authentications in odoo

* Public Authentication 

* User Authentication

but we want to build APIs with the bearer token authentication so 

First : We have to inherit the ir.http model 

Second : create new class function with our authentication name _auth_method_auth_name , just as bellow 

import logging
from odoo.http import request
from odoo.exceptions import AccessDenied
import pprint
from odoo import api, http, models, tools, SUPERUSER_ID, _
from odoo.http import Response

_logger = logging.getLogger(__name__)

class IrHttp(models.AbstractModel):
    _inherit = 'ir.http'

    @classmethod
    def _auth_method_api_token(cls):
        headers = request.httprequest.environ
        api_token = headers.get("HTTP_AUTHORIZATION")
        api_token = str(api_token)
        token = api_token.replace("Bearer ","")
        user_token = token.replace("'","")
        check_user = 0

        if api_token and 'Bearer' in api_token:
            user_ids = request.env['res.users'].sudo().search([])
            for user in user_ids:
                if user_token == user.access_token:
                    check_user = 1
            if check_user != 0:
                return True
            else:
                raise AccessDenied()
        else:
            raise AccessDenied()

In this case we created bearer token authentication with the name api_token 

Third : use the new authentication 

Eg :
@http.route('/api_name, type='http', auth='api_token', methods=['POST'], csrf=False)

 

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...