Web Base Url Freeze odoo


In order to get a static web base url it is necessary to add another system parameter
Key: web.base.url.freeze
Value: True

Advertisements

hide edit and create button for users – odoo


        <!-- hiding create and edit button for users -->
        <record id="fair_hide_view_picking_form" model="ir.ui.view">
        <field name="name">stock.picking.form</field>
        <field name="model">stock.picking</field>
        <field name="inherit_id" ref="stock.view_picking_form"/>
        <field name="groups_id" eval="[(4,ref('fair_construction.group_fair_user'))]"/>
        <field name="arch" type="xml">
            <xpath expr="/form" position="attributes">
                <attribute name="create">false</attribute>
                <attribute name="edit">false</attribute>
            </xpath>
        </field>
        </record> 

pdf Print from python function – odoo


Python File

class AMCPaintBooth(models.Model):
    _name = 'amc.paint.booth'    
    @api.multi
    def action_amc_jobcard_pb_print(self):
        context = {
                'lang': 'en_US', 
                'active_ids': [self.id], 
                'tz': False, 
                'uid': 1
                }
        return {
            'context': context,
            'data': None,
            'type': 'ir.actions.report.xml',
            'report_name': 'safe_amc.safe_report_amc_jobcard_pb',
            'report_type': 'qweb-pdf',
            'report_file': 'safe_amc.safe_report_amc_jobcard_pb',
            'name': 'AMC',
        }

xml File

<header>
   <button name="action_amc_jobcard_pb_print" string="Print AMC Job Card" icon="fa-bars" type="object" class="oe_highlight"/>
</header>

Mysql crashed and won’t start up


  1. Stop mysqld.
  2. Backup /var/lib/mysql/ib*
  3. Add the following line into /etc/my.cnf

innodb_force_recovery = 1 (they suggest 4, but its best to start with 1 and increment if it won’t start)

  1. Restart mysqld.
  2. Dump all tables:# mysqldump -A > dump.sql
  3. Drop all databases which need recovery.
  4. Stop mysqld.
  5. Remove /var/lib/mysql/ib*
  6. Comment out innodb_force_recovery in /etc/my.cnf
  7. Restart mysqld. Look at mysql error log. By default it should be /var/lib/mysql/server/hostname.com.err to see how it creates new ib* files.
  8. Restore databases from the dump:mysql < dump.sql

Get users ids from the group – odoo


@api.model
    def create(self, vals):
        result = super(HrExpenseAdvances,self).create(vals)        
        group_hr_expense_manager_id = self.env['ir.model.data'].xmlid_to_res_id('hr_expense.group_hr_expense_manager')
        hr_expense_managers = self.get_users_from_group(group_hr_expense_manager_id)
        if hr_expense_managers:
            result.message_subscribe_users(user_ids=hr_expense_managers)
        return result
    
    #passing group id using self.env['ir.model.data'].xmlid_to_res_id('hr_expense.group_hr_expense_manager')
    @api.multi
    def get_users_from_group(self,group_id):
        users_ids = []
        sql_query = """select uid from res_groups_users_rel where gid = %s"""                
        params = (group_id,)
        self.env.cr.execute(sql_query, params)
        results = self.env.cr.fetchall()
        for users_id in results:
            users_ids.append(users_id[0])
        return users_ids

How to track a field in messages in Odoo


.python file
For tracking a field we have to specify it in the field definition with parameter track_visibility set to ‘always’ or ‘onchange’ like the following:

from odoo import models,fields,api,_,SUPERUSER_ID

class SaleOrder(models.Models):
    _name = "sale.order"
    _inherit = ['mail.thread', 'ir.needaction_mixin']
    state = fields.Selection([
        ('draft', 'Draft'),
        ('submitted', 'Submitted'),
        ('paid', 'Paid'),
        ],default='draft',string='Status', copy=False, index=True,         readonly=True, store=True,track_visibility='always',

    @api.multi
    def action_submit(self):        
        self.state = 'submitted'
        body =_("Expense Advance Submitted")
        subject = _("Expense Advance")
        self.sudo().message_post(body=body,subject=subject, message_type="notification", subtype="mail.mt_comment", author_id=self.partner.id)

you can use author_id=SUPERUSER_ID

.xml file after sheet you can place this.

 
<div class="oe_chatter">
   <field name="message_follower_ids" widget="mail_followers" groups="base.group_user"/>
   <field name="message_ids" widget="mail_thread"/>
</div>

Create journal entry using code – odoo


   #Posting journal entries
    @api.multi
    def action_done(self):
        for rec in self:
            debit = credit = rec.currency_id.compute(rec.paid_amount, rec.currency_id)           
            if rec.state == 'draft':
                raise UserError(_("Only a Submitted payment can be posted. Trying to post a payment in state %s.") % rec.state)

            sequence_code = 'hr.advance.sequence'
            rec.name = self.env['ir.sequence'].with_context(ir_sequence_date=rec.payment_date).next_by_code(sequence_code)
            
            move = {
                'name': '/',
                'journal_id': rec.journal_id.id,
                'date': rec.payment_date,

                'line_ids': [(0, 0, {
                        'name': rec.name or '/',
                        'debit': debit,
                        'account_id': rec.advance_account.id,
                        'partner_id': rec.employee_id.user_id.partner_id.id,
                    }), (0, 0, {
                        'name': rec.name or '/',
                        'credit': credit,
                        'account_id': rec.journal_id.default_credit_account_id.id,
                        'partner_id': rec.employee_id.user_id.partner_id.id,
                    })]
            }
            move_id = self.env['account.move'].create(move)
            move_id.post()    
            return rec.write({'state': 'paid', 'move_id': move_id.id})

Automatically fill One2many in Odoo


#T&C in each SO
class sale_order(models.Model):
    _inherit = "sale.order"
    
    #By default load the terms and conditions configured in the master T&C configuration
    @api.model
    def _default_so_tc(self):
        terms_obj = self.env['so.terms.config']
        terms = []
        termsids = terms_obj.search([])
        for rec in termsids:
            values = {}
            values['name'] = rec.name
            values['description'] = rec.description
            terms.append((0, 0, values))
        return terms

    terms = fields.One2many('sale.order.terms.conditions','order_id',string='Terms and Conditions',copy=True,default=_default_so_tc)