Thursday, October 19, 2023

Remove xml entities from html field value in odoo

In this blog we will explain how to remove xml entities from html field value in odoo and to make it clear let us give an example 

We created new task in the project module , and this task has description as in the image bellow 

 

and we created custom task portal view , but when we try to view the task description it displayed with some tags and we need to remove them .. just as in the image bellow
 

so to fix this bug we can add new function inside the controller and this function take the description field value as an input and return the clean text of the description without tags , so the function can be as bellow 

def clean_text(self,html_field_value):
        clean = re.compile(r'<[^>]+>')
        clean_text =  re.sub(clean, '', html_field_value)
        return clean_text

#Note : we must import re package first as "import re"

website form in odoo

In this blog we will explain how to create website form in odoo website to create new record in internal odoo model , so let us say that we want to create website form to in order to create new products with the major fields {name, price,cost, type,image} from website , so ..

First :  create new template in which we will create the form with all inputs as bellow

<?xml version="1.0" encoding="UTF-8"?>
<odoo>
    <template id="new_products" name="New Products">
        <t t-call="website.layout">
            <form action='/create/product/form'>
                <input type="hidden" name="csrf_token" t-att-value="request.csrf_token()"/>
                <div class="row">
                    <div class="col-sm-4 col-md-3">
                        <table style="width:100%">
                            <tr style="width:100%">
                                <td style="width:20%">
                                    <b>
                                        Name
                                    </b>
                                </td>
                                <td style="width:80%">
                                    <input type="text" name="product_name" id="product_name" required="true" />
                                </td>
                            </tr>
                        </table>
                    </div>
                    <div class="col-sm-4 col-md-3">
                        <table style="width:100%">
                            <tr style="width:100%">
                                <td style="width:20%">
                                    <b>
                                        Price
                                    </b>
                                </td>
                                <td style="width:80%">
                                    <input type="number" name="product_price" id="product_price" required="true"/>
                                </td>
                            </tr>
                        </table>
                    </div>
                    <div class="col-sm-4 col-md-3">
                        <table style="width:100%">
                            <tr style="width:100%">
                                <td style="width:20%">
                                    <b>
                                        Cost
                                    </b>
                                </td>
                                <td style="width:80%">
                                    <input type="number" name="product_cost" id="product_cost" required="true"/>
                                </td>
                            </tr>
                        </table>
                    </div>
                    <div class="col-sm-4 col-md-3">
                        <table style="width:100%">
                            <tr style="width:100%">
                                <td style="width:20%">
                                    <b>
                                        Type
                                    </b>
                                </td>
                                <td style="width:80%">
                                    <select id="product_type" required="True" class="form-control" name="product_type">
                                        <option t-att-value="consu" class="type_option">
                                            Consumable
                                        </option>
                                        <option t-att-value="service" class="type_option">
                                            Service
                                        </option> 
                                        <option t-att-value="product" class="type_option">
                                            Storable Product
                                        </option>      
                                    </select>
                                </td>
                            </tr>
                        </table>
                    </div>
                    <div class="col-sm-4 col-md-3">
                        <table style="width:100%">
                            <tr style="width:100%">
                                <td style="width:20%">
                                    <b>
                                        Image
                                    </b>
                                </td>
                                <td style="width:80%">
                                    <input type="file" class="form-control s_website_form_input" name="image_file" id="image_file" required="true" accept="image/*"/>
                                </td>
                            </tr>
                        </table>
                    </div>
                </div>
                <br></br>
                <div class="control_buttons">
                    <button type='submit' class="submit_bnt" id="s_bnt">
                        Save
                    </button>
                </div>
            </form>
    </template>
</odoo>


Second : create python controller in which you must "import base64" & "from odoo.exceptions import AccessError, MissingError", then add function to create the product with the data posted from the form as bellow 

    @http.route(['/create/product/form/'], type='http', methods=['GET', 'POST'], auth="public", website=True)
    def create_new_product(self, **post):
        product_obj = request.env['product.template'].sudo()
        try:
            product_data = {
                'name':post['product_name'],
                'list_price':float(post['product_price']),
                'standard_price':float(post['product_cost']),
                'type':post['product_type'],
                'datas':base64.b64encode(post['image_file'].read()),
            }
            product_id = product_obj.create(product_data)
            return request.redirect(product_has_been_created_page)
        except (AccessError, MissingError):
            return request.redirect('/my')


#Note : You have to create product_has_been_created_page first

Tuesday, October 17, 2023

odoo mail configuration

