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})


Create url for pdf file in odoo

 

In this blog we will explain how to create url for pdf file in odoo


for an example : we want to generate url for the pdf invoice , so we can create function that takes the invoice id and return the url for the pdf invoice as bellow 

 

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

    def generate_pdf_invoice_url(self , invoice_id):

        # pdf = request.env.ref('module_name.report_action_id').with_user(SUPERUSER_ID)._render_qweb_pdf([invoice_id.id])
        pdf = request.env.ref('account.account_invoices').with_user(SUPERUSER_ID)._render_qweb_pdf([invoice_id.id])

        pdf_invoice = request.env['ir.attachment'].sudo().create({
            'name': invoice_id.name,
            'type': 'binary',
            'datas': base64.b64encode(pdf[0]),
            'store_fname': invoice_id.name,
            'res_model': 'account.payment',
            'res_id': invoice_id.id,
            'mimetype': 'application/x-pdf'
        })
        pdf_file_url + 'http://'+request.httprequest.__dict__['environ']['HTTP_HOST']+'/web/content/%s' % (pdf_invoice.id)
        
        return pdf_file_url
           

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