Showing posts with label JavaScript for odoo v8. Show all posts
Showing posts with label JavaScript for odoo v8. Show all posts

Wednesday, 22 March 2017

How many widget we can use in odoo v8?

Here are the latest widgets and its following classes usable in odoo js to include or extend also use syntax widget="any_widget_name" with odoo views,
    'char' : 'instance.web.form.FieldChar', 
    'id' : 'instance.web.form.FieldID',
    'email' : 'instance.web.form.FieldEmail',
    'url' : 'instance.web.form.FieldUrl',
    'text' : 'instance.web.form.FieldText',
    'html' : 'instance.web.form.FieldTextHtml',
    'char_domain': 'instance.web.form.FieldCharDomain',
    'date' : 'instance.web.form.FieldDate',
    'datetime' : 'instance.web.form.FieldDatetime', 
    'selection' : 'instance.web.form.FieldSelection', 
    'radio' : 'instance.web.form.FieldRadio',
    'many2one' : 'instance.web.form.FieldMany2One',
    'many2onebutton' : 'instance.web.form.Many2OneButton',
    'many2many' : 'instance.web.form.FieldMany2Many',
    'many2many_tags' : 'instance.web.form.FieldMany2ManyTags',
    'many2many_kanban' : 'instance.web.form.FieldMany2ManyKanban',
    'one2many' : 'instance.web.form.FieldOne2Many',
    'one2many_list' : 'instance.web.form.FieldOne2Many',
    'reference' : 'instance.web.form.FieldReference',
    'boolean' : 'instance.web.form.FieldBoolean',
    'float' : 'instance.web.form.FieldFloat',
    'percentpie': 'instance.web.form.FieldPercentPie',
    'barchart': 'instance.web.form.FieldBarChart',
    'integer': 'instance.web.form.FieldFloat',
    'float_time': 'instance.web.form.FieldFloat',
    'progressbar': 'instance.web.form.FieldProgressBar',
    'image': 'instance.web.form.FieldBinaryImage',
    'binary': 'instance.web.form.FieldBinaryFile',
    'many2many_binary': 'instance.web.form.FieldMany2ManyBinaryMultiFiles',
    'statusbar': 'instance.web.form.FieldStatus',
    'monetary': 'instance.web.form.FieldMonetary',
    'many2many_checkboxes': 'instance.web.form.FieldMany2ManyCheckBoxes',
    'x2many_counter': 'instance.web.form.X2ManyCounter',
    'priority':'instance.web.form.Priority',
    'kanban_state_selection':'instance.web.form.KanbanSelection',
    'statinfo': 'instance.web.form.StatInfo',

Wednesday, 15 March 2017

ValueError Expected singleton in Odoo

Expected Singleton:

Class methods required single invoking object (Single Browsable Record) to invoke the method and suppose it will call by multiple invoking objects (Browsable Recordsets) then method is not able to identify for which object it should process, therefore it will raise an error Expected Singleton.

New API decorator is used to define method calling pattern whether methods allows only single object or multiple objects to invoke this method.


For Example:


if self.location_id:     #face ValueError Expected singleton because self contains multiple recordset.

    ########

Need to change with following:

for warehouse in self:

    if warehouse.location_id:

        ########


@api.one

This decorator loops automatically on Records of RecordSet for you. Self is redefined as current record

Note:

Caution: the returned value is put in a list. This is not always supported by the web client, e.g. on button action
methods. In that case, you should use @api.multi to decorate your method, and probably call self.ensure_one() in
the method definition.

@api.multi

Self will be the current RecordSet without iteration. It is the default behavior (multiple browsable objects). Methods which returns non premitive type data(list, dictionary, function) must be decorated with @api.multi

@api.model

This decorator will convert old API calls to decorated function to new API signature. It allows to be polite when
migrating code. Self does not contain any record/recordset in methods which are decorated by this decorator.

So simply call like this

self.env['model_name'].method_name(arguments)

Friday, 10 March 2017

How to Add Search View Filters In Odoo ?

Filters for Datetime field

Today Filter

<filter icon="terp-go-month" string="Today " separator="1" domain="[('date','&lt;=', (datetime.date.today()).strftime('%Y-%m-%d')),('date','&gt;=',(datetime.date.today()).strftime('%Y-%m-%d'))]" help="Created today"/>
                                    OR

<filter icon="terp-go-today" string="Today" domain="[('date','&lt;=', datetime.datetime.combine(context_today(), datetime.time(23,59,59))), ('date','&gt;=', datetime.datetime.combine(context_today(), datetime.time(0,0,0)))]" help="Orders processed Today or planned for Today"/>


This Year


<filter string="This Year" name="year" domain="[('date','&lt;=', time.strftime('%%Y-12-31')),('date','&gt;=',time.strftime('%%Y-01-01'))]"/>




Last Week

