Author: ananthukrishna

Replace JS file in Odoo


<odoo>
        <template id="assets_inherit" inherit_id="website_slides.assets_frontend">
            <xpath expr="//script[@src='/website_slides/static/src/js/slides_upload.js']" position="replace">
                <script type="text/javascript" src="/website_slides_extend/static/src/js/slides_upload_inherit.js"></script>
            </xpath>
        </template>
</odoo>
Advertisements

open binary data (pdf) using controller odoo


URL :- http://192.168.56.11:8069/web/binary/download_pdf/12

import base64
import logging
import werkzeug

from odoo import http, _
from odoo.exceptions import AccessError, UserError
from odoo.http import request


class DownloadPDF(http.Controller):

    @http.route('/web/binary/download_pdf/<id>', type='http', auth="public",website=True)
    def download_pdf(self,id,**kw):        
        response = werkzeug.wrappers.Response()
        slide_slide_obj = request.env['slide.slide'].sudo().search([('id','=',id)])
        response.data = slide_slide_obj.datas and slide_slide_obj.datas.decode('base64') or ''
        response.mimetype = 'application/pdf'
        return response

show filename of binary files odoo


Pyhton Code

class MyModel(models.Model):
    _name = 'my.model'

    image = fields.Binary('Image', required=True)
    image_filename = fields.Char("Image Filename")

XML File

<record id="view_form_my_model" model="ir.ui.view">
  <field name="name">My Model Form</field>
  <field name="model">my.model</field>
  <field name="arch" type="xml">
    <form>
      <sheet>
        <group name="group_top">
          <group name="group_left">
            <field name="image_filename" invisible="0"/>
            <field widget="binary" height="64" name="image" filename="image_filename" />
          </group>
          <group name="group_right">
          </group>
        </group>
      </sheet>
    </form>
  </field>
</record>

Create sequence using code – odoo


@api.model
def _create_sequence(self, vals, refund=False):
""" Create new no_gap entry sequence for every new Journal"""
prefix = self._get_sequence_prefix(vals['code'], refund)
seq = {
'name': refund and vals['name'] + _(': Refund') or vals['name'],
'implementation': 'no_gap',
'prefix': prefix,
'padding': 4,
'number_increment': 1,
'use_date_range': True,
}
if 'company_id' in vals:
seq['company_id'] = vals['company_id']
return self.env['ir.sequence'].create(seq)

controller authentication cross site – Odoo


import requests
from requests import Request,Session
import json

b_url = "http://yourodooserver.com"
# or "http://201.100.100.12:8069" (or whatever the ip is)
url = "{}/web/session/authenticate".format(b_url)

db = "<YOURDBNAME>"
user = "<YOURUSERNAME>"
passwd = "<YOURPASSWORD>"

s = Session()

data = {
    'jsonrpc':'2.0',
    'params': {
        'context': {},
        'db': db,
        'login': user,
        'password': passwd,
    },
}

headers = {
    'Content-type': 'application/json'
}

req = Request('POST',url,data=json.dumps(data),headers=headers)

prepped = req.prepare()

resp = s.send(prepped)

session_id = json.loads(resp.text)['result']['session_id']

# NOW MAKE REQUESTS AND PASS YOUR SESSION ID

res = requests.get(b_url + "/your/controller/path",cookies={'session_id':str(session_id)})

print(res.text)

redirect to view using python – odoo


@api.multi
    def redirect_opportunity_view(self):
        self.ensure_one()
        # Get opportunity views
        form_view = self.env.ref('crm.crm_case_form_view_oppor')
        tree_view = self.env.ref('crm.crm_case_tree_view_oppor')
        return {
            'name': _('Opportunity'),
            'view_type': 'form',
            'view_mode': 'tree, form',
            'res_model': 'crm.lead',
            'domain': [('type', '=', 'opportunity')],
            'res_id': self.id,
            'view_id': False,
            'views': [
                (form_view.id, 'form'),
                (tree_view.id, 'tree'),
                (False, 'kanban'),
                (False, 'calendar'),
                (False, 'graph')
            ],
            'type': 'ir.actions.act_window',
            'context': {'default_type': 'opportunity'}
        }