25/02/24

Odoo: Facciamo chiarezza sulle XPath - Parte 1

Benvenuti!

Questo articolo contiene la prima parte di una serie di puntate dove voglio provare a fare chiarezza su un aspetto fondamentale per lo sviluppo delle UI di Odoo.
Il contenuto è rivolto ad un pubblico tecnico che possiede un background per sviluppare con il framework di Odoo.
Potete trovare il codice usato in questo tutorial su questo repository.

XPath: XML Path Language

XPath, acronimo di XML Path Language, è un linguaggio utilizzato per navigare e selezionare elementi in documenti XML.
Fornisce un modo per individuare e accedere a specifici nodi o elementi all'interno di un documento XML (DOM).
Utilizza una sintassi a Path (come nel filesystem) per indicare la posizione di un elemento nell'albero gerarchico di un documento XML.
Ad esempio, un'espressione XPath potrebbe essere "//book/title", che selezionerebbe tutti gli elementi "title" che sono discendenti diretti di un elemento "book" in un documento XML.
Per chi volesse approfondire l'argomento su questo linguaggio potete trovare materiale ai seguenti link:

Odoo e XPath: Un cocktail da brivido!

Le interfacce web (o view) di Odoo fanno uso di una sintassi, attravero il motore di template QWeb,
basata sul linguaggio XML per la rappresentazione nei vari formati (Tree, Form, Kanban, etc...).
In generale, il meccanismo e la struttura di Odoo non permette di modificare direttamente il codice sorgente della singola view di tipo QWeb.
Piuttosto, il principio di modifica delle view esistenti (quelle di sistema o di addons esterni)
si basa sul meccanismo di ereditarietà: in sostanza, viene costruita una nuova vista VE che eredità dall'originale V che altera (aggiunge o modifica) il contenuto dalla vista genitore.

Domanda: quale sintassi utilizzare per modificare il contenuto della vista genitore ?
Risposta: La risposta è XPath!

Nella documentazione ufficiale di Odoo (link) si fa riferimento a quello che ho descritto sopra. Attraverso degli esempi pratici andremmo ad approfondire meglio come si usano le XPath.

XPath Tutorial: Da zero a eroe delle XPath.

Per facilitare l'apprendimento delle XPath ho preparato un modulo (o addons) xpath_tutorial che trovate sul mio spazio GitHub.
Per semplicità, definisce solo un Controller su cui viene fatto il render di un template index definito dentro il file templates.xml.
Completata l'installazione del modulo, il risultato è che potete accedere all'indirizzo

http://localhost:8069/xpath_tutorial

e vederne il risultato.

        
        <template id="index" name="xpath_index">
          <t t-call="web.layout">
            <h1>Hello, Xpath!</h1>
          </t>
        </template>
        
        
Adesso, passo dopo passo vedremmo come riuscire a essere confindenti con le XPath

Esempio 1: Aggiugiamo un poco di colore al nostro template

L'obiettivo di questo esempio è modificare il colore dell'intestazione h1 con il colore rosso senza modificare il codice della vista originale.
Per fare questo andremmo a definire un nuovo template index_inherit che eredita da quello di origine index.
Il contenuto del template inizia con il tag <xpath> che ci permette di modificare il contenuto del template da cui ereditiamo.
Troviamo due attributi fondamentali:

  • expr: contiene l'espressione XPath (o selettore) del tag (o nodo) che vogliamo intercettare e modificare
  • position: contiene la "posizione" su cui vogliamo intervenire, nel caso dell'esempio usiamo il valore attributes per intervenire sugli attributi del tag intercettato.

Di seguito trovate l'esempio che riporta la modifica dell'attributo style del nodo <h1> che si trova dentro la vista ereditata.

        <template id="index_inherit" inherit_id="index" name="inherith">
        <xpath expr="//h1" position="attributes">
          <!-- Importante è il nome dell'attributo -->
          <attribute name="style" add="color:red" />
        </xpath>
      </template>
    

Il risultato finale sarà che l'intestazione si colora di rosso. Potete provare da soli a cambiare l'intesazione di un altro colore oppure rimuovere la riga <attribute> così da vederne l'effettivo risultato.

Nella seconda parte andremmo ad approfondire i valori possibili dell'attributo position utilizzato dentro il tag XPath.

05/01/23

Happy new 2023 and work!

Happy new year! And happy new work to me!

I'm very pleasure to announce that this year I got a new job within a startup, Pixora, where I will continue to invest to my capabilities with the role of  "Senior Project Development Manager".

If you want to get other information on this company, go to main website https://www.pixora.it/


So, let's begin with this new adventure...stay tuned!


29/06/21

Log raw mail content to file

Hi!

After a long time, I return to write on my personal blog with a personal challenge. I've a Linux box with some scheduled tasks using cron service. I want to send mail to my personal mailbox in case of error of a single task (preferably with error log). It's not hard to configure the environment because just use a simple MTA (personally prefer msmtp) to send mails via command line. But it's hard to set up the values of From and To fields because cron use the default of execution user. 
So, the first question was: How to write the raw content of mail to disk ? 
The solution is to write a simple bash script that replace sendmail and write content of the mail to disk.

  #!/bin/bash
  cat - >> /tmp/$(date +%s).txt
  

Then, create a symbolic link of the script with sendmail name, and it's ready to write raw content to tmp path. The next step is to change the values of From and To fields....easy. With

