Friday, November 27, 2020

many2one field domain from function

 ODOO

 odoo 14


 

in this blog we will explain how we can define domain for Many2one field in odoo 14 , so let us start 

 

First we have "object.a" model which is contain Many2many in which we will add many users , as bellow

user_ids = fields.Many2many('res.users', string="Users")

Second we have "object.b" model , which we have user_id field that contain the current user , and object_a_id which is Many2one field from object "A" , as bellow

user_id = fields.Many2one('res.users', string='Created By', default=lambda self: self.env.user, readonly=True)

object_a_id = fields.Many2one('object.a', string="A")

and we need to make domain over object_a_id field to show only the records from object A that the current user is involved in the user_ids field 

....

To make the domain we have to add function as bellow  

    @api.onchange('some_field_in_B_object')
    def _get_object_b_ids(self):
        """
        function to get the A records for the current user
        """
        object_a_ids = self.env['object.a'].search([])
        ids_lis = []
        domain = [('id','=', -1)]

        for rec in object_a_ids:
            for line in rec.user_ids:
                if self.user_id == line:
                    ids_lis.append(rec.id)
        res = {}
        res['domain'] = {'object_a_id': [('id', 'in', ids_lis)]}
        return res



 

Wednesday, November 11, 2020

create new branch on github

GitHub

 

In the blog we will explain how we can create new branch on github from terminal , so follow the steps bellow 

 

1/ create new folder with name local_branch 

2/ open the terminal with it's path and write the comands bellow one by one

 

git init // to initialize local repo 

git add . // to add all folders in this folder to your local repo

git checkout -b local_branch // to make checkout 

git commit -m "any text message you want" // to commit your changes

git remote add origin remote_repo_path on github // to add your local branch to remote branch 

git push origin local_branch //  to push your files to the remote branch

Monday, November 2, 2020

random function in odoo 14

in this blog we will explain example to generate random number of 4 digits in odoo so let us start 

 

- First we have to import choice class from random package as bellow

from random import choice

- Then we have to import digits class from string package as bellow 

from string import digits

- Last we can use the function bellow that will return the random number

def _default_random_pin(self):
        return ("".join(choice(digits) for i in range(4)))

 

Wednesday, October 21, 2020

How to use many2one field in demo data

Odoo

Demo Data

 

 

 In this blog we will explain how to add value for many2one field in the xml demo data file in odoo , so let us start 

First let us say we have python object by the name 'report.create' which is contain the model_id field which is many2one field from 'ir.model' as bellow 

class ReportCreater(models.Model):
    _name = 'report.creater'

    model_id = fields.Many2one(
        'ir.model', string='Model', required=True, ondelete="cascade",
        help="The type of document this template can be used with")

and we want to create demo data for this object and the records from the demo data must contain the model_id field , so we can do it as bellow 

<odoo>
  <data noupdate="0">
    <record id="simple_contract_template" model="desined.report.template">
      <field model="ir.model" name="model_id" search="[('model', '=', 'res.partner')]"/>
    </record>
  </data>
</odoo>

 

*NOTES

  • 'ir.model' this object contain all models in the data base
  • search="[('model', '=', 'res.partner')] in this step we made search in the 'ir.model' object to return record from it , and in that record the value of model field must be 'res.partner'
  • 'ir.model' contain the model field which is char field

Monday, October 19, 2020

defined as ondelete='restrict'

I face the error bellow we I tried to install some module in odoo 14 , the error is 

ValueError: Field mark_id of model rep.template is defined as ondelete='restrict 

 

of course ! , I have model by name 'rep.template', in which that I have many2one file defined as bellow 

mark_id = file.Many2one('rep.mark', string='Mark')

and I face the error above because of this field , so to fix this error I rewrite this field as bellow

mark_id = file.Many2one('rep.mark', string='Mark', ondelete="cascade")



github from terminal

GitHub

 

 

 On this topic we will explain how to deal with github from terminal 

First : if you don't have account on github , you can create it from here , then follow me :)

 

Login to git hub from terminal by the command 

git config --global user.email "your_email_address@example.com" 

 

Initialized empty git repository 

git init


Remove the initialized empty repository 

rm -rf .git


Show repository activity log

git log


Clone files from the repository in the current folder 

git clone repo_url . // the . means that we want to clone files in the current directory

 

View the information about the repository 

git remote -v 


List all branches in the repository 

git branch -a


Show the changes that we had made to the code 

git diff

 

Add all working files to the staging

git add -A


Commit the add files in the local device 

git commit - m "Some Message :)"


Push the committed changes locally to the remote repository at git.com (2 steps < pull and push >)

git pull origin branch_name // to check if any other developer make any changes in the repository (origin is the name of the remote repository)

