Tuesday, October 18, 2022

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)

 

Monday, July 4, 2022

Change user password with function in odoo

In this blog we will explain how to change user password with function in odoo 

Example : 

    We created view with button that call wizard enable user to enter new password and click button to change his password with the new one , so this button must call python function that change his password and this function can be just as bellow 


def change_password(self,user_id,new_password):
        user = self.env['res.users'].search([('id','=',user_id)])
        user.sudo().write({'password':new_password})

Generate access token in odoo

In this blog we will explain how to generate access token in odoo for each user step by step , so 

1- create new class which is inherits res.users class

2- create new char field which will contain the token , and it must be compute field

3- create function to generate the token

as bellow 

from odoo import models, fields, api, _
import hashlib
import hmac
import base64
import string
import math, random
from passlib import pwd, hash


class ResUsers(models.Model):
    _inherit = 'res.users'

    access_token = fields.Char(string="Access Token", readonly=True, compute='generate_access_token')

    def generate_access_token(self):
        for rec in self:
            token = ""

            # first way
            # pre_token = rec.name+rec.login
            # token = hash.pbkdf2_sha512.hash(pre_token)

            # second way
            hash_object = hashlib.sha256(rec.password.encode())
            token = hash_object.hexdigest()

            rec.sudo().write({'access_token': token})


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