<filter string="Last week" domain="[('date_order', '&gt;=', ((context_today()+relativedelta(weeks=-2,                                         days=1, weekday=0)).strftime('%Y-%m-%d'))),('date_order', '&lt;=', ((context_today()+relativedelta(weeks=-1,weekday=6)).strftime('%Y-%m-%d')))]"/>


This Month 

  <filter icon="terp-go-month" string="Month" name="month" domain="[('date_order','&lt;=',(datetime.date.today()+relativedelta(day=31)).strftime('%Y-%m-%d')),('date_order','&gt;=',(datetime.date.today()-relativedelta(day=1)).strftime('%Y-%m-%d'))]" help="Current Month"/>



Current week


<filter icon="terp-go-week"
        string="Current Week"
        separator="1"
        name="week"
        domain="[('date_order','&lt;=', (context_today() + relativedelta(weeks=0,day=7, weekday=-1)).strftime('%Y-%m-%d')),('date_order','&gt;=',(context_today() - relativedelta(weeks=1, weekday=0)).strftime('%Y-%m-%d'))]"
        help="Current Week"
        context="{'group_by':'date_order'}"/>


Previous Month


<filter string="Prev Month" name="prev_month" domain="[('date_order','&gt;=',(context_today()-relativedelta(months=1)).strftime('%Y-%m-01')),('date_order','&lt;',time.strftime('%Y-%m-01'))]"/>


Expiry checking

<filter string="Immediate Expiry" name="od_imm" domain="[('expiry_date','&gt;=', ((context_today()+datetime.timedelta(days=1)).strftime('%Y-%m-%d'))),('expiry_date','&lt;=', ((context_today()+datetime.timedelta(days=45)).strftime('%Y-%m-%d')))]"/>

Yesterday


<filter string="Yesterday" domain="[('date_order','&gt;=', ((context_today()-datetime.timedelta(days=1)).strftime('%Y-%m-%d 00:00:00'))), ('date_order','&lt;=', ((context_today()+datetime.timedelta(days=0)).strftime('%Y-%m-%d 00:00:00')))]"/>

Tomorrow


<filter string="Tomorrow" domain="[('date_order','&gt;=', ((context_today()+datetime.timedelta(days=0)).strftime('%Y-%m-%d 23:59:59'))), ('date_order','&lt;=', ((context_today()+datetime.timedelta(days=1)).strftime('%Y-%m-%d 23:59:59')))]"/>

Filters for Selection Field

Eg:<filter string="Quotations" name="draft" domain="[('state','in',('draft','sent'))]" help="Sales Order that haven't yet been confirmed"/>
Eg:<filter string="To Invoice" domain="[('state','=','manual')]" help="Sales Order ready to be invoiced"/>


Filters for Boolean Field

Eg:<filter icon="terp-gtk-jump-to-ltr" string="To Review" domain="[('to_check','=',True)]" help="Journal Entries to Review"/>

Filters for Many2one Field

<filter domain="[('user_id','=',uid)]" help="My Invoices" string="My Invoices"/>
for many2one field we are usually adding group by instead of filters.
you can add filters for many2one field by hard coding ids

Eg:-<filter domain="[('partner_id','in',(1,2,9,8,22)]" help="Selected" string="Special Partners"/>
Eg:-<filter domain="[('partner_id','=',1]" help="Selected" string="My Order"/>



Filters for Date Field


This week


<filter string="This Week"  name="This Week" separator="1"
                         domain="[('date_from','&gt;=', ((context_today()).strftime('%Y-%m-%d'))), ('date_from','&lt;=', ((context_today()+datetime.timedelta(days=7)).strftime('%Y-%m-%d')))]"
                           help="This Week"/>
Last Week


<filter string="Last Week"  name="Last Week" separator="1"
                         domain="[('date_from','&gt;=', ((context_today()+datetime.timedelta(days=-7)).strftime('%Y-%m-%d'))), ('date_from','&lt;=', ((context_today()).strftime('%Y-%m-%d')))]"
                           help="This Week"/>

yesterday

<filter string="Yesterday"  name="Yesterday" separator="1"
                         domain="[('date_from','&gt;=', ((context_today()+datetime.timedelta(days=-1)).strftime('%Y-%m-%d'))), ('date_from','&lt;=', ((context_today()+datetime.timedelta(days=-1)).strftime('%Y-%m-%d')))]"
                           help="Yesterday"/>

Today

<filter string="Today"  name="Today" separator="1"
                         domain="[('date_from','&gt;=', ((context_today()+datetime.timedelta(days=0)).strftime('%Y-%m-%d'))), ('date_from','&lt;=', ((context_today()+datetime.timedelta(days=0)).strftime('%Y-%m-%d')))]"
                           help="Today"/>

Tomorrow