git push origin branch_name // to push our changes to the repository (branch)

 

Create new branch 

 git branch branch_name  // this will create new branch in the current repository 

 

Switch the current working with branch 

git checkout branch_name 


Push changes to specific specific branch in the remote repository

git push -u origin branch_name // #not : we can to pull by this way too

 

Merge a  branch with the current branch 

git branch --merged

git merge branch_name


Delete Branch

git branch -d branch_name // this will delete the branch from the local 

git push origin --delete branch_name // this will push the deleted branches to the remote repository 



Note :

you can download the ref form here




Sunday, October 18, 2020

nginx with odoo 14

Odoo

Nginx 


 


I this topic we will explain how to configure nginx for localhost odoo server by steps, but before that you need to install nginx and running odoo server and then follow the steps 

First : go to /etc/nginx/enable-sites and add new configuration file by the name odoo14.localhost which will configure your nginx for odoo local server

Second : open the file /etc/hosts and add the localhost (127.0.0.1) to the file as bellow 

Third : add the ssl certification by going to etc/ssl and create new folder by name nginx and inside it run the command bellow 

openssl req -x509 -new -newkey rsa:2048 -nodes -keyout odoo.key -days 9999 -out odoo.crt

###

 

odoo14.localhost file will be as

#odoo server
  upstream odoo {
   server 127.0.0.1:8069;
  }
  upstream odoochat {
   server 127.0.0.1:8072;
  }
  # http -> https
  server {
     listen 80;
     #server_name odoo.mycompany.com;
     rewrite ^(.*) https://$host$1 permanent;
    }
  server {
   listen 443;
   #server_name odoo.mycompany.com;
   proxy_read_timeout 720s;
   proxy_connect_timeout 720s;
   proxy_send_timeout 720s;
   # Add Headers for odoo proxy mode
   proxy_set_header X-Forwarded-Host $host;
   proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
   proxy_set_header X-Forwarded-Proto $scheme;
   proxy_set_header X-Real-IP $remote_addr;
# SSL parameters
   ssl on;
   ssl_certificate /etc/ssl/nginx/odoo.crt;
   ssl_certificate_key /etc/ssl/nginx/odoo.key;
   ssl_session_timeout 30m;
   ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
   ssl_ciphers 'ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-DSS-AES128-GCM-SHA256:kEDH+AESGCM:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA:ECDHE-ECDSA-AES256-SHA:DHE-RSA-AES128-SHA256:DHE-RSA-AES128-SHA:DHE-DSS-AES128-SHA256:DHE-RSA-AES256-SHA256:DHE-DSS-AES256-SHA:DHE-RSA-AES256-SHA:AES128-GCM-SHA256:AES256-GCM-SHA384:AES128-SHA256:AES256-SHA256:AES128-SHA:AES256-SHA:AES:CAMELLIA:DES-CBC3-SHA:!aNULL:!eNULL:!EXPORT:!DES:!RC4:!MD5:!PSK:!aECDH:!EDH-DSS-DES-CBC3-SHA:!EDH-RSA-DES-CBC3-SHA:!KRB5-DES-CBC3-SHA';
   ssl_prefer_server_ciphers on;
  # log
   access_log /var/log/nginx/odoo.access.log;
   error_log /var/log/nginx/odoo.error.log;
   # Redirect longpoll requests to odoo longpolling port
   location /longpolling {
   proxy_pass http://odoochat;
   }
   # Redirect requests to odoo backend server
   location / {
     proxy_redirect off;
     proxy_pass http://127.0.0.1:8069;
   # common gzip
   gzip_types text/css text/less text/plain text/xml application/xml application/json application/javascript ;
}
}

 

The hosts file will be as

127.0.0.1    localhost
127.0.1.1    ahmed-HP-EliteBook-8460p
172.0.0.1       localhost.odoo

# The following lines are desirable for IPv6 capable hosts
::1     ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters

 

The odoo configuration file will be as 

[options]
; This is the password that allows database operations:
admin_passwd = Ahmmar
db_host = False
db_port = 5432
db_name = False
db_user = ahmed
xmlrpc_port = 8069
longpolling_port = 8072
db_password = False
addons_path = /home/ahmed/Desktop/Odoo_14/addons, /home/ahmed/Desktop/Odoo_14/addons/custom_addons
proxy_mode = True
dbfilter = ^%h$
logfile = /var/log/odoo14/odoo14-service.log


###

Here are some important commands

nginx -t >> to test if the nginx conf files are okay

systemctl restart nginx.service >> to restart nginx 

sudo su -- >> to login as root user


*NOTE

after doing the above configuration you have to restart the odoo server and go to your browser and open https://localhost which will open the creation page for new database

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