In this blog we will explain hot to setup odoo mail configuration

Create & use app passwords

Important: To create an app password, you need 2-Step Verification on your Google Account.

If you use 2-Step-Verification and get a "password incorrect" error when you sign in, you can try to use an app password.

  1. Go to your Google Account.
  2. Select Security.
  3. Under "Signing in to Google," select 2-Step Verification.
  4. At the bottom of the page, select App passwords.
  5. Enter a name that helps you remember where you’ll use the app password.
  6. Select Generate.
  7. To enter the app password, follow the instructions on your screen. The app password is the 16-character code that generates on your device.
  8. Select Done.

Thursday, October 12, 2023

Auto Images Slider in Odoo

 

In this blog we will explain how to create auto image slider in odoo website as in the image above , so

First : we have to add the images to the website page , for example let us say we have an odoo controller which call page and pass data to this page , and that data contains "image_ids" which is contains all image data , so we can add the images to the page as bellow 

 

<t t-foreach="image_ids" t-as="image">
    <center>
        <div class="mySlides">
            <img t-att-src="image['image_url']" class="img_01"/>
            <br></br>
            <span t-esc="image['image_name']" class="name_01"/>
        </div>
        <span class="dot"></span>
    </center>
</t>


Second : we have to create the JS script which will add the auto move to the slider and we can add it as bellow

 

<script>
    let slideIndex = 0;
    showSlides();

    function showSlides() {
      let i;
      let slides = document.getElementsByClassName("mySlides");
      let imgs = document.getElementsByClassName("img_01");
      let img_urls = document.getElementsByClassName("image_url");
      let dots = document.getElementsByClassName("dot");
      for (i = 0; i &lt; slides.length; i++) {
        slides[i].style.display = "none";
        slides[i].style.margin = "10px";
      }
      for (i = 0; i &lt; imgs.length; i++) {
        imgs[i].style.width = "80%";
        imgs[i].style.height = "470px";
        imgs[i].style.border = "2px solid black";
        imgs[i].style.borderColor = "#875A7B";
        imgs[i].style.borderRadius = "10px";
        imgs[i].style.boxShadow = "3px 5px #bfbfbf";
        imgs[i].style.marginBottom = "10px";
      }
      slideIndex++;
      if (slideIndex > slides.length) {slideIndex = 1}    
      for (i = 0; i &lt; dots.length; i++) {
        dots[i].className = dots[i].className.replace(" active", "");
      }
      slides[slideIndex-1].style.display = "block";  
      dots[slideIndex-1].className += " active";
      setTimeout(showSlides, 5000); // Change image every 5 seconds
    }
</script>


Third : adding the CSS style as bellow 

 

.img_01{
    width:80%;
    height:420px;
    transition: .1s ease;
  backface-visibility: hidden;
}
.img_01:hover {
    opacity: 0.3;
}

odoo module icons

In this blog we will explain how we can create icon for custom module as odoo standard module , so

First : we have to go to "Icon builder for odoo" website by open this link 


Second : click the internet icon to open the second website which is provides many icons , we choose an icon, then copy it's name and back to the first website 


Third : we put the copied icon class name in the "Icon Class" field, then choose the color and the other style setting for the icon , lastly click on download button to download the icon as png image




website images preview in odoo

 

In This blog we will explain how to create website images preview in odoo as popup zoom out view just as in the image above , so

First : we have to create the the images view for example 

<t t-foreach="image_ids" t-as="image">
    <img t-att-src="image.attachment_url" class="asset_img_01" alt="Asset"/>
    <div id="myModal" class="modal">
        <span class="close">
            <i class="fa fa-window-close" style="background-color:#ffffff;color:#ff4000;"/>
        </span>
        <img class="modal-content" id="img01"/>
        <div id="caption"></div>
    </div>
</t>

Second : add JS script to add the popup preview for each image which is work when we click on the image , as bellow 

<script>
    var modal = document.getElementById("myModal");

    var imgs = document.getElementsByClassName("asset_img_01");
    var modalImg = document.getElementById("img01");
    var captionText = document.getElementById("caption");
    for (i = 0; i &lt; imgs.length; i++) {
        imgs[i].onclick = function(){
            modal.style.display = "block";
            modalImg.src = this.src;
            captionText.innerHTML = this.alt;
        }
    }

    var span = document.getElementsByClassName("close")[0];

    span.onclick = function() {
      modal.style.display = "none";
    }
</script>


# note : the script must be inside the for loop body after the "myModal div"

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