Friday, July 19, 2019

controllers in odoo


Controllers is a way to make web pages and define unique path "unique URL" for any page
So to create web pages you have to follow these steps 
  • create the controller 
  • create the web page template 


Example :-

Let us say we wanna create login page so we will start with creating controller by create python file with name user_login.py and inside it create the controller as bellow 
    
     from odoo import http

     class user_login(http.Controller):
     @http.route('/user_login_page', type='http', auth='public', website=True)
     def render_login_page(self, **kw):

            user_ids = http.request.env['user.user'].search([])
            check_value = 0
            if kw.get('longin_password') and kw.get('login_email'):
                for rec in user_ids:
                    if rec.passwprd == kw['longin_password'] and rec.email == kw['login_email']:
                        check_value = check_value+1
                        return http.request.render('module_name.login_page_temp', {'docs': rec})
                if check_value == 0:
                    return http.request.render('module_name.login_error_temp', {})

Now let us explain what we did :

 class user_login(http.Controller): // define class user_login as controller class
 @http.route('/user_login_page', type='http', auth='public', website=True)
http.route // to define that we want to make routing 
 /user_login_page // this will be the url of our web page "localhost:8069/web/user_login_page"
 type='http' // to define that our page will run in http 
 auth='public' //  define the page four pubic user 
 website=True // to link the page 
then inside the function we take the email end the password from the web page " if kw.get('longin_password') and kw.get('login_email'): " and compare them with which saved in the database " if rec.passwprd == kw['longin_password'] and rec.email == kw['login_email']:" and return true if it's true or return error page "return http.request.render('module_name.login_error_temp', {})" if it's wrong 

then we will create template with name user_login_page to design the web page as bellow 

<?xml version="1.0" encoding="utf-8"?>
<openerp>
  <data>

      <template id="user_login_page_id" name="User Login Page" page="True">
    <t t-call="website.layout">
      <div class="oe_structure oe_empty">
        <div class="container">
          <center>
            <br></br><br></br><br></br>
            <form class="oe_login_form" role="form" t-attf-action="/user_login_page{{ '?debug' if debug else '' }}" method="post" >
              <div class="form-group form-field o_website_form_required_custom">   
                <label class="col-md-4 col-sm-5 control-label" for="login_email" style="color:#6666ff;">
                                 
                </label>
                <div class="col-md-5 col-sm-6">      
                  <input type="text" class="form-control o_website_form_input" name="login_email" required="1" style="width:250px;text-align:center;border-color:#e67300;" placeholder="Email"/>  
                </div>
                <br></br><br></br>
                <label class="col-md-4 col-sm-5 control-label" for="longin_password" style="color:#6666ff;">
                           
                </label>
                <div class="col-md-5 col-sm-6">      
                  <input type="longin_password" class="form-control o_website_form_input" name="longin_password" required="1" style="width:250px;text-align:center;border-color:#e67300;" placeholder="password"/>  
                </div>
              </div>
              <br></br><br></br> 
              <div class="form-group">
                <div class="col-md-offset-3 col-sm-offset-4 col-sm-8 col-md-7">
                  <input type="hidden" name="redirect" t-att-value="redirect"/>
                  <button type="submit" class="btn" style="background-color:#e67300;">Login</button>
                </div>
              </div>
            </form>
          </center>
        </div>
      </div>
    </t>
  </template>

</data>
</openerp> 

The last important thing that we have to create the login_error_temp page which will called if there is an error with the entered login data with error message in the same way of creating the user_login_temp

No comments:

Post a Comment

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