<filter string="Tomorrow"  name="Tomorrow" separator="1"
                         domain="[('date_from','&gt;=', ((context_today()+datetime.timedelta(days=1)).strftime('%Y-%m-%d'))), ('date_from','&lt;=', ((context_today()+datetime.timedelta(days=1)).strftime('%Y-%m-%d')))]"
                           help="Tomorrow"/>

Expiry


<filter string="Immediate Expiry"  name="Immediate Expiry" separator="1"
                         domain="[('date_from','&gt;=', ((context_today()+datetime.timedelta(days=0)).strftime('%Y-%m-%d'))), ('date_from','&lt;=', ((context_today()+datetime.timedelta(days=45)).strftime('%Y-%m-%d')))]"
                           help="Immediate Expiry"/>


Last Month

<filter icon="terp-go-month" string="Last Month"
                         domain="[('date_from','&lt;=', (context_today()-relativedelta(day=31, months=1)).strftime('%Y-%m-%d')),
                         ('date_from','&gt;=',(context_today()-relativedelta(day=1,months=1)).strftime('%Y-%m-%d'))]"
                         help="last month"/>

Current Month



<filter string="Current Month" name="current_month" domain="[('date_from','&lt;',(context_today()+relativedelta(months=1)).strftime('%Y-%m-01')), ('date_from','&gt;=',time.strftime('%Y-%m-01'))]"/>


Last Year
<filter  icon="terp-go-month" string="Last Year"
                domain="[('date', '&gt;=' ,(context_today()-relativedelta(years=1)).strftime('%Y-01-01')),('date','&lt;=',time.strftime('%Y-01-01'))]" help="last year"/>


This Year
<filter icon="terp-go-year" string="Year"
                            domain="[('date_from','&lt;=', time.strftime('%Y-%m-%d')),('date_from','&gt;=',time.strftime('%Y-01-01'))]"
                            help="Current Year"/>


Tuesday, 7 March 2017

How to call a wizard from js in odoo ?

this.do_action({

                   type: 'ir.actions.act_window',

                  res_model: 'Model name',

                 view_mode: 'form',

                 view_type: 'form',

                 views: [[false, 'form']],

                 target: 'new',

                 context:{}
});

How to add Facebook Open Graph Tag in the head section?



you can add using this :

        <template id="login_layout_replace1_new" inherit_id="web.layout" name="Web layout">
            <xpath expr="html/head" position="replace">
                <head>
                    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"/>
                    <meta http-equiv="content-type" content="text/html; charset=utf-8" />
                    <title>Odoo</title>
                    <link rel="stylesheet" href="/web/static/src/css/full.css" />
                    <t t-raw="head or ''"/>
                    //you can add your code here.
                </head>
            </xpath>
        </template>

Hope this will help you!!

Monday, 6 March 2017

How to Store Image in binary field from system path in odoo ?

    def get_file(self, cr, uid, ids, field_name=None, arg=None,context=None):
        result = dict.fromkeys(ids)
        for record_browse in self.browse(cr, uid, ids):
        addons_path = http.addons_manifest['web']['addons_path']
        f = open(os.path.join(addons_path,'modulename','static','src','img', 'image.png'), 'rb')
       result[record_browse.id] = base64.encodestring(f.read())
       f.close()
    return result
    
 _columns = { 
'web_icon': fields.function(get_file, method=True,store=False,type='binary',string="Download File"),
    }

Tuesday, 21 February 2017

How to Make Custom Keyboard shortcuts for odoo ?

Here, i will show you some example to get custom shortcuts works in odoo. you can also override default shortcuts in odoo. as you can see for that we have to create one js file in out module. the path of the file is module / static / src / js / file_js.js
and put the below code into file .


$.shortcut = function(key, callback, args) {
    $(document).keydown(function(e) {
        if(!args) args=[]; // IE barks when args is null
        console.log(e.keyCode)
        if((e.keyCode == key.charCodeAt(0) || e.keyCode == key)) {
            callback.apply(this, args);
            return false;
        }
    });
};

                    //Edit the current object F2
$.shortcut('113', function() {
$('.oe_form_button_edit').each(function() {
if($(this).parents('div:hidden').length == 0){
$(this).trigger('click');
}
});
});


with this user can save the record with F2 button.
so as simple way we can also create record, delete record, make tree view to unlimited , remove the filter , open calendar and so many things using key board key.

Friday, 18 September 2015

How can we use JQ or JavaScript for a specific field in Odoo v8? | OpenERP

Add id for particular field in XML
than add js code in js file like below:

openerp.module_name = function(instance) {

var QWeb = openerp.web.qweb;
    _t = instance.web._t;

instance.web.FormView.include({
    load_form: function(data) {
        var self = this;
        this.$el.find('#field id').on('change', function() {
            alert( this.value ); // or $(this).val()
            // here put your logic for validation field.
        });
        return self._super(data);
    },

});
};