sed "s/From: root\.*/From: [email protected] /" | sed "s/To: root/To: [email protected]/" | /usr/bin/msmtp $*
and it's done! Now, my mailbox has a set of mail sent from my linux box.

See youu.....

04/03/21

nmap Quick Reference

Basic usage

Enum SSL Chipers on specific port (RDP 3389)
nmap -sV --script ssl-enum-ciphers -p 3389 192.168.x.x
Simple ping hosts
nmap -sn 192.168.x.x

20/05/20

Docker Quick Reference

This post is for me a place where to store notes about Docker. I hope that this can be helpful for you.

Basic usage

How do you disable auto-restart on a container? (Ref.)
docker update --restart=no my-container
How can we see all possible variables for a format?
docker container ls -a --format '{{json .}}'
How to analyze disk usage of a Docker container ? (Ref.)
docker ps --size
or
docker system df --verbose
How to show container bind ports ?
docker port container_name

Images

How to export all images ?
eval $(docker image ls --format 'docker save {{.ID}} -o "{{.ID }}.tar.gz";')

25/06/19

Vim Quick Reference

Basically, vim has three working modes:

  1. Command mode
  2. Insert Mode
  3. Ex-mode

Basic VIM Movement

  • b move back from word
  • w move next to word

Commands

Disable text wrap

:set wrap!
(you can use ~/.vimrc)

Display or hide line numbers?

:set number
for display
:set nonumber
for hide

29/04/19

VMWARE - Get snapshots and size of VMs


Today I need to check the snapshots of all VMs of a VMware infrstructure. Unfortunately I have found that with vSphere Client and vSphere Web Client it's not possibile to get a list of snaps associated to VMs.

So, after a advanced google search, I have found that this is possibile using VMware PowerCLI tools.
I've installed the version 5.5 available in this page https://code.vmware.com/web/tool/5.5/vmware-powercli

After this, I use powershell for loading the snapin and connect to my vcenter server with this:

add-pssnapin vmware.vimautomation.core
connect-viserver myvcentersrv_FQDN


Then with the commmand I get the list with VM name, number of snapshot and size:

Get-VM | Format-Table Name, @{Label="NumSnapshots";Expression={(Get-Snapshot -VM $_ | Measure-Object).Count}}, @{Label="TotalSnapShotSizeMB";Expression={(Get-Snapshot -VM $_ | Measure-Object -Sum SizeMB).Sum}}

Ref: http://www.vhersey.com/2016/03/01/quick-powercli-to-get-snapshots-and-size/

04/12/18

Pip Quick Reference

Basic pip usage

Upgrade PIP
python -m pip install --upgrade pip
Find outdated/updatable pip packages
pip list --outdated
Getting info on your python environment
python -m site

08/09/18

Git Hooks - A way to automate deploy tasks

During my work I use git for automate the deploy on production system. Sometimes I need execute some command or script immediately after checkout. Git has a very powerful mechanism called hooks (you can read more about it on official documentation follow this link).
On client side to install hooks what you have to do is to create a dir called hooks under .git/ . But  you do not have to do any of this because when you have initialized your project git create it for you.
If you list the content of hooks dir you can found a list of samples script to use (all of them are bash scripts). Most of this script start with the prefix  pre- or post- followed by action name.
The script that I found helpful is post-checkout that is automatically executed after a successful git checkout. My post-checkout script contain commands for adjust permissions on files and ask to restart web server.

#!/bin/bash
git ls-files -z --with-tree="$2" --directory | xargs -0 chmod o-rxw --
git ls-files -z --with-tree="$2" --directory | xargs -0 chown www-data:www-data --
find . -type d | xargs -I {} chown www-data:www-data {}
find . -type d | xargs -I {} chmod o-rwx {}
exec < /dev/tty

while true;
   do
      read -p "Do you want to restart apache?  [Y/N]:" choice
      case "$choice" in
         y|Y) service apache2 restart;break;;
         n|N) break;;
         *) echo "invalid" && break;;
      esac
   done

Remember to add execute permission to this script or git won't execute it after the checkout.

16/03/18

Git Handbook

Git Setup

How to setup git with SSH Key

git remote set-url origin [email protected]:<Username>/<Project>.git

Configure username and mail

git config --global user.name "Name Surname"
git config --global user.email "mymail(at)mydomain.com"

Filename too long in git for windows

git config --system core.longpaths true

Note: You need to run as administrator.
Ref: https://stackoverflow.com/questions/22575662/filename-too-long-in-git-for-windows

Windows CRLF chars.

Su windows bisogna disattivare questa funziona altrimenti non funzionano i caratteri CRLF.
git config core.autocrlf false

How can I debug git/git-shell related problems?

If you have problem with Git and you need to debug you can run this:

GIT_CURL_VERBOSE=1 GIT_TRACE=1 git pull
Ref: https://stackoverflow.com/questions/6178401/how-can-i-debug-git-git-shell-related-problems

How to get list of files between change log ?

If you need to get list of files (included directory tree) from change list you can use:

git diff-tree -r --name-only 2c636a^ 5b47402 | xargs -I {} rsync -aR {} output/

Delete files from git index when they are already deleted

git ls-files --deleted -z | xargs -0 git rm

How do I show my global git config?

git config --list

Show remote branches

git branch -vr

Git Commands

How to commit only staged modified and deleted files?

git add -u

List log with tag date


git log --date-order --graph --tags --simplify-by-decoration --pretty=format:'%ai %h %d'